]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dcclient.cpp | |
3 | // Purpose: wxClientDC class | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 09/21/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
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 | #include "wx/string.h" | |
24 | #include "wx/log.h" | |
25 | #include "wx/window.h" | |
26 | ||
27 | #include "wx/os2/private.h" | |
28 | ||
29 | #include "wx/dcclient.h" | |
30 | ||
31 | // ---------------------------------------------------------------------------- | |
32 | // array/list types | |
33 | // ---------------------------------------------------------------------------- | |
34 | ||
35 | struct WXDLLEXPORT wxPaintDCInfo | |
36 | { | |
37 | wxPaintDCInfo(wxWindow *win, wxDC *dc) | |
38 | { | |
39 | hwnd = win->GetHWND(); | |
40 | hdc = dc->GetHDC(); | |
41 | count = 1; | |
42 | } | |
43 | ||
44 | WXHWND hwnd; // window for this DC | |
45 | WXHDC hdc; // the DC handle | |
46 | size_t count; // usage count | |
47 | }; | |
48 | ||
49 | #include "wx/arrimpl.cpp" | |
50 | ||
51 | WX_DEFINE_OBJARRAY(wxArrayDCInfo); | |
52 | ||
53 | // ---------------------------------------------------------------------------- | |
54 | // macros | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
57 | IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC) | |
58 | IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC) | |
59 | IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC) | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // global variables | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | static RECT g_paintStruct; | |
66 | ||
67 | #ifdef __WXDEBUG__ | |
68 | // a global variable which we check to verify that wxPaintDC are only | |
69 | // created in resopnse to WM_PAINT message - doing this from elsewhere is a | |
70 | // common programming error among wxWindows programmers and might lead to | |
71 | // very subtle and difficult to debug refresh/repaint bugs. | |
72 | int g_isPainting = 0; | |
73 | #endif // __WXDEBUG__ | |
74 | ||
75 | // =========================================================================== | |
76 | // implementation | |
77 | // =========================================================================== | |
78 | ||
79 | // ---------------------------------------------------------------------------- | |
80 | // wxWindowDC | |
81 | // ---------------------------------------------------------------------------- | |
82 | ||
83 | wxWindowDC::wxWindowDC() | |
84 | { | |
85 | m_pCanvas = NULL; | |
86 | } | |
87 | ||
88 | wxWindowDC::wxWindowDC(wxWindow *the_canvas) | |
89 | { | |
90 | ERRORID vError; | |
91 | wxString sError; | |
92 | ||
93 | m_pCanvas = the_canvas; | |
94 | m_hDC = (WXHDC) ::WinOpenWindowDC(GetWinHwnd(the_canvas) ); | |
95 | m_nDCCount++; | |
96 | // | |
97 | // default under PM is that Window and Client DC's are the same | |
98 | // so we offer a separate Presentation Space to use for the | |
99 | // entire window. Otherwise, calling BeginPaint will just create | |
100 | // chached-micro client presentation space | |
101 | // | |
102 | m_hPS = GpiCreatePS( m_hab | |
103 | ,m_hDC | |
104 | ,&m_PageSize | |
105 | ,PU_PELS | GPIF_LONG | GPIA_ASSOC | |
106 | ); | |
107 | ::GpiAssociate(m_hPS, NULLHANDLE); | |
108 | ::GpiAssociate(m_hPS, m_hDC); | |
109 | // Set the wxWindows color table | |
110 | if (!::GpiCreateLogColorTable( m_hPS | |
111 | ,0L | |
112 | ,LCOLF_CONSECRGB | |
113 | ,0L | |
114 | ,(LONG)wxTheColourDatabase->m_nSize | |
115 | ,(PLONG)wxTheColourDatabase->m_palTable | |
116 | )) | |
117 | { | |
118 | vError = ::WinGetLastError(vHabmain); | |
119 | sError = wxPMErrorToStr(vError); | |
120 | wxLogError("Unable to set current color table. Error: %s\n", sError); | |
121 | } | |
122 | SetBackground(wxBrush(m_pCanvas->GetBackgroundColour(), wxSOLID)); | |
123 | } | |
124 | ||
125 | wxWindowDC::~wxWindowDC() | |
126 | { | |
127 | if (m_pCanvas && m_hDC) | |
128 | { | |
129 | SelectOldObjects(m_hDC); | |
130 | ||
131 | // | |
132 | // In PM one does not explicitly close or release an open WindowDC | |
133 | // They automatically close with the window, unless explicitly detached | |
134 | // but we need to destroy our PS | |
135 | // | |
136 | if(m_hPS) | |
137 | { | |
138 | ::GpiAssociate(m_hPS, NULLHANDLE); | |
139 | ::GpiDestroyPS(m_hPS); | |
140 | } | |
141 | m_hPS = NULLHANDLE; | |
142 | m_hDC = NULLHANDLE; | |
143 | } | |
144 | m_nDCCount--; | |
145 | } | |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
148 | // wxClientDC | |
149 | // ---------------------------------------------------------------------------- | |
150 | ||
151 | wxClientDC::wxClientDC() | |
152 | { | |
153 | m_pCanvas = NULL; | |
154 | } | |
155 | ||
156 | wxClientDC::wxClientDC(wxWindow *the_canvas) | |
157 | { | |
158 | SIZEL vSizl = { 0,0}; | |
159 | ERRORID vError; | |
160 | wxString sError; | |
161 | ||
162 | m_pCanvas = the_canvas; | |
163 | ||
164 | // | |
165 | // default under PM is that Window and Client DC's are the same | |
166 | // | |
167 | m_hDC = (WXHDC) ::WinOpenWindowDC(GetWinHwnd(the_canvas)); | |
168 | m_hPS = ::GpiCreatePS( wxGetInstance() | |
169 | ,m_hDC | |
170 | ,&vSizl | |
171 | ,PU_PELS | GPIF_LONG | GPIA_ASSOC | |
172 | ); | |
173 | ||
174 | // Set the wxWindows color table | |
175 | if (!::GpiCreateLogColorTable( m_hPS | |
176 | ,0L | |
177 | ,LCOLF_CONSECRGB | |
178 | ,0L | |
179 | ,(LONG)wxTheColourDatabase->m_nSize | |
180 | ,(PLONG)wxTheColourDatabase->m_palTable | |
181 | )) | |
182 | { | |
183 | vError = ::WinGetLastError(vHabmain); | |
184 | sError = wxPMErrorToStr(vError); | |
185 | wxLogError("Unable to set current color table. Error: %s\n", sError); | |
186 | } | |
187 | // | |
188 | // Default mode is BM_LEAVEALONE so we make no call Set the mix | |
189 | // | |
190 | SetBackground(wxBrush( m_pCanvas->GetBackgroundColour() | |
191 | ,wxSOLID | |
192 | ) | |
193 | ); | |
194 | } | |
195 | ||
196 | wxClientDC::~wxClientDC() | |
197 | { | |
198 | if ( m_pCanvas && GetHdc() ) | |
199 | { | |
200 | SelectOldObjects(m_hDC); | |
201 | ||
202 | // | |
203 | // We don't explicitly release Device contexts in PM and | |
204 | // the cached micro PS is already gone | |
205 | // | |
206 | m_hDC = 0; | |
207 | } | |
208 | } | |
209 | ||
210 | // ---------------------------------------------------------------------------- | |
211 | // wxPaintDC | |
212 | // ---------------------------------------------------------------------------- | |
213 | ||
214 | // VZ: initial implementation (by JACS) only remembered the last wxPaintDC | |
215 | // created and tried to reuse - this was supposed to take care of a | |
216 | // situation when a derived class OnPaint() calls base class OnPaint() | |
217 | // because in this case ::BeginPaint() shouldn't be called second time. | |
218 | // | |
219 | // I'm not sure how useful this is, however we must remember the HWND | |
220 | // associated with the last HDC as well - otherwise we may (and will!) try | |
221 | // to reuse the HDC for another HWND which is a nice recipe for disaster. | |
222 | // | |
223 | // So we store a list of windows for which we already have the DC and not | |
224 | // just one single hDC. This seems to work, but I'm really not sure about | |
225 | // the usefullness of the whole idea - IMHO it's much better to not call | |
226 | // base class OnPaint() at all, or, if we really want to allow it, add a | |
227 | // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's | |
228 | // !NULL instead of creating a new DC. | |
229 | ||
230 | wxArrayDCInfo wxPaintDC::ms_cache; | |
231 | ||
232 | wxPaintDC::wxPaintDC() | |
233 | { | |
234 | m_pCanvas = NULL; | |
235 | m_hDC = 0; | |
236 | } | |
237 | ||
238 | wxPaintDC::wxPaintDC( | |
239 | wxWindow* pCanvas | |
240 | ) | |
241 | { | |
242 | wxCHECK_RET(pCanvas, wxT("NULL canvas in wxPaintDC ctor")); | |
243 | RECTL vRect; | |
244 | ||
245 | #ifdef __WXDEBUG__ | |
246 | if (g_isPainting <= 0) | |
247 | { | |
248 | wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") ); | |
249 | return; | |
250 | } | |
251 | #endif // __WXDEBUG__ | |
252 | ||
253 | m_pCanvas = pCanvas; | |
254 | ||
255 | // | |
256 | // Do we have a DC for this window in the cache? | |
257 | // | |
258 | wxPaintDCInfo* pInfo = FindInCache(); | |
259 | ||
260 | if (pInfo) | |
261 | { | |
262 | m_hDC = pInfo->hdc; | |
263 | pInfo->count++; | |
264 | } | |
265 | else // not in cache, create a new one | |
266 | { | |
267 | HPS hPS; | |
268 | ||
269 | hPS = ::WinBeginPaint( GetWinHwnd(m_pCanvas) | |
270 | ,NULLHANDLE | |
271 | ,&g_paintStruct | |
272 | ); | |
273 | if(hPS) | |
274 | { | |
275 | m_hOldPS = m_hPS; | |
276 | m_hPS = hPS; | |
277 | ::GpiCreateLogColorTable( m_hPS | |
278 | ,0L | |
279 | ,LCOLF_CONSECRGB | |
280 | ,0L | |
281 | ,(LONG)wxTheColourDatabase->m_nSize | |
282 | ,(PLONG)wxTheColourDatabase->m_palTable | |
283 | ); | |
284 | ::GpiCreateLogColorTable( m_hPS | |
285 | ,0L | |
286 | ,LCOLF_RGB | |
287 | ,0L | |
288 | ,0L | |
289 | ,NULL | |
290 | ); | |
291 | } | |
292 | m_bIsPaintTime = TRUE; | |
293 | m_hDC = (WXHDC) -1; // to satisfy those anonizmous efforts | |
294 | m_vRclPaint = g_paintStruct; | |
295 | ms_cache.Add(new wxPaintDCInfo(m_pCanvas, this)); | |
296 | } | |
297 | SetBackground(wxBrush(m_pCanvas->GetBackgroundColour(), wxSOLID)); | |
298 | } | |
299 | ||
300 | wxPaintDC::~wxPaintDC() | |
301 | { | |
302 | if ( m_hDC ) | |
303 | { | |
304 | SelectOldObjects(m_hDC); | |
305 | ||
306 | size_t index; | |
307 | wxPaintDCInfo *info = FindInCache(&index); | |
308 | ||
309 | wxCHECK_RET( info, wxT("existing DC should have a cache entry") ); | |
310 | ||
311 | if ( !--info->count ) | |
312 | { | |
313 | ::WinEndPaint(m_hPS); | |
314 | m_hPS = m_hOldPS; | |
315 | m_bIsPaintTime = FALSE; | |
316 | ms_cache.Remove(index); | |
317 | } | |
318 | //else: cached DC entry is still in use | |
319 | ||
320 | // prevent the base class dtor from ReleaseDC()ing it again | |
321 | m_hDC = 0; | |
322 | } | |
323 | } | |
324 | ||
325 | wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const | |
326 | { | |
327 | wxPaintDCInfo *info = NULL; | |
328 | size_t nCache = ms_cache.GetCount(); | |
329 | for ( size_t n = 0; n < nCache; n++ ) | |
330 | { | |
331 | info = &ms_cache[n]; | |
332 | if ( info->hwnd == m_pCanvas->GetHWND() ) | |
333 | { | |
334 | if ( index ) | |
335 | *index = n; | |
336 | break; | |
337 | } | |
338 | } | |
339 | ||
340 | return info; | |
341 | } |