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