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()
147 vColor
.InitFromName("BLACK");
148 m_pen
.SetColour(vColor
);
150 m_brush
.SetColour(vColor
);
152 // The background mode is only used for text background and is set in
153 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
155 ::GpiSetBackMix(GetHPS(), BM_LEAVEALONE
);
158 // Default bg colour is pne of the window
160 SetBackground(wxBrush(m_pCanvas
->GetBackgroundColour(), wxSOLID
));
161 } // end of wxWindowDC::InitDC
163 void wxWindowDC::DoGetSize(
168 wxCHECK_RET( m_pCanvas
, _T("wxWindowDC without a window?") );
169 m_pCanvas
->GetSize( pnWidth
172 } // end of wxWindowDC::DoGetSize
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 wxClientDC::wxClientDC()
183 wxClientDC::wxClientDC(
187 SIZEL vSizl
= { 0,0};
191 m_pCanvas
= pTheCanvas
;
194 // default under PM is that Window and Client DC's are the same
196 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas
));
197 m_hPS
= ::GpiCreatePS( wxGetInstance()
200 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
203 // Set the wxWindows color table
204 if (!::GpiCreateLogColorTable( m_hPS
208 ,(LONG
)wxTheColourDatabase
->m_nSize
209 ,(PLONG
)wxTheColourDatabase
->m_palTable
212 vError
= ::WinGetLastError(vHabmain
);
213 sError
= wxPMErrorToStr(vError
);
214 wxLogError("Unable to set current color table. Error: %s\n", sError
);
216 ::GpiCreateLogColorTable( m_hPS
224 // Set the DC/PS rectangle
226 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
230 } // end of wxClientDC::wxClientDC
232 void wxClientDC::InitDC()
234 wxWindowDC::InitDC();
236 // in wxUniv build we must manually do some DC adjustments usually
237 // performed by Windows for us
238 #ifdef __WXUNIVERSAL__
239 wxPoint ptOrigin
= m_pCanvas
->GetClientAreaOrigin();
240 if ( ptOrigin
.x
|| ptOrigin
.y
)
242 // no need to shift DC origin if shift is null
243 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
246 // clip the DC to avoid overwriting the non client area
247 SetClippingRegion(wxPoint(0, 0), m_pCanvas
->GetClientSize());
248 #endif // __WXUNIVERSAL__
249 } // end of wxClientDC::InitDC
251 wxClientDC::~wxClientDC()
253 } // end of wxClientDC::~wxClientDC
255 void wxClientDC::DoGetSize(
260 wxCHECK_RET( m_pCanvas
, _T("wxWindowDC without a window?") );
261 m_pCanvas
->GetClientSize( pnWidth
264 } // end of wxClientDC::DoGetSize
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 wxArrayDCInfo
wxPaintDC::ms_cache
;
272 wxPaintDC::wxPaintDC()
278 wxPaintDC::wxPaintDC(
282 wxCHECK_RET(pCanvas
, wxT("NULL canvas in wxPaintDC ctor"));
285 if (g_isPainting
<= 0)
287 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
290 #endif // __WXDEBUG__
295 // Do we have a DC for this window in the cache?
297 wxPaintDCInfo
* pInfo
= FindInCache();
301 m_hDC
= pInfo
->m_hDC
;
304 else // not in cache, create a new one
308 hPS
= ::WinBeginPaint( GetWinHwnd(m_pCanvas
)
316 ::GpiCreateLogColorTable( m_hPS
320 ,(LONG
)wxTheColourDatabase
->m_nSize
321 ,(PLONG
)wxTheColourDatabase
->m_palTable
323 ::GpiCreateLogColorTable( m_hPS
331 ::WinFillRect(hPS
, &g_paintStruct
, m_pCanvas
->GetBackgroundColour().GetPixel());
332 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
337 m_bIsPaintTime
= TRUE
;
338 m_hDC
= (WXHDC
) -1; // to satisfy those anonizmous efforts
339 ms_cache
.Add(new wxPaintDCInfo(m_pCanvas
, this));
342 } // end of wxPaintDC::wxPaintDC
344 wxPaintDC::~wxPaintDC()
348 SelectOldObjects(m_hDC
);
351 wxPaintDCInfo
* pInfo
= FindInCache(&nIndex
);
353 wxCHECK_RET( pInfo
, wxT("existing DC should have a cache entry") );
355 if ( !--pInfo
->m_nCount
)
357 ::WinEndPaint(m_hPS
);
359 m_bIsPaintTime
= FALSE
;
360 ms_cache
.RemoveAt(nIndex
);
362 //else: cached DC entry is still in use
364 // prevent the base class dtor from ReleaseDC()ing it again
369 wxPaintDCInfo
* wxPaintDC::FindInCache(
373 wxPaintDCInfo
* pInfo
= NULL
;
374 size_t nCache
= ms_cache
.GetCount();
376 for (size_t n
= 0; n
< nCache
; n
++)
378 pInfo
= &ms_cache
[n
];
379 if (pInfo
->m_hWnd
== m_pCanvas
->GetHWND())
387 } // end of wxPaintDC::FindInCache
389 // find the entry for this DC in the cache (keyed by the window)
390 WXHDC
wxPaintDC::FindDCInCache(
394 wxPaintDCInfo
* pInfo
= NULL
;
395 size_t nCache
= ms_cache
.GetCount();
397 for (size_t n
= 0; n
< nCache
; n
++)
399 pInfo
= &ms_cache
[n
];
400 if (pInfo
->m_hWnd
== pWin
->GetHWND())
406 } // end of wxPaintDC::FindInCache