This weeks interface and compile catchup
[wxWidgets.git] / src / os2 / dcclient.cpp
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 wxWindows 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 }
90
91 wxWindowDC::wxWindowDC(
92 wxWindow* pTheCanvas
93 )
94 {
95 ERRORID vError;
96 wxString sError;
97
98 m_pCanvas = pTheCanvas;
99 m_hDC = (WXHDC) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas) );
100
101 //
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
106 //
107 m_hPS = ::GpiCreatePS( vHabmain
108 ,m_hDC
109 ,&m_PageSize
110 ,PU_PELS | GPIF_LONG | GPIA_ASSOC
111 );
112 ::GpiAssociate(m_hPS, NULLHANDLE);
113 ::GpiAssociate(m_hPS, m_hDC);
114
115 //
116 // Set the wxWindows color table
117 //
118 if (!::GpiCreateLogColorTable( m_hPS
119 ,0L
120 ,LCOLF_CONSECRGB
121 ,0L
122 ,(LONG)wxTheColourDatabase->m_nSize
123 ,(PLONG)wxTheColourDatabase->m_palTable
124 ))
125 {
126 vError = ::WinGetLastError(vHabmain);
127 sError = wxPMErrorToStr(vError);
128 wxLogError("Unable to set current color table. Error: %s\n", sError);
129 }
130 ::GpiCreateLogColorTable( m_hPS
131 ,0L
132 ,LCOLF_RGB
133 ,0L
134 ,0L
135 ,NULL
136 );
137 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas)
138 ,&m_vRclPaint
139 );
140 InitDC();
141 } // end of wxWindowDC::wxWindowDC
142
143 void wxWindowDC::InitDC()
144 {
145 wxColour vColor;
146
147 vColor.InitFromName("BLACK");
148 m_pen.SetColour(vColor);
149 vColor.Set("WHITE");
150 m_brush.SetColour(vColor);
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);
156
157 //
158 // Default bg colour is pne of the window
159 //
160 SetBackground(wxBrush(m_pCanvas->GetBackgroundColour(), wxSOLID));
161 } // end of wxWindowDC::InitDC
162
163 void wxWindowDC::DoGetSize(
164 int* pnWidth
165 , int* pnHeight
166 ) const
167 {
168 wxCHECK_RET( m_pCanvas, _T("wxWindowDC without a window?") );
169 m_pCanvas->GetSize( pnWidth
170 ,pnHeight
171 );
172 } // end of wxWindowDC::DoGetSize
173
174 // ----------------------------------------------------------------------------
175 // wxClientDC
176 // ----------------------------------------------------------------------------
177
178 wxClientDC::wxClientDC()
179 {
180 m_pCanvas = NULL;
181 }
182
183 wxClientDC::wxClientDC(
184 wxWindow* pTheCanvas
185 )
186 {
187 SIZEL vSizl = { 0,0};
188 ERRORID vError;
189 wxString sError;
190
191 m_pCanvas = pTheCanvas;
192
193 //
194 // default under PM is that Window and Client DC's are the same
195 //
196 m_hDC = (WXHDC) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas));
197 m_hPS = ::GpiCreatePS( wxGetInstance()
198 ,m_hDC
199 ,&vSizl
200 ,PU_PELS | GPIF_LONG | GPIA_ASSOC
201 );
202
203 // Set the wxWindows color table
204 if (!::GpiCreateLogColorTable( m_hPS
205 ,0L
206 ,LCOLF_CONSECRGB
207 ,0L
208 ,(LONG)wxTheColourDatabase->m_nSize
209 ,(PLONG)wxTheColourDatabase->m_palTable
210 ))
211 {
212 vError = ::WinGetLastError(vHabmain);
213 sError = wxPMErrorToStr(vError);
214 wxLogError("Unable to set current color table. Error: %s\n", sError);
215 }
216 ::GpiCreateLogColorTable( m_hPS
217 ,0L
218 ,LCOLF_RGB
219 ,0L
220 ,0L
221 ,NULL
222 );
223 //
224 // Set the DC/PS rectangle
225 //
226 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas)
227 ,&m_vRclPaint
228 );
229 InitDC();
230 } // end of wxClientDC::wxClientDC
231
232 void wxClientDC::InitDC()
233 {
234 wxWindowDC::InitDC();
235
236 // in wxUniv build we must manually do some DC adjustments usually
237 // performed by Windows for us
238 #ifdef __WXUNIVERSAL__
239 wxPoint ptOrigin = m_pCanvas->GetClientAreaOrigin();
240 if ( ptOrigin.x || ptOrigin.y )
241 {
242 // no need to shift DC origin if shift is null
243 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
244 }
245
246 // clip the DC to avoid overwriting the non client area
247 SetClippingRegion(wxPoint(0, 0), m_pCanvas->GetClientSize());
248 #endif // __WXUNIVERSAL__
249 } // end of wxClientDC::InitDC
250
251 wxClientDC::~wxClientDC()
252 {
253 } // end of wxClientDC::~wxClientDC
254
255 void wxClientDC::DoGetSize(
256 int* pnWidth
257 , int* pnHeight
258 ) const
259 {
260 wxCHECK_RET( m_pCanvas, _T("wxWindowDC without a window?") );
261 m_pCanvas->GetClientSize( pnWidth
262 ,pnHeight
263 );
264 } // end of wxClientDC::DoGetSize
265
266 // ----------------------------------------------------------------------------
267 // wxPaintDC
268 // ----------------------------------------------------------------------------
269
270 wxArrayDCInfo wxPaintDC::ms_cache;
271
272 wxPaintDC::wxPaintDC()
273 {
274 m_pCanvas = NULL;
275 m_hDC = 0;
276 }
277
278 wxPaintDC::wxPaintDC(
279 wxWindow* pCanvas
280 )
281 {
282 wxCHECK_RET(pCanvas, wxT("NULL canvas in wxPaintDC ctor"));
283
284 #ifdef __WXDEBUG__
285 if (g_isPainting <= 0)
286 {
287 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
288 return;
289 }
290 #endif // __WXDEBUG__
291
292 m_pCanvas = pCanvas;
293
294 //
295 // Do we have a DC for this window in the cache?
296 //
297 wxPaintDCInfo* pInfo = FindInCache();
298
299 if (pInfo)
300 {
301 m_hDC = pInfo->m_hDC;
302 pInfo->m_nCount++;
303 }
304 else // not in cache, create a new one
305 {
306 HPS hPS;
307
308 hPS = ::WinBeginPaint( GetWinHwnd(m_pCanvas)
309 ,NULLHANDLE
310 ,&g_paintStruct
311 );
312 if(hPS)
313 {
314 m_hOldPS = m_hPS;
315 m_hPS = hPS;
316 ::GpiCreateLogColorTable( m_hPS
317 ,0L
318 ,LCOLF_CONSECRGB
319 ,0L
320 ,(LONG)wxTheColourDatabase->m_nSize
321 ,(PLONG)wxTheColourDatabase->m_palTable
322 );
323 ::GpiCreateLogColorTable( m_hPS
324 ,0L
325 ,LCOLF_RGB
326 ,0L
327 ,0L
328 ,NULL
329 );
330
331 ::WinFillRect(hPS, &g_paintStruct, m_pCanvas->GetBackgroundColour().GetPixel());
332 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas)
333 ,&m_vRclPaint
334 );
335 }
336
337 m_bIsPaintTime = TRUE;
338 m_hDC = (WXHDC) -1; // to satisfy those anonizmous efforts
339 ms_cache.Add(new wxPaintDCInfo(m_pCanvas, this));
340 }
341 InitDC();
342 } // end of wxPaintDC::wxPaintDC
343
344 wxPaintDC::~wxPaintDC()
345 {
346 if ( m_hDC )
347 {
348 SelectOldObjects(m_hDC);
349
350 size_t nIndex;
351 wxPaintDCInfo* pInfo = FindInCache(&nIndex);
352
353 wxCHECK_RET( pInfo, wxT("existing DC should have a cache entry") );
354
355 if ( !--pInfo->m_nCount )
356 {
357 ::WinEndPaint(m_hPS);
358 m_hPS = m_hOldPS;
359 m_bIsPaintTime = FALSE;
360 ms_cache.RemoveAt(nIndex);
361 }
362 //else: cached DC entry is still in use
363
364 // prevent the base class dtor from ReleaseDC()ing it again
365 m_hDC = 0;
366 }
367 }
368
369 wxPaintDCInfo* wxPaintDC::FindInCache(
370 size_t* pIndex
371 ) const
372 {
373 wxPaintDCInfo* pInfo = NULL;
374 size_t nCache = ms_cache.GetCount();
375
376 for (size_t n = 0; n < nCache; n++)
377 {
378 pInfo = &ms_cache[n];
379 if (pInfo->m_hWnd == m_pCanvas->GetHWND())
380 {
381 if (pIndex)
382 *pIndex = n;
383 break;
384 }
385 }
386 return pInfo;
387 } // end of wxPaintDC::FindInCache
388
389 // find the entry for this DC in the cache (keyed by the window)
390 WXHDC wxPaintDC::FindDCInCache(
391 wxWindow* pWin
392 )
393 {
394 wxPaintDCInfo* pInfo = NULL;
395 size_t nCache = ms_cache.GetCount();
396
397 for (size_t n = 0; n < nCache; n++)
398 {
399 pInfo = &ms_cache[n];
400 if (pInfo->m_hWnd == pWin->GetHWND())
401 {
402 return pInfo->m_hDC;
403 }
404 }
405 return 0;
406 } // end of wxPaintDC::FindInCache
407