]> git.saurik.com Git - wxWidgets.git/commitdiff
Replaced GraphicsHDC from src/msw/renderer.cpp with wxDC::GetTempHDC().
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 5 Oct 2009 22:56:58 +0000 (22:56 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 5 Oct 2009 22:56:58 +0000 (22:56 +0000)
wxDC::GetTempHDC() method provides a convenient and safe way to retrieve HDC
from a wxDC object, whether it is using GDI or GDI+. It is implemented using
(MSW-specific) virtual functions in wxDC and so doesn't need ugly hacks like
wxDynamicCast which were used in src/msw/renderer.cpp to achieve the same
effect.

Also, we now use GetTempHDC() consistently in all wxMSW rendering methods as
the old GraphicsHDC was only used in some of them meaning that many methods
didn't work at all with wxGCDC.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62294 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/dc.h
include/wx/dcgraph.h
src/msw/graphics.cpp
src/msw/renderer.cpp

index aa5110066df80a6335d2fd4432b9f88e6e9f838e..4981bda64adbe6e15d105183db90e4650a78abe4 100644 (file)
@@ -1199,7 +1199,50 @@ public:
 #endif  // WXWIN_COMPATIBILITY_2_8
 
 #ifdef __WXMSW__
+    // GetHDC() is the simplest way to retrieve an HDC From a wxDC but only
+    // works if this wxDC is GDI-based and fails for GDI+ contexts (and
+    // anything else without HDC, e.g. wxPostScriptDC)
     WXHDC GetHDC() const;
+
+    // don't use these methods manually, use GetTempHDC() instead
+    virtual WXHDC AcquireHDC() { return GetHDC(); }
+    virtual void ReleaseHDC(WXHDC WXUNUSED(hdc)) { }
+
+    // helper class holding the result of GetTempHDC() with std::auto_ptr<>-like
+    // semantics, i.e. it is moved when copied
+    class TempHDC
+    {
+    public:
+        TempHDC(wxDC& dc)
+            : m_dc(dc),
+              m_hdc(dc.AcquireHDC())
+        {
+        }
+
+        TempHDC(const TempHDC& thdc)
+            : m_dc(thdc.m_dc),
+              m_hdc(thdc.m_hdc)
+        {
+            const_cast<TempHDC&>(thdc).m_hdc = 0;
+        }
+
+        ~TempHDC()
+        {
+            if ( m_hdc )
+                m_dc.ReleaseHDC(m_hdc);
+        }
+
+        WXHDC GetHDC() const { return m_hdc; }
+
+    private:
+        wxDC& m_dc;
+        WXHDC m_hdc;
+
+        wxDECLARE_NO_ASSIGN_CLASS(TempHDC);
+    };
+
+    // GetTempHDC() also works for wxGCDC (but still not for wxPostScriptDC &c)
+    TempHDC GetTempHDC() { return TempHDC(*this); }
 #endif // __WXMSW__
 
 protected:
index de977b7577eed796dd3cd4a59f813044fe5f0dad..a5c7e6ff975dc434d636c2f11407f73ef52eacb7 100644 (file)
@@ -35,6 +35,14 @@ public:
     wxGraphicsContext* GetGraphicsContext();
     void SetGraphicsContext( wxGraphicsContext* ctx );
 
+#ifdef __WXMSW__
+    // override wxDC virtual functions to provide access to HDC associated with
+    // this Graphics object (implemented in src/msw/graphics.cpp)
+    virtual WXHDC AcquireHDC();
+    virtual void ReleaseHDC(WXHDC hdc);
+#endif // __WXMSW__
+
+private:
     DECLARE_DYNAMIC_CLASS(wxGCDC)
     wxDECLARE_NO_COPY_CLASS(wxGCDC);
 };
index 6562a8a55ef3e45fc6d0eee59855311edb879fb4..71ef3d4a34be44cfbbb5e3cab7f350c5ec7695d6 100644 (file)
@@ -40,6 +40,7 @@
 #include "wx/private/graphics.h"
 #include "wx/msw/wrapgdip.h"
 #include "wx/msw/dc.h"
+#include "wx/dcgraph.h"
 
 #include "wx/stack.h"
 
@@ -1850,4 +1851,32 @@ private:
 
 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule)
 
+// ----------------------------------------------------------------------------
+// wxMSW-specific parts of wxGCDC
+// ----------------------------------------------------------------------------
+
+WXHDC wxGCDC::AcquireHDC()
+{
+    wxGraphicsContext * const gc = GetGraphicsContext();
+    if ( !gc )
+        return NULL;
+
+    Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
+    return g ? g->GetHDC() : NULL;
+}
+
+void wxGCDC::ReleaseHDC(WXHDC hdc)
+{
+    if ( !hdc )
+        return;
+
+    wxGraphicsContext * const gc = GetGraphicsContext();
+    wxCHECK_RET( gc, "can't release HDC because there is no wxGraphicsContext" );
+
+    Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
+    wxCHECK_RET( g, "can't release HDC because there is no Graphics" );
+
+    g->ReleaseHDC((HDC)hdc);
+}
+
 #endif  // wxUSE_GRAPHICS_CONTEXT
index dab379511552636140d8505eb9cc96271ff0494a..6ae03002758c16c94b65654c4f9744fc54dcd547 100644 (file)
 #include "wx/splitter.h"
 #include "wx/renderer.h"
 #include "wx/msw/private.h"
