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 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
);
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 hPS
= ::WinBeginPaint( GetWinHwnd(m_pCanvas
)
330 ::GpiCreateLogColorTable( m_hPS
334 ,(LONG
)wxTheColourDatabase
->m_nSize
335 ,(PLONG
)wxTheColourDatabase
->m_palTable
337 ::GpiCreateLogColorTable( m_hPS
345 ::WinFillRect(hPS
, &g_paintStruct
, m_pCanvas
->GetBackgroundColour().GetPixel());
346 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
351 m_bIsPaintTime
= TRUE
;
352 m_hDC
= (WXHDC
) -1; // to satisfy those anonizmous efforts
353 ms_cache
.Add(new wxPaintDCInfo(m_pCanvas
, this));
356 } // end of wxPaintDC::wxPaintDC
358 wxPaintDC::~wxPaintDC()
362 SelectOldObjects(m_hDC
);
365 wxPaintDCInfo
* pInfo
= FindInCache(&nIndex
);
367 wxCHECK_RET( pInfo
, wxT("existing DC should have a cache entry") );
369 if ( !--pInfo
->m_nCount
)
371 ::WinEndPaint(m_hPS
);
373 m_bIsPaintTime
= FALSE
;
374 ms_cache
.RemoveAt(nIndex
);
376 //else: cached DC entry is still in use
378 // prevent the base class dtor from ReleaseDC()ing it again
383 wxPaintDCInfo
* wxPaintDC::FindInCache(
387 wxPaintDCInfo
* pInfo
= NULL
;
388 size_t nCache
= ms_cache
.GetCount();
390 for (size_t n
= 0; n
< nCache
; n
++)
392 pInfo
= &ms_cache
[n
];
393 if (pInfo
->m_hWnd
== m_pCanvas
->GetHWND())
401 } // end of wxPaintDC::FindInCache
403 // find the entry for this DC in the cache (keyed by the window)
404 WXHDC
wxPaintDC::FindDCInCache(
408 wxPaintDCInfo
* pInfo
= NULL
;
409 size_t nCache
= ms_cache
.GetCount();
411 for (size_t n
= 0; n
< nCache
; n
++)
413 pInfo
= &ms_cache
[n
];
414 if (pInfo
->m_hWnd
== pWin
->GetHWND())
420 } // end of wxPaintDC::FindInCache