From 7833c65c2a1cbda8b576a0d006fbc508988b284a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Mon, 21 Nov 2016 13:38:34 +0100 Subject: [PATCH] Add wxGraphicsContext::CreateFromUnknownDC() A convenience helper for writing generic code that may operate on different kinds of DCs, all supported by wxGraphicsContext, but without knowing its specific type. --- include/wx/graphics.h | 6 ++++++ interface/wx/graphics.h | 15 +++++++++++++++ src/common/graphcmn.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 66a45763d8..bb639a77f8 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -70,6 +70,7 @@ enum wxCompositionMode wxCOMPOSITION_ADD /* R = S + D */ }; +class WXDLLIMPEXP_FWD_CORE wxDC; class WXDLLIMPEXP_FWD_CORE wxWindowDC; class WXDLLIMPEXP_FWD_CORE wxMemoryDC; #if wxUSE_PRINTING_ARCHITECTURE @@ -437,6 +438,11 @@ public: #endif #endif +#ifndef wxNO_RTTI + // Create a context from a DC of unknown type, if supported, returns NULL otherwise + static wxGraphicsContext* CreateFromUnknownDC(const wxDC& dc); +#endif + static wxGraphicsContext* CreateFromNative( void * context ); static wxGraphicsContext* CreateFromNativeWindow( void * window ); diff --git a/interface/wx/graphics.h b/interface/wx/graphics.h index 5a78b3434f..62e4c47fb4 100644 --- a/interface/wx/graphics.h +++ b/interface/wx/graphics.h @@ -438,6 +438,21 @@ public: */ static wxGraphicsContext* Create(const wxEnhMetaFileDC& metaFileDC); + /** + Creates a wxGraphicsContext from a DC of unknown specific type. + + Creates a wxGraphicsContext if @a dc is a supported type (i.e. has a + corresponding Create() method, e.g. wxWindowDC or wxMemoryDC). + Returns @NULL if the DC is unsupported. + + This method is only useful as a helper in generic code that operates + with wxDC and doesn't known its exact type. Use Create() instead if + you know that the DC is e.g. wxWindowDC. + + @since 3.1.1 + */ + static wxGraphicsContext* CreateFromUnknownDC(wxDC& dc); + /** Creates a wxGraphicsContext associated with a wxImage. diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index f4226eadb9..d7062dadd4 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -22,12 +22,18 @@ #ifndef WX_PRECOMP #include "wx/icon.h" #include "wx/bitmap.h" + #include "wx/dcclient.h" #include "wx/dcmemory.h" + #include "wx/dcprint.h" #include "wx/math.h" #include "wx/region.h" #include "wx/log.h" #endif +#ifdef __WXMSW__ + #include "wx/msw/enhmeta.h" +#endif + #include "wx/private/graphics.h" //----------------------------------------------------------------------------- @@ -920,6 +926,31 @@ wxGraphicsBitmap wxGraphicsContext::CreateSubBitmap( const wxGraphicsBitmap &bmp #endif #endif +#ifndef wxNO_RTTI +wxGraphicsContext* wxGraphicsContext::CreateFromUnknownDC(const wxDC& dc) +{ + if ( const wxWindowDC *windc = dynamic_cast(&dc) ) + return Create(*windc); + + if ( const wxMemoryDC *memdc = dynamic_cast(&dc) ) + return Create(*memdc); + +#if wxUSE_PRINTING_ARCHITECTURE + if ( const wxPrinterDC *printdc = dynamic_cast(&dc) ) + return Create(*printdc); +#endif + +#ifdef __WXMSW__ +#if wxUSE_ENH_METAFILE + if ( const wxEnhMetaFileDC *mfdc = dynamic_cast(&dc) ) + return Create(*mfdc); +#endif +#endif + + return NULL; +} +#endif + wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context ) { return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeContext(context);