]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dcclient.cpp
More OS/2 Updates
[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"
0e320a79 26
ce44c50e 27#include "wx/os2/private.h"
0e320a79 28
23e4b7d8 29#include "wx/dcclient.h"
0e320a79 30
23e4b7d8
DW
31// ----------------------------------------------------------------------------
32// array/list types
33// ----------------------------------------------------------------------------
0e320a79 34
23e4b7d8 35struct WXDLLEXPORT wxPaintDCInfo
0e320a79 36{
23e4b7d8
DW
37 wxPaintDCInfo(wxWindow *win, wxDC *dc)
38 {
39 hwnd = win->GetHWND();
40 hdc = dc->GetHDC();
41 count = 1;
42 }
0e320a79 43
23e4b7d8
DW
44 WXHWND hwnd; // window for this DC
45 WXHDC hdc; // the DC handle
46 size_t count; // usage count
0e320a79
DW
47};
48
23e4b7d8 49#include "wx/arrimpl.cpp"
1408104d 50
23e4b7d8 51WX_DEFINE_OBJARRAY(wxArrayDCInfo);
1408104d 52
23e4b7d8
DW
53// ----------------------------------------------------------------------------
54// macros
55// ----------------------------------------------------------------------------
0e320a79 56
23e4b7d8
DW
57 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
58 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
59 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
0e320a79 60
23e4b7d8
DW
61// ----------------------------------------------------------------------------
62// global variables
63// ----------------------------------------------------------------------------
1408104d 64
ce44c50e 65static RECT g_paintStruct;
1408104d 66
23e4b7d8
DW
67#ifdef __WXDEBUG__
68 // a global variable which we check to verify that wxPaintDC are only
69 // created in resopnse to WM_PAINT message - doing this from elsewhere is a
70 // common programming error among wxWindows programmers and might lead to
71 // very subtle and difficult to debug refresh/repaint bugs.
72 int g_isPainting = 0;
73#endif // __WXDEBUG__
1408104d 74
23e4b7d8
DW
75// ===========================================================================
76// implementation
77// ===========================================================================
1408104d 78
23e4b7d8
DW
79// ----------------------------------------------------------------------------
80// wxWindowDC
81// ----------------------------------------------------------------------------
0e320a79 82
23e4b7d8 83wxWindowDC::wxWindowDC()
0e320a79 84{
f6bcfd97 85 m_pCanvas = NULL;
23e4b7d8 86}
0e320a79 87
23e4b7d8 88wxWindowDC::wxWindowDC(wxWindow *the_canvas)
0e320a79 89{
f6bcfd97 90 m_pCanvas = the_canvas;
23e4b7d8 91 m_hDC = (WXHDC) ::WinOpenWindowDC(GetWinHwnd(the_canvas) );
f6bcfd97 92 m_nDCCount++;
ce44c50e
DW
93 //
94 // default under PM is that Window and Client DC's are the same
95 // so we offer a separate Presentation Space to use for the
96 // entire window. Otherwise, calling BeginPaint will just create
97 // chached-micro client presentation space
98 //
99 m_hPS = GpiCreatePS( m_hab
100 ,m_hDC
101 ,&m_PageSize
102 ,PU_PELS | GPIF_LONG | GPIA_ASSOC
103 );
104 ::GpiAssociate(m_hPS, NULLHANDLE);
105 ::GpiAssociate(m_hPS, m_hDC);
f6bcfd97 106 SetBackground(wxBrush(m_pCanvas->GetBackgroundColour(), wxSOLID));
23e4b7d8 107}
0e320a79 108
23e4b7d8 109wxWindowDC::~wxWindowDC()
0e320a79 110{
f6bcfd97 111 if (m_pCanvas && m_hDC)
23e4b7d8
DW
112 {
113 SelectOldObjects(m_hDC);
1408104d 114
23e4b7d8
DW
115 //
116 // In PM one does not explicitly close or release an open WindowDC
117 // They automatically close with the window, unless explicitly detached
ce44c50e 118 // but we need to destroy our PS
23e4b7d8 119 //
7e99520b
DW
120 if(m_hPS)
121 {
122 ::GpiAssociate(m_hPS, NULLHANDLE);
123 ::GpiDestroyPS(m_hPS);
124 }
ce44c50e
DW
125 m_hPS = NULLHANDLE;
126 m_hDC = NULLHANDLE;
23e4b7d8 127 }
1408104d 128
f6bcfd97 129 m_nDCCount--;
23e4b7d8 130}
0e320a79 131
23e4b7d8
DW
132// ----------------------------------------------------------------------------
133// wxClientDC
134// ----------------------------------------------------------------------------
0e320a79 135
23e4b7d8 136wxClientDC::wxClientDC()
0e320a79 137{
f6bcfd97 138 m_pCanvas = NULL;
23e4b7d8 139}
0e320a79 140
23e4b7d8 141wxClientDC::wxClientDC(wxWindow *the_canvas)
0e320a79 142{
7e99520b 143 SIZEL vSizl = { 0,0};
0e320a79 144
7e99520b 145 m_pCanvas = the_canvas;
1408104d 146
7e99520b
DW
147 //
148 // default under PM is that Window and Client DC's are the same
149 //
150 m_hDC = (WXHDC) ::WinOpenWindowDC(GetWinHwnd(the_canvas));
151 m_hPS = ::GpiCreatePS( wxGetInstance()
152 ,m_hDC
f44fdfb0 153 ,&vSizl
7e99520b
DW
154 ,PU_PELS | GPIF_LONG | GPIA_ASSOC
155 );
156
157 //
158 // Default mode is BM_LEAVEALONE so we make no call Set the mix
159 //
160 SetBackground(wxBrush( m_pCanvas->GetBackgroundColour()
161 ,wxSOLID
162 )
163 );
23e4b7d8 164}
0e320a79 165
23e4b7d8 166wxClientDC::~wxClientDC()
0e320a79 167{
f6bcfd97 168 if ( m_pCanvas && GetHdc() )
0e320a79 169 {
23e4b7d8 170 SelectOldObjects(m_hDC);
0e320a79 171
ce44c50e
DW
172 // We don't explicitly release Device contexts in PM and
173 // the cached micro PS is already gone
174
23e4b7d8 175 m_hDC = 0;
0e320a79 176 }
23e4b7d8 177}
0e320a79 178
23e4b7d8
DW
179// ----------------------------------------------------------------------------
180// wxPaintDC
181// ----------------------------------------------------------------------------
182
183// VZ: initial implementation (by JACS) only remembered the last wxPaintDC
184// created and tried to reuse - this was supposed to take care of a
185// situation when a derived class OnPaint() calls base class OnPaint()
186// because in this case ::BeginPaint() shouldn't be called second time.
187//
188// I'm not sure how useful this is, however we must remember the HWND
189// associated with the last HDC as well - otherwise we may (and will!) try
190// to reuse the HDC for another HWND which is a nice recipe for disaster.
191//
192// So we store a list of windows for which we already have the DC and not
193// just one single hDC. This seems to work, but I'm really not sure about
194// the usefullness of the whole idea - IMHO it's much better to not call
195// base class OnPaint() at all, or, if we really want to allow it, add a
196// "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
197// !NULL instead of creating a new DC.
198
199wxArrayDCInfo wxPaintDC::ms_cache;
200
201wxPaintDC::wxPaintDC()
202{
f6bcfd97 203 m_pCanvas = NULL;
23e4b7d8
DW
204 m_hDC = 0;
205}
0e320a79 206
7e99520b
DW
207wxPaintDC::wxPaintDC(
208 wxWindow* pCanvas
209)
0e320a79 210{
f44fdfb0 211 wxCHECK_RET(pCanvas, wxT("NULL canvas in wxPaintDC ctor"));
1408104d 212
23e4b7d8 213#ifdef __WXDEBUG__
7e99520b 214 if (g_isPainting <= 0)
23e4b7d8 215 {
223d09f6 216 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
23e4b7d8
DW
217 return;
218 }
219#endif // __WXDEBUG__
0e320a79 220
7e99520b 221 m_pCanvas = pCanvas;
1408104d 222
7e99520b
DW
223 //
224 // Do we have a DC for this window in the cache?
225 //
226 wxPaintDCInfo* pInfo = FindInCache();
227
228 if (pInfo)
0e320a79 229 {
7e99520b
DW
230 m_hDC = pInfo->hdc;
231 pInfo->count++;
23e4b7d8
DW
232 }
233 else // not in cache, create a new one
234 {
7e99520b
DW
235 HPS hPS;
236
237 hPS = ::WinBeginPaint( GetWinHwnd(m_pCanvas)
238 ,NULLHANDLE
239 ,&g_paintStruct
240 );
241 if(hPS)
242 {
243 m_hOldPS = m_hPS;
244 m_hPS = hPS;
245 }
246 m_bIsPaintTime = TRUE;
247 m_hDC = (WXHDC) -1; // to satisfy those anonizmous efforts
248 m_vRclPaint = g_paintStruct;
f6bcfd97 249 ms_cache.Add(new wxPaintDCInfo(m_pCanvas, this));
0e320a79 250 }
f6bcfd97 251 SetBackground(wxBrush(m_pCanvas->GetBackgroundColour(), wxSOLID));
0e320a79
DW
252}
253
23e4b7d8 254wxPaintDC::~wxPaintDC()
0e320a79 255{
23e4b7d8
DW
256 if ( m_hDC )
257 {
258 SelectOldObjects(m_hDC);
0e320a79 259
23e4b7d8
DW
260 size_t index;
261 wxPaintDCInfo *info = FindInCache(&index);
0e320a79 262
223d09f6 263 wxCHECK_RET( info, wxT("existing DC should have a cache entry") );
0e320a79 264
23e4b7d8
DW
265 if ( !--info->count )
266 {
ce44c50e 267 ::WinEndPaint(m_hPS);
f44fdfb0 268 m_hPS = m_hOldPS;
7e99520b 269 m_bIsPaintTime = FALSE;
23e4b7d8
DW
270 ms_cache.Remove(index);
271 }
272 //else: cached DC entry is still in use
0e320a79 273
23e4b7d8
DW
274 // prevent the base class dtor from ReleaseDC()ing it again
275 m_hDC = 0;
0e320a79
DW
276 }
277}
278
23e4b7d8 279wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const
0e320a79 280{
23e4b7d8
DW
281 wxPaintDCInfo *info = NULL;
282 size_t nCache = ms_cache.GetCount();
283 for ( size_t n = 0; n < nCache; n++ )
0e320a79 284 {
23e4b7d8 285 info = &ms_cache[n];
f6bcfd97 286 if ( info->hwnd == m_pCanvas->GetHWND() )
23e4b7d8
DW
287 {
288 if ( index )
289 *index = n;
290 break;
291 }
0e320a79
DW
292 }
293
23e4b7d8
DW
294 return info;
295}