]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dcclient.cpp
remove default wxDC ctor to make it impossible to construct DCs without the associate...
[wxWidgets.git] / src / msw / dcclient.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dcclient.cpp
3 // Purpose: wxClientDC class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/dcclient.h"
28 #include "wx/msw/dcclient.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/string.h"
32 #include "wx/log.h"
33 #include "wx/window.h"
34 #endif
35
36 #include "wx/msw/private.h"
37
38 // ----------------------------------------------------------------------------
39 // array/list types
40 // ----------------------------------------------------------------------------
41
42 struct WXDLLEXPORT wxPaintDCInfo
43 {
44 wxPaintDCInfo(wxWindow *win, wxPaintDCImpl *dc)
45 {
46 hwnd = win->GetHWND();
47 hdc = dc->GetHDC();
48 count = 1;
49 }
50
51 WXHWND hwnd; // window for this DC
52 WXHDC hdc; // the DC handle
53 size_t count; // usage count
54 };
55
56 #include "wx/arrimpl.cpp"
57
58 WX_DEFINE_OBJARRAY(wxArrayDCInfo)
59
60 // ----------------------------------------------------------------------------
61 // macros
62 // ----------------------------------------------------------------------------
63
64 // ----------------------------------------------------------------------------
65 // global variables
66 // ----------------------------------------------------------------------------
67
68 static PAINTSTRUCT g_paintStruct;
69
70 #ifdef __WXDEBUG__
71 // a global variable which we check to verify that wxPaintDC are only
72 // created in response to WM_PAINT message - doing this from elsewhere is a
73 // common programming error among wxWidgets programmers and might lead to
74 // very subtle and difficult to debug refresh/repaint bugs.
75 int g_isPainting = 0;
76 #endif // __WXDEBUG__
77
78 // ===========================================================================
79 // implementation
80 // ===========================================================================
81
82 // ----------------------------------------------------------------------------
83 // wxMSWWindowDCImpl
84 // ----------------------------------------------------------------------------
85
86 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxMSWDCImpl)
87
88 wxWindowDCImpl::wxWindowDCImpl( wxDC *owner ) :
89 wxMSWDCImpl( owner )
90 {
91 }
92
93 wxWindowDCImpl::wxWindowDCImpl( wxDC *owner, wxWindow *window ) :
94 wxMSWDCImpl( owner )
95 {
96 wxCHECK_RET( window, _T("invalid window in wxWindowDCImpl") );
97
98 m_window = window;
99 m_hDC = (WXHDC) ::GetWindowDC(GetHwndOf(m_window));
100
101 // m_bOwnsDC was already set to false in the base class ctor, so the DC
102 // will be released (and not deleted) in ~wxDC
103 InitDC();
104 }
105
106 void wxWindowDCImpl::InitDC()
107 {
108 // the background mode is only used for text background and is set in
109 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
110 ::SetBkMode(GetHdc(), TRANSPARENT);
111
112 // default bg colour is pne of the window
113 SetBackground(wxBrush(m_window->GetBackgroundColour(), wxSOLID));
114
115 // since we are a window dc we need to grab the palette from the window
116 #if wxUSE_PALETTE
117 InitializePalette();
118 #endif
119 }
120
121 void wxWindowDCImpl::DoGetSize(int *width, int *height) const
122 {
123 wxCHECK_RET( m_window, _T("wxWindowDCImpl without a window?") );
124
125 m_window->GetSize(width, height);
126 }
127
128 // ----------------------------------------------------------------------------
129 // wxClientDCImpl
130 // ----------------------------------------------------------------------------
131
132 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
133
134 wxClientDCImpl::wxClientDCImpl( wxDC *owner ) :
135 wxWindowDCImpl( owner )
136 {
137 }
138
139 wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *window ) :
140 wxWindowDCImpl( owner )
141 {
142 wxCHECK_RET( window, _T("invalid window in wxClientDCImpl") );
143
144 m_window = window;
145 m_hDC = (WXHDC)::GetDC(GetHwndOf(window));
146
147 // m_bOwnsDC was already set to false in the base class ctor, so the DC
148 // will be released (and not deleted) in ~wxDC
149
150 InitDC();
151 }
152
153 void wxClientDCImpl::InitDC()
154 {
155 wxWindowDCImpl::InitDC();
156
157 // in wxUniv build we must manually do some DC adjustments usually
158 // performed by Windows for us
159 //
160 // we also need to take the menu/toolbar manually into account under
161 // Windows CE because they're just another control there, not anything
162 // special as usually under Windows
163 #if defined(__WXUNIVERSAL__) || defined(__WXWINCE__)
164 wxPoint ptOrigin = m_window->GetClientAreaOrigin();
165 if ( ptOrigin.x || ptOrigin.y )
166 {
167 // no need to shift DC origin if shift is null
168 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
169 }
170
171 // clip the DC to avoid overwriting the non client area
172 SetClippingRegion(wxPoint(0,0), m_window->GetClientSize());
173 #endif // __WXUNIVERSAL__ || __WXWINCE__
174 }
175
176 wxClientDCImpl::~wxClientDCImpl()
177 {
178 }
179
180 void wxClientDCImpl::DoGetSize(int *width, int *height) const
181 {
182 wxCHECK_RET( m_window, _T("wxClientDCImpl without a window?") );
183
184 m_window->GetClientSize(width, height);
185 }
186
187 // ----------------------------------------------------------------------------
188 // wxPaintDCImpl
189 // ----------------------------------------------------------------------------
190
191 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
192 // created and tried to reuse it - this was supposed to take care of a
193 // situation when a derived class OnPaint() calls base class OnPaint()
194 // because in this case ::BeginPaint() shouldn't be called second time.
195 //
196 // I'm not sure how useful this is, however we must remember the HWND
197 // associated with the last HDC as well - otherwise we may (and will!) try
198 // to reuse the HDC for another HWND which is a nice recipe for disaster.
199 //
200 // So we store a list of windows for which we already have the DC and not
201 // just one single hDC. This seems to work, but I'm really not sure about
202 // the usefullness of the whole idea - IMHO it's much better to not call
203 // base class OnPaint() at all, or, if we really want to allow it, add a
204 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
205 // !NULL instead of creating a new DC.
206
207 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxClientDCImpl)
208
209 wxArrayDCInfo wxPaintDCImpl::ms_cache;
210
211 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner ) :
212 wxClientDCImpl( owner )
213 {
214 }
215
216 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *window ) :
217 wxClientDCImpl( owner )
218 {
219 wxCHECK_RET( window, wxT("NULL canvas in wxPaintDCImpl ctor") );
220
221 #ifdef __WXDEBUG__
222 if ( g_isPainting <= 0 )
223 {
224 wxFAIL_MSG( wxT("wxPaintDCImpl may be created only in EVT_PAINT handler!") );
225
226 return;
227 }
228 #endif // __WXDEBUG__
229
230 m_window = window;
231
232 // do we have a DC for this window in the cache?
233 wxPaintDCInfo *info = FindInCache();
234 if ( info )
235 {
236 m_hDC = info->hdc;
237 info->count++;
238 }
239 else // not in cache, create a new one
240 {
241 m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_window), &g_paintStruct);
242 if (m_hDC)
243 ms_cache.Add(new wxPaintDCInfo(m_window, this));
244 }
245
246 // Note: at this point m_hDC can be NULL under MicroWindows, when dragging.
247 if (!GetHDC())
248 return;
249
250 // (re)set the DC parameters.
251 InitDC();
252
253 // the HDC can have a clipping box (which we didn't set), make sure our
254 // DoGetClippingBox() checks for it
255 m_clipping = true;
256 }
257
258 wxPaintDCImpl::~wxPaintDCImpl()
259 {
260 if ( m_hDC )
261 {
262 SelectOldObjects(m_hDC);
263
264 size_t index;
265 wxPaintDCInfo *info = FindInCache(&index);
266
267 wxCHECK_RET( info, wxT("existing DC should have a cache entry") );
268
269 if ( --info->count == 0 )
270 {
271 ::EndPaint(GetHwndOf(m_window), &g_paintStruct);
272
273 ms_cache.RemoveAt(index);
274
275 // Reduce the number of bogus reports of non-freed memory
276 // at app exit
277 if (ms_cache.IsEmpty())
278 ms_cache.Clear();
279 }
280 //else: cached DC entry is still in use
281
282 // prevent the base class dtor from ReleaseDC()ing it again
283 m_hDC = 0;
284 }
285 }
286
287 wxPaintDCInfo *wxPaintDCImpl::FindInCache(size_t *index) const
288 {
289 wxPaintDCInfo *info = NULL;
290 size_t nCache = ms_cache.GetCount();
291 for ( size_t n = 0; n < nCache; n++ )
292 {
293 wxPaintDCInfo *info1 = &ms_cache[n];
294 if ( info1->hwnd == m_window->GetHWND() )
295 {
296 info = info1;
297 if ( index )
298 *index = n;
299 break;
300 }
301 }
302
303 return info;
304 }
305
306 // find the entry for this DC in the cache (keyed by the window)
307 WXHDC wxPaintDCImpl::FindDCInCache(wxWindow* win)
308 {
309 size_t nCache = ms_cache.GetCount();
310 for ( size_t n = 0; n < nCache; n++ )
311 {
312 wxPaintDCInfo *info = &ms_cache[n];
313 if ( info->hwnd == win->GetHWND() )
314 {
315 return info->hdc;
316 }
317 }
318 return 0;
319 }
320
321 /*
322 * wxPaintDCEx
323 */
324
325 // TODO: don't duplicate wxPaintDCImpl code here!!
326
327 class wxPaintDCExImpl: public wxPaintDCImpl
328 {
329 public:
330 wxPaintDCExImpl( wxDC *owner, wxWindow *window, WXHDC dc );
331 ~wxPaintDCExImpl();
332
333 int m_saveState;
334 };
335
336
337 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCEx,wxPaintDC)
338
339 wxPaintDCEx::wxPaintDCEx(wxWindow *window, WXHDC dc)
340 : wxPaintDC(new wxPaintDCExImpl(this, window, dc))
341 {
342 }
343
344 wxPaintDCExImpl::wxPaintDCExImpl(wxDC *owner, wxWindow *window, WXHDC dc)
345 : wxPaintDCImpl( owner )
346 {
347 wxCHECK_RET( dc, wxT("wxPaintDCEx requires an existing device context") );
348
349 m_saveState = 0;
350
351 m_window = window;
352
353 wxPaintDCInfo *info = FindInCache();
354 if ( info )
355 {
356 m_hDC = info->hdc;
357 info->count++;
358 }
359 else // not in cache, create a new one
360 {
361 m_hDC = dc;
362 ms_cache.Add(new wxPaintDCInfo(m_window, this));
363 m_saveState = SaveDC((HDC) dc);
364 }
365 }
366
367 wxPaintDCExImpl::~wxPaintDCExImpl()
368 {
369 size_t index;
370 wxPaintDCInfo *info = FindInCache(&index);
371
372 wxCHECK_RET( info, wxT("existing DC should have a cache entry") );
373
374 if ( --info->count == 0 )
375 {
376 RestoreDC((HDC) m_hDC, m_saveState);
377 ms_cache.RemoveAt(index);
378
379 // Reduce the number of bogus reports of non-freed memory
380 // at app exit
381 if (ms_cache.IsEmpty())
382 ms_cache.Clear();
383 }
384 //else: cached DC entry is still in use
385
386 // prevent the base class dtor from ReleaseDC()ing it again
387 m_hDC = 0;
388 }
389
390