1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxClientDC class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #include "wx/string.h"
25 #include "wx/window.h"
28 #include "wx/os2/private.h"
30 #include "wx/dcclient.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 struct WXDLLEXPORT wxPaintDCInfo
38 wxPaintDCInfo( wxWindow
* pWin
42 m_hWnd
= pWin
->GetHWND();
43 m_hDC
= pDC
->GetHDC();
47 WXHWND m_hWnd
; // window for this DC
48 WXHDC m_hDC
; // the DC handle
49 size_t m_nCount
; // usage count
50 }; // end of wxPaintDCInfot
52 #include "wx/arrimpl.cpp"
54 WX_DEFINE_OBJARRAY(wxArrayDCInfo
);
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
61 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
62 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 static RECT g_paintStruct
;
71 // a global variable which we check to verify that wxPaintDC are only
72 // created in resopnse to WM_PAINT message - doing this from elsewhere is a
73 // common programming error among wxWindows programmers and might lead to
74 // very subtle and difficult to debug refresh/repaint bugs.
78 // ===========================================================================
80 // ===========================================================================
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 wxWindowDC::wxWindowDC()
91 wxWindowDC::wxWindowDC(
98 m_pCanvas
= pTheCanvas
;
99 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas
) );
102 // default under PM is that Window and Client DC's are the same
103 // so we offer a separate Presentation Space to use for the
104 // entire window. Otherwise, calling BeginPaint will just create
105 // chached-micro client presentation space
107 m_hPS
= ::GpiCreatePS( vHabmain
110 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
112 ::GpiAssociate(m_hPS
, NULLHANDLE
);
113 ::GpiAssociate(m_hPS
, m_hDC
);
116 // Set the wxWindows color table
118 if (!::GpiCreateLogColorTable( m_hPS
122 ,(LONG
)wxTheColourDatabase
->m_nSize
123 ,(PLONG
)wxTheColourDatabase
->m_palTable
126 vError
= ::WinGetLastError(vHabmain
);
127 sError
= wxPMErrorToStr(vError
);
128 wxLogError("Unable to set current color table. Error: %s\n", sError
);
130 ::GpiCreateLogColorTable( m_hPS
137 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
141 } // end of wxWindowDC::wxWindowDC
143 void wxWindowDC::InitDC()
148 // The background mode is only used for text background and is set in
149 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
151 ::GpiSetBackMix(GetHPS(), BM_LEAVEALONE
);
154 // Default bg colour is pne of the window
156 SetBackground(wxBrush(m_pCanvas
->GetBackgroundColour(), wxSOLID
));
158 vColor
.InitFromName("BLACK");
159 m_pen
.SetColour(vColor
);
161 m_brush
.SetColour(vColor
);
163 } // end of wxWindowDC::InitDC
165 void wxWindowDC::DoGetSize(
170 wxCHECK_RET( m_pCanvas
, _T("wxWindowDC without a window?") );
171 m_pCanvas
->GetSize( pnWidth
174 } // end of wxWindowDC::DoGetSize
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 wxClientDC::wxClientDC()
185 wxClientDC::wxClientDC(
189 SIZEL vSizl
= { 0,0};
193 m_pCanvas
= pTheCanvas
;
196 // default under PM is that Window and Client DC's are the same
198 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas
));
199 m_hPS
= ::GpiCreatePS( wxGetInstance()
202 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
205 // Set the wxWindows color table
206 if (!::GpiCreateLogColorTable( m_hPS
210 ,(LONG
)wxTheColourDatabase
->m_nSize
211 ,(PLONG
)wxTheColourDatabase
->m_palTable
214 vError
= ::WinGetLastError(vHabmain
);
215 sError
= wxPMErrorToStr(vError
);
216 wxLogError("Unable to set current color table. Error: %s\n", sError
);
218 ::GpiCreateLogColorTable( m_hPS
226 // Set the DC/PS rectangle
228 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
232 } // end of wxClientDC::wxClientDC
234 void wxClientDC::InitDC()
236 wxWindowDC::InitDC();
238 // in wxUniv build we must manually do some DC adjustments usually
239 // performed by Windows for us
240 #ifdef __WXUNIVERSAL__
241 wxPoint ptOrigin
= m_pCanvas
->GetClientAreaOrigin();
242 if ( ptOrigin
.x
|| ptOrigin
.y
)
244 // no need to shift DC origin if shift is null
245 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
248 // clip the DC to avoid overwriting the non client area
249 SetClippingRegion(wxPoint(0, 0), m_pCanvas
->GetClientSize());
250 #endif // __WXUNIVERSAL__
251 } // end of wxClientDC::InitDC
253 wxClientDC::~wxClientDC()
255 } // end of wxClientDC::~wxClientDC
257 void wxClientDC::DoGetSize(
262 wxCHECK_RET( m_pCanvas
, _T("wxWindowDC without a window?") );
263 m_pCanvas
->GetClientSize( pnWidth
266 } // end of wxClientDC::DoGetSize
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 wxArrayDCInfo
wxPaintDC::ms_cache
;
274 wxPaintDC::wxPaintDC()
280 wxPaintDC::wxPaintDC(
284 wxCHECK_RET(pCanvas
, wxT("NULL canvas in wxPaintDC ctor"));
287 if (g_isPainting
<= 0)
289 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
292 #endif // __WXDEBUG__
297 // Do we have a DC for this window in the cache?
299 wxPaintDCInfo
* pInfo
= FindInCache();
303 m_hDC
= pInfo
->m_hDC
;
306 else // not in cache, create a new one
310 hPS
= ::WinBeginPaint( GetWinHwnd(m_pCanvas
)
318 ::GpiCreateLogColorTable( m_hPS
322 ,(LONG
)wxTheColourDatabase
->m_nSize
323 ,(PLONG
)wxTheColourDatabase
->m_palTable
325 ::GpiCreateLogColorTable( m_hPS
333 ::WinFillRect(hPS
, &g_paintStruct
, m_pCanvas
->GetBackgroundColour().GetPixel());
334 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
339 m_bIsPaintTime
= TRUE
;
340 m_hDC
= (WXHDC
) -1; // to satisfy those anonizmous efforts
341 ms_cache
.Add(new wxPaintDCInfo(m_pCanvas
, this));
344 } // end of wxPaintDC::wxPaintDC
346 wxPaintDC::~wxPaintDC()
350 SelectOldObjects(m_hDC
);
353 wxPaintDCInfo
* pInfo
= FindInCache(&nIndex
);
355 wxCHECK_RET( pInfo
, wxT("existing DC should have a cache entry") );
357 if ( !--pInfo
->m_nCount
)
359 ::WinEndPaint(m_hPS
);
361 m_bIsPaintTime
= FALSE
;
362 ms_cache
.RemoveAt(nIndex
);
364 //else: cached DC entry is still in use
366 // prevent the base class dtor from ReleaseDC()ing it again
371 wxPaintDCInfo
* wxPaintDC::FindInCache(
375 wxPaintDCInfo
* pInfo
= NULL
;
376 size_t nCache
= ms_cache
.GetCount();
378 for (size_t n
= 0; n
< nCache
; n
++)
380 pInfo
= &ms_cache
[n
];
381 if (pInfo
->m_hWnd
== m_pCanvas
->GetHWND())
389 } // end of wxPaintDC::FindInCache
391 // find the entry for this DC in the cache (keyed by the window)
392 WXHDC
wxPaintDC::FindDCInCache(
396 wxPaintDCInfo
* pInfo
= NULL
;
397 size_t nCache
= ms_cache
.GetCount();
399 for (size_t n
= 0; n
< nCache
; n
++)
401 pInfo
= &ms_cache
[n
];
402 if (pInfo
->m_hWnd
== pWin
->GetHWND())
408 } // end of wxPaintDC::FindInCache