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