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
.c_str());
130 ::GpiCreateLogColorTable( m_hPS
137 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
141 } // end of wxWindowDC::wxWindowDC
143 void wxWindowDC::InitDC()
147 // The background mode is only used for text background and is set in
148 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
150 ::GpiSetBackMix(GetHPS(), BM_LEAVEALONE
);
153 // Default bg colour is pne of the window
155 SetBackground(wxBrush(m_pCanvas
->GetBackgroundColour(), wxSOLID
));
157 wxColour
vColor( wxT("BLACK") );
158 m_pen
.SetColour(vColor
);
160 vColor
.Set( wxT("WHITE") );
161 m_brush
.SetColour(vColor
);
163 wxFont
* pFont
= new wxFont( 12
171 // OS/2 default vertical character allignment needs to match the other OS's
173 ::GpiSetTextAlignment((HPS
)GetHPS(), TA_NORMAL_HORIZ
, TA_BOTTOM
);
175 } // end of wxWindowDC::InitDC
177 void wxWindowDC::DoGetSize(
182 wxCHECK_RET( m_pCanvas
, _T("wxWindowDC without a window?") );
183 m_pCanvas
->GetSize( pnWidth
186 } // end of wxWindowDC::DoGetSize
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 wxClientDC::wxClientDC()
197 wxClientDC::wxClientDC(
201 SIZEL vSizl
= { 0,0};
205 m_pCanvas
= pTheCanvas
;
208 // default under PM is that Window and Client DC's are the same
210 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas
));
211 m_hPS
= ::GpiCreatePS( wxGetInstance()
214 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
217 // Set the wxWindows color table
218 if (!::GpiCreateLogColorTable( m_hPS
222 ,(LONG
)wxTheColourDatabase
->m_nSize
223 ,(PLONG
)wxTheColourDatabase
->m_palTable
226 vError
= ::WinGetLastError(vHabmain
);
227 sError
= wxPMErrorToStr(vError
);
228 wxLogError("Unable to set current color table. Error: %s\n", sError
.c_str());
230 ::GpiCreateLogColorTable( m_hPS
238 // Set the DC/PS rectangle
240 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
244 } // end of wxClientDC::wxClientDC
246 void wxClientDC::InitDC()
248 wxWindowDC::InitDC();
250 // in wxUniv build we must manually do some DC adjustments usually
251 // performed by Windows for us
252 #ifdef __WXUNIVERSAL__
253 wxPoint ptOrigin
= m_pCanvas
->GetClientAreaOrigin();
254 if ( ptOrigin
.x
|| ptOrigin
.y
)
256 // no need to shift DC origin if shift is null
257 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
260 // clip the DC to avoid overwriting the non client area
261 SetClippingRegion(wxPoint(0, 0), m_pCanvas
->GetClientSize());
262 #endif // __WXUNIVERSAL__
263 } // end of wxClientDC::InitDC
265 wxClientDC::~wxClientDC()
267 } // end of wxClientDC::~wxClientDC
269 void wxClientDC::DoGetSize(
274 wxCHECK_RET( m_pCanvas
, _T("wxWindowDC without a window?") );
275 m_pCanvas
->GetClientSize( pnWidth
278 } // end of wxClientDC::DoGetSize
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 wxArrayDCInfo
wxPaintDC::ms_cache
;
286 wxPaintDC::wxPaintDC()
292 wxPaintDC::wxPaintDC(
296 wxCHECK_RET(pCanvas
, wxT("NULL canvas in wxPaintDC ctor"));
299 if (g_isPainting
<= 0)
301 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
304 #endif // __WXDEBUG__
309 // Do we have a DC for this window in the cache?
311 wxPaintDCInfo
* pInfo
= FindInCache();
315 m_hDC
= pInfo
->m_hDC
;
318 else // not in cache, create a new one
322 m_hDC
= ::WinOpenWindowDC(GetWinHwnd(m_pCanvas
));
323 hPS
= ::WinBeginPaint( GetWinHwnd(m_pCanvas
)
329 ::GpiAssociate(hPS
, m_hDC
);
332 ::GpiCreateLogColorTable( m_hPS
336 ,(LONG
)wxTheColourDatabase
->m_nSize
337 ,(PLONG
)wxTheColourDatabase
->m_palTable
339 ::GpiCreateLogColorTable( m_hPS
347 ::WinFillRect(hPS
, &g_paintStruct
, m_pCanvas
->GetBackgroundColour().GetPixel());
348 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
353 m_bIsPaintTime
= TRUE
;
354 ms_cache
.Add(new wxPaintDCInfo(m_pCanvas
, this));
357 } // end of wxPaintDC::wxPaintDC
359 wxPaintDC::~wxPaintDC()
363 SelectOldObjects(m_hDC
);
366 wxPaintDCInfo
* pInfo
= FindInCache(&nIndex
);
368 wxCHECK_RET( pInfo
, wxT("existing DC should have a cache entry") );
370 if ( !--pInfo
->m_nCount
)
372 ::WinEndPaint(m_hPS
);
374 m_bIsPaintTime
= FALSE
;
375 ms_cache
.RemoveAt(nIndex
);
377 //else: cached DC entry is still in use
379 // prevent the base class dtor from ReleaseDC()ing it again
384 wxPaintDCInfo
* wxPaintDC::FindInCache(
388 wxPaintDCInfo
* pInfo
= NULL
;
389 size_t nCache
= ms_cache
.GetCount();
391 for (size_t n
= 0; n
< nCache
; n
++)
393 pInfo
= &ms_cache
[n
];
394 if (pInfo
->m_hWnd
== m_pCanvas
->GetHWND())
402 } // end of wxPaintDC::FindInCache
404 // find the entry for this DC in the cache (keyed by the window)
405 WXHDC
wxPaintDC::FindDCInCache(
409 wxPaintDCInfo
* pInfo
= NULL
;
410 size_t nCache
= ms_cache
.GetCount();
412 for (size_t n
= 0; n
< nCache
; n
++)
414 pInfo
= &ms_cache
[n
];
415 if (pInfo
->m_hWnd
== pWin
->GetHWND())
421 } // end of wxPaintDC::FindInCache