From 507e331eb15bb8c3b043d9ce327db1d5494cfe9d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 6 Dec 2018 03:40:49 +0100 Subject: [PATCH] Improve creating wxGCDC from an existing wxGraphicsContext Avoid creating a default wxGraphicsContext unnecessarily, only to immediately delete it and replace it with the provided one. This was at best unnecessary and at worst resulted in a crash if the default context couldn't be created, as happened on a headless system (where wxImage-based wxGraphicsContext can still be created successfully). --- include/wx/dcgraph.h | 4 ++++ src/common/dcgraph.cpp | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/wx/dcgraph.h b/include/wx/dcgraph.h index 9a11a19ce4..b75d715c7b 100644 --- a/include/wx/dcgraph.h +++ b/include/wx/dcgraph.h @@ -60,6 +60,10 @@ public: #if defined(__WXMSW__) && wxUSE_ENH_METAFILE wxGCDCImpl( wxDC *owner, const wxEnhMetaFileDC& dc ); #endif + + // Ctor using an existing graphics context given to wxGCDC ctor. + wxGCDCImpl(wxDC *owner, wxGraphicsContext* context); + wxGCDCImpl( wxDC *owner ); virtual ~wxGCDCImpl(); diff --git a/src/common/dcgraph.cpp b/src/common/dcgraph.cpp index 832eb2bdc6..802ba8bf6a 100644 --- a/src/common/dcgraph.cpp +++ b/src/common/dcgraph.cpp @@ -106,9 +106,8 @@ wxGCDC::wxGCDC(const wxEnhMetaFileDC& dc) #endif wxGCDC::wxGCDC(wxGraphicsContext* context) : - wxDC( new wxGCDCImpl( this ) ) + wxDC(new wxGCDCImpl(this, context)) { - SetGraphicsContext(context); } wxGCDC::wxGCDC() : @@ -122,6 +121,12 @@ wxGCDC::~wxGCDC() wxIMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl, wxDCImpl); +wxGCDCImpl::wxGCDCImpl(wxDC *owner, wxGraphicsContext* context) : + wxDCImpl(owner) +{ + Init(context); +} + wxGCDCImpl::wxGCDCImpl( wxDC *owner ) : wxDCImpl( owner ) {