]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dcclient.cpp
wxDC::SetFont can now handle invalid font and doesn't assert anymore (other ports...
[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
147 vColor.InitFromName("BLACK");
148 m_pen.SetColour(vColor);
149 vColor.Set("WHITE");
150 m_brush.SetColour(vColor);
e1a688e4
DW
151 //
152 // The background mode is only used for text background and is set in
153 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
154 //
155 ::GpiSetBackMix(GetHPS(), BM_LEAVEALONE);
26ac77db 156
e1a688e4
DW
157 //
158 // Default bg colour is pne of the window
159 //
160 SetBackground(wxBrush(m_pCanvas->GetBackgroundColour(), wxSOLID));
161} // end of wxWindowDC::InitDC
0e320a79 162
23e4b7d8
DW
163// ----------------------------------------------------------------------------
164// wxClientDC
165// ----------------------------------------------------------------------------
0e320a79 166
23e4b7d8 167wxClientDC::wxClientDC()
0e320a79 168{
26ac77db 169 m_pCanvas = NULL;
23e4b7d8 170}
0e320a79 171
e99762c0
DW
172wxClientDC::wxClientDC(
173 wxWindow* pTheCanvas
174)
0e320a79 175{
7e99520b 176 SIZEL vSizl = { 0,0};
26ac77db
DW
177 ERRORID vError;
178 wxString sError;
0e320a79 179
e99762c0 180 m_pCanvas = pTheCanvas;
1408104d 181
7e99520b
DW
182 //
183 // default under PM is that Window and Client DC's are the same
184 //
e99762c0 185 m_hDC = (WXHDC) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas));
7e99520b
DW
186 m_hPS = ::GpiCreatePS( wxGetInstance()
187 ,m_hDC
f44fdfb0 188 ,&vSizl
7e99520b
DW
189 ,PU_PELS | GPIF_LONG | GPIA_ASSOC
190 );
191
26ac77db
DW
192 // Set the wxWindows color table
193 if (!::GpiCreateLogColorTable( m_hPS
194 ,0L
195 ,LCOLF_CONSECRGB
196 ,0L
197 ,(LONG)wxTheColourDatabase->m_nSize
198 ,(PLONG)wxTheColourDatabase->m_palTable
199 ))
200 {
201 vError = ::WinGetLastError(vHabmain);
202 sError = wxPMErrorToStr(vError);
203 wxLogError("Unable to set current color table. Error: %s\n", sError);
204 }
5afb9458
DW
205 ::GpiCreateLogColorTable( m_hPS
206 ,0L
207 ,LCOLF_RGB
208 ,0L
209 ,0L
210 ,NULL
211 );
7e99520b 212 //
5afb9458
DW
213 // Set the DC/PS rectangle
214 //
215 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas)
216 ,&m_vRclPaint
217 );
e1a688e4 218 InitDC();
e99762c0 219} // end of wxClientDC::wxClientDC
0e320a79 220
0367c1c0
DW
221void wxClientDC::InitDC()
222{
223 wxWindowDC::InitDC();
224
225 // in wxUniv build we must manually do some DC adjustments usually
226 // performed by Windows for us
227#ifdef __WXUNIVERSAL__
19193a2c 228 wxPoint ptOrigin = m_pCanvas->GetClientAreaOrigin();
0367c1c0
DW
229 if ( ptOrigin.x || ptOrigin.y )
230 {
231 // no need to shift DC origin if shift is null
232 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
233 }
234
235 // clip the DC to avoid overwriting the non client area
19193a2c 236 SetClippingRegion(wxPoint(0, 0), m_pCanvas->GetClientSize());
0367c1c0
DW
237#endif // __WXUNIVERSAL__
238} // end of wxClientDC::InitDC
239
240wxClientDC::~wxClientDC()
241{
242} // end of wxClientDC::~wxClientDC
243
23e4b7d8
DW
244// ----------------------------------------------------------------------------
245// wxPaintDC
246// ----------------------------------------------------------------------------
247
23e4b7d8
DW
248wxArrayDCInfo wxPaintDC::ms_cache;
249
250wxPaintDC::wxPaintDC()
251{
f6bcfd97 252 m_pCanvas = NULL;
23e4b7d8
DW
253 m_hDC = 0;
254}
0e320a79 255
7e99520b
DW
256wxPaintDC::wxPaintDC(
257 wxWindow* pCanvas
258)
0e320a79 259{
f44fdfb0 260 wxCHECK_RET(pCanvas, wxT("NULL canvas in wxPaintDC ctor"));
1408104d 261
23e4b7d8 262#ifdef __WXDEBUG__
7e99520b 263 if (g_isPainting <= 0)
23e4b7d8 264 {
223d09f6 265 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
23e4b7d8
DW
266 return;
267 }
268#endif // __WXDEBUG__
0e320a79 269
7e99520b 270 m_pCanvas = pCanvas;
1408104d 271
7e99520b
DW
272 //
273 // Do we have a DC for this window in the cache?
274 //
275 wxPaintDCInfo* pInfo = FindInCache();
276
277 if (pInfo)
0e320a79 278 {
e99762c0
DW
279 m_hDC = pInfo->m_hDC;
280 pInfo->m_nCount++;
23e4b7d8
DW
281 }
282 else // not in cache, create a new one
283 {
7e99520b
DW
284 HPS hPS;
285
286 hPS = ::WinBeginPaint( GetWinHwnd(m_pCanvas)
287 ,NULLHANDLE
288 ,&g_paintStruct
289 );
290 if(hPS)
291 {
292 m_hOldPS = m_hPS;
293 m_hPS = hPS;
26ac77db
DW
294 ::GpiCreateLogColorTable( m_hPS
295 ,0L
296 ,LCOLF_CONSECRGB
297 ,0L
298 ,(LONG)wxTheColourDatabase->m_nSize
299 ,(PLONG)wxTheColourDatabase->m_palTable
300 );
301 ::GpiCreateLogColorTable( m_hPS
302 ,0L
303 ,LCOLF_RGB
304 ,0L
305 ,0L
306 ,NULL
307 );
8d854fa9
DW
308
309 ::WinFillRect(hPS, &g_paintStruct, m_pCanvas->GetBackgroundColour().GetPixel());
eeff964a
DW
310 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas)
311 ,&m_vRclPaint
312 );
7e99520b 313 }
e99762c0 314
7e99520b
DW
315 m_bIsPaintTime = TRUE;
316 m_hDC = (WXHDC) -1; // to satisfy those anonizmous efforts
f6bcfd97 317 ms_cache.Add(new wxPaintDCInfo(m_pCanvas, this));
0e320a79 318 }
e1a688e4
DW
319 InitDC();
320} // end of wxPaintDC::wxPaintDC
0e320a79 321
23e4b7d8 322wxPaintDC::~wxPaintDC()
0e320a79 323{
23e4b7d8
DW
324 if ( m_hDC )
325 {
326 SelectOldObjects(m_hDC);
0e320a79 327
e99762c0
DW
328 size_t nIndex;
329 wxPaintDCInfo* pInfo = FindInCache(&nIndex);
0e320a79 330
e99762c0 331 wxCHECK_RET( pInfo, wxT("existing DC should have a cache entry") );
0e320a79 332
e99762c0 333 if ( !--pInfo->m_nCount )
23e4b7d8 334 {
ce44c50e 335 ::WinEndPaint(m_hPS);
f44fdfb0 336 m_hPS = m_hOldPS;
7e99520b 337 m_bIsPaintTime = FALSE;
893758d5 338 ms_cache.RemoveAt(nIndex);
23e4b7d8
DW
339 }
340 //else: cached DC entry is still in use
0e320a79 341
23e4b7d8
DW
342 // prevent the base class dtor from ReleaseDC()ing it again
343 m_hDC = 0;
0e320a79
DW
344 }
345}
346
e99762c0
DW
347wxPaintDCInfo* wxPaintDC::FindInCache(
348 size_t* pIndex
349) const
0e320a79 350{
e99762c0
DW
351 wxPaintDCInfo* pInfo = NULL;
352 size_t nCache = ms_cache.GetCount();
353
354 for (size_t n = 0; n < nCache; n++)
0e320a79 355 {
e99762c0
DW
356 pInfo = &ms_cache[n];
357 if (pInfo->m_hWnd == m_pCanvas->GetHWND())
23e4b7d8 358 {
e99762c0
DW
359 if (pIndex)
360 *pIndex = n;
23e4b7d8
DW
361 break;
362 }
0e320a79 363 }
e99762c0
DW
364 return pInfo;
365} // end of wxPaintDC::FindInCache
0e320a79 366
e1a688e4
DW
367// find the entry for this DC in the cache (keyed by the window)
368WXHDC wxPaintDC::FindDCInCache(
369 wxWindow* pWin
370)
371{
372 wxPaintDCInfo* pInfo = NULL;
373 size_t nCache = ms_cache.GetCount();
374
375 for (size_t n = 0; n < nCache; n++)
376 {
377 pInfo = &ms_cache[n];
378 if (pInfo->m_hWnd == pWin->GetHWND())
379 {
380 return pInfo->m_hDC;
381 }
382 }
383 return 0;
384} // end of wxPaintDC::FindInCache
385