1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dcclient.cpp
3 // Purpose: wxClientDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/dcclient.h"
28 #include "wx/msw/dcclient.h"
31 #include "wx/string.h"
33 #include "wx/window.h"
36 #if wxUSE_GRAPHICS_CONTEXT
37 #include "wx/graphics.h"
40 #include "wx/msw/private.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 struct WXDLLEXPORT wxPaintDCInfo
48 wxPaintDCInfo(wxWindow
*win
, wxPaintDCImpl
*dc
)
50 hwnd
= win
->GetHWND();
55 WXHWND hwnd
; // window for this DC
56 WXHDC hdc
; // the DC handle
57 size_t count
; // usage count
60 #include "wx/arrimpl.cpp"
62 WX_DEFINE_OBJARRAY(wxArrayDCInfo
)
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 static PAINTSTRUCT g_paintStruct
;
75 // a global variable which we check to verify that wxPaintDC are only
76 // created in response to WM_PAINT message - doing this from elsewhere is a
77 // common programming error among wxWidgets programmers and might lead to
78 // very subtle and difficult to debug refresh/repaint bugs.
82 // ===========================================================================
84 // ===========================================================================
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl
, wxMSWDCImpl
)
92 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
) :
97 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
) :
100 wxCHECK_RET( window
, _T("invalid window in wxWindowDCImpl") );
103 m_hDC
= (WXHDC
) ::GetWindowDC(GetHwndOf(m_window
));
105 // m_bOwnsDC was already set to false in the base class ctor, so the DC
106 // will be released (and not deleted) in ~wxDC
110 void wxWindowDCImpl::InitDC()
112 // the background mode is only used for text background and is set in
113 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT,
114 ::SetBkMode(GetHdc(), TRANSPARENT
);
116 // default bg colour is pne of the window
117 SetBackground(wxBrush(m_window
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
119 // since we are a window dc we need to grab the palette from the window
125 #if wxUSE_GRAPHICS_CONTEXT
126 wxGraphicsContext
* wxWindowDCImpl::CreateGraphicsContext()
128 wxWindowDC
*windowdc
= (wxWindowDC
*) GetOwner();
129 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext( *windowdc
);
133 void wxWindowDCImpl::DoGetSize(int *width
, int *height
) const
135 wxCHECK_RET( m_window
, _T("wxWindowDCImpl without a window?") );
137 m_window
->GetSize(width
, height
);
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
146 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
) :
147 wxWindowDCImpl( owner
)
151 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*window
) :
152 wxWindowDCImpl( owner
)
154 wxCHECK_RET( window
, _T("invalid window in wxClientDCImpl") );
157 m_hDC
= (WXHDC
)::GetDC(GetHwndOf(window
));
159 // m_bOwnsDC was already set to false in the base class ctor, so the DC
160 // will be released (and not deleted) in ~wxDC
165 void wxClientDCImpl::InitDC()
167 wxWindowDCImpl::InitDC();
169 // in wxUniv build we must manually do some DC adjustments usually
170 // performed by Windows for us
172 // we also need to take the menu/toolbar manually into account under
173 // Windows CE because they're just another control there, not anything
174 // special as usually under Windows
175 #if defined(__WXUNIVERSAL__) || defined(__WXWINCE__)
176 wxPoint ptOrigin
= m_window
->GetClientAreaOrigin();
177 if ( ptOrigin
.x
|| ptOrigin
.y
)
179 // no need to shift DC origin if shift is null
180 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
183 // clip the DC to avoid overwriting the non client area
184 wxSize size
= m_window
->GetClientSize();
185 DoSetClippingRegion(0, 0, size
.x
, size
.y
);
186 #endif // __WXUNIVERSAL__ || __WXWINCE__
189 wxClientDCImpl::~wxClientDCImpl()
193 void wxClientDCImpl::DoGetSize(int *width
, int *height
) const
195 wxCHECK_RET( m_window
, _T("wxClientDCImpl without a window?") );
197 m_window
->GetClientSize(width
, height
);
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
205 // created and tried to reuse it - this was supposed to take care of a
206 // situation when a derived class OnPaint() calls base class OnPaint()
207 // because in this case ::BeginPaint() shouldn't be called second time.
209 // I'm not sure how useful this is, however we must remember the HWND
210 // associated with the last HDC as well - otherwise we may (and will!) try
211 // to reuse the HDC for another HWND which is a nice recipe for disaster.
213 // So we store a list of windows for which we already have the DC and not
214 // just one single hDC. This seems to work, but I'm really not sure about
215 // the usefullness of the whole idea - IMHO it's much better to not call
216 // base class OnPaint() at all, or, if we really want to allow it, add a
217 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
218 // !NULL instead of creating a new DC.
220 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxClientDCImpl
)
222 wxArrayDCInfo
wxPaintDCImpl::ms_cache
;
224 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
) :
225 wxClientDCImpl( owner
)
229 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*window
) :
230 wxClientDCImpl( owner
)
232 wxCHECK_RET( window
, wxT("NULL canvas in wxPaintDCImpl ctor") );
235 if ( g_isPainting
<= 0 )
237 wxFAIL_MSG( wxT("wxPaintDCImpl may be created only in EVT_PAINT handler!") );
241 #endif // __WXDEBUG__
245 // do we have a DC for this window in the cache?
246 wxPaintDCInfo
*info
= FindInCache();
252 else // not in cache, create a new one
254 m_hDC
= (WXHDC
)::BeginPaint(GetHwndOf(m_window
), &g_paintStruct
);
256 ms_cache
.Add(new wxPaintDCInfo(m_window
, this));
259 // Note: at this point m_hDC can be NULL under MicroWindows, when dragging.
263 // (re)set the DC parameters.
266 // the HDC can have a clipping box (which we didn't set), make sure our
267 // DoGetClippingBox() checks for it
271 wxPaintDCImpl::~wxPaintDCImpl()
275 SelectOldObjects(m_hDC
);
278 wxPaintDCInfo
*info
= FindInCache(&index
);
280 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
282 if ( --info
->count
== 0 )
284 ::EndPaint(GetHwndOf(m_window
), &g_paintStruct
);
286 ms_cache
.RemoveAt(index
);
288 // Reduce the number of bogus reports of non-freed memory
290 if (ms_cache
.IsEmpty())
293 //else: cached DC entry is still in use
295 // prevent the base class dtor from ReleaseDC()ing it again
300 wxPaintDCInfo
*wxPaintDCImpl::FindInCache(size_t *index
) const
302 wxPaintDCInfo
*info
= NULL
;
303 size_t nCache
= ms_cache
.GetCount();
304 for ( size_t n
= 0; n
< nCache
; n
++ )
306 wxPaintDCInfo
*info1
= &ms_cache
[n
];
307 if ( info1
->hwnd
== m_window
->GetHWND() )
319 // find the entry for this DC in the cache (keyed by the window)
320 WXHDC
wxPaintDCImpl::FindDCInCache(wxWindow
* win
)
322 size_t nCache
= ms_cache
.GetCount();
323 for ( size_t n
= 0; n
< nCache
; n
++ )
325 wxPaintDCInfo
*info
= &ms_cache
[n
];
326 if ( info
->hwnd
== win
->GetHWND() )
338 // TODO: don't duplicate wxPaintDCImpl code here!!
340 class wxPaintDCExImpl
: public wxPaintDCImpl
343 wxPaintDCExImpl( wxDC
*owner
, wxWindow
*window
, WXHDC dc
);
350 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCEx
,wxPaintDC
)
352 wxPaintDCEx::wxPaintDCEx(wxWindow
*window
, WXHDC dc
)
353 : wxPaintDC(new wxPaintDCExImpl(this, window
, dc
))
357 wxPaintDCExImpl::wxPaintDCExImpl(wxDC
*owner
, wxWindow
*window
, WXHDC dc
)
358 : wxPaintDCImpl( owner
)
360 wxCHECK_RET( dc
, wxT("wxPaintDCEx requires an existing device context") );
366 wxPaintDCInfo
*info
= FindInCache();
372 else // not in cache, create a new one
375 ms_cache
.Add(new wxPaintDCInfo(m_window
, this));
376 m_saveState
= SaveDC((HDC
) dc
);
380 wxPaintDCExImpl::~wxPaintDCExImpl()
383 wxPaintDCInfo
*info
= FindInCache(&index
);
385 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
387 if ( --info
->count
== 0 )
389 RestoreDC((HDC
) m_hDC
, m_saveState
);
390 ms_cache
.RemoveAt(index
);
392 // Reduce the number of bogus reports of non-freed memory
394 if (ms_cache
.IsEmpty())
397 //else: cached DC entry is still in use
399 // prevent the base class dtor from ReleaseDC()ing it again