]>
Commit | Line | Data |
---|---|---|
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c6eba8f8 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
c6eba8f8 VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
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 | ||
43 | struct 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 | ||
59 | WX_DEFINE_OBJARRAY(wxArrayDCInfo); | |
60 | ||
c6eba8f8 VZ |
61 | // ---------------------------------------------------------------------------- |
62 | // macros | |
63 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 64 | |
7ba4fbeb VZ |
65 | IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC) |
66 | IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC) | |
1e6feb95 | 67 | IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxClientDC) |
2bda0e17 | 68 | |
c6eba8f8 VZ |
69 | // ---------------------------------------------------------------------------- |
70 | // global variables | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
73 | static PAINTSTRUCT g_paintStruct; | |
74 | ||
75 | #ifdef __WXDEBUG__ | |
76 | // a global variable which we check to verify that wxPaintDC are only | |
77 | // created in resopnse to WM_PAINT message - doing this from elsewhere is a | |
78 | // common programming error among wxWindows programmers and might lead to | |
79 | // very subtle and difficult to debug refresh/repaint bugs. | |
edccf428 | 80 | int g_isPainting = 0; |
c6eba8f8 VZ |
81 | #endif // __WXDEBUG__ |
82 | ||
83 | // =========================================================================== | |
84 | // implementation | |
85 | // =========================================================================== | |
e6460682 | 86 | |
c6eba8f8 VZ |
87 | // ---------------------------------------------------------------------------- |
88 | // wxWindowDC | |
89 | // ---------------------------------------------------------------------------- | |
90 | ||
91 | wxWindowDC::wxWindowDC() | |
2bda0e17 | 92 | { |
7ba4fbeb | 93 | m_canvas = NULL; |
2bda0e17 KB |
94 | } |
95 | ||
7ba4fbeb | 96 | wxWindowDC::wxWindowDC(wxWindow *canvas) |
2bda0e17 | 97 | { |
7ba4fbeb | 98 | wxCHECK_RET( canvas, _T("invalid window in wxWindowDC") ); |
a91b47e8 | 99 | |
7ba4fbeb VZ |
100 | m_canvas = canvas; |
101 | m_hDC = (WXHDC) ::GetWindowDC(GetHwndOf(m_canvas)); | |
2bda0e17 | 102 | |
7ba4fbeb VZ |
103 | // m_bOwnsDC was already set to false in the base class ctor, so the DC |
104 | // will be released (and not deleted) in ~wxDC | |
7ba4fbeb VZ |
105 | InitDC(); |
106 | } | |
edccf428 | 107 | |
7ba4fbeb VZ |
108 | void wxWindowDC::InitDC() |
109 | { | |
110 | // the background mode is only used for text background and is set in | |
111 | // DrawText() to OPAQUE as required, otherwise always TRANSPARENT, | |
112 | ::SetBkMode(GetHdc(), TRANSPARENT); | |
c6eba8f8 | 113 | |
7ba4fbeb VZ |
114 | // default bg colour is pne of the window |
115 | SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID)); | |
574c939e KB |
116 | |
117 | // since we are a window dc we need to grab the palette from the window | |
118 | #if wxUSE_PALETTE | |
119 | InitializePalette(); | |
120 | #endif | |
2bda0e17 KB |
121 | } |
122 | ||
994a3786 VZ |
123 | void wxWindowDC::DoGetSize(int *width, int *height) const |
124 | { | |
125 | wxCHECK_RET( m_canvas, _T("wxWindowDC without a window?") ); | |
126 | ||
127 | m_canvas->GetSize(width, height); | |
128 | } | |
129 | ||
c6eba8f8 VZ |
130 | // ---------------------------------------------------------------------------- |
131 | // wxClientDC | |
132 | // ---------------------------------------------------------------------------- | |
e6460682 | 133 | |
c6eba8f8 | 134 | wxClientDC::wxClientDC() |
2bda0e17 | 135 | { |
7ba4fbeb | 136 | m_canvas = NULL; |
2bda0e17 KB |
137 | } |
138 | ||
7ba4fbeb | 139 | wxClientDC::wxClientDC(wxWindow *canvas) |
2bda0e17 | 140 | { |
7ba4fbeb | 141 | wxCHECK_RET( canvas, _T("invalid window in wxClientDC") ); |
a91b47e8 | 142 | |
7ba4fbeb VZ |
143 | m_canvas = canvas; |
144 | m_hDC = (WXHDC)::GetDC(GetHwndOf(m_canvas)); | |
2bda0e17 | 145 | |
7ba4fbeb VZ |
146 | // m_bOwnsDC was already set to false in the base class ctor, so the DC |
147 | // will be released (and not deleted) in ~wxDC | |
2bda0e17 | 148 | |
7ba4fbeb | 149 | InitDC(); |
2bda0e17 KB |
150 | } |
151 | ||
1e6feb95 VZ |
152 | void wxClientDC::InitDC() |
153 | { | |
154 | wxWindowDC::InitDC(); | |
155 | ||
156 | // in wxUniv build we must manually do some DC adjustments usually | |
157 | // performed by Windows for us | |
158 | #ifdef __WXUNIVERSAL__ | |
159 | wxPoint ptOrigin = m_canvas->GetClientAreaOrigin(); | |
160 | if ( ptOrigin.x || ptOrigin.y ) | |
161 | { | |
162 | // no need to shift DC origin if shift is null | |
163 | SetDeviceOrigin(ptOrigin.x, ptOrigin.y); | |
164 | } | |
165 | ||
166 | // clip the DC to avoid overwriting the non client area | |
167 | SetClippingRegion(wxPoint(0, 0), m_canvas->GetClientSize()); | |
168 | #endif // __WXUNIVERSAL__ | |
169 | } | |
170 | ||
171 | wxClientDC::~wxClientDC() | |
172 | { | |
173 | } | |
174 | ||
994a3786 VZ |
175 | void wxClientDC::DoGetSize(int *width, int *height) const |
176 | { | |
177 | wxCHECK_RET( m_canvas, _T("wxClientDC without a window?") ); | |
178 | ||
179 | m_canvas->GetClientSize(width, height); | |
180 | } | |
181 | ||
c6eba8f8 VZ |
182 | // ---------------------------------------------------------------------------- |
183 | // wxPaintDC | |
184 | // ---------------------------------------------------------------------------- | |
e6460682 | 185 | |
3a5ffa81 | 186 | // VZ: initial implementation (by JACS) only remembered the last wxPaintDC |
7ba4fbeb | 187 | // created and tried to reuse it - this was supposed to take care of a |
3a5ffa81 VZ |
188 | // situation when a derived class OnPaint() calls base class OnPaint() |
189 | // because in this case ::BeginPaint() shouldn't be called second time. | |
190 | // | |
191 | // I'm not sure how useful this is, however we must remember the HWND | |
192 | // associated with the last HDC as well - otherwise we may (and will!) try | |
193 | // to reuse the HDC for another HWND which is a nice recipe for disaster. | |
194 | // | |
195 | // So we store a list of windows for which we already have the DC and not | |
196 | // just one single hDC. This seems to work, but I'm really not sure about | |
197 | // the usefullness of the whole idea - IMHO it's much better to not call | |
85833f5c | 198 | // base class OnPaint() at all, or, if we really want to allow it, add a |
3a5ffa81 VZ |
199 | // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's |
200 | // !NULL instead of creating a new DC. | |
201 | ||
202 | wxArrayDCInfo wxPaintDC::ms_cache; | |
2bda0e17 | 203 | |
c6eba8f8 VZ |
204 | wxPaintDC::wxPaintDC() |
205 | { | |
3a5ffa81 | 206 | m_canvas = NULL; |
c6eba8f8 VZ |
207 | } |
208 | ||
83626bfa | 209 | wxPaintDC::wxPaintDC(wxWindow *canvas) |
2bda0e17 | 210 | { |
223d09f6 | 211 | wxCHECK_RET( canvas, wxT("NULL canvas in wxPaintDC ctor") ); |
3a5ffa81 | 212 | |
c455ab93 | 213 | #ifdef __WXDEBUG__ |
edccf428 | 214 | if ( g_isPainting <= 0 ) |
3a5ffa81 | 215 | { |
223d09f6 | 216 | wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") ); |
2bda0e17 | 217 | |
3a5ffa81 VZ |
218 | return; |
219 | } | |
220 | #endif // __WXDEBUG__ | |
c6eba8f8 | 221 | |
3a5ffa81 | 222 | m_canvas = canvas; |
83626bfa | 223 | |
3a5ffa81 VZ |
224 | // do we have a DC for this window in the cache? |
225 | wxPaintDCInfo *info = FindInCache(); | |
226 | if ( info ) | |
227 | { | |
228 | m_hDC = info->hdc; | |
229 | info->count++; | |
230 | } | |
231 | else // not in cache, create a new one | |
232 | { | |
7ba4fbeb | 233 | m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_canvas), &g_paintStruct); |
5adad466 JS |
234 | if (m_hDC) |
235 | ms_cache.Add(new wxPaintDCInfo(m_canvas, this)); | |
3a5ffa81 | 236 | } |
a91b47e8 | 237 | |
5adad466 JS |
238 | // (re)set the DC parameters. |
239 | // Note: at this point m_hDC can be NULL under MicroWindows, when dragging. | |
240 | if (GetHDC()) | |
241 | InitDC(); | |
2bda0e17 KB |
242 | } |
243 | ||
83626bfa | 244 | wxPaintDC::~wxPaintDC() |
2bda0e17 | 245 | { |
3a5ffa81 VZ |
246 | if ( m_hDC ) |
247 | { | |
edccf428 VZ |
248 | SelectOldObjects(m_hDC); |
249 | ||
3a5ffa81 VZ |
250 | size_t index; |
251 | wxPaintDCInfo *info = FindInCache(&index); | |
252 | ||
223d09f6 | 253 | wxCHECK_RET( info, wxT("existing DC should have a cache entry") ); |
3a5ffa81 VZ |
254 | |
255 | if ( !--info->count ) | |
256 | { | |
7ba4fbeb | 257 | ::EndPaint(GetHwndOf(m_canvas), &g_paintStruct); |
3a5ffa81 | 258 | |
b54e41c5 | 259 | ms_cache.RemoveAt(index); |
2bc1aa11 JS |
260 | |
261 | // Reduce the number of bogus reports of non-freed memory | |
262 | // at app exit | |
263 | if (ms_cache.IsEmpty()) | |
264 | ms_cache.Clear(); | |
3a5ffa81 VZ |
265 | } |
266 | //else: cached DC entry is still in use | |
edccf428 VZ |
267 | |
268 | // prevent the base class dtor from ReleaseDC()ing it again | |
269 | m_hDC = 0; | |
1c089c47 | 270 | } |
2bda0e17 | 271 | } |
81d66cf3 | 272 | |
3a5ffa81 VZ |
273 | wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const |
274 | { | |
275 | wxPaintDCInfo *info = NULL; | |
276 | size_t nCache = ms_cache.GetCount(); | |
277 | for ( size_t n = 0; n < nCache; n++ ) | |
278 | { | |
279 | info = &ms_cache[n]; | |
280 | if ( info->hwnd == m_canvas->GetHWND() ) | |
281 | { | |
282 | if ( index ) | |
283 | *index = n; | |
284 | break; | |
285 | } | |
286 | } | |
287 | ||
288 | return info; | |
289 | } | |
63da7df7 JS |
290 | |
291 | // find the entry for this DC in the cache (keyed by the window) | |
292 | WXHDC wxPaintDC::FindDCInCache(wxWindow* win) | |
293 | { | |
294 | wxPaintDCInfo *info = NULL; | |
295 | size_t nCache = ms_cache.GetCount(); | |
296 | for ( size_t n = 0; n < nCache; n++ ) | |
297 | { | |
298 | info = &ms_cache[n]; | |
299 | if ( info->hwnd == win->GetHWND() ) | |
300 | { | |
301 | return info->hdc; | |
302 | } | |
303 | } | |
304 | return 0; | |
305 | } | |
306 |