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