| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/palmos/dcclient.cpp |
| 3 | // Purpose: wxClientDC class |
| 4 | // Author: William Osborne - minimal working wxPalmOS port |
| 5 | // Modified by: |
| 6 | // Created: 10/13/04 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) William Osborne |
| 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 | #ifdef __BORLANDC__ |
| 24 | #pragma hdrstop |
| 25 | #endif |
| 26 | |
| 27 | #include "wx/dcclient.h" |
| 28 | #include "wx/palmos/dcclient.h" |
| 29 | |
| 30 | #ifndef WX_PRECOMP |
| 31 | #include "wx/string.h" |
| 32 | #include "wx/log.h" |
| 33 | #include "wx/window.h" |
| 34 | #endif |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | // array/list types |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | struct WXDLLEXPORT wxPaintDCInfo |
| 41 | { |
| 42 | wxPaintDCInfo(wxWindow *win, wxPaintDCImpl *dc) |
| 43 | { |
| 44 | hwnd = win->GetHWND(); |
| 45 | hdc = dc->GetHDC(); |
| 46 | count = 1; |
| 47 | } |
| 48 | |
| 49 | WXHWND hwnd; // window for this DC |
| 50 | WXHDC hdc; // the DC handle |
| 51 | size_t count; // usage count |
| 52 | }; |
| 53 | |
| 54 | #include "wx/arrimpl.cpp" |
| 55 | |
| 56 | WX_DEFINE_OBJARRAY(wxArrayDCInfo) |
| 57 | |
| 58 | // =========================================================================== |
| 59 | // implementation |
| 60 | // =========================================================================== |
| 61 | |
| 62 | // ---------------------------------------------------------------------------- |
| 63 | // wxWindowDCImpl |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxPalmDCImpl) |
| 67 | |
| 68 | wxWindowDCImpl::wxWindowDCImpl( wxDC *owner ) : |
| 69 | wxPalmDCImpl( owner ) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | wxWindowDCImpl::wxWindowDCImpl( wxDC *owner, wxWindow *window ) : |
| 74 | wxPalmDCImpl( owner ) |
| 75 | { |
| 76 | wxCHECK_RET( window, _T("invalid window in wxWindowDCImpl") ); |
| 77 | } |
| 78 | |
| 79 | void wxWindowDCImpl::InitDC() |
| 80 | { |
| 81 | |
| 82 | // since we are a window dc we need to grab the palette from the window |
| 83 | #if wxUSE_PALETTE |
| 84 | InitializePalette(); |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | void wxWindowDCImpl::DoGetSize(int *width, int *height) const |
| 89 | { |
| 90 | wxCHECK_RET( m_window, _T("wxWindowDCImpl without a window?") ); |
| 91 | |
| 92 | m_window->GetSize(width, height); |
| 93 | } |
| 94 | |
| 95 | // ---------------------------------------------------------------------------- |
| 96 | // wxClientDCImpl |
| 97 | // ---------------------------------------------------------------------------- |
| 98 | |
| 99 | IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl) |
| 100 | |
| 101 | wxClientDCImpl::wxClientDCImpl( wxDC *owner ) : |
| 102 | wxWindowDCImpl( owner ) |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *window ) : |
| 107 | wxWindowDCImpl( owner ) |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | void wxClientDCImpl::InitDC() |
| 112 | { |
| 113 | wxWindowDCImpl::InitDC(); |
| 114 | |
| 115 | // in wxUniv build we must manually do some DC adjustments usually |
| 116 | // performed by Windows for us |
| 117 | // |
| 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 ) |
| 124 | { |
| 125 | // no need to shift DC origin if shift is null |
| 126 | SetDeviceOrigin(ptOrigin.x, ptOrigin.y); |
| 127 | } |
| 128 | |
| 129 | // clip the DC to avoid overwriting the non client area |
| 130 | SetClippingRegion(wxPoint(0,0), m_window->GetClientSize()); |
| 131 | #endif // __WXUNIVERSAL__ || __WXWINCE__ |
| 132 | } |
| 133 | |
| 134 | wxClientDCImpl::~wxClientDCImpl() |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | void wxClientDCImpl::DoGetSize(int *width, int *height) const |
| 139 | { |
| 140 | wxCHECK_RET( m_window, _T("wxClientDCImpl without a window?") ); |
| 141 | |
| 142 | m_window->GetClientSize(width, height); |
| 143 | } |
| 144 | |
| 145 | // ---------------------------------------------------------------------------- |
| 146 | // wxPaintDCImpl |
| 147 | // ---------------------------------------------------------------------------- |
| 148 | |
| 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. |
| 153 | // |
| 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. |
| 157 | // |
| 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. |
| 164 | |
| 165 | IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxClientDCImpl) |
| 166 | |
| 167 | wxArrayDCInfo wxPaintDCImpl::ms_cache; |
| 168 | |
| 169 | wxPaintDCImpl::wxPaintDCImpl( wxDC *owner ) : |
| 170 | wxClientDCImpl( owner ) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *window ) : |
| 175 | wxClientDCImpl( owner ) |
| 176 | { |
| 177 | wxCHECK_RET( window, wxT("NULL canvas in wxPaintDCImpl ctor") ); |
| 178 | |
| 179 | m_window = window; |
| 180 | |
| 181 | // do we have a DC for this window in the cache? |
| 182 | wxPaintDCInfo *info = FindInCache(); |
| 183 | if ( info ) |
| 184 | { |
| 185 | m_hDC = info->hdc; |
| 186 | info->count++; |
| 187 | } |
| 188 | else // not in cache, create a new one |
| 189 | { |
| 190 | //m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_window), &g_paintStruct); |
| 191 | if (m_hDC) |
| 192 | ms_cache.Add(new wxPaintDCInfo(m_window, this)); |
| 193 | } |
| 194 | |
| 195 | // Note: at this point m_hDC can be NULL under MicroWindows, when dragging. |
| 196 | if (!GetHDC()) |
| 197 | return; |
| 198 | |
| 199 | // (re)set the DC parameters. |
| 200 | InitDC(); |
| 201 | |
| 202 | // the HDC can have a clipping box (which we didn't set), make sure our |
| 203 | // DoGetClippingBox() checks for it |
| 204 | m_clipping = true; |
| 205 | } |
| 206 | |
| 207 | wxPaintDCImpl::~wxPaintDCImpl() |
| 208 | { |
| 209 | if ( m_hDC ) |
| 210 | { |
| 211 | SelectOldObjects(m_hDC); |
| 212 | |
| 213 | size_t index; |
| 214 | wxPaintDCInfo *info = FindInCache(&index); |
| 215 | |
| 216 | wxCHECK_RET( info, wxT("existing DC should have a cache entry") ); |
| 217 | |
| 218 | if ( --info->count == 0 ) |
| 219 | { |
| 220 | //::EndPaint(GetHwndOf(m_window), &g_paintStruct); |
| 221 | |
| 222 | ms_cache.RemoveAt(index); |
| 223 | |
| 224 | // Reduce the number of bogus reports of non-freed memory |
| 225 | // at app exit |
| 226 | if (ms_cache.IsEmpty()) |
| 227 | ms_cache.Clear(); |
| 228 | } |
| 229 | //else: cached DC entry is still in use |
| 230 | |
| 231 | // prevent the base class dtor from ReleaseDC()ing it again |
| 232 | m_hDC = 0; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | wxPaintDCInfo *wxPaintDCImpl::FindInCache(size_t *index) const |
| 237 | { |
| 238 | wxPaintDCInfo *info = NULL; |
| 239 | size_t nCache = ms_cache.GetCount(); |
| 240 | for ( size_t n = 0; n < nCache; n++ ) |
| 241 | { |
| 242 | wxPaintDCInfo *info1 = &ms_cache[n]; |
| 243 | if ( info1->hwnd == m_window->GetHWND() ) |
| 244 | { |
| 245 | info = info1; |
| 246 | if ( index ) |
| 247 | *index = n; |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return info; |
| 253 | } |
| 254 | |
| 255 | // find the entry for this DC in the cache (keyed by the window) |
| 256 | WXHDC wxPaintDCImpl::FindDCInCache(wxWindow* win) |
| 257 | { |
| 258 | size_t nCache = ms_cache.GetCount(); |
| 259 | for ( size_t n = 0; n < nCache; n++ ) |
| 260 | { |
| 261 | wxPaintDCInfo *info = &ms_cache[n]; |
| 262 | if ( info->hwnd == win->GetHWND() ) |
| 263 | { |
| 264 | return info->hdc; |
| 265 | } |
| 266 | } |
| 267 | return 0; |
| 268 | } |