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()
146 // The background mode is only used for text background and is set in
147 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
149 ::GpiSetBackMix(GetHPS(), BM_LEAVEALONE
);
152 // Default bg colour is pne of the window
154 SetBackground(wxBrush(m_pCanvas
->GetBackgroundColour(), wxSOLID
));
155 } // end of wxWindowDC::InitDC
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 wxClientDC::wxClientDC()
166 wxClientDC::wxClientDC(
170 SIZEL vSizl
= { 0,0};
174 m_pCanvas
= pTheCanvas
;
177 // default under PM is that Window and Client DC's are the same
179 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas
));
180 m_hPS
= ::GpiCreatePS( wxGetInstance()
183 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
186 // Set the wxWindows color table
187 if (!::GpiCreateLogColorTable( m_hPS
191 ,(LONG
)wxTheColourDatabase
->m_nSize
192 ,(PLONG
)wxTheColourDatabase
->m_palTable
195 vError
= ::WinGetLastError(vHabmain
);
196 sError
= wxPMErrorToStr(vError
);
197 wxLogError("Unable to set current color table. Error: %s\n", sError
);
199 ::GpiCreateLogColorTable( m_hPS
207 // Set the DC/PS rectangle
209 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
213 } // end of wxClientDC::wxClientDC
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 wxArrayDCInfo
wxPaintDC::ms_cache
;
221 wxPaintDC::wxPaintDC()
227 wxPaintDC::wxPaintDC(
231 wxCHECK_RET(pCanvas
, wxT("NULL canvas in wxPaintDC ctor"));
234 if (g_isPainting
<= 0)
236 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
239 #endif // __WXDEBUG__
244 // Do we have a DC for this window in the cache?
246 wxPaintDCInfo
* pInfo
= FindInCache();
250 m_hDC
= pInfo
->m_hDC
;
253 else // not in cache, create a new one
257 hPS
= ::WinBeginPaint( GetWinHwnd(m_pCanvas
)
265 ::GpiCreateLogColorTable( m_hPS
269 ,(LONG
)wxTheColourDatabase
->m_nSize
270 ,(PLONG
)wxTheColourDatabase
->m_palTable
272 ::GpiCreateLogColorTable( m_hPS
280 ::WinFillRect(hPS
, &g_paintStruct
, m_pCanvas
->GetBackgroundColour().GetPixel());
281 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
286 m_bIsPaintTime
= TRUE
;
287 m_hDC
= (WXHDC
) -1; // to satisfy those anonizmous efforts
288 ms_cache
.Add(new wxPaintDCInfo(m_pCanvas
, this));
291 } // end of wxPaintDC::wxPaintDC
293 wxPaintDC::~wxPaintDC()
297 SelectOldObjects(m_hDC
);
300 wxPaintDCInfo
* pInfo
= FindInCache(&nIndex
);
302 wxCHECK_RET( pInfo
, wxT("existing DC should have a cache entry") );
304 if ( !--pInfo
->m_nCount
)
306 ::WinEndPaint(m_hPS
);
308 m_bIsPaintTime
= FALSE
;
309 ms_cache
.Remove(nIndex
);
311 //else: cached DC entry is still in use
313 // prevent the base class dtor from ReleaseDC()ing it again
318 wxPaintDCInfo
* wxPaintDC::FindInCache(
322 wxPaintDCInfo
* pInfo
= NULL
;
323 size_t nCache
= ms_cache
.GetCount();
325 for (size_t n
= 0; n
< nCache
; n
++)
327 pInfo
= &ms_cache
[n
];
328 if (pInfo
->m_hWnd
== m_pCanvas
->GetHWND())
336 } // end of wxPaintDC::FindInCache
338 // find the entry for this DC in the cache (keyed by the window)
339 WXHDC
wxPaintDC::FindDCInCache(
343 wxPaintDCInfo
* pInfo
= NULL
;
344 size_t nCache
= ms_cache
.GetCount();
346 for (size_t n
= 0; n
< nCache
; n
++)
348 pInfo
= &ms_cache
[n
];
349 if (pInfo
->m_hWnd
== pWin
->GetHWND())
355 } // end of wxPaintDC::FindInCache