-#include "wx/msw/dc.h"
 #include "wx/msw/uxtheme.h"
 
-#if wxUSE_GRAPHICS_CONTEXT
-// TODO remove this dependency (gdiplus needs the macros)
-#ifndef max
-#define max(a,b)            (((a) > (b)) ? (a) : (b))
-#endif
-
-#ifndef min
-#define min(a,b)            (((a) < (b)) ? (a) : (b))
-#endif
-
-#include "wx/dcgraph.h"
-#include "gdiplus.h"
-using namespace Gdiplus;
-#endif // wxUSE_GRAPHICS_CONTEXT
-
 // tmschema.h is in Win32 Platform SDK and might not be available with earlier
 // compilers
 #ifndef CP_DROPDOWNBUTTON
@@ -110,45 +94,6 @@ using namespace Gdiplus;
     #define TMT_EDGEFILLCOLOR   3808
 #endif
 
-
-// ----------------------------------------------------------------------------
-// If the DC is a wxGCDC then pull out the HDC from the GraphicsContext when
-// it is needed, and handle the Release when done.
-
-class GraphicsHDC
-{
-public:
-    GraphicsHDC(wxDC* dc)
-    {
-#if wxUSE_GRAPHICS_CONTEXT
-        m_graphics = NULL;
-        wxGCDC* gcdc = wxDynamicCast(dc, wxGCDC);
-        if (gcdc) {
-            m_graphics = (Graphics*)gcdc->GetGraphicsContext()->GetNativeContext();
-            m_hdc = m_graphics->GetHDC();
-        }
-        else
-#endif
-            m_hdc = GetHdcOf(*((wxMSWDCImpl*)dc->GetImpl()));
-    }
-
-    ~GraphicsHDC()
-    {
-#if wxUSE_GRAPHICS_CONTEXT
-        if (m_graphics)
-            m_graphics->ReleaseHDC(m_hdc);
-#endif
-    }
-
-    operator HDC() const { return m_hdc; }
-
-private:
-    HDC         m_hdc;
-#if wxUSE_GRAPHICS_CONTEXT
-    Graphics*   m_graphics;
-#endif
-};
-
 #if defined(__WXWINCE__)
     #ifndef DFCS_FLAT
         #define DFCS_FLAT 0
@@ -345,7 +290,7 @@ void wxRendererMSWBase::DrawFocusRect(wxWindow * WXUNUSED(win),
     RECT rc;
     wxCopyRectToRECT(rect, rc);
 
-    ::DrawFocusRect(GraphicsHDC(&dc), &rc);
+    ::DrawFocusRect(GetHdcOf(dc.GetTempHDC()), &rc);
 }
 
 void wxRendererMSWBase::DrawItemSelectionRect(wxWindow *win,
@@ -418,7 +363,7 @@ wxRendererMSW::DrawComboBoxDropButton(wxWindow * WXUNUSED(win),
     if ( flags & wxCONTROL_PRESSED )
         style |= DFCS_PUSHED | DFCS_FLAT;
 
-    ::DrawFrameControl(GraphicsHDC(&dc), &r, DFC_SCROLL, style);
+    ::DrawFrameControl(GetHdcOf(dc.GetTempHDC()), &r, DFC_SCROLL, style);
 }
 
 void
@@ -443,7 +388,7 @@ wxRendererMSW::DoDrawButton(UINT kind,
     if ( flags & wxCONTROL_CURRENT )
         style |= DFCS_HOT;
 
-    ::DrawFrameControl(GraphicsHDC(&dc), &r, DFC_BUTTON, style);
+    ::DrawFrameControl(GetHdcOf(dc.GetTempHDC()), &r, DFC_BUTTON, style);
 }
 
 void
@@ -602,7 +547,7 @@ wxRendererXP::DrawComboBoxDropButton(wxWindow * win,
     wxUxThemeEngine::Get()->DrawThemeBackground
                             (
                                 hTheme,
-                                GetHdcOf(*((wxMSWDCImpl*)dc.GetImpl())),
+                                GetHdcOf(dc.GetTempHDC()),
                                 CP_DROPDOWNBUTTON,
                                 state,
                                 &r,
@@ -638,7 +583,7 @@ wxRendererXP::DrawHeaderButton(wxWindow *win,
     wxUxThemeEngine::Get()->DrawThemeBackground
                             (
                                 hTheme,
-                                GetHdcOf(*((wxMSWDCImpl*)dc.GetImpl())),
+                                GetHdcOf(dc.GetTempHDC()),
                                 HP_HEADERITEM,
                                 state,
                                 &r,
@@ -674,7 +619,7 @@ wxRendererXP::DrawTreeItemButton(wxWindow *win,
     wxUxThemeEngine::Get()->DrawThemeBackground
                             (
                                 hTheme,
-                                GetHdcOf(*((wxMSWDCImpl*)dc.GetImpl())),
+                                GetHdcOf(dc.GetTempHDC()),
                                 TVP_GLYPH,
                                 state,
                                 &r,
@@ -748,7 +693,7 @@ wxRendererXP::DoDrawXPButton(int kind,
     wxUxThemeEngine::Get()->DrawThemeBackground
                             (
                                 hTheme,
-                                GetHdcOf(*((wxMSWDCImpl*)dc.GetImpl())),
+                                GetHdcOf(dc.GetTempHDC()),
                                 kind,
                                 state,
                                 &r,