]> git.saurik.com Git - wxWidgets.git/commitdiff
fix for releasing the HDC in WM_DRAWITEM handler
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 21 May 2001 23:10:31 +0000 (23:10 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 21 May 2001 23:10:31 +0000 (23:10 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@10261 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/msw/dc.h
src/msw/listbox.cpp
src/msw/window.cpp

index 257f86cd50fe62f818670e236113aa082b2ae478..4d53cb72053ad64c1829fb66c140a436e25c4caa 100644 (file)
@@ -73,8 +73,6 @@
 
 class WXDLLEXPORT wxDC : public wxDCBase
 {
-    DECLARE_DYNAMIC_CLASS(wxDC)
-
 public:
     wxDC();
     ~wxDC();
@@ -224,6 +222,20 @@ protected:
     WXHBRUSH          m_oldBrush;
     WXHFONT           m_oldFont;
     WXHPALETTE        m_oldPalette;
+
+    DECLARE_DYNAMIC_CLASS(wxDC)
+};
+
+// ----------------------------------------------------------------------------
+// wxDCTemp: a wxDC which doesn't free the given HDC (used by wxWindows
+// only/mainly)
+// ----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxDCTemp : public wxDC
+{
+public:
+    wxDCTemp(WXHDC hdc) { SetHDC(hdc); }
+    virtual ~wxDCTemp() { SetHDC((WXHDC)NULL); }
 };
 
 #endif
index 462c26bb822ddd8c8297fb7c77b54811007ffa89..147e73c917c49c68fff0260baa60ca31ef0cc24b 100644 (file)
@@ -773,14 +773,13 @@ bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
 
     wxListBoxItem *pItem = (wxListBoxItem *)data;
 
-    wxDC dc;
-    dc.SetHDC((WXHDC)pStruct->hDC, FALSE);
+    wxDCTemp dc((WXHDC)pStruct->hDC);
     wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
                 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
 
     return pItem->OnDrawItem(dc, rect,
-            (wxOwnerDrawn::wxODAction)pStruct->itemAction,
-            (wxOwnerDrawn::wxODStatus)pStruct->itemState);
+                             (wxOwnerDrawn::wxODAction)pStruct->itemAction,
+                             (wxOwnerDrawn::wxODStatus)pStruct->itemState);
 }
 
 #endif
index 11a6da140e97fc263a63dc80d6458afad498e0e5..184d7da24b96da2eece9c6c087d35d04126f95c0 100644 (file)
@@ -2923,19 +2923,20 @@ bool wxWindow::MSWOnDrawItem(int id, WXDRAWITEMSTRUCT *itemStruct)
 
         wxCHECK( pMenuItem->IsKindOf(CLASSINFO(wxMenuItem)), FALSE );
 
-        // prepare to call OnDrawItem()
-        wxDC dc;
-        dc.SetHDC((WXHDC)pDrawStruct->hDC, FALSE);
+        // prepare to call OnDrawItem(): notice using of wxDCTemp to prevent
+        // the DC from being released
+        wxDCTemp dc((WXHDC)pDrawStruct->hDC);
         wxRect rect(pDrawStruct->rcItem.left, pDrawStruct->rcItem.top,
                     pDrawStruct->rcItem.right - pDrawStruct->rcItem.left,
                     pDrawStruct->rcItem.bottom - pDrawStruct->rcItem.top);
 
         return pMenuItem->OnDrawItem
-                          (
-                            dc, rect,
-                            (wxOwnerDrawn::wxODAction)pDrawStruct->itemAction,
-                            (wxOwnerDrawn::wxODStatus)pDrawStruct->itemState
-                          );
+               (
+                dc,
+                rect,
+                (wxOwnerDrawn::wxODAction)pDrawStruct->itemAction,
+                (wxOwnerDrawn::wxODStatus)pDrawStruct->itemState
+               );
     }
 
     wxWindow *item = FindItem(id);
@@ -3103,9 +3104,8 @@ bool wxWindow::HandleEraseBkgnd(WXHDC hdc)
     if ( ::IsIconic(GetHwnd()) )
         return TRUE;
 
-    wxDC dc;
+    wxDCTemp dc(hdc);
 
-    dc.SetHDC(hdc);
     dc.SetWindow(this);
     dc.BeginDrawing();
 
@@ -3114,8 +3114,9 @@ bool wxWindow::HandleEraseBkgnd(WXHDC hdc)
     bool rc = GetEventHandler()->ProcessEvent(event);
 
     dc.EndDrawing();
+
+    // must be called manually as ~wxDC doesn't do anything for wxDCTemp
     dc.SelectOldObjects(hdc);
-    dc.SetHDC((WXHDC) NULL);
 
     return rc;
 }