1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxClientDC class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dcclient.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/string.h"
33 #include "wx/window.h"
35 #include "wx/os2/private.h"
37 #include "wx/dcclient.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 struct WXDLLEXPORT wxPaintDCInfo
45 wxPaintDCInfo(wxWindow
*win
, wxDC
*dc
)
47 hwnd
= win
->GetHWND();
52 WXHWND hwnd
; // window for this DC
53 WXHDC hdc
; // the DC handle
54 size_t count
; // usage count
57 #include "wx/arrimpl.cpp"
59 WX_DEFINE_OBJARRAY(wxArrayDCInfo
);
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 #if !USE_SHARED_LIBRARY
66 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
67 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
68 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 static RECT g_paintStruct
;
78 // a global variable which we check to verify that wxPaintDC are only
79 // created in resopnse to WM_PAINT message - doing this from elsewhere is a
80 // common programming error among wxWindows programmers and might lead to
81 // very subtle and difficult to debug refresh/repaint bugs.
85 // ===========================================================================
87 // ===========================================================================
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 wxWindowDC::wxWindowDC()
98 wxWindowDC::wxWindowDC(wxWindow
*the_canvas
)
100 m_canvas
= the_canvas
;
101 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(the_canvas
) );
104 // default under PM is that Window and Client DC's are the same
105 // so we offer a separate Presentation Space to use for the
106 // entire window. Otherwise, calling BeginPaint will just create
107 // chached-micro client presentation space
109 m_hPS
= GpiCreatePS( m_hab
112 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
114 ::GpiAssociate(m_hPS
, NULLHANDLE
);
115 ::GpiAssociate(m_hPS
, m_hDC
);
116 SetBackground(wxBrush(m_canvas
->GetBackgroundColour(), wxSOLID
));
119 wxWindowDC::~wxWindowDC()
121 if (m_canvas
&& m_hDC
)
123 SelectOldObjects(m_hDC
);
126 // In PM one does not explicitly close or release an open WindowDC
127 // They automatically close with the window, unless explicitly detached
128 // but we need to destroy our PS
130 ::GpiAssociate(m_hPS
, NULLHANDLE
);
131 ::GpiDestroyPS(m_hPS
);
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 wxClientDC::wxClientDC()
148 wxClientDC::wxClientDC(wxWindow
*the_canvas
)
150 m_canvas
= the_canvas
;
153 // default under PM is that Window and Client DC's are the same
155 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(the_canvas
));
158 // Default mode is BM_LEAVEALONE so we make no call Set the mix
160 SetBackground(wxBrush(m_canvas
->GetBackgroundColour(), wxSOLID
));
163 wxClientDC::~wxClientDC()
165 if ( m_canvas
&& GetHdc() )
167 SelectOldObjects(m_hDC
);
169 // We don't explicitly release Device contexts in PM and
170 // the cached micro PS is already gone
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
181 // created and tried to reuse - this was supposed to take care of a
182 // situation when a derived class OnPaint() calls base class OnPaint()
183 // because in this case ::BeginPaint() shouldn't be called second time.
185 // I'm not sure how useful this is, however we must remember the HWND
186 // associated with the last HDC as well - otherwise we may (and will!) try
187 // to reuse the HDC for another HWND which is a nice recipe for disaster.
189 // So we store a list of windows for which we already have the DC and not
190 // just one single hDC. This seems to work, but I'm really not sure about
191 // the usefullness of the whole idea - IMHO it's much better to not call
192 // base class OnPaint() at all, or, if we really want to allow it, add a
193 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
194 // !NULL instead of creating a new DC.
196 wxArrayDCInfo
wxPaintDC::ms_cache
;
198 wxPaintDC::wxPaintDC()
204 wxPaintDC::wxPaintDC(wxWindow
*canvas
)
206 wxCHECK_RET( canvas
, wxT("NULL canvas in wxPaintDC ctor") );
209 if ( g_isPainting
<= 0 )
211 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
215 #endif // __WXDEBUG__
219 // do we have a DC for this window in the cache?
220 wxPaintDCInfo
*info
= FindInCache();
226 else // not in cache, create a new one
228 m_hDC
= (WXHDC
)::WinBeginPaint(GetWinHwnd(m_canvas
), NULLHANDLE
, &g_paintStruct
);
229 ms_cache
.Add(new wxPaintDCInfo(m_canvas
, this));
231 SetBackground(wxBrush(m_canvas
->GetBackgroundColour(), wxSOLID
));
234 wxPaintDC::~wxPaintDC()
238 SelectOldObjects(m_hDC
);
241 wxPaintDCInfo
*info
= FindInCache(&index
);
243 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
245 if ( !--info
->count
)
247 ::WinEndPaint(m_hPS
);
249 ms_cache
.Remove(index
);
251 //else: cached DC entry is still in use
253 // prevent the base class dtor from ReleaseDC()ing it again
258 wxPaintDCInfo
*wxPaintDC::FindInCache(size_t *index
) const
260 wxPaintDCInfo
*info
= NULL
;
261 size_t nCache
= ms_cache
.GetCount();
262 for ( size_t n
= 0; n
< nCache
; n
++ )
265 if ( info
->hwnd
== m_canvas
->GetHWND() )