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