]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcclient.cpp
flags moved to chkconf
[wxWidgets.git] / src / msw / dcclient.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
df91131c 2// Name: src/msw/dcclient.cpp
2bda0e17
KB
3// Purpose: wxClientDC class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
c6eba8f8
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
c6eba8f8 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
df91131c
WS
27#include "wx/dcclient.h"
28
29#ifndef WX_PRECOMP
30 #include "wx/string.h"
e4db172a 31 #include "wx/log.h"
cdccdfab 32 #include "wx/window.h"
df91131c
WS
33#endif
34
c6eba8f8
VZ
35#include "wx/msw/private.h"
36
3a5ffa81
VZ
37// ----------------------------------------------------------------------------
38// array/list types
39// ----------------------------------------------------------------------------
40
41struct WXDLLEXPORT wxPaintDCInfo
42{
43 wxPaintDCInfo(wxWindow *win, wxDC *dc)
44 {
45 hwnd = win->GetHWND();
46 hdc = dc->GetHDC();
47 count = 1;
48 }
49
50 WXHWND hwnd; // window for this DC
51 WXHDC hdc; // the DC handle
52 size_t count; // usage count
53};
54
55#include "wx/arrimpl.cpp"
56
259c43f6 57WX_DEFINE_OBJARRAY(wxArrayDCInfo)
3a5ffa81 58
c6eba8f8
VZ
59// ----------------------------------------------------------------------------
60// macros
61// ----------------------------------------------------------------------------
2bda0e17 62
7ba4fbeb
VZ
63IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
64IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
1e6feb95 65IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxClientDC)
c6151f2a 66IMPLEMENT_CLASS(wxPaintDCEx, wxPaintDC)
2bda0e17 67
c6eba8f8
VZ
68// ----------------------------------------------------------------------------
69// global variables
70// ----------------------------------------------------------------------------
71
72static PAINTSTRUCT g_paintStruct;
73
74#ifdef __WXDEBUG__
75 // a global variable which we check to verify that wxPaintDC are only
a0d9c6cb 76 // created in response to WM_PAINT message - doing this from elsewhere is a
77ffb593 77 // common programming error among wxWidgets programmers and might lead to
c6eba8f8 78 // very subtle and difficult to debug refresh/repaint bugs.
edccf428 79 int g_isPainting = 0;
c6eba8f8
VZ
80#endif // __WXDEBUG__
81
82// ===========================================================================
83// implementation
84// ===========================================================================
e6460682 85
c6eba8f8
VZ
86// ----------------------------------------------------------------------------
87// wxWindowDC
88// ----------------------------------------------------------------------------
89
90wxWindowDC::wxWindowDC()
2bda0e17 91{
7ba4fbeb 92 m_canvas = NULL;
2bda0e17
KB
93}
94
7ba4fbeb 95wxWindowDC::wxWindowDC(wxWindow *canvas)
2bda0e17 96{
7ba4fbeb 97 wxCHECK_RET( canvas, _T("invalid window in wxWindowDC") );
a91b47e8 98
7ba4fbeb
VZ
99 m_canvas = canvas;
100 m_hDC = (WXHDC) ::GetWindowDC(GetHwndOf(m_canvas));
2bda0e17 101
7ba4fbeb
VZ
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
7ba4fbeb
VZ
104 InitDC();
105}
edccf428 106
7ba4fbeb
VZ
107void wxWindowDC::InitDC()
108{
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);
c6eba8f8 112
7ba4fbeb
VZ
113 // default bg colour is pne of the window
114 SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID));
574c939e
KB
115
116 // since we are a window dc we need to grab the palette from the window
117#if wxUSE_PALETTE
118 InitializePalette();
119#endif
2bda0e17
KB
120}
121
994a3786
VZ
122void wxWindowDC::DoGetSize(int *width, int *height) const
123{
124 wxCHECK_RET( m_canvas, _T("wxWindowDC without a window?") );
125
126 m_canvas->GetSize(width, height);
127}
128
c6eba8f8
VZ
129// ----------------------------------------------------------------------------
130// wxClientDC
131// ----------------------------------------------------------------------------
e6460682 132
c6eba8f8 133wxClientDC::wxClientDC()
2bda0e17 134{
7ba4fbeb 135 m_canvas = NULL;
2bda0e17
KB
136}
137
7ba4fbeb 138wxClientDC::wxClientDC(wxWindow *canvas)
2bda0e17 139{
7ba4fbeb 140 wxCHECK_RET( canvas, _T("invalid window in wxClientDC") );
a91b47e8 141
7ba4fbeb
VZ
142 m_canvas = canvas;
143 m_hDC = (WXHDC)::GetDC(GetHwndOf(m_canvas));
2bda0e17 144
7ba4fbeb
VZ
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
2bda0e17 147
7ba4fbeb 148 InitDC();
2bda0e17
KB
149}
150
1e6feb95
VZ
151void wxClientDC::InitDC()
152{
153 wxWindowDC::InitDC();
154
155 // in wxUniv build we must manually do some DC adjustments usually
156 // performed by Windows for us
1fa6ebf7
VZ
157 //
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__)
1e6feb95
VZ
162 wxPoint ptOrigin = m_canvas->GetClientAreaOrigin();
163 if ( ptOrigin.x || ptOrigin.y )
164 {
165 // no need to shift DC origin if shift is null
166 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
167 }
168
169 // clip the DC to avoid overwriting the non client area
c47addef 170 SetClippingRegion(wxPoint(0,0), m_canvas->GetClientSize());
1fa6ebf7 171#endif // __WXUNIVERSAL__ || __WXWINCE__
1e6feb95
VZ
172}
173
174wxClientDC::~wxClientDC()
175{
176}
177
994a3786
VZ
178void wxClientDC::DoGetSize(int *width, int *height) const
179{
180 wxCHECK_RET( m_canvas, _T("wxClientDC without a window?") );
181
182 m_canvas->GetClientSize(width, height);
183}
184
c6eba8f8
VZ
185// ----------------------------------------------------------------------------
186// wxPaintDC
187// ----------------------------------------------------------------------------
e6460682 188
3a5ffa81 189// VZ: initial implementation (by JACS) only remembered the last wxPaintDC
7ba4fbeb 190// created and tried to reuse it - this was supposed to take care of a
3a5ffa81
VZ
191// situation when a derived class OnPaint() calls base class OnPaint()
192// because in this case ::BeginPaint() shouldn't be called second time.
193//
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.
197//
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
85833f5c 201// base class OnPaint() at all, or, if we really want to allow it, add a
3a5ffa81
VZ
202// "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
203// !NULL instead of creating a new DC.
204
205wxArrayDCInfo wxPaintDC::ms_cache;
2bda0e17 206
c6eba8f8
VZ
207wxPaintDC::wxPaintDC()
208{
3a5ffa81 209 m_canvas = NULL;
c6eba8f8
VZ
210}
211
83626bfa 212wxPaintDC::wxPaintDC(wxWindow *canvas)
2bda0e17 213{
223d09f6 214 wxCHECK_RET( canvas, wxT("NULL canvas in wxPaintDC ctor") );
3a5ffa81 215
c455ab93 216#ifdef __WXDEBUG__
edccf428 217 if ( g_isPainting <= 0 )
3a5ffa81 218 {
223d09f6 219 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
2bda0e17 220
3a5ffa81
VZ
221 return;
222 }
223#endif // __WXDEBUG__
c6eba8f8 224
3a5ffa81 225 m_canvas = canvas;
83626bfa 226
3a5ffa81
VZ
227 // do we have a DC for this window in the cache?
228 wxPaintDCInfo *info = FindInCache();
229 if ( info )
230 {
231 m_hDC = info->hdc;
232 info->count++;
233 }
234 else // not in cache, create a new one
235 {
7ba4fbeb 236 m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_canvas), &g_paintStruct);
1fa6ebf7 237 if (m_hDC)
5adad466 238 ms_cache.Add(new wxPaintDCInfo(m_canvas, this));
3a5ffa81 239 }
a91b47e8 240
5adad466 241 // Note: at this point m_hDC can be NULL under MicroWindows, when dragging.
27539df8
VZ
242 if (!GetHDC())
243 return;
244
245 // (re)set the DC parameters.
246 InitDC();
247
248 // the HDC can have a clipping box (which we didn't set), make sure our
249 // DoGetClippingBox() checks for it
e9f8a82e 250 m_clipping = true;
2bda0e17
KB
251}
252
83626bfa 253wxPaintDC::~wxPaintDC()
2bda0e17 254{
3a5ffa81
VZ
255 if ( m_hDC )
256 {
edccf428
VZ
257 SelectOldObjects(m_hDC);
258
3a5ffa81
VZ
259 size_t index;
260 wxPaintDCInfo *info = FindInCache(&index);
261
223d09f6 262 wxCHECK_RET( info, wxT("existing DC should have a cache entry") );
3a5ffa81 263
8d7eaf91 264 if ( --info->count == 0 )
3a5ffa81 265 {
7ba4fbeb 266 ::EndPaint(GetHwndOf(m_canvas), &g_paintStruct);
3a5ffa81 267
b54e41c5 268 ms_cache.RemoveAt(index);
2bc1aa11
JS
269
270 // Reduce the number of bogus reports of non-freed memory
271 // at app exit
272 if (ms_cache.IsEmpty())
273 ms_cache.Clear();
3a5ffa81
VZ
274 }
275 //else: cached DC entry is still in use
edccf428
VZ
276
277 // prevent the base class dtor from ReleaseDC()ing it again
278 m_hDC = 0;
1c089c47 279 }
2bda0e17 280}
81d66cf3 281
3a5ffa81
VZ
282wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const
283{
284 wxPaintDCInfo *info = NULL;
285 size_t nCache = ms_cache.GetCount();
286 for ( size_t n = 0; n < nCache; n++ )
287 {
968bed8c
JS
288 wxPaintDCInfo *info1 = &ms_cache[n];
289 if ( info1->hwnd == m_canvas->GetHWND() )
3a5ffa81 290 {
968bed8c 291 info = info1;
3a5ffa81
VZ
292 if ( index )
293 *index = n;
294 break;
295 }
296 }
297
298 return info;
299}
63da7df7
JS
300
301// find the entry for this DC in the cache (keyed by the window)
302WXHDC wxPaintDC::FindDCInCache(wxWindow* win)
303{
63da7df7
JS
304 size_t nCache = ms_cache.GetCount();
305 for ( size_t n = 0; n < nCache; n++ )
306 {
999836aa 307 wxPaintDCInfo *info = &ms_cache[n];
63da7df7
JS
308 if ( info->hwnd == win->GetHWND() )
309 {
310 return info->hdc;
311 }
312 }
313 return 0;
314}
315
c6151f2a
JS
316/*
317 * wxPaintDCEx
318 */
d71cc120 319
fbaf7d14
VZ
320// TODO: don't duplicate wxPaintDC code here!!
321
c6151f2a
JS
322wxPaintDCEx::wxPaintDCEx(wxWindow *canvas, WXHDC dc) : saveState(0)
323{
fbaf7d14 324 wxCHECK_RET( dc, wxT("wxPaintDCEx requires an existing device context") );
c6151f2a
JS
325
326 m_canvas = canvas;
327
328 wxPaintDCInfo *info = FindInCache();
329 if ( info )
330 {
331 m_hDC = info->hdc;
332 info->count++;
333 }
334 else // not in cache, create a new one
335 {
336 m_hDC = dc;
337 ms_cache.Add(new wxPaintDCInfo(m_canvas, this));
338 saveState = SaveDC((HDC) dc);
339 }
340}
341
342wxPaintDCEx::~wxPaintDCEx()
343{
344 size_t index;
345 wxPaintDCInfo *info = FindInCache(&index);
346
347 wxCHECK_RET( info, wxT("existing DC should have a cache entry") );
348
8d7eaf91 349 if ( --info->count == 0 )
c6151f2a
JS
350 {
351 RestoreDC((HDC) m_hDC, saveState);
352 ms_cache.RemoveAt(index);
353
354 // Reduce the number of bogus reports of non-freed memory
355 // at app exit
356 if (ms_cache.IsEmpty())
357 ms_cache.Clear();
358 }
359 //else: cached DC entry is still in use
360
361 // prevent the base class dtor from ReleaseDC()ing it again
362 m_hDC = 0;
363}