1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxClientDC class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #include "wx/string.h"
25 #include "wx/window.h"
27 #include "wx/os2/private.h"
29 #include "wx/dcclient.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 struct WXDLLEXPORT wxPaintDCInfo
37 wxPaintDCInfo( wxWindow
* pWin
41 m_hWnd
= pWin
->GetHWND();
42 m_hDC
= pDC
->GetHDC();
46 WXHWND m_hWnd
; // window for this DC
47 WXHDC m_hDC
; // the DC handle
48 size_t m_nCount
; // usage count
49 }; // end of wxPaintDCInfot
51 #include "wx/arrimpl.cpp"
53 WX_DEFINE_OBJARRAY(wxArrayDCInfo
);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
60 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
61 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 static RECT g_paintStruct
;
70 // a global variable which we check to verify that wxPaintDC are only
71 // created in resopnse to WM_PAINT message - doing this from elsewhere is a
72 // common programming error among wxWindows programmers and might lead to
73 // very subtle and difficult to debug refresh/repaint bugs.
77 // ===========================================================================
79 // ===========================================================================
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 wxWindowDC::wxWindowDC()
90 wxWindowDC::wxWindowDC(
97 m_pCanvas
= pTheCanvas
;
98 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas
) );
101 // default under PM is that Window and Client DC's are the same
102 // so we offer a separate Presentation Space to use for the
103 // entire window. Otherwise, calling BeginPaint will just create
104 // chached-micro client presentation space
106 m_hPS
= ::GpiCreatePS( vHabmain
109 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
111 ::GpiAssociate(m_hPS
, NULLHANDLE
);
112 ::GpiAssociate(m_hPS
, m_hDC
);
115 // Set the wxWindows color table
117 if (!::GpiCreateLogColorTable( m_hPS
121 ,(LONG
)wxTheColourDatabase
->m_nSize
122 ,(PLONG
)wxTheColourDatabase
->m_palTable
125 vError
= ::WinGetLastError(vHabmain
);
126 sError
= wxPMErrorToStr(vError
);
127 wxLogError("Unable to set current color table. Error: %s\n", sError
);
129 ::GpiCreateLogColorTable( m_hPS
136 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
140 } // end of wxWindowDC::wxWindowDC
142 void wxWindowDC::InitDC()
145 // The background mode is only used for text background and is set in
146 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
148 ::GpiSetBackMix(GetHPS(), BM_LEAVEALONE
);
151 // Default bg colour is pne of the window
153 SetBackground(wxBrush(m_pCanvas
->GetBackgroundColour(), wxSOLID
));
154 } // end of wxWindowDC::InitDC
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 wxClientDC::wxClientDC()
165 wxClientDC::wxClientDC(
169 SIZEL vSizl
= { 0,0};
173 m_pCanvas
= pTheCanvas
;
176 // default under PM is that Window and Client DC's are the same
178 m_hDC
= (WXHDC
) ::WinOpenWindowDC(GetWinHwnd(pTheCanvas
));
179 m_hPS
= ::GpiCreatePS( wxGetInstance()
182 ,PU_PELS
| GPIF_LONG
| GPIA_ASSOC
185 // Set the wxWindows color table
186 if (!::GpiCreateLogColorTable( m_hPS
190 ,(LONG
)wxTheColourDatabase
->m_nSize
191 ,(PLONG
)wxTheColourDatabase
->m_palTable
194 vError
= ::WinGetLastError(vHabmain
);
195 sError
= wxPMErrorToStr(vError
);
196 wxLogError("Unable to set current color table. Error: %s\n", sError
);
198 ::GpiCreateLogColorTable( m_hPS
206 // Set the DC/PS rectangle
208 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
212 } // end of wxClientDC::wxClientDC
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 wxArrayDCInfo
wxPaintDC::ms_cache
;
220 wxPaintDC::wxPaintDC()
226 wxPaintDC::wxPaintDC(
230 wxCHECK_RET(pCanvas
, wxT("NULL canvas in wxPaintDC ctor"));
233 if (g_isPainting
<= 0)
235 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
238 #endif // __WXDEBUG__
243 // Do we have a DC for this window in the cache?
245 wxPaintDCInfo
* pInfo
= FindInCache();
249 m_hDC
= pInfo
->m_hDC
;
252 else // not in cache, create a new one
256 hPS
= ::WinBeginPaint( GetWinHwnd(m_pCanvas
)
264 ::GpiCreateLogColorTable( m_hPS
268 ,(LONG
)wxTheColourDatabase
->m_nSize
269 ,(PLONG
)wxTheColourDatabase
->m_palTable
271 ::GpiCreateLogColorTable( m_hPS
279 ::WinFillRect(hPS
, &g_paintStruct
, m_pCanvas
->GetBackgroundColour().GetPixel());
280 ::WinQueryWindowRect( GetWinHwnd(m_pCanvas
)
285 m_bIsPaintTime
= TRUE
;
286 m_hDC
= (WXHDC
) -1; // to satisfy those anonizmous efforts
287 ms_cache
.Add(new wxPaintDCInfo(m_pCanvas
, this));
290 } // end of wxPaintDC::wxPaintDC
292 wxPaintDC::~wxPaintDC()
296 SelectOldObjects(m_hDC
);
299 wxPaintDCInfo
* pInfo
= FindInCache(&nIndex
);
301 wxCHECK_RET( pInfo
, wxT("existing DC should have a cache entry") );
303 if ( !--pInfo
->m_nCount
)
305 ::WinEndPaint(m_hPS
);
307 m_bIsPaintTime
= FALSE
;
308 ms_cache
.Remove(nIndex
);
310 //else: cached DC entry is still in use
312 // prevent the base class dtor from ReleaseDC()ing it again
317 wxPaintDCInfo
* wxPaintDC::FindInCache(
321 wxPaintDCInfo
* pInfo
= NULL
;
322 size_t nCache
= ms_cache
.GetCount();
324 for (size_t n
= 0; n
< nCache
; n
++)
326 pInfo
= &ms_cache
[n
];
327 if (pInfo
->m_hWnd
== m_pCanvas
->GetHWND())
335 } // end of wxPaintDC::FindInCache
337 // find the entry for this DC in the cache (keyed by the window)
338 WXHDC
wxPaintDC::FindDCInCache(
342 wxPaintDCInfo
* pInfo
= NULL
;
343 size_t nCache
= ms_cache
.GetCount();
345 for (size_t n
= 0; n
< nCache
; n
++)
347 pInfo
= &ms_cache
[n
];
348 if (pInfo
->m_hWnd
== pWin
->GetHWND())
354 } // end of wxPaintDC::FindInCache