1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxClientDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/string.h"
29 #include "wx/window.h"
31 #include "wx/msw/private.h"
33 #include "wx/dcclient.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 struct WXDLLEXPORT wxPaintDCInfo
41 wxPaintDCInfo(wxWindow
*win
, wxDC
*dc
)
43 hwnd
= win
->GetHWND();
48 WXHWND hwnd
; // window for this DC
49 WXHDC hdc
; // the DC handle
50 size_t count
; // usage count
53 #include "wx/arrimpl.cpp"
55 WX_DEFINE_OBJARRAY(wxArrayDCInfo
)
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
62 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
63 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxClientDC
)
64 IMPLEMENT_CLASS(wxPaintDCEx
, wxPaintDC
)
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 static PAINTSTRUCT g_paintStruct
;
73 // a global variable which we check to verify that wxPaintDC are only
74 // created in response to WM_PAINT message - doing this from elsewhere is a
75 // common programming error among wxWidgets programmers and might lead to
76 // very subtle and difficult to debug refresh/repaint bugs.
80 // ===========================================================================
82 // ===========================================================================
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 wxWindowDC::wxWindowDC()
93 wxWindowDC::wxWindowDC(wxWindow
*canvas
)
95 wxCHECK_RET( canvas
, _T("invalid window in wxWindowDC") );
98 m_hDC
= (WXHDC
) ::GetWindowDC(GetHwndOf(m_canvas
));
100 // m_bOwnsDC was already set to false in the base class ctor, so the DC
101 // will be released (and not deleted) in ~wxDC
105 void wxWindowDC::InitDC()
107 // the background mode is only used for text background and is set in
108 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
109 ::SetBkMode(GetHdc(), TRANSPARENT
);
111 // default bg colour is pne of the window
112 SetBackground(wxBrush(m_canvas
->GetBackgroundColour(), wxSOLID
));
114 // since we are a window dc we need to grab the palette from the window
120 void wxWindowDC::DoGetSize(int *width
, int *height
) const
122 wxCHECK_RET( m_canvas
, _T("wxWindowDC without a window?") );
124 m_canvas
->GetSize(width
, height
);
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 wxClientDC::wxClientDC()
136 wxClientDC::wxClientDC(wxWindow
*canvas
)
138 wxCHECK_RET( canvas
, _T("invalid window in wxClientDC") );
141 m_hDC
= (WXHDC
)::GetDC(GetHwndOf(m_canvas
));
143 // m_bOwnsDC was already set to false in the base class ctor, so the DC
144 // will be released (and not deleted) in ~wxDC
149 void wxClientDC::InitDC()
151 wxWindowDC::InitDC();
153 // in wxUniv build we must manually do some DC adjustments usually
154 // performed by Windows for us
156 // we also need to take the menu/toolbar manually into account under
157 // Windows CE because they're just another control there, not anything
158 // special as usually under Windows
159 #if defined(__WXUNIVERSAL__) || defined(__WXWINCE__)
160 wxPoint ptOrigin
= m_canvas
->GetClientAreaOrigin();
161 if ( ptOrigin
.x
|| ptOrigin
.y
)
163 // no need to shift DC origin if shift is null
164 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
167 // clip the DC to avoid overwriting the non client area
168 SetClippingRegion(wxPoint(0,0), m_canvas
->GetClientSize());
169 #endif // __WXUNIVERSAL__ || __WXWINCE__
172 wxClientDC::~wxClientDC()
176 void wxClientDC::DoGetSize(int *width
, int *height
) const
178 wxCHECK_RET( m_canvas
, _T("wxClientDC without a window?") );
180 m_canvas
->GetClientSize(width
, height
);
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
188 // created and tried to reuse it - this was supposed to take care of a
189 // situation when a derived class OnPaint() calls base class OnPaint()
190 // because in this case ::BeginPaint() shouldn't be called second time.
192 // I'm not sure how useful this is, however we must remember the HWND
193 // associated with the last HDC as well - otherwise we may (and will!) try
194 // to reuse the HDC for another HWND which is a nice recipe for disaster.
196 // So we store a list of windows for which we already have the DC and not
197 // just one single hDC. This seems to work, but I'm really not sure about
198 // the usefullness of the whole idea - IMHO it's much better to not call
199 // base class OnPaint() at all, or, if we really want to allow it, add a
200 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
201 // !NULL instead of creating a new DC.
203 wxArrayDCInfo
wxPaintDC::ms_cache
;
205 wxPaintDC::wxPaintDC()
210 wxPaintDC::wxPaintDC(wxWindow
*canvas
)
212 wxCHECK_RET( canvas
, wxT("NULL canvas in wxPaintDC ctor") );
215 if ( g_isPainting
<= 0 )
217 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
221 #endif // __WXDEBUG__
225 // do we have a DC for this window in the cache?
226 wxPaintDCInfo
*info
= FindInCache();
232 else // not in cache, create a new one
234 m_hDC
= (WXHDC
)::BeginPaint(GetHwndOf(m_canvas
), &g_paintStruct
);
236 ms_cache
.Add(new wxPaintDCInfo(m_canvas
, this));
239 // (re)set the DC parameters.
240 // Note: at this point m_hDC can be NULL under MicroWindows, when dragging.
245 wxPaintDC::~wxPaintDC()
249 SelectOldObjects(m_hDC
);
252 wxPaintDCInfo
*info
= FindInCache(&index
);
254 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
256 if ( --info
->count
== 0 )
258 ::EndPaint(GetHwndOf(m_canvas
), &g_paintStruct
);
260 ms_cache
.RemoveAt(index
);
262 // Reduce the number of bogus reports of non-freed memory
264 if (ms_cache
.IsEmpty())
267 //else: cached DC entry is still in use
269 // prevent the base class dtor from ReleaseDC()ing it again
274 wxPaintDCInfo
*wxPaintDC::FindInCache(size_t *index
) const
276 wxPaintDCInfo
*info
= NULL
;
277 size_t nCache
= ms_cache
.GetCount();
278 for ( size_t n
= 0; n
< nCache
; n
++ )
280 wxPaintDCInfo
*info1
= &ms_cache
[n
];
281 if ( info1
->hwnd
== m_canvas
->GetHWND() )
293 // find the entry for this DC in the cache (keyed by the window)
294 WXHDC
wxPaintDC::FindDCInCache(wxWindow
* win
)
296 size_t nCache
= ms_cache
.GetCount();
297 for ( size_t n
= 0; n
< nCache
; n
++ )
299 wxPaintDCInfo
*info
= &ms_cache
[n
];
300 if ( info
->hwnd
== win
->GetHWND() )
312 // TODO: don't duplicate wxPaintDC code here!!
314 wxPaintDCEx::wxPaintDCEx(wxWindow
*canvas
, WXHDC dc
) : saveState(0)
316 wxCHECK_RET( dc
, wxT("wxPaintDCEx requires an existing device context") );
320 wxPaintDCInfo
*info
= FindInCache();
326 else // not in cache, create a new one
329 ms_cache
.Add(new wxPaintDCInfo(m_canvas
, this));
330 saveState
= SaveDC((HDC
) dc
);
334 wxPaintDCEx::~wxPaintDCEx()
337 wxPaintDCInfo
*info
= FindInCache(&index
);
339 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
341 if ( --info
->count
== 0 )
343 RestoreDC((HDC
) m_hDC
, saveState
);
344 ms_cache
.RemoveAt(index
);
346 // Reduce the number of bogus reports of non-freed memory
348 if (ms_cache
.IsEmpty())
351 //else: cached DC entry is still in use
353 // prevent the base class dtor from ReleaseDC()ing it again