]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dcclient.cpp
docopydocs is not needed for this script.
[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
29 #ifndef WX_PRECOMP
30 #include "wx/string.h"
31 #include "wx/log.h"
32 #endif
33
34 #include "wx/window.h"
35
36 #include "wx/msw/private.h"
37
38 // ----------------------------------------------------------------------------
39 // array/list types
40 // ----------------------------------------------------------------------------
41
42 struct WXDLLEXPORT wxPaintDCInfo
43 {
44 wxPaintDCInfo(wxWindow *win, wxDC *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 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
65 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
66 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxClientDC)
67 IMPLEMENT_CLASS(wxPaintDCEx, wxPaintDC)
68
69 // ----------------------------------------------------------------------------
70 // global variables
71 // ----------------------------------------------------------------------------
72
73 static PAINTSTRUCT g_paintStruct;
74
75 #ifdef __WXDEBUG__
76 // a global variable which we check to verify that wxPaintDC are only
77 // created in response to WM_PAINT message - doing this from elsewhere is a
78 // common programming error among wxWidgets programmers and might lead to
79 // very subtle and difficult to debug refresh/repaint bugs.
80 int g_isPainting = 0;
81 #endif // __WXDEBUG__
82
83 // ===========================================================================
84 // implementation
85 // ===========================================================================
86
87 // ----------------------------------------------------------------------------
88 // wxWindowDC
89 // ----------------------------------------------------------------------------
90
91 wxWindowDC::wxWindowDC()
92 {
93 m_canvas = NULL;
94 }
95
96 wxWindowDC::wxWindowDC(wxWindow *canvas)
97 {
98 wxCHECK_RET( canvas, _T("invalid window in wxWindowDC") );
99
100 m_canvas = canvas;
101 m_hDC = (WXHDC) ::GetWindowDC(GetHwndOf(m_canvas));
102
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
105 InitDC();
106 }
107
108 void wxWindowDC::InitDC()
109 {
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);
113
114 // default bg colour is pne of the window
115 SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID));
116
117 // since we are a window dc we need to grab the palette from the window
118 #if wxUSE_PALETTE
119 InitializePalette();
120 #endif
121 }
122
123 void wxWindowDC::DoGetSize(int *width, int *height) const
124 {
125 wxCHECK_RET( m_canvas, _T("wxWindowDC without a window?") );
126
127 m_canvas->GetSize(width, height);
128 }
129
130 // ----------------------------------------------------------------------------
131 // wxClientDC
132 // ----------------------------------------------------------------------------
133
134 wxClientDC::wxClientDC()
135 {
136 m_canvas = NULL;
137 }
138
139 wxClientDC::wxClientDC(wxWindow *canvas)
140 {
141 wxCHECK_RET( canvas, _T("invalid window in wxClientDC") );
142
143 m_canvas = canvas;
144 m_hDC = (WXHDC)::GetDC(GetHwndOf(m_canvas));
145
146 // m_bOwnsDC was already set to false in the base class ctor, so the DC
147 // will be released (and not deleted) in ~wxDC
148
149 InitDC();
150 }
151
152 void wxClientDC::InitDC()
153 {
154 wxWindowDC::InitDC();
155
156 // in wxUniv build we must manually do some DC adjustments usually
157 // performed by Windows for us
158 //
159 // we also need to take the menu/toolbar manually into account under
160 // Windows CE because they're just another control there, not anything
161 // special as usually under Windows
162 #if defined(__WXUNIVERSAL__) || defined(__WXWINCE__)
163 wxPoint ptOrigin = m_canvas->GetClientAreaOrigin();
164 if ( ptOrigin.x || ptOrigin.y )
165 {
166 // no need to shift DC origin if shift is null
167 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
168 }
169
170 // clip the DC to avoid overwriting the non client area
171 SetClippingRegion(wxPoint(0,0), m_canvas->GetClientSize());
172 #endif // __WXUNIVERSAL__ || __WXWINCE__
173 }
174
175 wxClientDC::~wxClientDC()
176 {
177 }
178
179 void wxClientDC::DoGetSize(int *width, int *height) const
180 {
181 wxCHECK_RET( m_canvas, _T("wxClientDC without a window?") );
182
183 m_canvas->GetClientSize(width, height);
184 }
185
186 // ----------------------------------------------------------------------------
187 // wxPaintDC
188 // ----------------------------------------------------------------------------
189
190 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
191 // created and tried to reuse it - this was supposed to take care of a
192 // situation when a derived class OnPaint() calls base class OnPaint()
193 // because in this case ::BeginPaint() shouldn't be called second time.
194 //
195 // I'm not sure how useful this is, however we must remember the HWND
196 // associated with the last HDC as well - otherwise we may (and will!) try
197 // to reuse the HDC for another HWND which is a nice recipe for disaster.
198 //
199 // So we store a list of windows for which we already have the DC and not
200 // just one single hDC. This seems to work, but I'm really not sure about
201 // the usefullness of the whole idea - IMHO it's much better to not call
202 // base class OnPaint() at all, or, if we really want to allow it, add a
203 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
204 // !NULL instead of creating a new DC.
205
206 wxArrayDCInfo wxPaintDC::ms_cache;
207
208 wxPaintDC::wxPaintDC()
209 {
210 m_canvas = NULL;
211 }
212
213 wxPaintDC::wxPaintDC(wxWindow *canvas)
214 {
215 wxCHECK_RET( canvas, wxT("NULL canvas in wxPaintDC ctor") );
216
217 #ifdef __WXDEBUG__
218 if ( g_isPainting <= 0 )
219 {
220 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
221
222 return;
223 }
224 #endif // __WXDEBUG__
225
226 m_canvas = canvas;
227
228 // do we have a DC for this window in the cache?
229 wxPaintDCInfo *info = FindInCache();
230 if ( info )
231 {
232 m_hDC = info->hdc;
233 info->count++;
234 }
235 else // not in cache, create a new one
236 {
237 m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_canvas), &g_paintStruct);
238 if (m_hDC)
239 ms_cache.Add(new wxPaintDCInfo(m_canvas, this));
240 }
241
242 // (re)set the DC parameters.
243 // Note: at this point m_hDC can be NULL under MicroWindows, when dragging.
244 if (GetHDC())
245 InitDC();
246 }
247
248 wxPaintDC::~wxPaintDC()
249 {
250 if ( m_hDC )
251 {
252 SelectOldObjects(m_hDC);
253
254 size_t index;
255 wxPaintDCInfo *info = FindInCache(&index);
256
257 wxCHECK_RET( info, wxT("existing DC should have a cache entry") );
258
259 if ( --info->count == 0 )
260 {
261 ::EndPaint(GetHwndOf(m_canvas), &g_paintStruct);
262
263 ms_cache.RemoveAt(index);
264
265 // Reduce the number of bogus reports of non-freed memory
266 // at app exit
267 if (ms_cache.IsEmpty())
268 ms_cache.Clear();
269 }
270 //else: cached DC entry is still in use
271
272 // prevent the base class dtor from ReleaseDC()ing it again
273 m_hDC = 0;
274 }
275 }
276
277 wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const
278 {
279 wxPaintDCInfo *info = NULL;
280 size_t nCache = ms_cache.GetCount();
281 for ( size_t n = 0; n < nCache; n++ )
282 {
283 wxPaintDCInfo *info1 = &ms_cache[n];
284 if ( info1->hwnd == m_canvas->GetHWND() )
285 {
286 info = info1;
287 if ( index )
288 *index = n;
289 break;
290 }
291 }
292
293 return info;
294 }
295
296 // find the entry for this DC in the cache (keyed by the window)
297 WXHDC wxPaintDC::FindDCInCache(wxWindow* win)
298 {
299 size_t nCache = ms_cache.GetCount();
300 for ( size_t n = 0; n < nCache; n++ )
301 {
302 wxPaintDCInfo *info = &ms_cache[n];
303 if ( info->hwnd == win->GetHWND() )
304 {
305 return info->hdc;
306 }
307 }
308 return 0;
309 }
310
311 /*
312 * wxPaintDCEx
313 */
314
315 // TODO: don't duplicate wxPaintDC code here!!
316
317 wxPaintDCEx::wxPaintDCEx(wxWindow *canvas, WXHDC dc) : saveState(0)
318 {
319 wxCHECK_RET( dc, wxT("wxPaintDCEx requires an existing device context") );
320
321 m_canvas = canvas;
322
323 wxPaintDCInfo *info = FindInCache();
324 if ( info )
325 {
326 m_hDC = info->hdc;
327 info->count++;
328 }
329 else // not in cache, create a new one
330 {
331 m_hDC = dc;
332 ms_cache.Add(new wxPaintDCInfo(m_canvas, this));
333 saveState = SaveDC((HDC) dc);
334 }
335 }
336
337 wxPaintDCEx::~wxPaintDCEx()
338 {
339 size_t index;
340 wxPaintDCInfo *info = FindInCache(&index);
341
342 wxCHECK_RET( info, wxT("existing DC should have a cache entry") );
343
344 if ( --info->count == 0 )
345 {
346 RestoreDC((HDC) m_hDC, saveState);
347 ms_cache.RemoveAt(index);
348
349 // Reduce the number of bogus reports of non-freed memory
350 // at app exit
351 if (ms_cache.IsEmpty())
352 ms_cache.Clear();
353 }
354 //else: cached DC entry is still in use
355
356 // prevent the base class dtor from ReleaseDC()ing it again
357 m_hDC = 0;
358 }