1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dcclient.cpp
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/dcclient.h"
30 #include "wx/string.h"
32 #include "wx/window.h"
35 #include "wx/msw/private.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 struct WXDLLEXPORT wxPaintDCInfo
43 wxPaintDCInfo(wxWindow
*win
, wxDC
*dc
)
45 hwnd
= win
->GetHWND();
50 WXHWND hwnd
; // window for this DC
51 WXHDC hdc
; // the DC handle
52 size_t count
; // usage count
55 #include "wx/arrimpl.cpp"
57 WX_DEFINE_OBJARRAY(wxArrayDCInfo
)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
64 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
65 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxClientDC
)
66 IMPLEMENT_CLASS(wxPaintDCEx
, wxPaintDC
)
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 static PAINTSTRUCT g_paintStruct
;
75 // a global variable which we check to verify that wxPaintDC are only
76 // created in response to WM_PAINT message - doing this from elsewhere is a
77 // common programming error among wxWidgets programmers and might lead to
78 // very subtle and difficult to debug refresh/repaint bugs.
82 // ===========================================================================
84 // ===========================================================================
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 wxWindowDC::wxWindowDC()
95 wxWindowDC::wxWindowDC(wxWindow
*canvas
)
97 wxCHECK_RET( canvas
, _T("invalid window in wxWindowDC") );
100 m_hDC
= (WXHDC
) ::GetWindowDC(GetHwndOf(m_canvas
));
102 // m_bOwnsDC was already set to false in the base class ctor, so the DC
103 // will be released (and not deleted) in ~wxDC
107 void wxWindowDC::InitDC()
109 // the background mode is only used for text background and is set in
110 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
111 ::SetBkMode(GetHdc(), TRANSPARENT
);
113 // default bg colour is pne of the window
114 SetBackground(wxBrush(m_canvas
->GetBackgroundColour(), wxSOLID
));
116 // since we are a window dc we need to grab the palette from the window
122 void wxWindowDC::DoGetSize(int *width
, int *height
) const
124 wxCHECK_RET( m_canvas
, _T("wxWindowDC without a window?") );
126 m_canvas
->GetSize(width
, height
);
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 wxClientDC::wxClientDC()
138 wxClientDC::wxClientDC(wxWindow
*canvas
)
140 wxCHECK_RET( canvas
, _T("invalid window in wxClientDC") );
143 m_hDC
= (WXHDC
)::GetDC(GetHwndOf(m_canvas
));
145 // m_bOwnsDC was already set to false in the base class ctor, so the DC
146 // will be released (and not deleted) in ~wxDC
151 void wxClientDC::InitDC()
153 wxWindowDC::InitDC();
155 // in wxUniv build we must manually do some DC adjustments usually
156 // performed by Windows for us
158 // we also need to take the menu/toolbar manually into account under
159 // Windows CE because they're just another control there, not anything
160 // special as usually under Windows
161 #if defined(__WXUNIVERSAL__) || defined(__WXWINCE__)
162 wxPoint ptOrigin
= m_canvas
->GetClientAreaOrigin();
163 if ( ptOrigin
.x
|| ptOrigin
.y
)
165 // no need to shift DC origin if shift is null
166 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
169 // clip the DC to avoid overwriting the non client area
170 SetClippingRegion(wxPoint(0,0), m_canvas
->GetClientSize());
171 #endif // __WXUNIVERSAL__ || __WXWINCE__
174 wxClientDC::~wxClientDC()
178 void wxClientDC::DoGetSize(int *width
, int *height
) const
180 wxCHECK_RET( m_canvas
, _T("wxClientDC without a window?") );
182 m_canvas
->GetClientSize(width
, height
);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
190 // created and tried to reuse it - this was supposed to take care of a
191 // situation when a derived class OnPaint() calls base class OnPaint()
192 // because in this case ::BeginPaint() shouldn't be called second time.
194 // I'm not sure how useful this is, however we must remember the HWND
195 // associated with the last HDC as well - otherwise we may (and will!) try
196 // to reuse the HDC for another HWND which is a nice recipe for disaster.
198 // So we store a list of windows for which we already have the DC and not
199 // just one single hDC. This seems to work, but I'm really not sure about
200 // the usefullness of the whole idea - IMHO it's much better to not call
201 // base class OnPaint() at all, or, if we really want to allow it, add a
202 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
203 // !NULL instead of creating a new DC.
205 wxArrayDCInfo
wxPaintDC::ms_cache
;
207 wxPaintDC::wxPaintDC()
212 wxPaintDC::wxPaintDC(wxWindow
*canvas
)
214 wxCHECK_RET( canvas
, wxT("NULL canvas in wxPaintDC ctor") );
217 if ( g_isPainting
<= 0 )
219 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
223 #endif // __WXDEBUG__
227 // do we have a DC for this window in the cache?
228 wxPaintDCInfo
*info
= FindInCache();
234 else // not in cache, create a new one
236 m_hDC
= (WXHDC
)::BeginPaint(GetHwndOf(m_canvas
), &g_paintStruct
);
238 ms_cache
.Add(new wxPaintDCInfo(m_canvas
, this));
241 // Note: at this point m_hDC can be NULL under MicroWindows, when dragging.
245 // (re)set the DC parameters.
248 // the HDC can have a clipping box (which we didn't set), make sure our
249 // DoGetClippingBox() checks for it
253 wxPaintDC::~wxPaintDC()
257 SelectOldObjects(m_hDC
);
260 wxPaintDCInfo
*info
= FindInCache(&index
);
262 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
264 if ( --info
->count
== 0 )
266 ::EndPaint(GetHwndOf(m_canvas
), &g_paintStruct
);
268 ms_cache
.RemoveAt(index
);
270 // Reduce the number of bogus reports of non-freed memory
272 if (ms_cache
.IsEmpty())
275 //else: cached DC entry is still in use
277 // prevent the base class dtor from ReleaseDC()ing it again
282 wxPaintDCInfo
*wxPaintDC::FindInCache(size_t *index
) const
284 wxPaintDCInfo
*info
= NULL
;
285 size_t nCache
= ms_cache
.GetCount();
286 for ( size_t n
= 0; n
< nCache
; n
++ )
288 wxPaintDCInfo
*info1
= &ms_cache
[n
];
289 if ( info1
->hwnd
== m_canvas
->GetHWND() )
301 // find the entry for this DC in the cache (keyed by the window)
302 WXHDC
wxPaintDC::FindDCInCache(wxWindow
* win
)
304 size_t nCache
= ms_cache
.GetCount();
305 for ( size_t n
= 0; n
< nCache
; n
++ )
307 wxPaintDCInfo
*info
= &ms_cache
[n
];
308 if ( info
->hwnd
== win
->GetHWND() )
320 // TODO: don't duplicate wxPaintDC code here!!
322 wxPaintDCEx::wxPaintDCEx(wxWindow
*canvas
, WXHDC dc
) : saveState(0)
324 wxCHECK_RET( dc
, wxT("wxPaintDCEx requires an existing device context") );
328 wxPaintDCInfo
*info
= FindInCache();
334 else // not in cache, create a new one
337 ms_cache
.Add(new wxPaintDCInfo(m_canvas
, this));
338 saveState
= SaveDC((HDC
) dc
);
342 wxPaintDCEx::~wxPaintDCEx()
345 wxPaintDCInfo
*info
= FindInCache(&index
);
347 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
349 if ( --info
->count
== 0 )
351 RestoreDC((HDC
) m_hDC
, saveState
);
352 ms_cache
.RemoveAt(index
);
354 // Reduce the number of bogus reports of non-freed memory
356 if (ms_cache
.IsEmpty())
359 //else: cached DC entry is still in use
361 // prevent the base class dtor from ReleaseDC()ing it again