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