1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/palmos/dcclient.cpp
3 // Purpose: wxClientDC class
4 // Author: William Osborne - minimal working wxPalmOS port
8 // Copyright: (c) William Osborne
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/palmos/dcclient.h"
31 #include "wx/string.h"
33 #include "wx/window.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 struct WXDLLEXPORT wxPaintDCInfo
42 wxPaintDCInfo(wxWindow
*win
, wxPaintDCImpl
*dc
)
44 hwnd
= win
->GetHWND();
49 WXHWND hwnd
; // window for this DC
50 WXHDC hdc
; // the DC handle
51 size_t count
; // usage count
54 #include "wx/arrimpl.cpp"
56 WX_DEFINE_OBJARRAY(wxArrayDCInfo
)
58 // ===========================================================================
60 // ===========================================================================
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl
, wxPalmDCImpl
)
68 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
) :
73 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
) :
76 wxCHECK_RET( window
, _T("invalid window in wxWindowDCImpl") );
79 void wxWindowDCImpl::InitDC()
82 // since we are a window dc we need to grab the palette from the window
88 void wxWindowDCImpl::DoGetSize(int *width
, int *height
) const
90 wxCHECK_RET( m_window
, _T("wxWindowDCImpl without a window?") );
92 m_window
->GetSize(width
, height
);
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
101 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
) :
102 wxWindowDCImpl( owner
)
106 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*window
) :
107 wxWindowDCImpl( owner
)
111 void wxClientDCImpl::InitDC()
113 wxWindowDCImpl::InitDC();
115 // in wxUniv build we must manually do some DC adjustments usually
116 // performed by Windows for us
118 // we also need to take the menu/toolbar manually into account under
119 // Windows CE because they're just another control there, not anything
120 // special as usually under Windows
121 #if defined(__WXUNIVERSAL__) || defined(__WXWINCE__)
122 wxPoint ptOrigin
= m_window
->GetClientAreaOrigin();
123 if ( ptOrigin
.x
|| ptOrigin
.y
)
125 // no need to shift DC origin if shift is null
126 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
129 // clip the DC to avoid overwriting the non client area
130 SetClippingRegion(wxPoint(0,0), m_window
->GetClientSize());
131 #endif // __WXUNIVERSAL__ || __WXWINCE__
134 wxClientDCImpl::~wxClientDCImpl()
138 void wxClientDCImpl::DoGetSize(int *width
, int *height
) const
140 wxCHECK_RET( m_window
, _T("wxClientDCImpl without a window?") );
142 m_window
->GetClientSize(width
, height
);
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 // VZ: initial implementation (by JACS) only remembered the last wxPaintDC
150 // created and tried to reuse it - this was supposed to take care of a
151 // situation when a derived class OnPaint() calls base class OnPaint()
152 // because in this case ::BeginPaint() shouldn't be called second time.
154 // I'm not sure how useful this is, however we must remember the HWND
155 // associated with the last HDC as well - otherwise we may (and will!) try
156 // to reuse the HDC for another HWND which is a nice recipe for disaster.
158 // So we store a list of windows for which we already have the DC and not
159 // just one single hDC. This seems to work, but I'm really not sure about
160 // the usefullness of the whole idea - IMHO it's much better to not call
161 // base class OnPaint() at all, or, if we really want to allow it, add a
162 // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
163 // !NULL instead of creating a new DC.
165 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxClientDCImpl
)
167 wxArrayDCInfo
wxPaintDCImpl::ms_cache
;
169 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
) :
170 wxClientDCImpl( owner
)
174 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*window
) :
175 wxClientDCImpl( owner
)
177 wxCHECK_RET( window
, wxT("NULL canvas in wxPaintDCImpl ctor") );
181 // do we have a DC for this window in the cache?
182 wxPaintDCInfo
*info
= FindInCache();
188 else // not in cache, create a new one
190 //m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_window), &g_paintStruct);
192 ms_cache
.Add(new wxPaintDCInfo(m_window
, this));
195 // Note: at this point m_hDC can be NULL under MicroWindows, when dragging.
199 // (re)set the DC parameters.
202 // the HDC can have a clipping box (which we didn't set), make sure our
203 // DoGetClippingBox() checks for it
207 wxPaintDCImpl::~wxPaintDCImpl()
211 SelectOldObjects(m_hDC
);
214 wxPaintDCInfo
*info
= FindInCache(&index
);
216 wxCHECK_RET( info
, wxT("existing DC should have a cache entry") );
218 if ( --info
->count
== 0 )
220 //::EndPaint(GetHwndOf(m_window), &g_paintStruct);
222 ms_cache
.RemoveAt(index
);
224 // Reduce the number of bogus reports of non-freed memory
226 if (ms_cache
.IsEmpty())
229 //else: cached DC entry is still in use
231 // prevent the base class dtor from ReleaseDC()ing it again
236 wxPaintDCInfo
*wxPaintDCImpl::FindInCache(size_t *index
) const
238 wxPaintDCInfo
*info
= NULL
;
239 size_t nCache
= ms_cache
.GetCount();
240 for ( size_t n
= 0; n
< nCache
; n
++ )
242 wxPaintDCInfo
*info1
= &ms_cache
[n
];
243 if ( info1
->hwnd
== m_window
->GetHWND() )
255 // find the entry for this DC in the cache (keyed by the window)
256 WXHDC
wxPaintDCImpl::FindDCInCache(wxWindow
* win
)
258 size_t nCache
= ms_cache
.GetCount();
259 for ( size_t n
= 0; n
< nCache
; n
++ )
261 wxPaintDCInfo
*info
= &ms_cache
[n
];
262 if ( info
->hwnd
== win
->GetHWND() )