1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxClientDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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/msw/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 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
66 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
67 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxClientDC
)
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 static PAINTSTRUCT g_paintStruct
;
76 // a global variable which we check to verify that wxPaintDC are only
77 // created in resopnse to WM_PAINT message - doing this from elsewhere is a
78 // common programming error among wxWindows programmers and might lead to
79 // very subtle and difficult to debug refresh/repaint bugs.
83 // ===========================================================================
85 // ===========================================================================
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 wxWindowDC::wxWindowDC()
96 wxWindowDC::wxWindowDC(wxWindow
*canvas
)
98 wxCHECK_RET( canvas
, _T("invalid window in wxWindowDC") );
101 m_hDC
= (WXHDC
) ::GetWindowDC(GetHwndOf(m_canvas
));
103 // m_bOwnsDC was already set to false in the base class ctor, so the DC
104 // will be released (and not deleted) in ~wxDC
108 void wxWindowDC::InitDC()
110 // the background mode is only used for text background and is set in
111 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
112 ::SetBkMode(GetHdc(), TRANSPARENT
);
114 // default bg colour is pne of the window
115 SetBackground(wxBrush(m_canvas
->GetBackgroundColour(), wxSOLID
));
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 wxClientDC::wxClientDC()
127 wxClientDC::wxClientDC(wxWindow
*canvas
)
129 wxCHECK_RET( canvas
, _T("invalid window in wxClientDC") );
132 m_hDC
= (WXHDC
)::GetDC(GetHwndOf(m_canvas
));
134 // m_bOwnsDC was already set to false in the base class ctor, so the DC
135 // will be released (and not deleted) in ~wxDC
140 void wxClientDC::InitDC()
142 wxWindowDC::InitDC();
144 // in wxUniv build we must manually do some DC adjustments usually
145 // performed by Windows for us
146 #ifdef __WXUNIVERSAL__
147 wxPoint ptOrigin
= m_canvas
->GetClientAreaOrigin();
148 if ( ptOrigin
.x
|| ptOrigin
.y
)
150 // no need to shift DC origin if shift is null
151 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
154 // clip the DC to avoid overwriting the non client area
155 SetClippingRegion(wxPoint(0, 0), m_canvas
->GetClientSize());
156 #endif // __WXUNIVERSAL__
159 wxClientDC::~wxClientDC()
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
168 // created and tried to reuse it - this was supposed to take care of a
169 // situation when a derived class OnPaint() calls base class OnPaint()
170 // because in this case ::BeginPaint() shouldn't be called second time.
172 // I'm not sure how useful this is, however we must remember the HWND
173 // associated with the last HDC as well - otherwise we may (and will!) try
174 // to reuse the HDC for another HWND which is a nice recipe for disaster.
176 // So we store a list of windows for which we already have the DC and not
177 // just one single hDC. This seems to work, but I'm really not sure about
178 // the usefullness of the whole idea - IMHO it's much better to not call
179 // base class OnPaint() at all, or, if we really want to allow it, add a
180 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
181 // !NULL instead of creating a new DC.
183 wxArrayDCInfo
wxPaintDC::ms_cache
;
185 wxPaintDC::wxPaintDC()
190 wxPaintDC::wxPaintDC(wxWindow
*canvas
)
192 wxCHECK_RET( canvas
, wxT("NULL canvas in wxPaintDC ctor") );
195 if ( g_isPainting
<= 0 )
197 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
201 #endif // __WXDEBUG__
205 // do we have a DC for this window in the cache?
206 wxPaintDCInfo
*info
= FindInCache();
212 else // not in cache, create a new one
214 m_hDC
= (WXHDC
)::BeginPaint(GetHwndOf(m_canvas
), &g_paintStruct
);
216 ms_cache
.Add(new wxPaintDCInfo(m_canvas
, this));
219 // (re)set the DC parameters.
220 // Note: at this point m_hDC can be NULL under MicroWindows, when dragging.
225 wxPaintDC::~wxPaintDC()
229 SelectOldObjects(m_hDC
);
232 wxPaintDCInfo
*info
= FindInCache(&index
);
234 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
236 if ( !--info
->count
)
238 ::EndPaint(GetHwndOf(m_canvas
), &g_paintStruct
);
240 ms_cache
.Remove(index
);
242 // Reduce the number of bogus reports of non-freed memory
244 if (ms_cache
.IsEmpty())
247 //else: cached DC entry is still in use
249 // prevent the base class dtor from ReleaseDC()ing it again
254 wxPaintDCInfo
*wxPaintDC::FindInCache(size_t *index
) const
256 wxPaintDCInfo
*info
= NULL
;
257 size_t nCache
= ms_cache
.GetCount();
258 for ( size_t n
= 0; n
< nCache
; n
++ )
261 if ( info
->hwnd
== m_canvas
->GetHWND() )
272 // find the entry for this DC in the cache (keyed by the window)
273 WXHDC
wxPaintDC::FindDCInCache(wxWindow
* win
)
275 wxPaintDCInfo
*info
= NULL
;
276 size_t nCache
= ms_cache
.GetCount();
277 for ( size_t n
= 0; n
< nCache
; n
++ )
280 if ( info
->hwnd
== win
->GetHWND() )