Refactor wxGCDC to delegate acquiring/releasing HDC to wxGraphicsContext

Instead of implementing MSW-specific code to handle HDC for GDI+ context
directly in wxGCDC delegate acquiring/releasing HDC to underlying
wxGraphicsContext. Decoupling GDI+-specific code from wxGCDC will allow
us to implement handling HDC in other graphics renderers in a clean way.
This commit is contained in:
Artur Wieczorek
2021-06-27 23:32:57 +02:00
parent 996a981170
commit 0939130158
6 changed files with 63 additions and 36 deletions

View File

@@ -38,7 +38,7 @@ public:
#ifdef __WXMSW__ #ifdef __WXMSW__
// override wxDC virtual functions to provide access to HDC associated with // override wxDC virtual functions to provide access to HDC associated with
// this Graphics object (implemented in src/msw/graphics.cpp) // underlying wxGraphicsContext
virtual WXHDC AcquireHDC() wxOVERRIDE; virtual WXHDC AcquireHDC() wxOVERRIDE;
virtual void ReleaseHDC(WXHDC hdc) wxOVERRIDE; virtual void ReleaseHDC(WXHDC hdc) wxOVERRIDE;
#endif // __WXMSW__ #endif // __WXMSW__

View File

@@ -881,6 +881,11 @@ public:
void SetContentScaleFactor(double contentScaleFactor); void SetContentScaleFactor(double contentScaleFactor);
double GetContentScaleFactor() const { return m_contentScaleFactor; } double GetContentScaleFactor() const { return m_contentScaleFactor; }
#ifdef __WXMSW__
virtual WXHDC GetNativeHDC() = 0;
virtual void ReleaseNativeHDC(WXHDC hdc) = 0;
#endif
protected: protected:
// These fields must be initialized in the derived class ctors. // These fields must be initialized in the derived class ctors.
wxDouble m_width, wxDouble m_width,

View File

@@ -110,6 +110,25 @@ wxGCDC::~wxGCDC()
{ {
} }
#ifdef __WXMSW__
WXHDC wxGCDC::AcquireHDC()
{
wxGraphicsContext* const gc = GetGraphicsContext();
wxCHECK_MSG(gc, NULL, "can't acquire HDC because there is no wxGraphicsContext");
return gc->GetNativeHDC();
}
void wxGCDC::ReleaseHDC(WXHDC hdc)
{
if ( !hdc )
return;
wxGraphicsContext* const gc = GetGraphicsContext();
wxCHECK_RET(gc, "can't release HDC because there is no wxGraphicsContext");
gc->ReleaseNativeHDC(hdc);
}
#endif // __WXMSW__
wxIMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl, wxDCImpl); wxIMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl, wxDCImpl);
wxGCDCImpl::wxGCDCImpl(wxDC *owner, wxGraphicsContext* context) : wxGCDCImpl::wxGCDCImpl(wxDC *owner, wxGraphicsContext* context) :

View File

@@ -512,6 +512,18 @@ public:
wxDouble *descent, wxDouble *externalLeading ) const wxOVERRIDE; wxDouble *descent, wxDouble *externalLeading ) const wxOVERRIDE;
virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const wxOVERRIDE; virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const wxOVERRIDE;
#ifdef __WXMSW__
virtual WXHDC GetNativeHDC() wxOVERRIDE
{
wxFAIL_MSG("Can't get HDC from Cairo context");
return NULL;
};
virtual void ReleaseNativeHDC(WXHDC WXUNUSED(hdc)) wxOVERRIDE
{
wxFAIL_MSG("Can't release HDC for Cairo context");
};
#endif
protected: protected:
virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y ) wxOVERRIDE; virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y ) wxOVERRIDE;

View File

@@ -483,6 +483,9 @@ public:
Graphics* GetGraphics() const { return m_context; } Graphics* GetGraphics() const { return m_context; }
virtual WXHDC GetNativeHDC() wxOVERRIDE;
virtual void ReleaseNativeHDC(WXHDC hdc) wxOVERRIDE;
protected: protected:
// Used from ctors (including those in the derived classes) and takes // Used from ctors (including those in the derived classes) and takes
// ownership of the graphics pointer that must be non-NULL. // ownership of the graphics pointer that must be non-NULL.
@@ -2506,6 +2509,17 @@ void wxGDIPlusContext::GetDPI(wxDouble* dpiX, wxDouble* dpiY) const
} }
} }
WXHDC wxGDIPlusContext::GetNativeHDC()
{
return m_context->GetHDC();
}
void wxGDIPlusContext::ReleaseNativeHDC(WXHDC hdc)
{
if ( hdc )
m_context->ReleaseHDC((HDC)hdc);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxGDIPlusPrintingContext implementation // wxGDIPlusPrintingContext implementation
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -3003,39 +3017,4 @@ private:
wxIMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule); wxIMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule);
// ----------------------------------------------------------------------------
// wxMSW-specific parts of wxGCDC
// ----------------------------------------------------------------------------
WXHDC wxGCDC::AcquireHDC()
{
wxGraphicsContext * const gc = GetGraphicsContext();
if ( !gc )
return NULL;
// we can't get the HDC if it is not a GDI+ context
wxCHECK_MSG(gc->GetRenderer() == wxGraphicsRenderer::GetGDIPlusRenderer(), NULL,
"can't get HDC because this is not GDI+ context");
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" );
wxCHECK_RET(gc->GetRenderer() == wxGraphicsRenderer::GetGDIPlusRenderer(),
"can't release HDC because this is not GDI+ context");
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_GDIPLUS #endif // wxUSE_GRAPHICS_GDIPLUS

View File

@@ -3836,6 +3836,8 @@ public:
void PushState() wxOVERRIDE {} void PushState() wxOVERRIDE {}
void PopState() wxOVERRIDE {} void PopState() wxOVERRIDE {}
void Flush() wxOVERRIDE {} void Flush() wxOVERRIDE {}
WXHDC GetNativeHDC() wxOVERRIDE { return NULL; };
void ReleaseNativeHDC(WXHDC WXUNUSED(hdc)) wxOVERRIDE {};
protected: protected:
void DoDrawText(const wxString&, wxDouble, wxDouble) wxOVERRIDE {} void DoDrawText(const wxString&, wxDouble, wxDouble) wxOVERRIDE {}
@@ -3992,6 +3994,16 @@ public:
return GetRenderTarget(); return GetRenderTarget();
} }
WXHDC GetNativeHDC() wxOVERRIDE
{
wxFAIL_MSG("Can't get HDC from Direct2D context");
return NULL;
};
void ReleaseNativeHDC(WXHDC WXUNUSED(hdc)) wxOVERRIDE
{
wxFAIL_MSG("Can't release HDC for Direct2D context");
};
private: private:
void Init(); void Init();