| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: dcclient.cpp |
| 3 | // Purpose: wxClientDC class |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 01/02/97 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // =========================================================================== |
| 13 | // declarations |
| 14 | // =========================================================================== |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 21 | #pragma implementation "dcclient.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #include "wx/string.h" |
| 32 | #include "wx/log.h" |
| 33 | #include "wx/window.h" |
| 34 | |
| 35 | #include "wx/msw/private.h" |
| 36 | |
| 37 | #include "wx/dcclient.h" |
| 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | // array/list types |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | |
| 43 | struct WXDLLEXPORT wxPaintDCInfo |
| 44 | { |
| 45 | wxPaintDCInfo(wxWindow *win, wxDC *dc) |
| 46 | { |
| 47 | hwnd = win->GetHWND(); |
| 48 | hdc = dc->GetHDC(); |
| 49 | count = 1; |
| 50 | } |
| 51 | |
| 52 | WXHWND hwnd; // window for this DC |
| 53 | WXHDC hdc; // the DC handle |
| 54 | size_t count; // usage count |
| 55 | }; |
| 56 | |
| 57 | #include "wx/arrimpl.cpp" |
| 58 | |
| 59 | WX_DEFINE_OBJARRAY(wxArrayDCInfo); |
| 60 | |
| 61 | // ---------------------------------------------------------------------------- |
| 62 | // macros |
| 63 | // ---------------------------------------------------------------------------- |
| 64 | |
| 65 | IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC) |
| 66 | IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC) |
| 67 | IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxClientDC) |
| 68 | IMPLEMENT_CLASS(wxPaintDCEx, wxPaintDC) |
| 69 | |
| 70 | // ---------------------------------------------------------------------------- |
| 71 | // global variables |
| 72 | // ---------------------------------------------------------------------------- |
| 73 | |
| 74 | static PAINTSTRUCT g_paintStruct; |
| 75 | |
| 76 | #ifdef __WXDEBUG__ |
| 77 | // a global variable which we check to verify that wxPaintDC are only |
| 78 | // created in resopnse to WM_PAINT message - doing this from elsewhere is a |
| 79 | // common programming error among wxWindows programmers and might lead to |
| 80 | // very subtle and difficult to debug refresh/repaint bugs. |
| 81 | int g_isPainting = 0; |
| 82 | #endif // __WXDEBUG__ |
| 83 | |
| 84 | // =========================================================================== |
| 85 | // implementation |
| 86 | // =========================================================================== |
| 87 | |
| 88 | // ---------------------------------------------------------------------------- |
| 89 | // wxWindowDC |
| 90 | // ---------------------------------------------------------------------------- |
| 91 | |
| 92 | wxWindowDC::wxWindowDC() |
| 93 | { |
| 94 | m_canvas = NULL; |
| 95 | } |
| 96 | |
| 97 | wxWindowDC::wxWindowDC(wxWindow *canvas) |
| 98 | { |
| 99 | wxCHECK_RET( canvas, _T("invalid window in wxWindowDC") ); |
| 100 | |
| 101 | m_canvas = canvas; |
| 102 | m_hDC = (WXHDC) ::GetWindowDC(GetHwndOf(m_canvas)); |
| 103 | |
| 104 | // m_bOwnsDC was already set to false in the base class ctor, so the DC |
| 105 | // will be released (and not deleted) in ~wxDC |
| 106 | InitDC(); |
| 107 | } |
| 108 | |
| 109 | void wxWindowDC::InitDC() |
| 110 | { |
| 111 | // the background mode is only used for text background and is set in |
| 112 | // DrawText() to OPAQUE as required, otherwise always TRANSPARENT, |
| 113 | ::SetBkMode(GetHdc(), TRANSPARENT); |
| 114 | |
| 115 | // default bg colour is pne of the window |
| 116 | SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID)); |
| 117 | |
| 118 | // since we are a window dc we need to grab the palette from the window |
| 119 | #if wxUSE_PALETTE |
| 120 | InitializePalette(); |
| 121 | #endif |
| 122 | } |
| 123 | |
| 124 | void wxWindowDC::DoGetSize(int *width, int *height) const |
| 125 | { |
| 126 | wxCHECK_RET( m_canvas, _T("wxWindowDC without a window?") ); |
| 127 | |
| 128 | m_canvas->GetSize(width, height); |
| 129 | } |
| 130 | |
| 131 | // ---------------------------------------------------------------------------- |
| 132 | // wxClientDC |
| 133 | // ---------------------------------------------------------------------------- |
| 134 | |
| 135 | wxClientDC::wxClientDC() |
| 136 | { |
| 137 | m_canvas = NULL; |
| 138 | } |
| 139 | |
| 140 | wxClientDC::wxClientDC(wxWindow *canvas) |
| 141 | { |
| 142 | wxCHECK_RET( canvas, _T("invalid window in wxClientDC") ); |
| 143 | |
| 144 | m_canvas = canvas; |
| 145 | m_hDC = (WXHDC)::GetDC(GetHwndOf(m_canvas)); |
| 146 | |
| 147 | // m_bOwnsDC was already set to false in the base class ctor, so the DC |
| 148 | // will be released (and not deleted) in ~wxDC |
| 149 | |
| 150 | InitDC(); |
| 151 | } |
| 152 | |
| 153 | void wxClientDC::InitDC() |
| 154 | { |
| 155 | wxWindowDC::InitDC(); |
| 156 | |
| 157 | // in wxUniv build we must manually do some DC adjustments usually |
| 158 | // performed by Windows for us |
| 159 | #ifdef __WXUNIVERSAL__ |
| 160 | wxPoint ptOrigin = m_canvas->GetClientAreaOrigin(); |
| 161 | if ( ptOrigin.x || ptOrigin.y ) |
| 162 | { |
| 163 | // no need to shift DC origin if shift is null |
| 164 | SetDeviceOrigin(ptOrigin.x, ptOrigin.y); |
| 165 | } |
| 166 | |
| 167 | // clip the DC to avoid overwriting the non client area |
| 168 | SetClippingRegion(wxPoint(0, 0), m_canvas->GetClientSize()); |
| 169 | #endif // __WXUNIVERSAL__ |
| 170 | } |
| 171 | |
| 172 | wxClientDC::~wxClientDC() |
| 173 | { |
| 174 | } |
| 175 | |
| 176 | void wxClientDC::DoGetSize(int *width, int *height) const |
| 177 | { |
| 178 | wxCHECK_RET( m_canvas, _T("wxClientDC without a window?") ); |
| 179 | |
| 180 | m_canvas->GetClientSize(width, height); |
| 181 | } |
| 182 | |
| 183 | // ---------------------------------------------------------------------------- |
| 184 | // wxPaintDC |
| 185 | // ---------------------------------------------------------------------------- |
| 186 | |
| 187 | // VZ: initial implementation (by JACS) only remembered the last wxPaintDC |
| 188 | // created and tried to reuse it - this was supposed to take care of a |
| 189 | // situation when a derived class OnPaint() calls base class OnPaint() |
| 190 | // because in this case ::BeginPaint() shouldn't be called second time. |
| 191 | // |
| 192 | // I'm not sure how useful this is, however we must remember the HWND |
| 193 | // associated with the last HDC as well - otherwise we may (and will!) try |
| 194 | // to reuse the HDC for another HWND which is a nice recipe for disaster. |
| 195 | // |
| 196 | // So we store a list of windows for which we already have the DC and not |
| 197 | // just one single hDC. This seems to work, but I'm really not sure about |
| 198 | // the usefullness of the whole idea - IMHO it's much better to not call |
| 199 | // base class OnPaint() at all, or, if we really want to allow it, add a |
| 200 | // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's |
| 201 | // !NULL instead of creating a new DC. |
| 202 | |
| 203 | wxArrayDCInfo wxPaintDC::ms_cache; |
| 204 | |
| 205 | wxPaintDC::wxPaintDC() |
| 206 | { |
| 207 | m_canvas = NULL; |
| 208 | } |
| 209 | |
| 210 | wxPaintDC::wxPaintDC(wxWindow *canvas) |
| 211 | { |
| 212 | wxCHECK_RET( canvas, wxT("NULL canvas in wxPaintDC ctor") ); |
| 213 | |
| 214 | #ifdef __WXDEBUG__ |
| 215 | if ( g_isPainting <= 0 ) |
| 216 | { |
| 217 | wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") ); |
| 218 | |
| 219 | return; |
| 220 | } |
| 221 | #endif // __WXDEBUG__ |
| 222 | |
| 223 | m_canvas = canvas; |
| 224 | |
| 225 | // do we have a DC for this window in the cache? |
| 226 | wxPaintDCInfo *info = FindInCache(); |
| 227 | if ( info ) |
| 228 | { |
| 229 | m_hDC = info->hdc; |
| 230 | info->count++; |
| 231 | } |
| 232 | else // not in cache, create a new one |
| 233 | { |
| 234 | m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_canvas), &g_paintStruct); |
| 235 | if (m_hDC) |
| 236 | ms_cache.Add(new wxPaintDCInfo(m_canvas, this)); |
| 237 | } |
| 238 | |
| 239 | // (re)set the DC parameters. |
| 240 | // Note: at this point m_hDC can be NULL under MicroWindows, when dragging. |
| 241 | if (GetHDC()) |
| 242 | InitDC(); |
| 243 | } |
| 244 | |
| 245 | wxPaintDC::~wxPaintDC() |
| 246 | { |
| 247 | if ( m_hDC ) |
| 248 | { |
| 249 | SelectOldObjects(m_hDC); |
| 250 | |
| 251 | size_t index; |
| 252 | wxPaintDCInfo *info = FindInCache(&index); |
| 253 | |
| 254 | wxCHECK_RET( info, wxT("existing DC should have a cache entry") ); |
| 255 | |
| 256 | if ( !--info->count ) |
| 257 | { |
| 258 | ::EndPaint(GetHwndOf(m_canvas), &g_paintStruct); |
| 259 | |
| 260 | ms_cache.RemoveAt(index); |
| 261 | |
| 262 | // Reduce the number of bogus reports of non-freed memory |
| 263 | // at app exit |
| 264 | if (ms_cache.IsEmpty()) |
| 265 | ms_cache.Clear(); |
| 266 | } |
| 267 | //else: cached DC entry is still in use |
| 268 | |
| 269 | // prevent the base class dtor from ReleaseDC()ing it again |
| 270 | m_hDC = 0; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const |
| 275 | { |
| 276 | wxPaintDCInfo *info = NULL; |
| 277 | size_t nCache = ms_cache.GetCount(); |
| 278 | for ( size_t n = 0; n < nCache; n++ ) |
| 279 | { |
| 280 | wxPaintDCInfo *info1 = &ms_cache[n]; |
| 281 | if ( info1->hwnd == m_canvas->GetHWND() ) |
| 282 | { |
| 283 | info = info1; |
| 284 | if ( index ) |
| 285 | *index = n; |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return info; |
| 291 | } |
| 292 | |
| 293 | // find the entry for this DC in the cache (keyed by the window) |
| 294 | WXHDC wxPaintDC::FindDCInCache(wxWindow* win) |
| 295 | { |
| 296 | wxPaintDCInfo *info = NULL; |
| 297 | size_t nCache = ms_cache.GetCount(); |
| 298 | for ( size_t n = 0; n < nCache; n++ ) |
| 299 | { |
| 300 | info = &ms_cache[n]; |
| 301 | if ( info->hwnd == win->GetHWND() ) |
| 302 | { |
| 303 | return info->hdc; |
| 304 | } |
| 305 | } |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * wxPaintDCEx |
| 311 | */ |
| 312 | |
| 313 | // TODO: don't duplicate wxPaintDC code here!! |
| 314 | |
| 315 | wxPaintDCEx::wxPaintDCEx(wxWindow *canvas, WXHDC dc) : saveState(0) |
| 316 | { |
| 317 | wxCHECK_RET( dc, wxT("wxPaintDCEx requires an existing device context") ); |
| 318 | |
| 319 | m_canvas = canvas; |
| 320 | |
| 321 | wxPaintDCInfo *info = FindInCache(); |
| 322 | if ( info ) |
| 323 | { |
| 324 | m_hDC = info->hdc; |
| 325 | info->count++; |
| 326 | } |
| 327 | else // not in cache, create a new one |
| 328 | { |
| 329 | m_hDC = dc; |
| 330 | ms_cache.Add(new wxPaintDCInfo(m_canvas, this)); |
| 331 | saveState = SaveDC((HDC) dc); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | wxPaintDCEx::~wxPaintDCEx() |
| 336 | { |
| 337 | size_t index; |
| 338 | wxPaintDCInfo *info = FindInCache(&index); |
| 339 | |
| 340 | wxCHECK_RET( info, wxT("existing DC should have a cache entry") ); |
| 341 | |
| 342 | if ( !--info->count ) |
| 343 | { |
| 344 | RestoreDC((HDC) m_hDC, saveState); |
| 345 | ms_cache.RemoveAt(index); |
| 346 | |
| 347 | // Reduce the number of bogus reports of non-freed memory |
| 348 | // at app exit |
| 349 | if (ms_cache.IsEmpty()) |
| 350 | ms_cache.Clear(); |
| 351 | } |
| 352 | //else: cached DC entry is still in use |
| 353 | |
| 354 | // prevent the base class dtor from ReleaseDC()ing it again |
| 355 | m_hDC = 0; |
| 356 | } |
| 357 | |