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"
27 #include "wx/os2/private.h"
29 #include "wx/dcclient.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 struct WXDLLEXPORT wxPaintDCInfo
37 wxPaintDCInfo(wxWindow
*win
, wxDC
*dc
)
39 hwnd
= win
->GetHWND();
44 WXHWND hwnd
; // window for this DC
45 WXHDC hdc
; // the DC handle
46 size_t count
; // usage count
49 #include "wx/arrimpl.cpp"
51 WX_DEFINE_OBJARRAY(wxArrayDCInfo
);
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
58 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
59 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 static RECT g_paintStruct
;
68 // a global variable which we check to verify that wxPaintDC are only
69 // created in resopnse to WM_PAINT message - doing this from elsewhere is a
70 // common programming error among wxWindows programmers and might lead to
71 // very subtle and difficult to debug refresh/repaint bugs.
75 // ===========================================================================
77 // ===========================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 wxWindowDC::wxWindowDC()
88 wxWindowDC::wxWindowDC(wxWindow
*the_canvas
)
93 m_pCanvas
= the_canvas
;
94 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(the_canvas
) );
97 // default under PM is that Window and Client DC's are the same
98 // so we offer a separate Presentation Space to use for the
99 // entire window. Otherwise, calling BeginPaint will just create
100 // chached-micro client presentation space
102 m_hPS
= GpiCreatePS( m_hab
105 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
107 ::GpiAssociate(m_hPS
, NULLHANDLE
);
108 ::GpiAssociate(m_hPS
, m_hDC
);
109 // Set the wxWindows color table
110 if (!::GpiCreateLogColorTable( m_hPS
114 ,(LONG
)wxTheColourDatabase
->m_nSize
115 ,(PLONG
)wxTheColourDatabase
->m_palTable
118 vError
= ::WinGetLastError(vHabmain
);
119 sError
= wxPMErrorToStr(vError
);
120 wxLogError("Unable to set current color table. Error: %s\n", sError
);
122 SetBackground(wxBrush(m_pCanvas
->GetBackgroundColour(), wxSOLID
));
125 wxWindowDC::~wxWindowDC()
127 if (m_pCanvas
&& m_hDC
)
129 SelectOldObjects(m_hDC
);
132 // In PM one does not explicitly close or release an open WindowDC
133 // They automatically close with the window, unless explicitly detached
134 // but we need to destroy our PS
138 ::GpiAssociate(m_hPS
, NULLHANDLE
);
139 ::GpiDestroyPS(m_hPS
);
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 wxClientDC::wxClientDC()
156 wxClientDC::wxClientDC(wxWindow
*the_canvas
)
158 SIZEL vSizl
= { 0,0};
162 m_pCanvas
= the_canvas
;
165 // default under PM is that Window and Client DC's are the same
167 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(the_canvas
));
168 m_hPS
= ::GpiCreatePS( wxGetInstance()
171 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
174 // Set the wxWindows color table
175 if (!::GpiCreateLogColorTable( m_hPS
179 ,(LONG
)wxTheColourDatabase
->m_nSize
180 ,(PLONG
)wxTheColourDatabase
->m_palTable
183 vError
= ::WinGetLastError(vHabmain
);
184 sError
= wxPMErrorToStr(vError
);
185 wxLogError("Unable to set current color table. Error: %s\n", sError
);
188 // Default mode is BM_LEAVEALONE so we make no call Set the mix
190 SetBackground(wxBrush( m_pCanvas
->GetBackgroundColour()
196 wxClientDC::~wxClientDC()
198 if ( m_pCanvas
&& GetHdc() )
200 SelectOldObjects(m_hDC
);
203 // We don't explicitly release Device contexts in PM and
204 // the cached micro PS is already gone
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
215 // created and tried to reuse - this was supposed to take care of a
216 // situation when a derived class OnPaint() calls base class OnPaint()
217 // because in this case ::BeginPaint() shouldn't be called second time.
219 // I'm not sure how useful this is, however we must remember the HWND
220 // associated with the last HDC as well - otherwise we may (and will!) try
221 // to reuse the HDC for another HWND which is a nice recipe for disaster.
223 // So we store a list of windows for which we already have the DC and not
224 // just one single hDC. This seems to work, but I'm really not sure about
225 // the usefullness of the whole idea - IMHO it's much better to not call
226 // base class OnPaint() at all, or, if we really want to allow it, add a
227 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
228 // !NULL instead of creating a new DC.
230 wxArrayDCInfo
wxPaintDC::ms_cache
;
232 wxPaintDC::wxPaintDC()
238 wxPaintDC::wxPaintDC(
242 wxCHECK_RET(pCanvas
, wxT("NULL canvas in wxPaintDC ctor"));
246 if (g_isPainting
<= 0)
248 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
251 #endif // __WXDEBUG__
256 // Do we have a DC for this window in the cache?
258 wxPaintDCInfo
* pInfo
= FindInCache();
265 else // not in cache, create a new one
269 hPS
= ::WinBeginPaint( GetWinHwnd(m_pCanvas
)
277 ::GpiCreateLogColorTable( m_hPS
281 ,(LONG
)wxTheColourDatabase
->m_nSize
282 ,(PLONG
)wxTheColourDatabase
->m_palTable
284 ::GpiCreateLogColorTable( m_hPS
292 m_bIsPaintTime
= TRUE
;
293 m_hDC
= (WXHDC
) -1; // to satisfy those anonizmous efforts
294 m_vRclPaint
= g_paintStruct
;
295 ms_cache
.Add(new wxPaintDCInfo(m_pCanvas
, this));
297 SetBackground(wxBrush(m_pCanvas
->GetBackgroundColour(), wxSOLID
));
300 wxPaintDC::~wxPaintDC()
304 SelectOldObjects(m_hDC
);
307 wxPaintDCInfo
*info
= FindInCache(&index
);
309 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
311 if ( !--info
->count
)
313 ::WinEndPaint(m_hPS
);
315 m_bIsPaintTime
= FALSE
;
316 ms_cache
.Remove(index
);
318 //else: cached DC entry is still in use
320 // prevent the base class dtor from ReleaseDC()ing it again
325 wxPaintDCInfo
*wxPaintDC::FindInCache(size_t *index
) const
327 wxPaintDCInfo
*info
= NULL
;
328 size_t nCache
= ms_cache
.GetCount();
329 for ( size_t n
= 0; n
< nCache
; n
++ )
332 if ( info
->hwnd
== m_pCanvas
->GetHWND() )