]>
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 | #include "wx/app.h" | |
27 | ||
28 | #include "wx/os2/private.h" | |
29 | ||
30 | #include "wx/dcclient.h" | |
31 | ||
32 | // ---------------------------------------------------------------------------- | |
33 | // array/list types | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | struct WXDLLEXPORT wxPaintDCInfo | |
37 | { | |
38 | wxPaintDCInfo( wxWindow* pWin | |
39 | ,wxDC* pDC | |
40 | ) | |
41 | { | |
42 | m_hWnd = pWin->GetHWND(); | |
43 | m_hDC = pDC->GetHDC(); | |
44 | m_nCount = 1; | |
45 | } | |
46 | ||
47 | WXHWND m_hWnd; // window for this DC | |
48 | WXHDC m_hDC; // the DC handle | |
49 | size_t m_nCount; // usage count | |
50 | }; // end of wxPaintDCInfot | |
51 | ||
52 | #include "wx/arrimpl.cpp" | |
53 | ||
54 | WX_DEFINE_OBJARRAY(wxArrayDCInfo); | |
55 | ||
56 | // ---------------------------------------------------------------------------- | |
57 | // macros | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC) | |
61 | IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC) | |
62 | IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC) | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // global variables | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | static RECT g_paintStruct; | |
69 | ||
70 | #ifdef __WXDEBUG__ | |
71 | // a global variable which we check to verify that wxPaintDC are only | |
72 | // created in resopnse to WM_PAINT message - doing this from elsewhere is a | |
73 | // common programming error among wxWidgets programmers and might lead to | |
74 | // very subtle and difficult to debug refresh/repaint bugs. | |
75 | int g_isPainting = 0; | |
76 | #endif // __WXDEBUG__ | |
77 | ||
78 | // =========================================================================== | |
79 | // implementation | |
80 | // =========================================================================== | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // wxWindowDC | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | wxWindowDC::wxWindowDC() | |
87 | { | |
88 | m_pCanvas = NULL; | |
89 | m_PageSize.cx = m_PageSize.cy = 0; | |
90 | ||
91 | } | |
92 | ||
93 | wxWindowDC::wxWindowDC( | |
94 | wxWindow* pTheCanvas | |
95 | ) | |
96 | { | |
97 | ERRORID vError; | |
98 | wxString sError; | |
99 | int nWidth, nHeight; | |
100 | ||
101 | m_pCanvas = pTheCanvas; | |
102 | DoGetSize(&nWidth, &nHeight); | |
103 | m_PageSize.cx = nWidth; | |
104 | m_PageSize.cy = nHeight; | |
105 | m_hDC = (WXHDC) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas) ); | |
106 | ||
107 | // | |
108 | // default under PM is that Window and Client DC's are the same | |
109 | // so we offer a separate Presentation Space to use for the | |
110 | // entire window. Otherwise, calling BeginPaint will just create | |
111 | // chached-micro client presentation space | |
112 | // | |
113 | m_hPS = ::GpiCreatePS( vHabmain | |
114 | ,m_hDC | |
115 | ,&m_PageSize | |
116 | ,PU_PELS | GPIF_LONG | GPIA_ASSOC | |
117 | ); | |
118 | if (!m_hPS) | |
119 | { | |
120 | vError = ::WinGetLastError(vHabmain); | |
121 | sError = wxPMErrorToStr(vError); | |
122 | wxLogError(_T("Unable to create presentation space. Error: %s\n"), sError.c_str()); | |
123 | } | |
124 | ::GpiAssociate(m_hPS, NULLHANDLE); | |
125 | ::GpiAssociate(m_hPS, m_hDC); | |
126 | ||
127 | // | |
128 | // Set the wxWidgets color table | |
129 | // | |
130 | if (!::GpiCreateLogColorTable( m_hPS | |
131 | ,0L | |
132 | ,LCOLF_CONSECRGB | |
133 | ,0L | |
134 | ,(LONG)wxTheColourDatabase->m_nSize | |
135 | ,(PLONG)wxTheColourDatabase->m_palTable | |
136 | )) | |
137 | { | |
138 | vError = ::WinGetLastError(vHabmain); | |
139 | sError = wxPMErrorToStr(vError); | |
140 | wxLogError(_T("Unable to set current color table. Error: %s\n"), sError.c_str()); | |
141 | } | |
142 | ::GpiCreateLogColorTable( m_hPS | |
143 | ,0L | |
144 | ,LCOLF_RGB | |
145 | ,0L | |
146 | ,0L | |
147 | ,NULL | |
148 | ); | |
149 | ::WinQueryWindowRect( GetWinHwnd(m_pCanvas) | |
150 | ,&m_vRclPaint | |
151 | ); | |
152 | InitDC(); | |
153 | } // end of wxWindowDC::wxWindowDC | |
154 | ||
155 | void wxWindowDC::InitDC() | |
156 | { | |
157 | ||
158 | // | |
159 | // The background mode is only used for text background and is set in | |
160 | // DrawText() to OPAQUE as required, otherwise always TRANSPARENT, | |
161 | // | |
162 | ::GpiSetBackMix(GetHPS(), BM_LEAVEALONE); | |
163 | ||
164 | // | |
165 | // Default bg colour is pne of the window | |
166 | // | |
167 | SetBackground(wxBrush(m_pCanvas->GetBackgroundColour(), wxSOLID)); | |
168 | ||
169 | wxColour vColor( wxT("BLACK") ); | |
170 | m_pen.SetColour(vColor); | |
171 | ||
172 | vColor.Set( wxT("WHITE") ); | |
173 | m_brush.SetColour(vColor); | |
174 | InitializePalette(); | |
175 | wxFont* pFont = new wxFont( 10 | |
176 | ,wxMODERN | |
177 | ,wxNORMAL | |
178 | ,wxBOLD | |
179 | ); | |
180 | SetFont(*pFont); | |
181 | delete pFont; | |
182 | // | |
183 | // OS/2 default vertical character alignment needs to match the other OS's | |
184 | // | |
185 | ::GpiSetTextAlignment((HPS)GetHPS(), TA_NORMAL_HORIZ, TA_BOTTOM); | |
186 | ||
187 | } // end of wxWindowDC::InitDC | |
188 | ||
189 | void wxWindowDC::DoGetSize( | |
190 | int* pnWidth | |
191 | , int* pnHeight | |
192 | ) const | |
193 | { | |
194 | wxCHECK_RET( m_pCanvas, _T("wxWindowDC without a window?") ); | |
195 | m_pCanvas->GetSize( pnWidth | |
196 | ,pnHeight | |
197 | ); | |
198 | } // end of wxWindowDC::DoGetSize | |
199 | ||
200 | // ---------------------------------------------------------------------------- | |
201 | // wxClientDC | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | wxClientDC::wxClientDC() | |
205 | { | |
206 | m_pCanvas = NULL; | |
207 | } | |
208 | ||
209 | wxClientDC::wxClientDC( | |
210 | wxWindow* pTheCanvas | |
211 | ) | |
212 | { | |
213 | SIZEL vSizl = { 0,0}; | |
214 | ERRORID vError; | |
215 | wxString sError; | |
216 | ||
217 | m_pCanvas = pTheCanvas; | |
218 | ||
219 | // | |
220 | // default under PM is that Window and Client DC's are the same | |
221 | // | |
222 | m_hDC = (WXHDC) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas)); | |
223 | m_hPS = ::GpiCreatePS( wxGetInstance() | |
224 | ,m_hDC | |
225 | ,&vSizl | |
226 | ,PU_PELS | GPIF_LONG | GPIA_ASSOC | |
227 | ); | |
228 | ||
229 | // Set the wxWidgets color table | |
230 | if (!::GpiCreateLogColorTable( m_hPS | |
231 | ,0L | |
232 | ,LCOLF_CONSECRGB | |
233 | ,0L | |
234 | ,(LONG)wxTheColourDatabase->m_nSize | |
235 | ,(PLONG)wxTheColourDatabase->m_palTable | |
236 | )) | |
237 | { | |
238 | vError = ::WinGetLastError(vHabmain); | |
239 | sError = wxPMErrorToStr(vError); | |
240 | wxLogError(_T("Unable to set current color table. Error: %s\n"), sError.c_str()); | |
241 | } | |
242 | ::GpiCreateLogColorTable( m_hPS | |
243 | ,0L | |
244 | ,LCOLF_RGB | |
245 | ,0L | |
246 | ,0L | |
247 | ,NULL | |
248 | ); | |
249 | // | |
250 | // Set the DC/PS rectangle | |
251 | // | |
252 | ::WinQueryWindowRect( GetWinHwnd(m_pCanvas) | |
253 | ,&m_vRclPaint | |
254 | ); | |
255 | InitDC(); | |
256 | } // end of wxClientDC::wxClientDC | |
257 | ||
258 | void wxClientDC::InitDC() | |
259 | { | |
260 | wxWindowDC::InitDC(); | |
261 | ||
262 | // in wxUniv build we must manually do some DC adjustments usually | |
263 | // performed by Windows for us | |
264 | #ifdef __WXUNIVERSAL__ | |
265 | wxPoint ptOrigin = m_pCanvas->GetClientAreaOrigin(); | |
266 | if ( ptOrigin.x || ptOrigin.y ) | |
267 | { | |
268 | // no need to shift DC origin if shift is null | |
269 | SetDeviceOrigin(ptOrigin.x, ptOrigin.y); | |
270 | } | |
271 | ||
272 | // clip the DC to avoid overwriting the non client area | |
273 | SetClippingRegion(wxPoint(0, 0), m_pCanvas->GetClientSize()); | |
274 | #endif // __WXUNIVERSAL__ | |
275 | } // end of wxClientDC::InitDC | |
276 | ||
277 | wxClientDC::~wxClientDC() | |
278 | { | |
279 | } // end of wxClientDC::~wxClientDC | |
280 | ||
281 | void wxClientDC::DoGetSize( | |
282 | int* pnWidth | |
283 | , int* pnHeight | |
284 | ) const | |
285 | { | |
286 | wxCHECK_RET( m_pCanvas, _T("wxWindowDC without a window?") ); | |
287 | m_pCanvas->GetClientSize( pnWidth | |
288 | ,pnHeight | |
289 | ); | |
290 | } // end of wxClientDC::DoGetSize | |
291 | ||
292 | // ---------------------------------------------------------------------------- | |
293 | // wxPaintDC | |
294 | // ---------------------------------------------------------------------------- | |
295 | ||
296 | wxArrayDCInfo wxPaintDC::ms_cache; | |
297 | ||
298 | wxPaintDC::wxPaintDC() | |
299 | { | |
300 | m_pCanvas = NULL; | |
301 | m_hDC = 0; | |
302 | } | |
303 | ||
304 | wxPaintDC::wxPaintDC( | |
305 | wxWindow* pCanvas | |
306 | ) | |
307 | { | |
308 | wxCHECK_RET(pCanvas, wxT("NULL canvas in wxPaintDC ctor")); | |
309 | ||
310 | #ifdef __WXDEBUG__ | |
311 | if (g_isPainting <= 0) | |
312 | { | |
313 | wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") ); | |
314 | return; | |
315 | } | |
316 | #endif // __WXDEBUG__ | |
317 | ||
318 | m_pCanvas = pCanvas; | |
319 | ||
320 | // | |
321 | // Do we have a DC for this window in the cache? | |
322 | // | |
323 | wxPaintDCInfo* pInfo = FindInCache(); | |
324 | ||
325 | if (pInfo) | |
326 | { | |
327 | m_hDC = pInfo->m_hDC; | |
328 | pInfo->m_nCount++; | |
329 | } | |
330 | else // not in cache, create a new one | |
331 | { | |
332 | HPS hPS; | |
333 | ||
334 | m_hDC = ::WinOpenWindowDC(GetWinHwnd(m_pCanvas)); | |
335 | hPS = ::WinBeginPaint( GetWinHwnd(m_pCanvas) | |
336 | ,NULLHANDLE | |
337 | ,&g_paintStruct | |
338 | ); | |
339 | if(hPS) | |
340 | { | |
341 | ::GpiAssociate(hPS, m_hDC); | |
342 | m_hOldPS = m_hPS; | |
343 | m_hPS = hPS; | |
344 | ::GpiCreateLogColorTable( m_hPS | |
345 | ,0L | |
346 | ,LCOLF_CONSECRGB | |
347 | ,0L | |
348 | ,(LONG)wxTheColourDatabase->m_nSize | |
349 | ,(PLONG)wxTheColourDatabase->m_palTable | |
350 | ); | |
351 | ::GpiCreateLogColorTable( m_hPS | |
352 | ,0L | |
353 | ,LCOLF_RGB | |
354 | ,0L | |
355 | ,0L | |
356 | ,NULL | |
357 | ); | |
358 | ||
359 | ::WinFillRect(hPS, &g_paintStruct, m_pCanvas->GetBackgroundColour().GetPixel()); | |
360 | ::WinQueryWindowRect( GetWinHwnd(m_pCanvas) | |
361 | ,&m_vRclPaint | |
362 | ); | |
363 | } | |
364 | ||
365 | m_bIsPaintTime = TRUE; | |
366 | ms_cache.Add(new wxPaintDCInfo(m_pCanvas, this)); | |
367 | } | |
368 | InitDC(); | |
369 | } // end of wxPaintDC::wxPaintDC | |
370 | ||
371 | wxPaintDC::~wxPaintDC() | |
372 | { | |
373 | if ( m_hDC ) | |
374 | { | |
375 | SelectOldObjects(m_hDC); | |
376 | ||
377 | size_t nIndex; | |
378 | wxPaintDCInfo* pInfo = FindInCache(&nIndex); | |
379 | ||
380 | wxCHECK_RET( pInfo, wxT("existing DC should have a cache entry") ); | |
381 | ||
382 | if ( !--pInfo->m_nCount ) | |
383 | { | |
384 | ::WinEndPaint(m_hPS); | |
385 | m_hPS = m_hOldPS; | |
386 | m_bIsPaintTime = FALSE; | |
387 | ms_cache.RemoveAt(nIndex); | |
388 | } | |
389 | //else: cached DC entry is still in use | |
390 | ||
391 | // prevent the base class dtor from ReleaseDC()ing it again | |
392 | m_hDC = 0; | |
393 | } | |
394 | } | |
395 | ||
396 | wxPaintDCInfo* wxPaintDC::FindInCache( | |
397 | size_t* pIndex | |
398 | ) const | |
399 | { | |
400 | wxPaintDCInfo* pInfo = NULL; | |
401 | size_t nCache = ms_cache.GetCount(); | |
402 | ||
403 | for (size_t n = 0; n < nCache; n++) | |
404 | { | |
405 | pInfo = &ms_cache[n]; | |
406 | if (pInfo->m_hWnd == m_pCanvas->GetHWND()) | |
407 | { | |
408 | if (pIndex) | |
409 | *pIndex = n; | |
410 | break; | |
411 | } | |
412 | } | |
413 | return pInfo; | |
414 | } // end of wxPaintDC::FindInCache | |
415 | ||
416 | // find the entry for this DC in the cache (keyed by the window) | |
417 | WXHDC wxPaintDC::FindDCInCache( | |
418 | wxWindow* pWin | |
419 | ) | |
420 | { | |
421 | wxPaintDCInfo* pInfo = NULL; | |
422 | size_t nCache = ms_cache.GetCount(); | |
423 | ||
424 | for (size_t n = 0; n < nCache; n++) | |
425 | { | |
426 | pInfo = &ms_cache[n]; | |
427 | if (pInfo->m_hWnd == pWin->GetHWND()) | |
428 | { | |
429 | return pInfo->m_hDC; | |
430 | } | |
431 | } | |
432 | return 0; | |
433 | } // end of wxPaintDC::FindInCache | |
434 |