]>
Commit | Line | Data |
---|---|---|
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 | 27 | #include "wx/dcclient.h" |
888dde65 | 28 | #include "wx/msw/dcclient.h" |
df91131c WS |
29 | |
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/string.h" | |
e4db172a | 32 | #include "wx/log.h" |
cdccdfab | 33 | #include "wx/window.h" |
df91131c WS |
34 | #endif |
35 | ||
c6eba8f8 VZ |
36 | #include "wx/msw/private.h" |
37 | ||
3a5ffa81 VZ |
38 | // ---------------------------------------------------------------------------- |
39 | // array/list types | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | struct WXDLLEXPORT wxPaintDCInfo | |
43 | { | |
888dde65 | 44 | wxPaintDCInfo(wxWindow *win, wxPaintDCImpl *dc) |
3a5ffa81 VZ |
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 | ||
259c43f6 | 58 | WX_DEFINE_OBJARRAY(wxArrayDCInfo) |
3a5ffa81 | 59 | |
c6eba8f8 VZ |
60 | // ---------------------------------------------------------------------------- |
61 | // macros | |
62 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 63 | |
c6eba8f8 VZ |
64 | // ---------------------------------------------------------------------------- |
65 | // global variables | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | static PAINTSTRUCT g_paintStruct; | |
69 | ||
70 | #ifdef __WXDEBUG__ | |
71 | // a global variable which we check to verify that wxPaintDC are only | |
a0d9c6cb | 72 | // created in response to WM_PAINT message - doing this from elsewhere is a |
77ffb593 | 73 | // common programming error among wxWidgets programmers and might lead to |
c6eba8f8 | 74 | // very subtle and difficult to debug refresh/repaint bugs. |
edccf428 | 75 | int g_isPainting = 0; |
c6eba8f8 VZ |
76 | #endif // __WXDEBUG__ |
77 | ||
78 | // =========================================================================== | |
79 | // implementation | |
80 | // =========================================================================== | |
e6460682 | 81 | |
c6eba8f8 | 82 | // ---------------------------------------------------------------------------- |
888dde65 | 83 | // wxMSWWindowDCImpl |
c6eba8f8 VZ |
84 | // ---------------------------------------------------------------------------- |
85 | ||
888dde65 RR |
86 | IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxMSWDCImpl) |
87 | ||
88 | wxWindowDCImpl::wxWindowDCImpl( wxDC *owner ) : | |
89 | wxMSWDCImpl( owner ) | |
2bda0e17 | 90 | { |
2bda0e17 KB |
91 | } |
92 | ||
f0875501 | 93 | wxWindowDCImpl::wxWindowDCImpl( wxDC *owner, wxWindow *window ) : |
888dde65 | 94 | wxMSWDCImpl( owner ) |
2bda0e17 | 95 | { |
888dde65 | 96 | wxCHECK_RET( window, _T("invalid window in wxWindowDCImpl") ); |
a91b47e8 | 97 | |
888dde65 RR |
98 | m_window = window; |
99 | m_hDC = (WXHDC) ::GetWindowDC(GetHwndOf(m_window)); | |
2bda0e17 | 100 | |
7ba4fbeb VZ |
101 | // m_bOwnsDC was already set to false in the base class ctor, so the DC |
102 | // will be released (and not deleted) in ~wxDC | |
7ba4fbeb VZ |
103 | InitDC(); |
104 | } | |
edccf428 | 105 | |
888dde65 | 106 | void wxWindowDCImpl::InitDC() |
7ba4fbeb VZ |
107 | { |
108 | // the background mode is only used for text background and is set in | |
109 | // DrawText() to OPAQUE as required, otherwise always TRANSPARENT, | |
110 | ::SetBkMode(GetHdc(), TRANSPARENT); | |
c6eba8f8 | 111 | |
7ba4fbeb | 112 | // default bg colour is pne of the window |
cb129171 | 113 | SetBackground(wxBrush(m_window->GetBackgroundColour(), wxBRUSHSTYLE_SOLID)); |
574c939e KB |
114 | |
115 | // since we are a window dc we need to grab the palette from the window | |
116 | #if wxUSE_PALETTE | |
117 | InitializePalette(); | |
118 | #endif | |
2bda0e17 KB |
119 | } |
120 | ||
888dde65 | 121 | void wxWindowDCImpl::DoGetSize(int *width, int *height) const |
994a3786 | 122 | { |
888dde65 | 123 | wxCHECK_RET( m_window, _T("wxWindowDCImpl without a window?") ); |
994a3786 | 124 | |
888dde65 | 125 | m_window->GetSize(width, height); |
994a3786 VZ |
126 | } |
127 | ||
c6eba8f8 | 128 | // ---------------------------------------------------------------------------- |
888dde65 | 129 | // wxClientDCImpl |
c6eba8f8 | 130 | // ---------------------------------------------------------------------------- |
e6460682 | 131 | |
888dde65 RR |
132 | IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl) |
133 | ||
134 | wxClientDCImpl::wxClientDCImpl( wxDC *owner ) : | |
135 | wxWindowDCImpl( owner ) | |
2bda0e17 | 136 | { |
2bda0e17 KB |
137 | } |
138 | ||
888dde65 RR |
139 | wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *window ) : |
140 | wxWindowDCImpl( owner ) | |
2bda0e17 | 141 | { |
888dde65 | 142 | wxCHECK_RET( window, _T("invalid window in wxClientDCImpl") ); |
a91b47e8 | 143 | |
888dde65 RR |
144 | m_window = window; |
145 | m_hDC = (WXHDC)::GetDC(GetHwndOf(window)); | |
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 | ||
888dde65 | 153 | void wxClientDCImpl::InitDC() |
1e6feb95 | 154 | { |
888dde65 | 155 | wxWindowDCImpl::InitDC(); |
1e6feb95 VZ |
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__) | |
888dde65 | 164 | wxPoint ptOrigin = m_window->GetClientAreaOrigin(); |
1e6feb95 VZ |
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 | |
c7d6c563 VS |
172 | wxSize size = m_window->GetClientSize(); |
173 | DoSetClippingRegion(0, 0, size.x, size.y); | |
1fa6ebf7 | 174 | #endif // __WXUNIVERSAL__ || __WXWINCE__ |
1e6feb95 VZ |
175 | } |
176 | ||
888dde65 | 177 | wxClientDCImpl::~wxClientDCImpl() |
1e6feb95 VZ |
178 | { |
179 | } | |
180 | ||
888dde65 | 181 | void wxClientDCImpl::DoGetSize(int *width, int *height) const |
994a3786 | 182 | { |
888dde65 | 183 | wxCHECK_RET( m_window, _T("wxClientDCImpl without a window?") ); |
994a3786 | 184 | |
888dde65 | 185 | m_window->GetClientSize(width, height); |
994a3786 VZ |
186 | } |
187 | ||
c6eba8f8 | 188 | // ---------------------------------------------------------------------------- |
888dde65 | 189 | // wxPaintDCImpl |
c6eba8f8 | 190 | // ---------------------------------------------------------------------------- |
e6460682 | 191 | |
3a5ffa81 | 192 | // VZ: initial implementation (by JACS) only remembered the last wxPaintDC |
7ba4fbeb | 193 | // created and tried to reuse it - this was supposed to take care of a |
3a5ffa81 VZ |
194 | // situation when a derived class OnPaint() calls base class OnPaint() |
195 | // because in this case ::BeginPaint() shouldn't be called second time. | |
196 | // | |
197 | // I'm not sure how useful this is, however we must remember the HWND | |
198 | // associated with the last HDC as well - otherwise we may (and will!) try | |
199 | // to reuse the HDC for another HWND which is a nice recipe for disaster. | |
200 | // | |
201 | // So we store a list of windows for which we already have the DC and not | |
202 | // just one single hDC. This seems to work, but I'm really not sure about | |
203 | // the usefullness of the whole idea - IMHO it's much better to not call | |
85833f5c | 204 | // base class OnPaint() at all, or, if we really want to allow it, add a |
3a5ffa81 VZ |
205 | // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's |
206 | // !NULL instead of creating a new DC. | |
207 | ||
888dde65 | 208 | IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxClientDCImpl) |
2bda0e17 | 209 | |
888dde65 RR |
210 | wxArrayDCInfo wxPaintDCImpl::ms_cache; |
211 | ||
212 | wxPaintDCImpl::wxPaintDCImpl( wxDC *owner ) : | |
213 | wxClientDCImpl( owner ) | |
c6eba8f8 | 214 | { |
c6eba8f8 VZ |
215 | } |
216 | ||
888dde65 RR |
217 | wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *window ) : |
218 | wxClientDCImpl( owner ) | |
2bda0e17 | 219 | { |
888dde65 | 220 | wxCHECK_RET( window, wxT("NULL canvas in wxPaintDCImpl ctor") ); |
3a5ffa81 | 221 | |
c455ab93 | 222 | #ifdef __WXDEBUG__ |
edccf428 | 223 | if ( g_isPainting <= 0 ) |
3a5ffa81 | 224 | { |
888dde65 | 225 | wxFAIL_MSG( wxT("wxPaintDCImpl may be created only in EVT_PAINT handler!") ); |
2bda0e17 | 226 | |
3a5ffa81 VZ |
227 | return; |
228 | } | |
229 | #endif // __WXDEBUG__ | |
c6eba8f8 | 230 | |
888dde65 | 231 | m_window = window; |
83626bfa | 232 | |
3a5ffa81 VZ |
233 | // do we have a DC for this window in the cache? |
234 | wxPaintDCInfo *info = FindInCache(); | |
235 | if ( info ) | |
236 | { | |
237 | m_hDC = info->hdc; | |
238 | info->count++; | |
239 | } | |
240 | else // not in cache, create a new one | |
241 | { | |
888dde65 | 242 | m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_window), &g_paintStruct); |
1fa6ebf7 | 243 | if (m_hDC) |
888dde65 | 244 | ms_cache.Add(new wxPaintDCInfo(m_window, this)); |
3a5ffa81 | 245 | } |
a91b47e8 | 246 | |
5adad466 | 247 | // Note: at this point m_hDC can be NULL under MicroWindows, when dragging. |
27539df8 VZ |
248 | if (!GetHDC()) |
249 | return; | |
250 | ||
251 | // (re)set the DC parameters. | |
252 | InitDC(); | |
253 | ||
254 | // the HDC can have a clipping box (which we didn't set), make sure our | |
255 | // DoGetClippingBox() checks for it | |
e9f8a82e | 256 | m_clipping = true; |
2bda0e17 KB |
257 | } |
258 | ||
888dde65 | 259 | wxPaintDCImpl::~wxPaintDCImpl() |
2bda0e17 | 260 | { |
3a5ffa81 VZ |
261 | if ( m_hDC ) |
262 | { | |
edccf428 VZ |
263 | SelectOldObjects(m_hDC); |
264 | ||
3a5ffa81 VZ |
265 | size_t index; |
266 | wxPaintDCInfo *info = FindInCache(&index); | |
267 | ||
223d09f6 | 268 | wxCHECK_RET( info, wxT("existing DC should have a cache entry") ); |
3a5ffa81 | 269 | |
8d7eaf91 | 270 | if ( --info->count == 0 ) |
3a5ffa81 | 271 | { |
888dde65 | 272 | ::EndPaint(GetHwndOf(m_window), &g_paintStruct); |
3a5ffa81 | 273 | |
b54e41c5 | 274 | ms_cache.RemoveAt(index); |
2bc1aa11 JS |
275 | |
276 | // Reduce the number of bogus reports of non-freed memory | |
277 | // at app exit | |
278 | if (ms_cache.IsEmpty()) | |
279 | ms_cache.Clear(); | |
3a5ffa81 VZ |
280 | } |
281 | //else: cached DC entry is still in use | |
edccf428 VZ |
282 | |
283 | // prevent the base class dtor from ReleaseDC()ing it again | |
284 | m_hDC = 0; | |
1c089c47 | 285 | } |
2bda0e17 | 286 | } |
81d66cf3 | 287 | |
888dde65 | 288 | wxPaintDCInfo *wxPaintDCImpl::FindInCache(size_t *index) const |
3a5ffa81 VZ |
289 | { |
290 | wxPaintDCInfo *info = NULL; | |
291 | size_t nCache = ms_cache.GetCount(); | |
292 | for ( size_t n = 0; n < nCache; n++ ) | |
293 | { | |
968bed8c | 294 | wxPaintDCInfo *info1 = &ms_cache[n]; |
888dde65 | 295 | if ( info1->hwnd == m_window->GetHWND() ) |
3a5ffa81 | 296 | { |
968bed8c | 297 | info = info1; |
3a5ffa81 VZ |
298 | if ( index ) |
299 | *index = n; | |
300 | break; | |
301 | } | |
302 | } | |
303 | ||
304 | return info; | |
305 | } | |
63da7df7 JS |
306 | |
307 | // find the entry for this DC in the cache (keyed by the window) | |
888dde65 | 308 | WXHDC wxPaintDCImpl::FindDCInCache(wxWindow* win) |
63da7df7 | 309 | { |
63da7df7 JS |
310 | size_t nCache = ms_cache.GetCount(); |
311 | for ( size_t n = 0; n < nCache; n++ ) | |
312 | { | |
999836aa | 313 | wxPaintDCInfo *info = &ms_cache[n]; |
63da7df7 JS |
314 | if ( info->hwnd == win->GetHWND() ) |
315 | { | |
316 | return info->hdc; | |
317 | } | |
318 | } | |
319 | return 0; | |
320 | } | |
321 | ||
c6151f2a JS |
322 | /* |
323 | * wxPaintDCEx | |
324 | */ | |
d71cc120 | 325 | |
888dde65 | 326 | // TODO: don't duplicate wxPaintDCImpl code here!! |
fbaf7d14 | 327 | |
888dde65 | 328 | class wxPaintDCExImpl: public wxPaintDCImpl |
c6151f2a | 329 | { |
888dde65 RR |
330 | public: |
331 | wxPaintDCExImpl( wxDC *owner, wxWindow *window, WXHDC dc ); | |
332 | ~wxPaintDCExImpl(); | |
f0875501 | 333 | |
888dde65 RR |
334 | int m_saveState; |
335 | }; | |
c6151f2a | 336 | |
c6151f2a | 337 | |
888dde65 RR |
338 | IMPLEMENT_ABSTRACT_CLASS(wxPaintDCEx,wxPaintDC) |
339 | ||
f0875501 VZ |
340 | wxPaintDCEx::wxPaintDCEx(wxWindow *window, WXHDC dc) |
341 | : wxPaintDC(new wxPaintDCExImpl(this, window, dc)) | |
888dde65 | 342 | { |
c6151f2a JS |
343 | } |
344 | ||
f0875501 VZ |
345 | wxPaintDCExImpl::wxPaintDCExImpl(wxDC *owner, wxWindow *window, WXHDC dc) |
346 | : wxPaintDCImpl( owner ) | |
c6151f2a | 347 | { |
888dde65 | 348 | wxCHECK_RET( dc, wxT("wxPaintDCEx requires an existing device context") ); |
c6151f2a | 349 | |
888dde65 | 350 | m_saveState = 0; |
f0875501 | 351 | |
888dde65 | 352 | m_window = window; |
c6151f2a | 353 | |
888dde65 RR |
354 | wxPaintDCInfo *info = FindInCache(); |
355 | if ( info ) | |
356 | { | |
357 | m_hDC = info->hdc; | |
358 | info->count++; | |
359 | } | |
360 | else // not in cache, create a new one | |
361 | { | |
362 | m_hDC = dc; | |
363 | ms_cache.Add(new wxPaintDCInfo(m_window, this)); | |
364 | m_saveState = SaveDC((HDC) dc); | |
365 | } | |
366 | } | |
c6151f2a | 367 | |
888dde65 RR |
368 | wxPaintDCExImpl::~wxPaintDCExImpl() |
369 | { | |
370 | size_t index; | |
371 | wxPaintDCInfo *info = FindInCache(&index); | |
372 | ||
373 | wxCHECK_RET( info, wxT("existing DC should have a cache entry") ); | |
c6151f2a | 374 | |
888dde65 RR |
375 | if ( --info->count == 0 ) |
376 | { | |
377 | RestoreDC((HDC) m_hDC, m_saveState); | |
378 | ms_cache.RemoveAt(index); | |
379 | ||
380 | // Reduce the number of bogus reports of non-freed memory | |
381 | // at app exit | |
382 | if (ms_cache.IsEmpty()) | |
383 | ms_cache.Clear(); | |
384 | } | |
385 | //else: cached DC entry is still in use | |
386 | ||
387 | // prevent the base class dtor from ReleaseDC()ing it again | |
388 | m_hDC = 0; | |
c6151f2a | 389 | } |
888dde65 RR |
390 | |
391 |