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