]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dcclient.cpp
Various compile fixes.
[wxWidgets.git] / src / msw / dcclient.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dcclient.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/string.h"
32 #include "wx/log.h"
33 #include "wx/window.h"
34
35 #include "wx/msw/private.h"
36
37 #include "wx/dcclient.h"
38
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
61 // ----------------------------------------------------------------------------
62 // macros
63 // ----------------------------------------------------------------------------
64
65 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
66 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
67 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxClientDC)
68
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.
80 int g_isPainting = 0;
81 #endif // __WXDEBUG__
82
83 // ===========================================================================
84 // implementation
85 // ===========================================================================
86
87 // ----------------------------------------------------------------------------
88 // wxWindowDC
89 // ----------------------------------------------------------------------------
90
91 wxWindowDC::wxWindowDC()
92 {
93 m_canvas = NULL;
94 }
95
96 wxWindowDC::wxWindowDC(wxWindow *canvas)
97 {
98 wxCHECK_RET( canvas, _T("invalid window in wxWindowDC") );
99
100 m_canvas = canvas;
101 m_hDC = (WXHDC) ::GetWindowDC(GetHwndOf(m_canvas));
102
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
105 InitDC();
106 }
107
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);
113
114 // default bg colour is pne of the window
115 SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID));
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
121 }
122
123 // ----------------------------------------------------------------------------
124 // wxClientDC
125 // ----------------------------------------------------------------------------
126
127 wxClientDC::wxClientDC()
128 {
129 m_canvas = NULL;
130 }
131
132 wxClientDC::wxClientDC(wxWindow *canvas)
133 {
134 wxCHECK_RET( canvas, _T("invalid window in wxClientDC") );
135
136 m_canvas = canvas;
137 m_hDC = (WXHDC)::GetDC(GetHwndOf(m_canvas));
138
139 // m_bOwnsDC was already set to false in the base class ctor, so the DC
140 // will be released (and not deleted) in ~wxDC
141
142 InitDC();
143 }
144
145 void wxClientDC::InitDC()
146 {
147 wxWindowDC::InitDC();
148
149 // in wxUniv build we must manually do some DC adjustments usually
150 // performed by Windows for us
151 #ifdef __WXUNIVERSAL__
152 wxPoint ptOrigin = m_canvas->GetClientAreaOrigin();
153 if ( ptOrigin.x || ptOrigin.y )
154 {
155 // no need to shift DC origin if shift is null
156 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
157 }
158
159 // clip the DC to avoid overwriting the non client area
160 SetClippingRegion(wxPoint(0, 0), m_canvas->GetClientSize());
161 #endif // __WXUNIVERSAL__
162 }
163
164 wxClientDC::~wxClientDC()
165 {
166 }
167
168 // ----------------------------------------------------------------------------
169 // wxPaintDC
170 // ----------------------------------------------------------------------------
171
172 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
173 // created and tried to reuse it - this was supposed to take care of a
174 // situation when a derived class OnPaint() calls base class OnPaint()
175 // because in this case ::BeginPaint() shouldn't be called second time.
176 //
177 // I'm not sure how useful this is, however we must remember the HWND
178 // associated with the last HDC as well - otherwise we may (and will!) try
179 // to reuse the HDC for another HWND which is a nice recipe for disaster.
180 //
181 // So we store a list of windows for which we already have the DC and not
182 // just one single hDC. This seems to work, but I'm really not sure about
183 // the usefullness of the whole idea - IMHO it's much better to not call
184 // base class OnPaint() at all, or, if we really want to allow it, add a
185 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
186 // !NULL instead of creating a new DC.
187
188 wxArrayDCInfo wxPaintDC::ms_cache;
189
190 wxPaintDC::wxPaintDC()
191 {
192 m_canvas = NULL;
193 }
194
195 wxPaintDC::wxPaintDC(wxWindow *canvas)
196 {
197 wxCHECK_RET( canvas, wxT("NULL canvas in wxPaintDC ctor") );
198
199 #ifdef __WXDEBUG__
200 if ( g_isPainting <= 0 )
201 {
202 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
203
204 return;
205 }
206 #endif // __WXDEBUG__
207
208 m_canvas = canvas;
209
210 // do we have a DC for this window in the cache?
211 wxPaintDCInfo *info = FindInCache();
212 if ( info )
213 {
214 m_hDC = info->hdc;
215 info->count++;
216 }
217 else // not in cache, create a new one
218 {
219 m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_canvas), &g_paintStruct);
220 if (m_hDC)
221 ms_cache.Add(new wxPaintDCInfo(m_canvas, this));
222 }
223
224 // (re)set the DC parameters.
225 // Note: at this point m_hDC can be NULL under MicroWindows, when dragging.
226 if (GetHDC())
227 InitDC();
228 }
229
230 wxPaintDC::~wxPaintDC()
231 {
232 if ( m_hDC )
233 {
234 SelectOldObjects(m_hDC);
235
236 size_t index;
237 wxPaintDCInfo *info = FindInCache(&index);
238
239 wxCHECK_RET( info, wxT("existing DC should have a cache entry") );
240
241 if ( !--info->count )
242 {
243 ::EndPaint(GetHwndOf(m_canvas), &g_paintStruct);
244
245 ms_cache.RemoveAt(index);
246
247 // Reduce the number of bogus reports of non-freed memory
248 // at app exit
249 if (ms_cache.IsEmpty())
250 ms_cache.Clear();
251 }
252 //else: cached DC entry is still in use
253
254 // prevent the base class dtor from ReleaseDC()ing it again
255 m_hDC = 0;
256 }
257 }
258
259 wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const
260 {
261 wxPaintDCInfo *info = NULL;
262 size_t nCache = ms_cache.GetCount();
263 for ( size_t n = 0; n < nCache; n++ )
264 {
265 info = &ms_cache[n];
266 if ( info->hwnd == m_canvas->GetHWND() )
267 {
268 if ( index )
269 *index = n;
270 break;
271 }
272 }
273
274 return info;
275 }
276
277 // find the entry for this DC in the cache (keyed by the window)
278 WXHDC wxPaintDC::FindDCInCache(wxWindow* win)
279 {
280 wxPaintDCInfo *info = NULL;
281 size_t nCache = ms_cache.GetCount();
282 for ( size_t n = 0; n < nCache; n++ )
283 {
284 info = &ms_cache[n];
285 if ( info->hwnd == win->GetHWND() )
286 {
287 return info->hdc;
288 }
289 }
290 return 0;
291 }
292