]>
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 | |
c6eba8f8 VZ |
65 | IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC) |
66 | IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC) | |
67 | IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC) | |
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 KB |
92 | { |
93 | m_canvas = NULL; | |
94 | } | |
95 | ||
e6460682 | 96 | wxWindowDC::wxWindowDC(wxWindow *the_canvas) |
2bda0e17 KB |
97 | { |
98 | m_canvas = the_canvas; | |
c6eba8f8 VZ |
99 | m_hDC = (WXHDC) ::GetWindowDC(GetWinHwnd(the_canvas) ); |
100 | m_hDCCount++; | |
a91b47e8 JS |
101 | |
102 | SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID)); | |
2bda0e17 KB |
103 | } |
104 | ||
c6eba8f8 | 105 | wxWindowDC::~wxWindowDC() |
2bda0e17 | 106 | { |
e6460682 | 107 | if (m_canvas && m_hDC) |
2bda0e17 KB |
108 | { |
109 | SelectOldObjects(m_hDC); | |
110 | ||
edccf428 VZ |
111 | if ( !::ReleaseDC(GetWinHwnd(m_canvas), GetHdc()) ) |
112 | { | |
f6bcfd97 | 113 | wxLogLastError(wxT("ReleaseDC")); |
edccf428 VZ |
114 | } |
115 | ||
c6eba8f8 | 116 | m_hDC = 0; |
2bda0e17 | 117 | } |
c6eba8f8 VZ |
118 | |
119 | m_hDCCount--; | |
2bda0e17 KB |
120 | } |
121 | ||
c6eba8f8 VZ |
122 | // ---------------------------------------------------------------------------- |
123 | // wxClientDC | |
124 | // ---------------------------------------------------------------------------- | |
e6460682 | 125 | |
c6eba8f8 | 126 | wxClientDC::wxClientDC() |
2bda0e17 KB |
127 | { |
128 | m_canvas = NULL; | |
129 | } | |
130 | ||
e6460682 | 131 | wxClientDC::wxClientDC(wxWindow *the_canvas) |
2bda0e17 KB |
132 | { |
133 | m_canvas = the_canvas; | |
c6eba8f8 | 134 | m_hDC = (WXHDC) ::GetDC(GetWinHwnd(the_canvas)); |
a91b47e8 | 135 | |
c45a644e RR |
136 | // the background mode is only used for text background |
137 | // and is set in DrawText() to OPAQUE as required, other- | |
138 | // wise always TRANSPARENT, RR | |
139 | ::SetBkMode( GetHdc(), TRANSPARENT ); | |
140 | ||
a91b47e8 | 141 | SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID)); |
2bda0e17 KB |
142 | } |
143 | ||
c6eba8f8 | 144 | wxClientDC::~wxClientDC() |
2bda0e17 | 145 | { |
c6eba8f8 | 146 | if ( m_canvas && GetHdc() ) |
2bda0e17 KB |
147 | { |
148 | SelectOldObjects(m_hDC); | |
149 | ||
c6eba8f8 VZ |
150 | ::ReleaseDC(GetWinHwnd(m_canvas), GetHdc()); |
151 | m_hDC = 0; | |
2bda0e17 | 152 | } |
2bda0e17 KB |
153 | } |
154 | ||
c6eba8f8 VZ |
155 | // ---------------------------------------------------------------------------- |
156 | // wxPaintDC | |
157 | // ---------------------------------------------------------------------------- | |
e6460682 | 158 | |
3a5ffa81 VZ |
159 | // VZ: initial implementation (by JACS) only remembered the last wxPaintDC |
160 | // created and tried to reuse - this was supposed to take care of a | |
161 | // situation when a derived class OnPaint() calls base class OnPaint() | |
162 | // because in this case ::BeginPaint() shouldn't be called second time. | |
163 | // | |
164 | // I'm not sure how useful this is, however we must remember the HWND | |
165 | // associated with the last HDC as well - otherwise we may (and will!) try | |
166 | // to reuse the HDC for another HWND which is a nice recipe for disaster. | |
167 | // | |
168 | // So we store a list of windows for which we already have the DC and not | |
169 | // just one single hDC. This seems to work, but I'm really not sure about | |
170 | // the usefullness of the whole idea - IMHO it's much better to not call | |
85833f5c | 171 | // base class OnPaint() at all, or, if we really want to allow it, add a |
3a5ffa81 VZ |
172 | // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's |
173 | // !NULL instead of creating a new DC. | |
174 | ||
175 | wxArrayDCInfo wxPaintDC::ms_cache; | |
2bda0e17 | 176 | |
c6eba8f8 VZ |
177 | wxPaintDC::wxPaintDC() |
178 | { | |
3a5ffa81 VZ |
179 | m_canvas = NULL; |
180 | m_hDC = 0; | |
c6eba8f8 VZ |
181 | } |
182 | ||
83626bfa | 183 | wxPaintDC::wxPaintDC(wxWindow *canvas) |
2bda0e17 | 184 | { |
223d09f6 | 185 | wxCHECK_RET( canvas, wxT("NULL canvas in wxPaintDC ctor") ); |
3a5ffa81 | 186 | |
c455ab93 | 187 | #ifdef __WXDEBUG__ |
edccf428 | 188 | if ( g_isPainting <= 0 ) |
3a5ffa81 | 189 | { |
223d09f6 | 190 | wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") ); |
2bda0e17 | 191 | |
3a5ffa81 VZ |
192 | return; |
193 | } | |
194 | #endif // __WXDEBUG__ | |
c6eba8f8 | 195 | |
3a5ffa81 | 196 | m_canvas = canvas; |
83626bfa | 197 | |
3a5ffa81 VZ |
198 | // do we have a DC for this window in the cache? |
199 | wxPaintDCInfo *info = FindInCache(); | |
200 | if ( info ) | |
201 | { | |
202 | m_hDC = info->hdc; | |
203 | info->count++; | |
204 | } | |
205 | else // not in cache, create a new one | |
206 | { | |
207 | m_hDC = (WXHDC)::BeginPaint(GetWinHwnd(m_canvas), &g_paintStruct); | |
208 | ms_cache.Add(new wxPaintDCInfo(m_canvas, this)); | |
209 | } | |
a91b47e8 | 210 | |
c45a644e RR |
211 | // the background mode is only used for text background |
212 | // and is set in DrawText() to OPAQUE as required, other- | |
213 | // wise always TRANSPARENT, RR | |
214 | ::SetBkMode( GetHdc(), TRANSPARENT ); | |
215 | ||
3a5ffa81 | 216 | SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID)); |
2bda0e17 KB |
217 | } |
218 | ||
83626bfa | 219 | wxPaintDC::~wxPaintDC() |
2bda0e17 | 220 | { |
3a5ffa81 VZ |
221 | if ( m_hDC ) |
222 | { | |
edccf428 VZ |
223 | SelectOldObjects(m_hDC); |
224 | ||
3a5ffa81 VZ |
225 | size_t index; |
226 | wxPaintDCInfo *info = FindInCache(&index); | |
227 | ||
223d09f6 | 228 | wxCHECK_RET( info, wxT("existing DC should have a cache entry") ); |
3a5ffa81 VZ |
229 | |
230 | if ( !--info->count ) | |
231 | { | |
232 | ::EndPaint(GetWinHwnd(m_canvas), &g_paintStruct); | |
233 | ||
234 | ms_cache.Remove(index); | |
2bc1aa11 JS |
235 | |
236 | // Reduce the number of bogus reports of non-freed memory | |
237 | // at app exit | |
238 | if (ms_cache.IsEmpty()) | |
239 | ms_cache.Clear(); | |
3a5ffa81 VZ |
240 | } |
241 | //else: cached DC entry is still in use | |
edccf428 VZ |
242 | |
243 | // prevent the base class dtor from ReleaseDC()ing it again | |
244 | m_hDC = 0; | |
1c089c47 | 245 | } |
2bda0e17 | 246 | } |
81d66cf3 | 247 | |
3a5ffa81 VZ |
248 | wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const |
249 | { | |
250 | wxPaintDCInfo *info = NULL; | |
251 | size_t nCache = ms_cache.GetCount(); | |
252 | for ( size_t n = 0; n < nCache; n++ ) | |
253 | { | |
254 | info = &ms_cache[n]; | |
255 | if ( info->hwnd == m_canvas->GetHWND() ) | |
256 | { | |
257 | if ( index ) | |
258 | *index = n; | |
259 | break; | |
260 | } | |
261 | } | |
262 | ||
263 | return info; | |
264 | } | |
63da7df7 JS |
265 | |
266 | // find the entry for this DC in the cache (keyed by the window) | |
267 | WXHDC wxPaintDC::FindDCInCache(wxWindow* win) | |
268 | { | |
269 | wxPaintDCInfo *info = NULL; | |
270 | size_t nCache = ms_cache.GetCount(); | |
271 | for ( size_t n = 0; n < nCache; n++ ) | |
272 | { | |
273 | info = &ms_cache[n]; | |
274 | if ( info->hwnd == win->GetHWND() ) | |
275 | { | |
276 | return info->hdc; | |
277 | } | |
278 | } | |
279 | return 0; | |
280 | } | |
281 |