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 #if !USE_SHARED_LIBRARY
58 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
59 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
60 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 static RECT g_paintStruct
;
70 // a global variable which we check to verify that wxPaintDC are only
71 // created in resopnse to WM_PAINT message - doing this from elsewhere is a
72 // common programming error among wxWindows programmers and might lead to
73 // very subtle and difficult to debug refresh/repaint bugs.
77 // ===========================================================================
79 // ===========================================================================
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 wxWindowDC::wxWindowDC()
90 wxWindowDC::wxWindowDC(wxWindow
*the_canvas
)
92 m_canvas
= the_canvas
;
93 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(the_canvas
) );
96 // default under PM is that Window and Client DC's are the same
97 // so we offer a separate Presentation Space to use for the
98 // entire window. Otherwise, calling BeginPaint will just create
99 // chached-micro client presentation space
101 m_hPS
= GpiCreatePS( m_hab
104 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
106 ::GpiAssociate(m_hPS
, NULLHANDLE
);
107 ::GpiAssociate(m_hPS
, m_hDC
);
108 SetBackground(wxBrush(m_canvas
->GetBackgroundColour(), wxSOLID
));
111 wxWindowDC::~wxWindowDC()
113 if (m_canvas
&& m_hDC
)
115 SelectOldObjects(m_hDC
);
118 // In PM one does not explicitly close or release an open WindowDC
119 // They automatically close with the window, unless explicitly detached
120 // but we need to destroy our PS
122 ::GpiAssociate(m_hPS
, NULLHANDLE
);
123 ::GpiDestroyPS(m_hPS
);
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 wxClientDC::wxClientDC()
140 wxClientDC::wxClientDC(wxWindow
*the_canvas
)
142 m_canvas
= the_canvas
;
145 // default under PM is that Window and Client DC's are the same
147 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(the_canvas
));
150 // Default mode is BM_LEAVEALONE so we make no call Set the mix
152 SetBackground(wxBrush(m_canvas
->GetBackgroundColour(), wxSOLID
));
155 wxClientDC::~wxClientDC()
157 if ( m_canvas
&& GetHdc() )
159 SelectOldObjects(m_hDC
);
161 // We don't explicitly release Device contexts in PM and
162 // the cached micro PS is already gone
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
173 // created and tried to reuse - this was supposed to take care of a
174 // situation when a derived class OnPaint() calls base class OnPaint()
175 // because in this case ::BeginPaint() shouldn't be called second time.
177 // I'm not sure how useful this is, however we must remember the HWND
178 // associated with the last HDC as well - otherwise we may (and will!) try
179 // to reuse the HDC for another HWND which is a nice recipe for disaster.
181 // So we store a list of windows for which we already have the DC and not
182 // just one single hDC. This seems to work, but I'm really not sure about
183 // the usefullness of the whole idea - IMHO it's much better to not call
184 // base class OnPaint() at all, or, if we really want to allow it, add a
185 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
186 // !NULL instead of creating a new DC.
188 wxArrayDCInfo
wxPaintDC::ms_cache
;
190 wxPaintDC::wxPaintDC()
196 wxPaintDC::wxPaintDC(wxWindow
*canvas
)
198 wxCHECK_RET( canvas
, wxT("NULL canvas in wxPaintDC ctor") );
201 if ( g_isPainting
<= 0 )
203 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
207 #endif // __WXDEBUG__
211 // do we have a DC for this window in the cache?
212 wxPaintDCInfo
*info
= FindInCache();
218 else // not in cache, create a new one
220 m_hDC
= (WXHDC
)::WinBeginPaint(GetWinHwnd(m_canvas
), NULLHANDLE
, &g_paintStruct
);
221 ms_cache
.Add(new wxPaintDCInfo(m_canvas
, this));
223 SetBackground(wxBrush(m_canvas
->GetBackgroundColour(), wxSOLID
));
226 wxPaintDC::~wxPaintDC()
230 SelectOldObjects(m_hDC
);
233 wxPaintDCInfo
*info
= FindInCache(&index
);
235 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
237 if ( !--info
->count
)
239 ::WinEndPaint(m_hPS
);
241 ms_cache
.Remove(index
);
243 //else: cached DC entry is still in use
245 // prevent the base class dtor from ReleaseDC()ing it again
250 wxPaintDCInfo
*wxPaintDC::FindInCache(size_t *index
) const
252 wxPaintDCInfo
*info
= NULL
;
253 size_t nCache
= ms_cache
.GetCount();
254 for ( size_t n
= 0; n
< nCache
; n
++ )
257 if ( info
->hwnd
== m_canvas
->GetHWND() )