From bc562289c66cb58e59842462bed2329f0f52a3da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20T=C3=A9tar?= Date: Tue, 2 May 2017 11:02:37 +0200 Subject: [PATCH 01/10] Introduce wxPenInfo class --- include/wx/dfb/pen.h | 2 ++ include/wx/gtk/pen.h | 2 ++ include/wx/gtk1/pen.h | 2 ++ include/wx/msw/pen.h | 3 ++ include/wx/osx/pen.h | 3 ++ include/wx/pen.h | 76 +++++++++++++++++++++++++++++++++++++++++++ include/wx/x11/pen.h | 3 ++ interface/wx/pen.h | 42 ++++++++++++++++++++++++ src/dfb/pen.cpp | 8 +++++ src/gtk/pen.cpp | 13 ++++++++ src/gtk1/pen.cpp | 14 ++++++++ src/msw/pen.cpp | 14 ++++++++ src/osx/pen.cpp | 16 +++++++++ src/x11/pen.cpp | 14 ++++++++ 14 files changed, 212 insertions(+) diff --git a/include/wx/dfb/pen.h b/include/wx/dfb/pen.h index 642c6569f6..24b03a9404 100644 --- a/include/wx/dfb/pen.h +++ b/include/wx/dfb/pen.h @@ -35,6 +35,8 @@ public: wxPen(const wxBitmap& stipple, int width); + wxPen(const wxPenInfo& info); + bool operator==(const wxPen& pen) const; bool operator!=(const wxPen& pen) const { return !(*this == pen); } diff --git a/include/wx/gtk/pen.h b/include/wx/gtk/pen.h index c6bfbe3b0b..6e95eb9c40 100644 --- a/include/wx/gtk/pen.h +++ b/include/wx/gtk/pen.h @@ -20,6 +20,8 @@ public: wxPen( const wxColour &colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID ); + wxPen( const wxPenInfo& info ); + virtual ~wxPen(); bool operator==(const wxPen& pen) const; diff --git a/include/wx/gtk1/pen.h b/include/wx/gtk1/pen.h index 77e754ec4c..b3124dc9bf 100644 --- a/include/wx/gtk1/pen.h +++ b/include/wx/gtk1/pen.h @@ -38,6 +38,8 @@ public: wxPen( const wxColour &colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID ); + wxPen( const wxPenInfo& info ); + bool operator==(const wxPen& pen) const; bool operator!=(const wxPen& pen) const { return !(*this == pen); } diff --git a/include/wx/msw/pen.h b/include/wx/msw/pen.h index f1648cc82a..00e021bd87 100644 --- a/include/wx/msw/pen.h +++ b/include/wx/msw/pen.h @@ -25,6 +25,9 @@ public: wxPen(const wxColour& col, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID); wxPen(const wxBitmap& stipple, int width); + + wxPen(const wxPenInfo& info); + virtual ~wxPen() { } bool operator==(const wxPen& pen) const; diff --git a/include/wx/osx/pen.h b/include/wx/osx/pen.h index c203e2faeb..f028df68e9 100644 --- a/include/wx/osx/pen.h +++ b/include/wx/osx/pen.h @@ -23,6 +23,9 @@ public: wxPen(const wxColour& col, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID); wxPen(const wxBitmap& stipple, int width); + + wxPen(const wxPenInfo& info); + virtual ~wxPen(); bool operator==(const wxPen& pen) const; diff --git a/include/wx/pen.h b/include/wx/pen.h index e7eb2ff4a1..56100361b9 100644 --- a/include/wx/pen.h +++ b/include/wx/pen.h @@ -59,6 +59,82 @@ enum wxPenCap wxCAP_BUTT }; +// ---------------------------------------------------------------------------- +// wxPenInfo describes a wxPen +// ---------------------------------------------------------------------------- + +class wxPenInfo +{ +public: + wxPenInfo() : + m_colour(wxNullColour) + { + Init(); + + m_width = 1; + m_style = wxPENSTYLE_SOLID; + } + + explicit wxPenInfo(const wxColour& colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID) : + m_colour((wxColour&) colour) + { + Init(); + + m_width = width; + m_style = style; + } + + // Setters for the various attributes. All of them return the object itself + // so that the calls to them could be chained. + + wxPenInfo& Colour(const wxColour& colour) + { m_colour = colour; return *this; } + + wxPenInfo& Width(int width) + { m_width = width; return *this; } + + wxPenInfo& Style(wxPenStyle style) + { m_style = style; return *this; } + wxPenInfo& Stipple(const wxBitmap& stipple) + { m_stipple = stipple; m_style = wxPENSTYLE_STIPPLE; return *this; } + wxPenInfo& Dashes(int nb_dashes, const wxDash *dash) + { m_nb_dashes = nb_dashes; m_dash = (wxDash *)dash; return *this; } + wxPenInfo& Join(wxPenJoin join) + { m_join = join; return *this; } + wxPenInfo& Cap(wxPenCap cap) + { m_cap = cap; return *this; } + + // Accessors are mostly meant to be used by wxPen itself. + + wxColour GetColour() const { return m_colour; } + wxBitmap* GetStipple() { return &m_stipple; } + wxPenStyle GetStyle() const { return m_style; } + wxPenJoin GetJoin() const { return m_join; } + wxPenCap GetCap() const { return m_cap; } + int GetWidth() const { return m_width; } + int GetDashes(wxDash **ptr) const { *ptr = m_dash; return m_nb_dashes; } + +private: + void Init() + { + m_stipple = wxNullBitmap; + m_nb_dashes = 0; + m_dash = NULL; + m_join = wxJOIN_ROUND; + m_cap = wxCAP_ROUND; + } + + wxColour& m_colour; + int m_width; + wxBitmap m_stipple; + wxPenStyle m_style; + wxPenJoin m_join; + wxPenCap m_cap; + + int m_nb_dashes; + wxDash* m_dash; +}; + class WXDLLIMPEXP_CORE wxPenBase : public wxGDIObject { diff --git a/include/wx/x11/pen.h b/include/wx/x11/pen.h index b71eb45cc7..bb3f9dc2e4 100644 --- a/include/wx/x11/pen.h +++ b/include/wx/x11/pen.h @@ -36,6 +36,9 @@ public: wxPen( const wxColour &colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID ); wxPen( const wxBitmap &stipple, int width ); + + wxPen( const wxPenInfo& info ); + virtual ~wxPen(); bool operator == ( const wxPen& pen ) const; diff --git a/interface/wx/pen.h b/interface/wx/pen.h index 6d42f19e36..3b9a361dea 100644 --- a/interface/wx/pen.h +++ b/interface/wx/pen.h @@ -100,6 +100,43 @@ enum wxPenCap }; +/** + @class wxPenInfo + + This class is a helper used for wxPen creation using named parameter + idiom: it allows to specify various wxPen attributes using the chained + calls to its clearly named methods instead of passing them in the fixed + order to wxPen constructors. + + @since 3.1.0 + */ +class wxPenInfo +{ +public: + + wxPenInfo(); + + explicit wxPen(const wxColour& colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID); + + wxPenInfo& (); + + wxPenInfo& Colour(const wxColour& col); + + wxPenInfo& Width(int width); + + wxPenInfo& Style(wxPenStyle style); + + wxPenInfo& Style(wxPenStyle style); + + wxPenInfo& Stipple(const wxBitmap& stipple); + + wxPenInfo& Dashes(int nb_dashes, const wxDash *dash); + + wxPenInfo& Join(wxPenJoin join); + + wxPenInfo& Cap(wxPenCap cap); +}; + /** @class wxPen @@ -157,6 +194,11 @@ public: */ wxPen(); + /** + Creates a pen object using the specified pen description. + */ + wxPen(const wxPenInfo& info); + /** Constructs a pen from a colour object, pen width and style. diff --git a/src/dfb/pen.cpp b/src/dfb/pen.cpp index e36ed6dde0..4b6d2f824a 100644 --- a/src/dfb/pen.cpp +++ b/src/dfb/pen.cpp @@ -79,6 +79,14 @@ wxPen::wxPen(const wxBitmap& WXUNUSED(stipple), int WXUNUSED(width)) m_refData = new wxPenRefData(); } +wxPen::wxPen(const wxPenInfo& info) +{ + m_refData = new wxPenRefData( + info.GetColour(), + info.GetStyle() + ); +} + bool wxPen::operator==(const wxPen& pen) const { #warning "this is incorrect" diff --git a/src/gtk/pen.cpp b/src/gtk/pen.cpp index d6d4da8427..0adc41a458 100644 --- a/src/gtk/pen.cpp +++ b/src/gtk/pen.cpp @@ -103,6 +103,19 @@ wxPen::wxPen(const wxColour& colour, int width, int style) M_PENDATA->m_colour = colour; } +wxPen::wxPen(const wxPenInfo& info) +{ + m_refData = new wxPenRefData(); + M_PENDATA->m_colour = info.GetColour(); + M_PENDATA->m_width = info.GetWidth(); + M_PENDATA->m_style = info.GetStyle(); + M_PENDATA->m_joinStyle = info.GetJoin(); + M_PENDATA->m_capStyle = info.GetCap(); + wxDash *dashes; + M_PENDATA->m_countDashes = info.GetDashes(&dashes); + M_PENDATA->m_dash = dashes; +} + wxPen::~wxPen() { // m_refData unrefed in ~wxObject diff --git a/src/gtk1/pen.cpp b/src/gtk1/pen.cpp index 8e5d64ea25..0e6eab5239 100644 --- a/src/gtk1/pen.cpp +++ b/src/gtk1/pen.cpp @@ -103,6 +103,20 @@ wxPen::wxPen(const wxColour& colour, int width, int style) M_PENDATA->m_colour = colour; } +wxPen::wxPen(const wxPenInfo& info) +{ + m_refData = new wxPenRefData(); + + M_PENDATA->m_colour = info.GetColour(); + M_PENDATA->m_width = info.GetWidth(); + M_PENDATA->m_style = info.GetStyle(); + M_PENDATA->m_joinStyle = info.GetJoin(); + M_PENDATA->m_capStyle = info.GetCap(); + wxDash *dashes; + M_PENDATA->m_countDashes = info.GetDashes(&dashes); + M_PENDATA->m_dash = dashes; +} + wxGDIRefData *wxPen::CreateGDIRefData() const { return new wxPenRefData; diff --git a/src/msw/pen.cpp b/src/msw/pen.cpp index 1e1dac1d7e..1992349f24 100644 --- a/src/msw/pen.cpp +++ b/src/msw/pen.cpp @@ -414,6 +414,20 @@ wxPen::wxPen(const wxBitmap& stipple, int width) m_refData = new wxPenRefData(stipple, width); } +wxPen::wxPen(const wxPenInfo& info) +{ + m_refData = new wxPenRefData(); + + M_PENDATA->SetColour(info.GetColour()); + M_PENDATA->SetWidth(info.GetWidth()); + M_PENDATA->SetStyle(info.GetStyle()); + M_PENDATA->SetJoin(info.GetJoin()); + M_PENDATA->SetCap(info.GetCap()); + wxDash *dash; + int nb_dashes = info.GetDashes(&dash); + M_PENDATA->SetDashes(nb_dashes, dash) +} + bool wxPen::operator==(const wxPen& pen) const { const wxPenRefData * diff --git a/src/osx/pen.cpp b/src/osx/pen.cpp index b835196261..b1b0ce79d7 100644 --- a/src/osx/pen.cpp +++ b/src/osx/pen.cpp @@ -141,6 +141,22 @@ wxPen::wxPen(const wxBitmap& stipple, int Width) RealizeResource(); } +wxPen::wxPen(const wxPenInfo& info) +{ + m_refData = new wxPenRefData; + + M_PENDATA->m_colour = info.GetColour(); + M_PENDATA->m_width = info.GetWidth(); + M_PENDATA->m_style = info.GetStyle(); + M_PENDATA->m_join = info.GetJoin(); + M_PENDATA->m_cap = info.GetCap(); + wxDash *dashes; + M_PENDATA->m_nbDash = info.GetDashes(&dashes); + M_PENDATA->m_dash = dashes; + + RealizeResource(); +} + wxGDIRefData *wxPen::CreateGDIRefData() const { return new wxPenRefData; diff --git a/src/x11/pen.cpp b/src/x11/pen.cpp index 036c52f3da..9c1699d97d 100644 --- a/src/x11/pen.cpp +++ b/src/x11/pen.cpp @@ -93,6 +93,20 @@ wxPen::wxPen(const wxColour& colour, int width, int style) M_PENDATA->m_colour = colour; } +wxPen::wxPen(const wxPenInfo& info) +{ + m_refData = new wxPenRefData(); + M_PENDATA->m_width = info.GetWidth(); + M_PENDATA->m_style = info.GetStyle(); + M_PENDATA->m_colour = info.GetColour(); + M_PENDATA->m_capStyle = info.GetCap(); + M_PENDATA->m_joinStyle = info.GetJoin(); + M_PENDATA->m_stipple = info.GetStipple(); + wxDash *dashes; + M_PENDATA->m_countDashes = info.GetDashes(&dashes); + M_PENDATA->m_dash = dashes; +} + wxPen::~wxPen() { // m_refData unrefed in ~wxObject From 23056045658bdbb19ea3f2d1f21655946452c670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20T=C3=A9tar?= Date: Tue, 2 May 2017 13:43:31 +0200 Subject: [PATCH 02/10] Introduce wxGraphicsPenInfo class --- include/wx/graphics.h | 43 +++++++++++++++++++ include/wx/pen.h | 4 ++ interface/wx/graphics.h | 49 +++++++++++++++++++++- interface/wx/pen.h | 2 +- src/generic/graphicc.cpp | 55 ++++++++++++++++++++---- src/msw/graphics.cpp | 68 +++++++++++++++++++++++------- src/msw/graphicsd2d.cpp | 84 ++++++++++++++++++++++++++++++------- src/osx/carbon/graphics.cpp | 63 ++++++++++++++++++++++------ 8 files changed, 314 insertions(+), 54 deletions(-) diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 2af989a3c4..3e506cc46f 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -132,6 +132,49 @@ protected: wxDECLARE_DYNAMIC_CLASS(wxGraphicsObject); }; +// ---------------------------------------------------------------------------- +// wxGraphicsPenInfo describes a wxGraphicsPen +// ---------------------------------------------------------------------------- + +class wxGraphicsPenInfo : public wxPenInfo +{ +public: + wxGraphicsPenInfo() + : wxPenInfo() + { + m_widthF = -1.0; + } + + explicit wxGraphicsPenInfo(const wxColour& colour, double widthF = 1.0, wxPenStyle style = wxPENSTYLE_SOLID) + : wxPenInfo(colour, 0, style) + { + m_widthF = widthF; + } + + // Setters for the various attributes. All of them return the object itself + // so that the calls to them could be chained. + + wxGraphicsPenInfo& Colour(const wxColour& colour); + + wxGraphicsPenInfo& Width(int width); + + wxGraphicsPenInfo& Style(wxPenStyle style); + wxGraphicsPenInfo& Stipple(const wxBitmap& stipple); + wxGraphicsPenInfo& Dashes(int nb_dashes, const wxDash *dash); + wxGraphicsPenInfo& Join(wxPenJoin join); + wxGraphicsPenInfo& Cap(wxPenCap cap); + + wxGraphicsPenInfo& WidthF(wxDouble widthF) + { m_widthF = widthF; return *this; } + + // Accessors are mostly meant to be used by wxGraphicsPen itself. + + wxDouble GetWidthF() const { return m_widthF; } + +private: + wxDouble m_widthF; +}; + class WXDLLIMPEXP_CORE wxGraphicsPen : public wxGraphicsObject { public: diff --git a/include/wx/pen.h b/include/wx/pen.h index 56100361b9..e7a4bad57e 100644 --- a/include/wx/pen.h +++ b/include/wx/pen.h @@ -114,6 +114,10 @@ public: int GetWidth() const { return m_width; } int GetDashes(wxDash **ptr) const { *ptr = m_dash; return m_nb_dashes; } + // Convenience + + bool IsTransparent() const { return m_style == wxPENSTYLE_TRANSPARENT; } + private: void Init() { diff --git a/interface/wx/graphics.h b/interface/wx/graphics.h index ba37013b88..fee1a22650 100644 --- a/interface/wx/graphics.h +++ b/interface/wx/graphics.h @@ -253,7 +253,7 @@ enum wxInterpolationQuality /** default interpolation, based on type of context, in general medium quality */ wxINTERPOLATION_DEFAULT, /** no interpolation */ - wxINTERPOLATION_NONE, + wxINTERPOLATION_NONE, /** fast interpolation, suited for interactivity */ wxINTERPOLATION_FAST, /** better quality */ @@ -331,7 +331,7 @@ public: wxImage ConvertToImage() const; /** - Return the pointer to the native bitmap data. (CGImageRef for Core Graphics, + Return the pointer to the native bitmap data. (CGImageRef for Core Graphics, cairo_surface_t for Cairo, Bitmap* for GDI+.) @since 2.9.4 @@ -680,6 +680,11 @@ public: */ virtual wxGraphicsPen CreatePen(const wxPen& pen) const; + /** + Creates a native pen from a wxGraphicsPenInfo. + */ + virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) const; + /** Sets the pen used for stroking. */ @@ -1527,6 +1532,46 @@ public: }; +/** + @class wxGraphicsPenInfo + + This class is a helper used for wxGraphicsPen creation using named parameter + idiom: it allows to specify various wxGraphicsPen attributes using the chained + calls to its clearly named methods instead of passing them in the fixed + order to wxGraphicsPen constructors. + + @since 3.1.0 + */ +class wxGraphicsPenInfo : public wxPenInfo +{ +public: + + wxGraphicsPenInfo(); + + explicit wxGraphicsPenInfo(const wxColour& colour, double widthF = 1.0, wxPenStyle style = wxPENSTYLE_SOLID); + + wxGraphicsPenInfo& (); + + wxGraphicsPenInfo& Colour(const wxColour& col); + + wxGraphicsPenInfo& Width(int width); + + wxGraphicsPenInfo& WidthF(wxDouble widthF); + + wxGraphicsPenInfo& Style(wxPenStyle style); + + wxGraphicsPenInfo& Style(wxPenStyle style); + + wxGraphicsPenInfo& Stipple(const wxBitmap& stipple); + + wxGraphicsPenInfo& Dashes(int nb_dashes, const wxDash *dash); + + wxGraphicsPenInfo& Join(wxPenJoin join); + + wxGraphicsPenInfo& Cap(wxPenCap cap); +}; + + /** @class wxGraphicsPen diff --git a/interface/wx/pen.h b/interface/wx/pen.h index 3b9a361dea..efafa1593f 100644 --- a/interface/wx/pen.h +++ b/interface/wx/pen.h @@ -116,7 +116,7 @@ public: wxPenInfo(); - explicit wxPen(const wxColour& colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID); + explicit wxPenInfo(const wxColour& colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID); wxPenInfo& (); diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index bc35b45a17..f45fe82999 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -285,9 +285,11 @@ class WXDLLIMPEXP_CORE wxCairoPenData : public wxCairoPenBrushBaseData { public: wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); + wxCairoPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ); ~wxCairoPenData(); void Init(); + void InitFromPenInfo( const wxGraphicsPenInfo& info ); virtual void Apply( wxGraphicsContext* context ) wxOVERRIDE; virtual wxDouble GetWidth() { return m_width; } @@ -735,13 +737,36 @@ void wxCairoPenData::Init() wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) : wxCairoPenBrushBaseData(renderer, pen.GetColour(), pen.IsTransparent()) +{ + wxDash *dashes; + int nb_dashes = pen.GetDashes(&dashes); + InitFromPenInfo(wxGraphicsPenInfo() + .Colour(pen.GetColour()) + .Width(pen.GetWidth()) + .Style(pen.GetStyle()) + .Stipple(*pen.GetStipple()) + .Dashes(nb_dashes, dashes) + .Join(pen.GetJoin()) + .Cap(pen.GetCap()) + ); +} + +wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ) + : wxCairoPenBrushBaseData(renderer, info.GetColour(), info.IsTransparent()) +{ + InitFromPenInfo(info); +} + +void wxCairoPenData::InitFromPenInfo( const wxGraphicsPenInfo &info ) { Init(); - m_width = pen.GetWidth(); + m_width = info.GetWidthF(); + if (m_width < 0) + m_width = info.GetWidth(); if (m_width <= 0.0) m_width = 0.1; - switch ( pen.GetCap() ) + switch ( info.GetCap() ) { case wxCAP_ROUND : m_cap = CAIRO_LINE_CAP_ROUND; @@ -760,7 +785,7 @@ wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) break; } - switch ( pen.GetJoin() ) + switch ( info.GetJoin() ) { case wxJOIN_BEVEL : m_join = CAIRO_LINE_JOIN_BEVEL; @@ -797,7 +822,7 @@ wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) 9.0 , 6.0 , 3.0 , 3.0 }; - switch ( pen.GetStyle() ) + switch ( info.GetStyle() ) { case wxPENSTYLE_SOLID : break; @@ -827,7 +852,7 @@ wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) case wxPENSTYLE_USER_DASH : { wxDash *wxdashes ; - m_count = pen.GetDashes( &wxdashes ) ; + m_count = info.GetDashes( &wxdashes ) ; if ((wxdashes != NULL) && (m_count > 0)) { m_userLengths = new double[m_count] ; @@ -848,14 +873,14 @@ wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) case wxPENSTYLE_STIPPLE : case wxPENSTYLE_STIPPLE_MASK : case wxPENSTYLE_STIPPLE_MASK_OPAQUE : - InitStipple(pen.GetStipple()); + InitStipple(((wxGraphicsPenInfo&) info).GetStipple()); break; default : - if ( pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH - && pen.GetStyle() <= wxPENSTYLE_LAST_HATCH ) + if ( info.GetStyle() >= wxPENSTYLE_FIRST_HATCH + && info.GetStyle() <= wxPENSTYLE_LAST_HATCH ) { - InitHatch(static_cast(pen.GetStyle())); + InitHatch(static_cast(info.GetStyle())); } break; } @@ -2901,6 +2926,7 @@ public : virtual wxGraphicsPen CreatePen(const wxPen& pen) wxOVERRIDE ; + virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) wxOVERRIDE ; virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) wxOVERRIDE ; @@ -3087,6 +3113,17 @@ wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen) return p; } +wxGraphicsPen wxCairoRenderer::CreatePen(const wxGraphicsPenInfo& info) +{ + wxGraphicsPen p; + ENSURE_LOADED_OR_RETURN(p); + if (info.GetStyle() != wxPENSTYLE_TRANSPARENT) + { + p.SetRefData(new wxCairoPenData( this, info )); + } + return p; +} + wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush ) { wxGraphicsBrush p; diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index 9aeea9ed01..e66931abbe 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -261,9 +261,11 @@ class wxGDIPlusPenData : public wxGraphicsObjectRefData { public: wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); + wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ); ~wxGDIPlusPenData(); void Init(); + void InitFromPenInfo( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ); virtual wxDouble GetWidth() { return m_width; } virtual Pen* GetGDIPlusPen() { return m_pen; } @@ -400,7 +402,7 @@ public: virtual bool SetAntialiasMode(wxAntialiasMode antialias) wxOVERRIDE; virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation) wxOVERRIDE; - + virtual bool SetCompositionMode(wxCompositionMode op) wxOVERRIDE; virtual void BeginLayer(wxDouble opacity) wxOVERRIDE; @@ -575,6 +577,8 @@ public : virtual wxGraphicsPen CreatePen(const wxPen& pen) wxOVERRIDE; + virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& pen) wxOVERRIDE; + virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) wxOVERRIDE; virtual wxGraphicsBrush @@ -645,16 +649,39 @@ void wxGDIPlusPenData::Init() wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) : wxGraphicsObjectRefData(renderer) +{ + wxDash *dashes; + int nb_dashes = pen.GetDashes(&dashes); + InitFromPenInfo(renderer, wxGraphicsPenInfo() + .Colour(pen.GetColour()) + .Width(pen.GetWidth()) + .Style(pen.GetStyle()) + .Stipple(*pen.GetStipple()) + .Dashes(nb_dashes, dashes) + .Join(pen.GetJoin()) + .Cap(pen.GetCap()) + ); +} + +wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ) +: wxGraphicsObjectRefData(renderer) +{ + InitFromPenInfo(renderer, info); +} + +void wxGDIPlusPenData::InitFromPenInfo( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ) { Init(); - m_width = pen.GetWidth(); + m_width = info.GetWidthF(); + if (m_info < 0.0) + m_width = info.GetWidth(); if (m_width <= 0.0) m_width = 0.1; - m_pen = new Pen(wxColourToColor(pen.GetColour()), m_width ); + m_pen = new Pen(wxColourToColor(info.GetColour()), m_width ); LineCap cap; - switch ( pen.GetCap() ) + switch ( info.GetCap() ) { case wxCAP_ROUND : cap = LineCapRound; @@ -675,7 +702,7 @@ wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &p m_pen->SetLineCap(cap,cap, DashCapFlat); LineJoin join; - switch ( pen.GetJoin() ) + switch ( info.GetJoin() ) { case wxJOIN_BEVEL : join = LineJoinBevel; @@ -699,7 +726,7 @@ wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &p m_pen->SetDashStyle(DashStyleSolid); DashStyle dashStyle = DashStyleSolid; - switch ( pen.GetStyle() ) + switch ( info.GetStyle() ) { case wxPENSTYLE_SOLID : break; @@ -723,7 +750,7 @@ wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &p { dashStyle = DashStyleCustom; wxDash *dashes; - int count = pen.GetDashes( &dashes ); + int count = info.GetDashes( &dashes ); if ((dashes != NULL) && (count > 0)) { REAL *userLengths = new REAL[count]; @@ -738,7 +765,7 @@ wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &p break; case wxPENSTYLE_STIPPLE : { - wxBitmap* bmp = pen.GetStipple(); + wxBitmap* bmp = info.GetStipple(); if ( bmp && bmp->IsOk() ) { m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(), @@ -755,11 +782,11 @@ wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &p } break; default : - if ( pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH && - pen.GetStyle() <= wxPENSTYLE_LAST_HATCH ) + if ( info.GetStyle() >= wxPENSTYLE_FIRST_HATCH && + info.GetStyle() <= wxPENSTYLE_LAST_HATCH ) { HatchStyle style; - switch( pen.GetStyle() ) + switch( info.GetStyle() ) { case wxPENSTYLE_BDIAGONAL_HATCH : style = HatchStyleBackwardDiagonal; @@ -785,7 +812,7 @@ wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &p m_penBrush = new HatchBrush ( style, - wxColourToColor(pen.GetColour()), + wxColourToColor(info.GetColour()), Color::Transparent ); m_pen->SetBrush( m_penBrush ); @@ -2047,7 +2074,7 @@ void wxGDIPlusContext::DoDrawText(const wxString& str, wxGDIPlusFontData * const fontData = (wxGDIPlusFontData *)m_font.GetRefData(); - + m_context->DrawString ( str.wc_str(*wxConvUI), // string to draw, always Unicode @@ -2160,7 +2187,7 @@ bool wxGDIPlusContext::ShouldOffset() const { if ( !m_enableOffset ) return false; - + int penwidth = 0 ; if ( !m_pen.IsNull() ) { @@ -2461,6 +2488,19 @@ wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen) } } +wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxGraphicsPenInfo& info) +{ + ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen); + if ( !info.GetStyle() == wxPENSTYLE_TRANSPARENT ) + return wxNullGraphicsPen; + else + { + wxGraphicsPen p; + p.SetRefData(new wxGDIPlusPenData( this, info )); + return p; + } +} + wxGraphicsBrush wxGDIPlusRenderer::CreateBrush(const wxBrush& brush ) { ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); diff --git a/src/msw/graphicsd2d.cpp b/src/msw/graphicsd2d.cpp index cd9d7e9684..e059744534 100644 --- a/src/msw/graphicsd2d.cpp +++ b/src/msw/graphicsd2d.cpp @@ -2460,6 +2460,10 @@ class wxD2DPenData : public wxGraphicsObjectRefData, public wxD2DManagedGraphics public: wxD2DPenData(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, const wxPen& pen); + wxD2DPenData(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, const wxGraphicsPenInfo& info); + + void InitFromPenInfo(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory); + void CreateStrokeStyle(ID2D1Factory* const direct2dfactory); ID2D1Brush* GetBrush(); @@ -2474,9 +2478,9 @@ public: } private: - // We store the source pen for later when we need to recreate the + // We store the source info for later when we need to recreate the // device-dependent resources. - const wxPen m_sourcePen; + const wxGraphicsPenInfo& m_sourceInfo; // A stroke style is a device-independent resource. // Describes the caps, miter limit, line join, and dash information. @@ -2496,26 +2500,57 @@ private: wxD2DPenData::wxD2DPenData( wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, - const wxPen& pen) - : wxGraphicsObjectRefData(renderer), m_sourcePen(pen), m_width(pen.GetWidth()) + const wxPen& pen) : + wxGraphicsObjectRefData(renderer), + m_sourceInfo(wxGraphicsPenInfo()) + m_width(pen.GetWidth()) { - CreateStrokeStyle(direct2dFactory); + wxDash* dashes; + int nb_dashes = pen.GetDashes(&dashes); + m_sourceInfo + .Colour(pen.GetColour()) + .Width(pen.GetWidth()) + .Style(pen.GetStyle()) + .Stipple(*pen.GetStipple()) + .Dashes(nb_dashes, dashes) + .Join(pen.GetJoin()) + .Cap(pen.GetCap()) + InitFromPenInfo(renderer, direct2dFactory); +} + +wxD2DPenData::wxD2DPenData( + wxGraphicsRenderer* renderer, + ID2D1Factory* direct2dFactory, + const wxGraphicsPenInfo& info) + : wxGraphicsObjectRefData(renderer), m_sourceInfo(Info), m_width(info.GetWidthF()) +{ + if (m_width < 0.0) + m_width = info.GetWidth(); + + InitFromPenInfo(renderer, direct2dFactory); +} + +void wxD2DPenData::InitFromPenInfo( + wxGraphicsRenderer* renderer, + ID2D1Factory* direct2dFactory) +{ + CreateStrokeStyle(direct2dFactory, info); wxBrush strokeBrush; - if (m_sourcePen.GetStyle() == wxPENSTYLE_STIPPLE) + if (m_sourceInfo.GetStyle() == wxPENSTYLE_STIPPLE) { - strokeBrush.SetStipple(*(m_sourcePen.GetStipple())); + strokeBrush.SetStipple(*(m_sourceInfo.GetStipple())); strokeBrush.SetStyle(wxBRUSHSTYLE_STIPPLE); } - else if(wxIsHatchPenStyle(m_sourcePen.GetStyle())) + else if(wxIsHatchPenStyle(m_sourceInfo.GetStyle())) { - strokeBrush.SetStyle(wxConvertPenStyleToBrushStyle(m_sourcePen.GetStyle())); - strokeBrush.SetColour(m_sourcePen.GetColour()); + strokeBrush.SetStyle(wxConvertPenStyleToBrushStyle(m_sourceInfo.GetStyle())); + strokeBrush.SetColour(m_sourceInfo.GetColour()); } else { - strokeBrush.SetColour(m_sourcePen.GetColour()); + strokeBrush.SetColour(m_sourceInfo.GetColour()); strokeBrush.SetStyle(wxBRUSHSTYLE_SOLID); } @@ -2524,21 +2559,21 @@ wxD2DPenData::wxD2DPenData( void wxD2DPenData::CreateStrokeStyle(ID2D1Factory* const direct2dfactory) { - D2D1_CAP_STYLE capStyle = wxD2DConvertPenCap(m_sourcePen.GetCap()); - D2D1_LINE_JOIN lineJoin = wxD2DConvertPenJoin(m_sourcePen.GetJoin()); - D2D1_DASH_STYLE dashStyle = wxD2DConvertPenStyle(m_sourcePen.GetStyle()); + D2D1_CAP_STYLE capStyle = wxD2DConvertPenCap(m_sourceInfo.GetCap()); + D2D1_LINE_JOIN lineJoin = wxD2DConvertPenJoin(m_sourceInfo.GetJoin()); + D2D1_DASH_STYLE dashStyle = wxD2DConvertPenStyle(m_sourceInfo.GetStyle()); int dashCount = 0; FLOAT* dashes = NULL; if (dashStyle == D2D1_DASH_STYLE_CUSTOM) { - dashCount = m_sourcePen.GetDashCount(); + dashCount = m_sourceInfo.GetDashCount(); dashes = new FLOAT[dashCount]; for (int i = 0; i < dashCount; ++i) { - dashes[i] = m_sourcePen.GetDash()[i]; + dashes[i] = m_sourceInfo.GetDash()[i]; } } @@ -4386,6 +4421,8 @@ public : wxGraphicsPen CreatePen(const wxPen& pen) wxOVERRIDE; + wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) wxOVERRIDE; + wxGraphicsBrush CreateBrush(const wxBrush& brush) wxOVERRIDE; wxGraphicsBrush CreateLinearGradientBrush( @@ -4568,6 +4605,21 @@ wxGraphicsPen wxD2DRenderer::CreatePen(const wxPen& pen) } } +wxGraphicsPen wxD2DRenderer::CreatePen(const wxGraphicsPenInfo& info) +{ + if ( info.GetStyle() == wxPENSTYLE_TRANSPARENT ) + { + return wxNullGraphicsPen; + } + else + { + wxGraphicsPen p; + wxD2DPenData* penData = new wxD2DPenData(this, m_direct2dFactory, info); + p.SetRefData(penData); + return p; + } +} + wxGraphicsBrush wxD2DRenderer::CreateBrush(const wxBrush& brush) { if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT ) diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index 767e052367..d5f625a189 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -305,9 +305,11 @@ class wxMacCoreGraphicsPenData : public wxGraphicsObjectRefData { public: wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); + wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ); ~wxMacCoreGraphicsPenData(); void Init(); + void InitFromPenInfo( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ); virtual void Apply( wxGraphicsContext* context ); virtual wxDouble GetWidth() { return m_width; } @@ -331,17 +333,40 @@ protected : wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) : wxGraphicsObjectRefData( renderer ) +{ + wxDash *dashes; + int nb_dashes = pen.GetDashes(&dashes); + InitFromPenInfo(renderer, wxGraphicsPenInfo() + .Colour(pen.GetColour()) + .Width(pen.GetWidth()) + .Style(pen.GetStyle()) + .Stipple(*pen.GetStipple()) + .Dashes(nb_dashes, dashes) + .Join(pen.GetJoin()) + .Cap(pen.GetCap()) + ); +} + +wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ) : + wxGraphicsObjectRefData( renderer ) +{ + InitFromPenInfo(renderer, info); +} + +void wxMacCoreGraphicsPenData::InitFromPenInfo( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ) { Init(); - m_color.reset( wxMacCreateCGColor( pen.GetColour() ) ) ; + m_color.reset( wxMacCreateCGColor( info.GetColour() ) ) ; // TODO: * m_dc->m_scaleX - m_width = pen.GetWidth(); + m_width = info.GetWidthF(); + if (m_width < 0.0) + m_width = info.GetWidth(); if (m_width <= 0.0) m_width = (CGFloat) 0.1; - switch ( pen.GetCap() ) + switch ( info.GetCap() ) { case wxCAP_ROUND : m_cap = kCGLineCapRound; @@ -360,7 +385,7 @@ wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer break; } - switch ( pen.GetJoin() ) + switch ( info.GetJoin() ) { case wxJOIN_BEVEL : m_join = kCGLineJoinBevel; @@ -386,7 +411,7 @@ wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer static const CGFloat dashed[] = { (CGFloat) 19.0 , (CGFloat) 9.0 }; static const CGFloat dotted_dashed[] = { (CGFloat) 9.0 , (CGFloat) 6.0 , (CGFloat) 3.0 , (CGFloat) 3.0 }; - switch ( pen.GetStyle() ) + switch ( info.GetStyle() ) { case wxPENSTYLE_SOLID: break; @@ -415,7 +440,7 @@ wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer case wxPENSTYLE_USER_DASH: wxDash *dashes; - m_count = pen.GetDashes( &dashes ); + m_count = info.GetDashes( &dashes ); if ((dashes != NULL) && (m_count > 0)) { m_userLengths = new CGFloat[m_count]; @@ -434,7 +459,7 @@ wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer case wxPENSTYLE_STIPPLE: { - wxBitmap* bmp = pen.GetStipple(); + wxBitmap* bmp = info.GetStipple(); if ( bmp && bmp->IsOk() ) { m_colorSpace.reset( CGColorSpaceCreatePattern( NULL ) ); @@ -450,12 +475,12 @@ wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer { m_isPattern = true; m_colorSpace.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ); - m_pattern.reset( (CGPatternRef) *( new HatchPattern( pen.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) ); + m_pattern.reset( (CGPatternRef) *( new HatchPattern( info.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) ); m_patternColorComponents = new CGFloat[4] ; - m_patternColorComponents[0] = (CGFloat) (pen.GetColour().Red() / 255.0); - m_patternColorComponents[1] = (CGFloat) (pen.GetColour().Green() / 255.0); - m_patternColorComponents[2] = (CGFloat) (pen.GetColour().Blue() / 255.0); - m_patternColorComponents[3] = (CGFloat) (pen.GetColour().Alpha() / 255.0); + m_patternColorComponents[0] = (CGFloat) (info.GetColour().Red() / 255.0); + m_patternColorComponents[1] = (CGFloat) (info.GetColour().Green() / 255.0); + m_patternColorComponents[2] = (CGFloat) (info.GetColour().Blue() / 255.0); + m_patternColorComponents[3] = (CGFloat) (info.GetColour().Alpha() / 255.0); } break; } @@ -2538,6 +2563,8 @@ public : virtual wxGraphicsPen CreatePen(const wxPen& pen) wxOVERRIDE ; + virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) wxOVERRIDE ; + virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) wxOVERRIDE ; virtual wxGraphicsBrush @@ -2717,6 +2744,18 @@ wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen) } } +wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxGraphicsPenInfo& info) +{ + if ( info.GetStyle() == wxPENSTYLE_TRANSPARENT ) + return wxNullGraphicsPen; + else + { + wxGraphicsPen p; + p.SetRefData(new wxMacCoreGraphicsPenData( this, info )); + return p; + } +} + wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush ) { if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT ) From 999c750ca79ebecf3848a09d6b3d5b957884a945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20T=C3=A9tar?= Date: Wed, 3 May 2017 19:20:45 +0200 Subject: [PATCH 03/10] Review feedback --- include/wx/graphics.h | 50 +++++++++++---------- include/wx/pen.h | 90 ++++++++++++++++++++----------------- interface/wx/graphics.h | 22 +++++---- interface/wx/pen.h | 17 ++++--- src/common/graphcmn.cpp | 5 +++ src/generic/graphicc.cpp | 19 +++----- src/gtk/pen.cpp | 4 +- src/gtk1/pen.cpp | 4 +- src/msw/graphics.cpp | 32 +++++-------- src/msw/graphicsd2d.cpp | 31 ++++--------- src/msw/pen.cpp | 2 +- src/osx/carbon/graphics.cpp | 28 ++++-------- src/osx/pen.cpp | 4 +- src/x11/pen.cpp | 4 +- 14 files changed, 139 insertions(+), 173 deletions(-) diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 3e506cc46f..0c823f4cb5 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -22,6 +22,7 @@ #include "wx/font.h" #include "wx/image.h" #include "wx/vector.h" +#include "wx/pen.h" enum wxAntialiasMode { @@ -136,43 +137,40 @@ protected: // wxGraphicsPenInfo describes a wxGraphicsPen // ---------------------------------------------------------------------------- -class wxGraphicsPenInfo : public wxPenInfo +class wxGraphicsPenInfo : public wxPenInfoBase { public: - wxGraphicsPenInfo() - : wxPenInfo() + explicit wxGraphicsPenInfo(const wxColour& colour = wxColour(), wxDouble width = 1.0, wxPenStyle style = wxPENSTYLE_SOLID) + : wxPenInfoBase(colour, style) { - m_widthF = -1.0; + m_width = width; } - explicit wxGraphicsPenInfo(const wxColour& colour, double widthF = 1.0, wxPenStyle style = wxPENSTYLE_SOLID) - : wxPenInfo(colour, 0, style) + static wxGraphicsPenInfo CreateFromPen(const wxPen& pen) { - m_widthF = widthF; + wxDash *dashes; + int nb_dashes = pen.GetDashes(&dashes); + return wxGraphicsPenInfo() + .Colour(pen.GetColour()) + .Width(pen.GetWidth()) + .Style(pen.GetStyle()) + .Stipple(*pen.GetStipple()) + .Dashes(nb_dashes, dashes) + .Join(pen.GetJoin()) + .Cap(pen.GetCap()); } - // Setters for the various attributes. All of them return the object itself - // so that the calls to them could be chained. + // Setters - wxGraphicsPenInfo& Colour(const wxColour& colour); + wxGraphicsPenInfo& Width(wxDouble width) + { m_width = width; return *this; } - wxGraphicsPenInfo& Width(int width); + // Accessors - wxGraphicsPenInfo& Style(wxPenStyle style); - wxGraphicsPenInfo& Stipple(const wxBitmap& stipple); - wxGraphicsPenInfo& Dashes(int nb_dashes, const wxDash *dash); - wxGraphicsPenInfo& Join(wxPenJoin join); - wxGraphicsPenInfo& Cap(wxPenCap cap); - - wxGraphicsPenInfo& WidthF(wxDouble widthF) - { m_widthF = widthF; return *this; } - - // Accessors are mostly meant to be used by wxGraphicsPen itself. - - wxDouble GetWidthF() const { return m_widthF; } + wxDouble GetWidth() const { return m_width; } private: - wxDouble m_widthF; + wxDouble m_width; }; class WXDLLIMPEXP_CORE wxGraphicsPen : public wxGraphicsObject @@ -524,6 +522,8 @@ public: virtual wxGraphicsPen CreatePen(const wxPen& pen) const; + virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& pen) const; + virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) const; // sets the brush to a linear gradient, starting at (x1,y1) and ending at @@ -906,6 +906,8 @@ public: virtual wxGraphicsPen CreatePen(const wxPen& pen) = 0; + virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) = 0; + virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) = 0; // Gradient brush creation functions may not honour all the stops specified diff --git a/include/wx/pen.h b/include/wx/pen.h index e7a4bad57e..4a11734c7a 100644 --- a/include/wx/pen.h +++ b/include/wx/pen.h @@ -11,6 +11,7 @@ #ifndef _WX_PEN_H_BASE_ #define _WX_PEN_H_BASE_ +#include "wx/bitmap.h" #include "wx/gdiobj.h" #include "wx/gdicmn.h" @@ -63,73 +64,56 @@ enum wxPenCap // wxPenInfo describes a wxPen // ---------------------------------------------------------------------------- -class wxPenInfo +template +class wxPenInfoBase { public: - wxPenInfo() : - m_colour(wxNullColour) + wxPenInfoBase(const wxColour& colour, wxPenStyle style) { - Init(); + m_nb_dashes = 0; + m_dash = NULL; + m_join = wxJOIN_ROUND; + m_cap = wxCAP_ROUND; - m_width = 1; - m_style = wxPENSTYLE_SOLID; - } - - explicit wxPenInfo(const wxColour& colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID) : - m_colour((wxColour&) colour) - { - Init(); - - m_width = width; + m_colour = colour; m_style = style; } // Setters for the various attributes. All of them return the object itself // so that the calls to them could be chained. - wxPenInfo& Colour(const wxColour& colour) - { m_colour = colour; return *this; } + T &Colour(const wxColour& colour) + { m_colour = colour; return static_cast(*this); } - wxPenInfo& Width(int width) - { m_width = width; return *this; } - - wxPenInfo& Style(wxPenStyle style) - { m_style = style; return *this; } - wxPenInfo& Stipple(const wxBitmap& stipple) - { m_stipple = stipple; m_style = wxPENSTYLE_STIPPLE; return *this; } - wxPenInfo& Dashes(int nb_dashes, const wxDash *dash) - { m_nb_dashes = nb_dashes; m_dash = (wxDash *)dash; return *this; } - wxPenInfo& Join(wxPenJoin join) - { m_join = join; return *this; } - wxPenInfo& Cap(wxPenCap cap) - { m_cap = cap; return *this; } + T &Style(wxPenStyle style) + { m_style = style; return static_cast(*this); } + T &Stipple(const wxBitmap& stipple) + { m_stipple = stipple; m_style = wxPENSTYLE_STIPPLE; return static_cast(*this); } + T &Dashes(int nb_dashes, const wxDash *dash) + { m_nb_dashes = nb_dashes; m_dash = (wxDash *)dash; return static_cast(*this); } + T &Join(wxPenJoin join) + { m_join = join; return static_cast(*this); } + T &Cap(wxPenCap cap) + { m_cap = cap; return static_cast(*this); } // Accessors are mostly meant to be used by wxPen itself. wxColour GetColour() const { return m_colour; } - wxBitmap* GetStipple() { return &m_stipple; } + wxBitmap GetStipple() const { return m_stipple; } wxPenStyle GetStyle() const { return m_style; } wxPenJoin GetJoin() const { return m_join; } wxPenCap GetCap() const { return m_cap; } - int GetWidth() const { return m_width; } int GetDashes(wxDash **ptr) const { *ptr = m_dash; return m_nb_dashes; } + int GetDashCount() const { return m_nb_dashes; } + wxDash* GetDash() const { return m_dash; } + // Convenience bool IsTransparent() const { return m_style == wxPENSTYLE_TRANSPARENT; } private: - void Init() - { - m_stipple = wxNullBitmap; - m_nb_dashes = 0; - m_dash = NULL; - m_join = wxJOIN_ROUND; - m_cap = wxCAP_ROUND; - } - - wxColour& m_colour; - int m_width; + wxColour m_colour; wxBitmap m_stipple; wxPenStyle m_style; wxPenJoin m_join; @@ -139,6 +123,28 @@ private: wxDash* m_dash; }; +class wxPenInfo : public wxPenInfoBase +{ +public: + explicit wxPenInfo(const wxColour& colour = wxColour(), int width = 1, wxPenStyle style = wxPENSTYLE_SOLID) + : wxPenInfoBase(colour, style) + { + m_width = width; + } + + // Setters + + wxPenInfo& Width(int width) + { m_width = width; return *this; } + + // Accessors + + int GetWidth() const { return m_width; } + +private: + int m_width; +}; + class WXDLLIMPEXP_CORE wxPenBase : public wxGDIObject { diff --git a/interface/wx/graphics.h b/interface/wx/graphics.h index fee1a22650..fdc263c4ac 100644 --- a/interface/wx/graphics.h +++ b/interface/wx/graphics.h @@ -1532,6 +1532,7 @@ public: }; + /** @class wxGraphicsPenInfo @@ -1540,23 +1541,26 @@ public: calls to its clearly named methods instead of passing them in the fixed order to wxGraphicsPen constructors. - @since 3.1.0 + Typically you would use wxGraphicsPenInfo with a wxGraphicsContext: + @code + wxGraphicsContext ctx = wxGraphicsContext::Create(dc); + + ctx.SetPen(wxGraphicsPenInfo(*wxBLUE, 1.25)); + @endcode + + @since 3.1.1 */ -class wxGraphicsPenInfo : public wxPenInfo +class wxGraphicsPenInfo : public wxPenInfoBase { public: - wxGraphicsPenInfo(); + explicit wxGraphicsPenInfo(const wxColour& colour = wxColour(), wxDouble width = 1.0, wxPenStyle style = wxPENSTYLE_SOLID); - explicit wxGraphicsPenInfo(const wxColour& colour, double widthF = 1.0, wxPenStyle style = wxPENSTYLE_SOLID); - - wxGraphicsPenInfo& (); + static wxGraphicsPenInfo CreateFromPen(const wxPen& pen); wxGraphicsPenInfo& Colour(const wxColour& col); - wxGraphicsPenInfo& Width(int width); - - wxGraphicsPenInfo& WidthF(wxDouble widthF); + wxGraphicsPenInfo& Width(wxDouble width); wxGraphicsPenInfo& Style(wxPenStyle style); diff --git a/interface/wx/pen.h b/interface/wx/pen.h index efafa1593f..a9ecddb59c 100644 --- a/interface/wx/pen.h +++ b/interface/wx/pen.h @@ -100,6 +100,7 @@ enum wxPenCap }; + /** @class wxPenInfo @@ -108,17 +109,18 @@ enum wxPenCap calls to its clearly named methods instead of passing them in the fixed order to wxPen constructors. - @since 3.1.0 + For instance, to create a blue pen with a width of 0: + @code + wxPen pen(wxPenInfo(*wxBLUE, 0)); + @endcode + + @since 3.1.1 */ -class wxPenInfo +class wxPenInfo : public wxPenInfoBase { public: - wxPenInfo(); - - explicit wxPenInfo(const wxColour& colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID); - - wxPenInfo& (); + explicit wxPenInfo(const wxColour& colour = wxColour(), int width = 1, wxPenStyle style = wxPENSTYLE_SOLID); wxPenInfo& Colour(const wxColour& col); @@ -138,6 +140,7 @@ public: }; + /** @class wxPen diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index 979e4423e6..5f39c47e07 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -827,6 +827,11 @@ wxGraphicsPen wxGraphicsContext::CreatePen(const wxPen& pen) const return GetRenderer()->CreatePen(pen); } +wxGraphicsPen wxGraphicsContext::CreatePen(const wxGraphicsPenInfo& info) const +{ + return GetRenderer()->CreatePen(info); +} + wxGraphicsBrush wxGraphicsContext::CreateBrush(const wxBrush& brush ) const { return GetRenderer()->CreateBrush(brush); diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index f45fe82999..b24147bb4e 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -738,17 +738,7 @@ void wxCairoPenData::Init() wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) : wxCairoPenBrushBaseData(renderer, pen.GetColour(), pen.IsTransparent()) { - wxDash *dashes; - int nb_dashes = pen.GetDashes(&dashes); - InitFromPenInfo(wxGraphicsPenInfo() - .Colour(pen.GetColour()) - .Width(pen.GetWidth()) - .Style(pen.GetStyle()) - .Stipple(*pen.GetStipple()) - .Dashes(nb_dashes, dashes) - .Join(pen.GetJoin()) - .Cap(pen.GetCap()) - ); + InitFromPenInfo(wxGraphicsPenInfo::CreateFromPen(pen)); } wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ) @@ -760,8 +750,6 @@ wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxGraphicsPe void wxCairoPenData::InitFromPenInfo( const wxGraphicsPenInfo &info ) { Init(); - m_width = info.GetWidthF(); - if (m_width < 0) m_width = info.GetWidth(); if (m_width <= 0.0) m_width = 0.1; @@ -873,7 +861,10 @@ void wxCairoPenData::InitFromPenInfo( const wxGraphicsPenInfo &info ) case wxPENSTYLE_STIPPLE : case wxPENSTYLE_STIPPLE_MASK : case wxPENSTYLE_STIPPLE_MASK_OPAQUE : - InitStipple(((wxGraphicsPenInfo&) info).GetStipple()); + { + wxBitmap stipple = info.GetStipple(); + InitStipple(&stipple); + } break; default : diff --git a/src/gtk/pen.cpp b/src/gtk/pen.cpp index 0adc41a458..d95f0e1ab1 100644 --- a/src/gtk/pen.cpp +++ b/src/gtk/pen.cpp @@ -111,9 +111,7 @@ wxPen::wxPen(const wxPenInfo& info) M_PENDATA->m_style = info.GetStyle(); M_PENDATA->m_joinStyle = info.GetJoin(); M_PENDATA->m_capStyle = info.GetCap(); - wxDash *dashes; - M_PENDATA->m_countDashes = info.GetDashes(&dashes); - M_PENDATA->m_dash = dashes; + M_PENDATA->m_countDashes = info.GetDashes(&M_PENDATA->m_dash); } wxPen::~wxPen() diff --git a/src/gtk1/pen.cpp b/src/gtk1/pen.cpp index 0e6eab5239..d68c6bfce9 100644 --- a/src/gtk1/pen.cpp +++ b/src/gtk1/pen.cpp @@ -112,9 +112,7 @@ wxPen::wxPen(const wxPenInfo& info) M_PENDATA->m_style = info.GetStyle(); M_PENDATA->m_joinStyle = info.GetJoin(); M_PENDATA->m_capStyle = info.GetCap(); - wxDash *dashes; - M_PENDATA->m_countDashes = info.GetDashes(&dashes); - M_PENDATA->m_dash = dashes; + M_PENDATA->m_countDashes = info.GetDashes(&M_PENDATA->m_dash); } wxGDIRefData *wxPen::CreateGDIRefData() const diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index e66931abbe..2a084f4def 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -265,7 +265,7 @@ public: ~wxGDIPlusPenData(); void Init(); - void InitFromPenInfo( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ); + void InitFromPenInfo( const wxGraphicsPenInfo &info ); virtual wxDouble GetWidth() { return m_width; } virtual Pen* GetGDIPlusPen() { return m_pen; } @@ -650,31 +650,19 @@ void wxGDIPlusPenData::Init() wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) : wxGraphicsObjectRefData(renderer) { - wxDash *dashes; - int nb_dashes = pen.GetDashes(&dashes); - InitFromPenInfo(renderer, wxGraphicsPenInfo() - .Colour(pen.GetColour()) - .Width(pen.GetWidth()) - .Style(pen.GetStyle()) - .Stipple(*pen.GetStipple()) - .Dashes(nb_dashes, dashes) - .Join(pen.GetJoin()) - .Cap(pen.GetCap()) - ); + InitFromPenInfo(wxGraphicsPenInfo::CreateFromPen(pen)); } wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ) : wxGraphicsObjectRefData(renderer) { - InitFromPenInfo(renderer, info); + InitFromPenInfo(info); } -void wxGDIPlusPenData::InitFromPenInfo( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ) +void wxGDIPlusPenData::InitFromPenInfo( const wxGraphicsPenInfo &info ) { Init(); - m_width = info.GetWidthF(); - if (m_info < 0.0) - m_width = info.GetWidth(); + m_width = info.GetWidth(); if (m_width <= 0.0) m_width = 0.1; @@ -765,12 +753,12 @@ void wxGDIPlusPenData::InitFromPenInfo( wxGraphicsRenderer* renderer, const wxGr break; case wxPENSTYLE_STIPPLE : { - wxBitmap* bmp = info.GetStipple(); - if ( bmp && bmp->IsOk() ) + wxBitmap bmp = info.GetStipple(); + if ( bmp.IsOk() ) { - m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(), + m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(), #if wxUSE_PALETTE - (HPALETTE)bmp->GetPalette()->GetHPALETTE() + (HPALETTE)bmp.GetPalette()->GetHPALETTE() #else NULL #endif @@ -2491,7 +2479,7 @@ wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen) wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxGraphicsPenInfo& info) { ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen); - if ( !info.GetStyle() == wxPENSTYLE_TRANSPARENT ) + if ( info.GetStyle() == wxPENSTYLE_TRANSPARENT ) return wxNullGraphicsPen; else { diff --git a/src/msw/graphicsd2d.cpp b/src/msw/graphicsd2d.cpp index e059744534..e402644cfe 100644 --- a/src/msw/graphicsd2d.cpp +++ b/src/msw/graphicsd2d.cpp @@ -2462,7 +2462,7 @@ public: wxD2DPenData(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, const wxGraphicsPenInfo& info); - void InitFromPenInfo(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory); + void Init(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory); void CreateStrokeStyle(ID2D1Factory* const direct2dfactory); @@ -2480,7 +2480,7 @@ public: private: // We store the source info for later when we need to recreate the // device-dependent resources. - const wxGraphicsPenInfo& m_sourceInfo; + const wxGraphicsPenInfo m_sourceInfo; // A stroke style is a device-independent resource. // Describes the caps, miter limit, line join, and dash information. @@ -2502,45 +2502,32 @@ wxD2DPenData::wxD2DPenData( ID2D1Factory* direct2dFactory, const wxPen& pen) : wxGraphicsObjectRefData(renderer), - m_sourceInfo(wxGraphicsPenInfo()) + m_sourceInfo(wxGraphicsPenInfo::CreateFromPen(pen)), m_width(pen.GetWidth()) { - wxDash* dashes; - int nb_dashes = pen.GetDashes(&dashes); - m_sourceInfo - .Colour(pen.GetColour()) - .Width(pen.GetWidth()) - .Style(pen.GetStyle()) - .Stipple(*pen.GetStipple()) - .Dashes(nb_dashes, dashes) - .Join(pen.GetJoin()) - .Cap(pen.GetCap()) - InitFromPenInfo(renderer, direct2dFactory); + Init(renderer, direct2dFactory); } wxD2DPenData::wxD2DPenData( wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, const wxGraphicsPenInfo& info) - : wxGraphicsObjectRefData(renderer), m_sourceInfo(Info), m_width(info.GetWidthF()) + : wxGraphicsObjectRefData(renderer), m_sourceInfo(info), m_width(info.GetWidth()) { - if (m_width < 0.0) - m_width = info.GetWidth(); - - InitFromPenInfo(renderer, direct2dFactory); + Init(renderer, direct2dFactory); } -void wxD2DPenData::InitFromPenInfo( +void wxD2DPenData::Init( wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory) { - CreateStrokeStyle(direct2dFactory, info); + CreateStrokeStyle(direct2dFactory); wxBrush strokeBrush; if (m_sourceInfo.GetStyle() == wxPENSTYLE_STIPPLE) { - strokeBrush.SetStipple(*(m_sourceInfo.GetStipple())); + strokeBrush.SetStipple(m_sourceInfo.GetStipple()); strokeBrush.SetStyle(wxBRUSHSTYLE_STIPPLE); } else if(wxIsHatchPenStyle(m_sourceInfo.GetStyle())) diff --git a/src/msw/pen.cpp b/src/msw/pen.cpp index 1992349f24..cf8af1986b 100644 --- a/src/msw/pen.cpp +++ b/src/msw/pen.cpp @@ -425,7 +425,7 @@ wxPen::wxPen(const wxPenInfo& info) M_PENDATA->SetCap(info.GetCap()); wxDash *dash; int nb_dashes = info.GetDashes(&dash); - M_PENDATA->SetDashes(nb_dashes, dash) + M_PENDATA->SetDashes(nb_dashes, dash); } bool wxPen::operator==(const wxPen& pen) const diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index d5f625a189..3d7fcd9b6e 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -309,7 +309,7 @@ public: ~wxMacCoreGraphicsPenData(); void Init(); - void InitFromPenInfo( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ); + void InitFromPenInfo( const wxGraphicsPenInfo& info ); virtual void Apply( wxGraphicsContext* context ); virtual wxDouble GetWidth() { return m_width; } @@ -334,34 +334,22 @@ protected : wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) : wxGraphicsObjectRefData( renderer ) { - wxDash *dashes; - int nb_dashes = pen.GetDashes(&dashes); - InitFromPenInfo(renderer, wxGraphicsPenInfo() - .Colour(pen.GetColour()) - .Width(pen.GetWidth()) - .Style(pen.GetStyle()) - .Stipple(*pen.GetStipple()) - .Dashes(nb_dashes, dashes) - .Join(pen.GetJoin()) - .Cap(pen.GetCap()) - ); + InitFromPenInfo(wxGraphicsPenInfo::CreateFromPen(pen)); } wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ) : wxGraphicsObjectRefData( renderer ) { - InitFromPenInfo(renderer, info); + InitFromPenInfo(info); } -void wxMacCoreGraphicsPenData::InitFromPenInfo( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ) +void wxMacCoreGraphicsPenData::InitFromPenInfo( const wxGraphicsPenInfo& info ) { Init(); m_color.reset( wxMacCreateCGColor( info.GetColour() ) ) ; // TODO: * m_dc->m_scaleX - m_width = info.GetWidthF(); - if (m_width < 0.0) m_width = info.GetWidth(); if (m_width <= 0.0) m_width = (CGFloat) 0.1; @@ -459,11 +447,11 @@ void wxMacCoreGraphicsPenData::InitFromPenInfo( wxGraphicsRenderer* renderer, co case wxPENSTYLE_STIPPLE: { - wxBitmap* bmp = info.GetStipple(); - if ( bmp && bmp->IsOk() ) + wxBitmap bmp = info.GetStipple(); + if ( bmp.IsOk() ) { m_colorSpace.reset( CGColorSpaceCreatePattern( NULL ) ); - m_pattern.reset( (CGPatternRef) *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) ); + m_pattern.reset( (CGPatternRef) *( new ImagePattern( &bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) ); m_patternColorComponents = new CGFloat[1] ; m_patternColorComponents[0] = (CGFloat) 1.0; m_isPattern = true; @@ -2746,7 +2734,7 @@ wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen) wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxGraphicsPenInfo& info) { - if ( info.GetStyle() == wxPENSTYLE_TRANSPARENT ) + if ( info.IsTransparent() ) return wxNullGraphicsPen; else { diff --git a/src/osx/pen.cpp b/src/osx/pen.cpp index b1b0ce79d7..f6ea0a489d 100644 --- a/src/osx/pen.cpp +++ b/src/osx/pen.cpp @@ -150,9 +150,7 @@ wxPen::wxPen(const wxPenInfo& info) M_PENDATA->m_style = info.GetStyle(); M_PENDATA->m_join = info.GetJoin(); M_PENDATA->m_cap = info.GetCap(); - wxDash *dashes; - M_PENDATA->m_nbDash = info.GetDashes(&dashes); - M_PENDATA->m_dash = dashes; + M_PENDATA->m_nbDash = info.GetDashes(&M_PENDATA->m_dash); RealizeResource(); } diff --git a/src/x11/pen.cpp b/src/x11/pen.cpp index 9c1699d97d..9960466228 100644 --- a/src/x11/pen.cpp +++ b/src/x11/pen.cpp @@ -102,9 +102,7 @@ wxPen::wxPen(const wxPenInfo& info) M_PENDATA->m_capStyle = info.GetCap(); M_PENDATA->m_joinStyle = info.GetJoin(); M_PENDATA->m_stipple = info.GetStipple(); - wxDash *dashes; - M_PENDATA->m_countDashes = info.GetDashes(&dashes); - M_PENDATA->m_dash = dashes; + M_PENDATA->m_countDashes = info.GetDashes(&M_PENDATA->m_dash); } wxPen::~wxPen() From cc91a7d6d4a0507fc6baca62645e95e09bf53fd9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Sep 2017 01:09:46 +0200 Subject: [PATCH 04/10] Minor formatting and style changes in wxPenInfo code Use more standard formatting, wrap some overlong lines. --- include/wx/graphics.h | 6 ++++-- include/wx/pen.h | 42 +++++++++++++++++++++++-------------- src/dfb/pen.cpp | 5 +---- src/generic/graphicc.cpp | 2 +- src/msw/graphics.cpp | 5 +++-- src/msw/graphicsd2d.cpp | 40 ++++++++++++++++++----------------- src/osx/carbon/graphics.cpp | 7 ++++--- 7 files changed, 60 insertions(+), 47 deletions(-) diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 0c823f4cb5..0e340df715 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -140,8 +140,10 @@ protected: class wxGraphicsPenInfo : public wxPenInfoBase { public: - explicit wxGraphicsPenInfo(const wxColour& colour = wxColour(), wxDouble width = 1.0, wxPenStyle style = wxPENSTYLE_SOLID) - : wxPenInfoBase(colour, style) + explicit wxGraphicsPenInfo(const wxColour& colour = wxColour(), + wxDouble width = 1.0, + wxPenStyle style = wxPENSTYLE_SOLID) + : wxPenInfoBase(colour, style) { m_width = width; } diff --git a/include/wx/pen.h b/include/wx/pen.h index 4a11734c7a..13a9252304 100644 --- a/include/wx/pen.h +++ b/include/wx/pen.h @@ -61,9 +61,10 @@ enum wxPenCap }; // ---------------------------------------------------------------------------- -// wxPenInfo describes a wxPen +// wxPenInfoBase is a common base for wxPenInfo and wxGraphicsPenInfo // ---------------------------------------------------------------------------- +// This class uses CRTP, the template parameter is the derived class itself. template class wxPenInfoBase { @@ -82,21 +83,21 @@ public: // Setters for the various attributes. All of them return the object itself // so that the calls to them could be chained. - T &Colour(const wxColour& colour) - { m_colour = colour; return static_cast(*this); } + T& Colour(const wxColour& colour) + { m_colour = colour; return This(); } - T &Style(wxPenStyle style) - { m_style = style; return static_cast(*this); } - T &Stipple(const wxBitmap& stipple) - { m_stipple = stipple; m_style = wxPENSTYLE_STIPPLE; return static_cast(*this); } - T &Dashes(int nb_dashes, const wxDash *dash) - { m_nb_dashes = nb_dashes; m_dash = (wxDash *)dash; return static_cast(*this); } - T &Join(wxPenJoin join) - { m_join = join; return static_cast(*this); } - T &Cap(wxPenCap cap) - { m_cap = cap; return static_cast(*this); } + T& Style(wxPenStyle style) + { m_style = style; return This(); } + T& Stipple(const wxBitmap& stipple) + { m_stipple = stipple; m_style = wxPENSTYLE_STIPPLE; return This(); } + T& Dashes(int nb_dashes, const wxDash *dash) + { m_nb_dashes = nb_dashes; m_dash = (wxDash *)dash; return This(); } + T& Join(wxPenJoin join) + { m_join = join; return This(); } + T& Cap(wxPenCap cap) + { m_cap = cap; return This(); } - // Accessors are mostly meant to be used by wxPen itself. + // Accessors are mostly meant to be used by wxWidgets itself. wxColour GetColour() const { return m_colour; } wxBitmap GetStipple() const { return m_stipple; } @@ -113,6 +114,9 @@ public: bool IsTransparent() const { return m_style == wxPENSTYLE_TRANSPARENT; } private: + // Helper to return this object itself cast to its real type T. + T& This() { return static_cast(*this); } + wxColour m_colour; wxBitmap m_stipple; wxPenStyle m_style; @@ -123,11 +127,17 @@ private: wxDash* m_dash; }; +// ---------------------------------------------------------------------------- +// wxPenInfo contains all parameters describing a wxPen +// ---------------------------------------------------------------------------- + class wxPenInfo : public wxPenInfoBase { public: - explicit wxPenInfo(const wxColour& colour = wxColour(), int width = 1, wxPenStyle style = wxPENSTYLE_SOLID) - : wxPenInfoBase(colour, style) + explicit wxPenInfo(const wxColour& colour = wxColour(), + int width = 1, + wxPenStyle style = wxPENSTYLE_SOLID) + : wxPenInfoBase(colour, style) { m_width = width; } diff --git a/src/dfb/pen.cpp b/src/dfb/pen.cpp index 4b6d2f824a..02f290da02 100644 --- a/src/dfb/pen.cpp +++ b/src/dfb/pen.cpp @@ -81,10 +81,7 @@ wxPen::wxPen(const wxBitmap& WXUNUSED(stipple), int WXUNUSED(width)) wxPen::wxPen(const wxPenInfo& info) { - m_refData = new wxPenRefData( - info.GetColour(), - info.GetStyle() - ); + m_refData = new wxPenRefData(info.GetColour(), info.GetStyle()); } bool wxPen::operator==(const wxPen& pen) const diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index b24147bb4e..4afacfc251 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -750,7 +750,7 @@ wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxGraphicsPe void wxCairoPenData::InitFromPenInfo( const wxGraphicsPenInfo &info ) { Init(); - m_width = info.GetWidth(); + m_width = info.GetWidth(); if (m_width <= 0.0) m_width = 0.1; diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index 2a084f4def..2da964da7c 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -653,8 +653,9 @@ wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &p InitFromPenInfo(wxGraphicsPenInfo::CreateFromPen(pen)); } -wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ) -: wxGraphicsObjectRefData(renderer) +wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, + const wxGraphicsPenInfo &info ) + : wxGraphicsObjectRefData(renderer) { InitFromPenInfo(info); } diff --git a/src/msw/graphicsd2d.cpp b/src/msw/graphicsd2d.cpp index e402644cfe..b30b222cde 100644 --- a/src/msw/graphicsd2d.cpp +++ b/src/msw/graphicsd2d.cpp @@ -2478,9 +2478,9 @@ public: } private: - // We store the source info for later when we need to recreate the - // device-dependent resources. - const wxGraphicsPenInfo m_sourceInfo; + // We store the original pen description for later when we need to recreate + // the device-dependent resources. + const wxGraphicsPenInfo m_penInfo; // A stroke style is a device-independent resource. // Describes the caps, miter limit, line join, and dash information. @@ -2500,10 +2500,10 @@ private: wxD2DPenData::wxD2DPenData( wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, - const wxPen& pen) : - wxGraphicsObjectRefData(renderer), - m_sourceInfo(wxGraphicsPenInfo::CreateFromPen(pen)), - m_width(pen.GetWidth()) + const wxPen& pen) + : wxGraphicsObjectRefData(renderer), + m_penInfo(wxGraphicsPenInfo::CreateFromPen(pen)), + m_width(pen.GetWidth()) { Init(renderer, direct2dFactory); } @@ -2512,7 +2512,9 @@ wxD2DPenData::wxD2DPenData( wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, const wxGraphicsPenInfo& info) - : wxGraphicsObjectRefData(renderer), m_sourceInfo(info), m_width(info.GetWidth()) + : wxGraphicsObjectRefData(renderer), + m_penInfo(info), + m_width(info.GetWidth()) { Init(renderer, direct2dFactory); } @@ -2525,19 +2527,19 @@ void wxD2DPenData::Init( wxBrush strokeBrush; - if (m_sourceInfo.GetStyle() == wxPENSTYLE_STIPPLE) + if (m_penInfo.GetStyle() == wxPENSTYLE_STIPPLE) { - strokeBrush.SetStipple(m_sourceInfo.GetStipple()); + strokeBrush.SetStipple(m_penInfo.GetStipple()); strokeBrush.SetStyle(wxBRUSHSTYLE_STIPPLE); } - else if(wxIsHatchPenStyle(m_sourceInfo.GetStyle())) + else if(wxIsHatchPenStyle(m_penInfo.GetStyle())) { - strokeBrush.SetStyle(wxConvertPenStyleToBrushStyle(m_sourceInfo.GetStyle())); - strokeBrush.SetColour(m_sourceInfo.GetColour()); + strokeBrush.SetStyle(wxConvertPenStyleToBrushStyle(m_penInfo.GetStyle())); + strokeBrush.SetColour(m_penInfo.GetColour()); } else { - strokeBrush.SetColour(m_sourceInfo.GetColour()); + strokeBrush.SetColour(m_penInfo.GetColour()); strokeBrush.SetStyle(wxBRUSHSTYLE_SOLID); } @@ -2546,21 +2548,21 @@ void wxD2DPenData::Init( void wxD2DPenData::CreateStrokeStyle(ID2D1Factory* const direct2dfactory) { - D2D1_CAP_STYLE capStyle = wxD2DConvertPenCap(m_sourceInfo.GetCap()); - D2D1_LINE_JOIN lineJoin = wxD2DConvertPenJoin(m_sourceInfo.GetJoin()); - D2D1_DASH_STYLE dashStyle = wxD2DConvertPenStyle(m_sourceInfo.GetStyle()); + D2D1_CAP_STYLE capStyle = wxD2DConvertPenCap(m_penInfo.GetCap()); + D2D1_LINE_JOIN lineJoin = wxD2DConvertPenJoin(m_penInfo.GetJoin()); + D2D1_DASH_STYLE dashStyle = wxD2DConvertPenStyle(m_penInfo.GetStyle()); int dashCount = 0; FLOAT* dashes = NULL; if (dashStyle == D2D1_DASH_STYLE_CUSTOM) { - dashCount = m_sourceInfo.GetDashCount(); + dashCount = m_penInfo.GetDashCount(); dashes = new FLOAT[dashCount]; for (int i = 0; i < dashCount; ++i) { - dashes[i] = m_sourceInfo.GetDash()[i]; + dashes[i] = m_penInfo.GetDash()[i]; } } diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index 3d7fcd9b6e..eaba7e9b22 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -337,8 +337,9 @@ wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer InitFromPenInfo(wxGraphicsPenInfo::CreateFromPen(pen)); } -wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ) : - wxGraphicsObjectRefData( renderer ) +wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, + const wxGraphicsPenInfo& info ) + : wxGraphicsObjectRefData( renderer ) { InitFromPenInfo(info); } @@ -350,7 +351,7 @@ void wxMacCoreGraphicsPenData::InitFromPenInfo( const wxGraphicsPenInfo& info ) m_color.reset( wxMacCreateCGColor( info.GetColour() ) ) ; // TODO: * m_dc->m_scaleX - m_width = info.GetWidth(); + m_width = info.GetWidth(); if (m_width <= 0.0) m_width = (CGFloat) 0.1; From cc14a80501a42c9fa510773cc06b15f4a0c7602f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Sep 2017 01:11:25 +0200 Subject: [PATCH 05/10] Make wxPenInfoBase constructor protected This class is never supposed to be created directly. --- include/wx/pen.h | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/include/wx/pen.h b/include/wx/pen.h index 13a9252304..a1c59130e8 100644 --- a/include/wx/pen.h +++ b/include/wx/pen.h @@ -69,17 +69,6 @@ template class wxPenInfoBase { public: - wxPenInfoBase(const wxColour& colour, wxPenStyle style) - { - m_nb_dashes = 0; - m_dash = NULL; - m_join = wxJOIN_ROUND; - m_cap = wxCAP_ROUND; - - m_colour = colour; - m_style = style; - } - // Setters for the various attributes. All of them return the object itself // so that the calls to them could be chained. @@ -113,6 +102,18 @@ public: bool IsTransparent() const { return m_style == wxPENSTYLE_TRANSPARENT; } +protected: + wxPenInfoBase(const wxColour& colour, wxPenStyle style) + { + m_nb_dashes = 0; + m_dash = NULL; + m_join = wxJOIN_ROUND; + m_cap = wxCAP_ROUND; + + m_colour = colour; + m_style = style; + } + private: // Helper to return this object itself cast to its real type T. T& This() { return static_cast(*this); } From a156d5fa40679b32a2ad0e4a55666ab28b421c9c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Sep 2017 01:20:22 +0200 Subject: [PATCH 06/10] Extract wxPenInfoBase in a separate header This allows to avoid dependency of wx/graphics.h on wx/pen.h. --- Makefile.in | 1 + build/bakefiles/files.bkl | 1 + build/files | 1 + build/msw/wx_core.vcxproj | 1 + build/msw/wx_core.vcxproj.filters | 3 + build/msw/wx_vc7.sln | 1 - build/msw/wx_vc7_core.vcproj | 3 + build/msw/wx_vc8.sln | 1 - build/msw/wx_vc8_core.vcproj | 4 + build/msw/wx_vc9.sln | 1 - build/msw/wx_vc9_core.vcproj | 4 + include/wx/graphics.h | 20 ++--- include/wx/pen.h | 116 +------------------------- include/wx/peninfobase.h | 130 ++++++++++++++++++++++++++++++ src/common/graphcmn.cpp | 16 ++++ 15 files changed, 170 insertions(+), 133 deletions(-) create mode 100644 include/wx/peninfobase.h diff --git a/Makefile.in b/Makefile.in index 0199d265ce..cb53fe1902 100644 --- a/Makefile.in +++ b/Makefile.in @@ -3908,6 +3908,7 @@ COND_USE_GUI_1_ALL_GUI_HEADERS = \ wx/collheaderctrl.h \ wx/generic/collheaderctrl.h \ wx/itemattr.h \ + wx/peninfobase.h \ $(LOWLEVEL_HDR) \ $(GUI_CORE_HEADERS) \ $(ADVANCED_HDR) \ diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl index 630090e8b8..d163adb16f 100644 --- a/build/bakefiles/files.bkl +++ b/build/bakefiles/files.bkl @@ -1188,6 +1188,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! wx/collheaderctrl.h wx/generic/collheaderctrl.h wx/itemattr.h + wx/peninfobase.h diff --git a/build/files b/build/files index d5f864909d..bd58f84fcd 100644 --- a/build/files +++ b/build/files @@ -946,6 +946,7 @@ GUI_CMN_HDR = wx/palette.h wx/panel.h wx/pen.h + wx/peninfobase.h wx/position.h wx/preferences.h wx/radiobox.h diff --git a/build/msw/wx_core.vcxproj b/build/msw/wx_core.vcxproj index 7c52eeda3a..d530592aae 100644 --- a/build/msw/wx_core.vcxproj +++ b/build/msw/wx_core.vcxproj @@ -1382,6 +1382,7 @@ + diff --git a/build/msw/wx_core.vcxproj.filters b/build/msw/wx_core.vcxproj.filters index d4418f6617..eecc9366e6 100644 --- a/build/msw/wx_core.vcxproj.filters +++ b/build/msw/wx_core.vcxproj.filters @@ -1714,6 +1714,9 @@ Common Headers + + Common Headers + Common Headers diff --git a/build/msw/wx_vc7.sln b/build/msw/wx_vc7.sln index 4a7988c948..d1554b6b3c 100644 --- a/build/msw/wx_vc7.sln +++ b/build/msw/wx_vc7.sln @@ -1,4 +1,3 @@ - Microsoft Visual Studio Solution File, Format Version 8.00 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wxregex", "wx_vc7_wxregex.vcproj", "{7A1A5354-6DB4-53F1-B75C-FE909D796167}" EndProject diff --git a/build/msw/wx_vc7_core.vcproj b/build/msw/wx_vc7_core.vcproj index 9b9fd90ae9..40acebe351 100644 --- a/build/msw/wx_vc7_core.vcproj +++ b/build/msw/wx_vc7_core.vcproj @@ -2393,6 +2393,9 @@ + + diff --git a/build/msw/wx_vc8.sln b/build/msw/wx_vc8.sln index 052e8057e8..c5337121df 100644 --- a/build/msw/wx_vc8.sln +++ b/build/msw/wx_vc8.sln @@ -1,4 +1,3 @@ - Microsoft Visual Studio Solution File, Format Version 9.00 # Visual Studio 2005 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wxregex", "wx_vc8_wxregex.vcproj", "{078F4E39-D258-54B5-B1B1-4905D10E06DC}" diff --git a/build/msw/wx_vc8_core.vcproj b/build/msw/wx_vc8_core.vcproj index c8d45bf837..d874619c11 100644 --- a/build/msw/wx_vc8_core.vcproj +++ b/build/msw/wx_vc8_core.vcproj @@ -3692,6 +3692,10 @@ RelativePath="..\..\include\wx\pen.h" > + + diff --git a/build/msw/wx_vc9.sln b/build/msw/wx_vc9.sln index d5cda2e8b8..56f929fe3b 100644 --- a/build/msw/wx_vc9.sln +++ b/build/msw/wx_vc9.sln @@ -1,4 +1,3 @@ - Microsoft Visual Studio Solution File, Format Version 10.00 # Visual Studio 2008 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wxregex", "wx_vc9_wxregex.vcproj", "{56A4B526-BB81-5D01-AAA9-16D23BBB169D}" diff --git a/build/msw/wx_vc9_core.vcproj b/build/msw/wx_vc9_core.vcproj index e81eaeafc6..362cc6a67f 100644 --- a/build/msw/wx_vc9_core.vcproj +++ b/build/msw/wx_vc9_core.vcproj @@ -3688,6 +3688,10 @@ RelativePath="..\..\include\wx\pen.h" > + + diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 0e340df715..f9d7f33db2 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -21,8 +21,8 @@ #include "wx/dynarray.h" #include "wx/font.h" #include "wx/image.h" +#include "wx/peninfobase.h" #include "wx/vector.h" -#include "wx/pen.h" enum wxAntialiasMode { @@ -148,20 +148,6 @@ public: m_width = width; } - static wxGraphicsPenInfo CreateFromPen(const wxPen& pen) - { - wxDash *dashes; - int nb_dashes = pen.GetDashes(&dashes); - return wxGraphicsPenInfo() - .Colour(pen.GetColour()) - .Width(pen.GetWidth()) - .Style(pen.GetStyle()) - .Stipple(*pen.GetStipple()) - .Dashes(nb_dashes, dashes) - .Join(pen.GetJoin()) - .Cap(pen.GetCap()); - } - // Setters wxGraphicsPenInfo& Width(wxDouble width) @@ -171,6 +157,10 @@ public: wxDouble GetWidth() const { return m_width; } + // This is a helper used by wxWidgets itself, it doesn't make much sense to + // use it outside of the library. + static wxGraphicsPenInfo CreateFromPen(const wxPen& pen); + private: wxDouble m_width; }; diff --git a/include/wx/pen.h b/include/wx/pen.h index a1c59130e8..9ebddc3b6d 100644 --- a/include/wx/pen.h +++ b/include/wx/pen.h @@ -11,122 +11,8 @@ #ifndef _WX_PEN_H_BASE_ #define _WX_PEN_H_BASE_ -#include "wx/bitmap.h" #include "wx/gdiobj.h" -#include "wx/gdicmn.h" - -enum wxPenStyle -{ - wxPENSTYLE_INVALID = -1, - - wxPENSTYLE_SOLID = wxSOLID, - wxPENSTYLE_DOT = wxDOT, - wxPENSTYLE_LONG_DASH = wxLONG_DASH, - wxPENSTYLE_SHORT_DASH = wxSHORT_DASH, - wxPENSTYLE_DOT_DASH = wxDOT_DASH, - wxPENSTYLE_USER_DASH = wxUSER_DASH, - - wxPENSTYLE_TRANSPARENT = wxTRANSPARENT, - - wxPENSTYLE_STIPPLE_MASK_OPAQUE = wxSTIPPLE_MASK_OPAQUE, - wxPENSTYLE_STIPPLE_MASK = wxSTIPPLE_MASK, - wxPENSTYLE_STIPPLE = wxSTIPPLE, - - wxPENSTYLE_BDIAGONAL_HATCH = wxHATCHSTYLE_BDIAGONAL, - wxPENSTYLE_CROSSDIAG_HATCH = wxHATCHSTYLE_CROSSDIAG, - wxPENSTYLE_FDIAGONAL_HATCH = wxHATCHSTYLE_FDIAGONAL, - wxPENSTYLE_CROSS_HATCH = wxHATCHSTYLE_CROSS, - wxPENSTYLE_HORIZONTAL_HATCH = wxHATCHSTYLE_HORIZONTAL, - wxPENSTYLE_VERTICAL_HATCH = wxHATCHSTYLE_VERTICAL, - wxPENSTYLE_FIRST_HATCH = wxHATCHSTYLE_FIRST, - wxPENSTYLE_LAST_HATCH = wxHATCHSTYLE_LAST -}; - -enum wxPenJoin -{ - wxJOIN_INVALID = -1, - - wxJOIN_BEVEL = 120, - wxJOIN_MITER, - wxJOIN_ROUND -}; - -enum wxPenCap -{ - wxCAP_INVALID = -1, - - wxCAP_ROUND = 130, - wxCAP_PROJECTING, - wxCAP_BUTT -}; - -// ---------------------------------------------------------------------------- -// wxPenInfoBase is a common base for wxPenInfo and wxGraphicsPenInfo -// ---------------------------------------------------------------------------- - -// This class uses CRTP, the template parameter is the derived class itself. -template -class wxPenInfoBase -{ -public: - // Setters for the various attributes. All of them return the object itself - // so that the calls to them could be chained. - - T& Colour(const wxColour& colour) - { m_colour = colour; return This(); } - - T& Style(wxPenStyle style) - { m_style = style; return This(); } - T& Stipple(const wxBitmap& stipple) - { m_stipple = stipple; m_style = wxPENSTYLE_STIPPLE; return This(); } - T& Dashes(int nb_dashes, const wxDash *dash) - { m_nb_dashes = nb_dashes; m_dash = (wxDash *)dash; return This(); } - T& Join(wxPenJoin join) - { m_join = join; return This(); } - T& Cap(wxPenCap cap) - { m_cap = cap; return This(); } - - // Accessors are mostly meant to be used by wxWidgets itself. - - wxColour GetColour() const { return m_colour; } - wxBitmap GetStipple() const { return m_stipple; } - wxPenStyle GetStyle() const { return m_style; } - wxPenJoin GetJoin() const { return m_join; } - wxPenCap GetCap() const { return m_cap; } - int GetDashes(wxDash **ptr) const { *ptr = m_dash; return m_nb_dashes; } - - int GetDashCount() const { return m_nb_dashes; } - wxDash* GetDash() const { return m_dash; } - - // Convenience - - bool IsTransparent() const { return m_style == wxPENSTYLE_TRANSPARENT; } - -protected: - wxPenInfoBase(const wxColour& colour, wxPenStyle style) - { - m_nb_dashes = 0; - m_dash = NULL; - m_join = wxJOIN_ROUND; - m_cap = wxCAP_ROUND; - - m_colour = colour; - m_style = style; - } - -private: - // Helper to return this object itself cast to its real type T. - T& This() { return static_cast(*this); } - - wxColour m_colour; - wxBitmap m_stipple; - wxPenStyle m_style; - wxPenJoin m_join; - wxPenCap m_cap; - - int m_nb_dashes; - wxDash* m_dash; -}; +#include "wx/peninfobase.h" // ---------------------------------------------------------------------------- // wxPenInfo contains all parameters describing a wxPen diff --git a/include/wx/peninfobase.h b/include/wx/peninfobase.h new file mode 100644 index 0000000000..33a0c40674 --- /dev/null +++ b/include/wx/peninfobase.h @@ -0,0 +1,130 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: wx/peninfobase.h +// Purpose: Declaration of wxPenInfoBase class and related constants +// Author: Adrien Tétar, Vadim Zeitlin +// Created: 2017-09-10 +// Copyright: (c) 2017 Vadim Zeitlin +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_PENINFOBASE_H_ +#define _WX_PENINFOBASE_H_ + +#include "wx/bitmap.h" +#include "wx/colour.h" +#include "wx/gdicmn.h" // for wxDash + +enum wxPenStyle +{ + wxPENSTYLE_INVALID = -1, + + wxPENSTYLE_SOLID = wxSOLID, + wxPENSTYLE_DOT = wxDOT, + wxPENSTYLE_LONG_DASH = wxLONG_DASH, + wxPENSTYLE_SHORT_DASH = wxSHORT_DASH, + wxPENSTYLE_DOT_DASH = wxDOT_DASH, + wxPENSTYLE_USER_DASH = wxUSER_DASH, + + wxPENSTYLE_TRANSPARENT = wxTRANSPARENT, + + wxPENSTYLE_STIPPLE_MASK_OPAQUE = wxSTIPPLE_MASK_OPAQUE, + wxPENSTYLE_STIPPLE_MASK = wxSTIPPLE_MASK, + wxPENSTYLE_STIPPLE = wxSTIPPLE, + + wxPENSTYLE_BDIAGONAL_HATCH = wxHATCHSTYLE_BDIAGONAL, + wxPENSTYLE_CROSSDIAG_HATCH = wxHATCHSTYLE_CROSSDIAG, + wxPENSTYLE_FDIAGONAL_HATCH = wxHATCHSTYLE_FDIAGONAL, + wxPENSTYLE_CROSS_HATCH = wxHATCHSTYLE_CROSS, + wxPENSTYLE_HORIZONTAL_HATCH = wxHATCHSTYLE_HORIZONTAL, + wxPENSTYLE_VERTICAL_HATCH = wxHATCHSTYLE_VERTICAL, + wxPENSTYLE_FIRST_HATCH = wxHATCHSTYLE_FIRST, + wxPENSTYLE_LAST_HATCH = wxHATCHSTYLE_LAST +}; + +enum wxPenJoin +{ + wxJOIN_INVALID = -1, + + wxJOIN_BEVEL = 120, + wxJOIN_MITER, + wxJOIN_ROUND +}; + +enum wxPenCap +{ + wxCAP_INVALID = -1, + + wxCAP_ROUND = 130, + wxCAP_PROJECTING, + wxCAP_BUTT +}; + +// ---------------------------------------------------------------------------- +// wxPenInfoBase is a common base for wxPenInfo and wxGraphicsPenInfo +// ---------------------------------------------------------------------------- + +// This class uses CRTP, the template parameter is the derived class itself. +template +class wxPenInfoBase +{ +public: + // Setters for the various attributes. All of them return the object itself + // so that the calls to them could be chained. + + T& Colour(const wxColour& colour) + { m_colour = colour; return This(); } + + T& Style(wxPenStyle style) + { m_style = style; return This(); } + T& Stipple(const wxBitmap& stipple) + { m_stipple = stipple; m_style = wxPENSTYLE_STIPPLE; return This(); } + T& Dashes(int nb_dashes, const wxDash *dash) + { m_nb_dashes = nb_dashes; m_dash = (wxDash *)dash; return This(); } + T& Join(wxPenJoin join) + { m_join = join; return This(); } + T& Cap(wxPenCap cap) + { m_cap = cap; return This(); } + + // Accessors are mostly meant to be used by wxWidgets itself. + + wxColour GetColour() const { return m_colour; } + wxBitmap GetStipple() const { return m_stipple; } + wxPenStyle GetStyle() const { return m_style; } + wxPenJoin GetJoin() const { return m_join; } + wxPenCap GetCap() const { return m_cap; } + int GetDashes(wxDash **ptr) const { *ptr = m_dash; return m_nb_dashes; } + + int GetDashCount() const { return m_nb_dashes; } + wxDash* GetDash() const { return m_dash; } + + // Convenience + + bool IsTransparent() const { return m_style == wxPENSTYLE_TRANSPARENT; } + +protected: + wxPenInfoBase(const wxColour& colour, wxPenStyle style) + { + m_nb_dashes = 0; + m_dash = NULL; + m_join = wxJOIN_ROUND; + m_cap = wxCAP_ROUND; + + m_colour = colour; + m_style = style; + } + +private: + // Helper to return this object itself cast to its real type T. + T& This() { return static_cast(*this); } + + wxColour m_colour; + wxBitmap m_stipple; + wxPenStyle m_style; + wxPenJoin m_join; + wxPenCap m_cap; + + int m_nb_dashes; + wxDash* m_dash; +}; + +#endif // _WX_PENINFOBASE_H_ diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index 5f39c47e07..30d136b5ca 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -26,6 +26,7 @@ #include "wx/dcmemory.h" #include "wx/dcprint.h" #include "wx/math.h" + #include "wx/pen.h" #include "wx/region.h" #include "wx/log.h" #endif @@ -116,6 +117,21 @@ WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush; WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont; WXDLLIMPEXP_DATA_CORE(wxGraphicsBitmap) wxNullGraphicsBitmap; +/* static */ +wxGraphicsPenInfo wxGraphicsPenInfo::CreateFromPen(const wxPen& pen) +{ + wxDash *dashes; + int nb_dashes = pen.GetDashes(&dashes); + return wxGraphicsPenInfo() + .Colour(pen.GetColour()) + .Width(pen.GetWidth()) + .Style(pen.GetStyle()) + .Stipple(*pen.GetStipple()) + .Dashes(nb_dashes, dashes) + .Join(pen.GetJoin()) + .Cap(pen.GetCap()); +} + //----------------------------------------------------------------------------- // matrix //----------------------------------------------------------------------------- From af3581758b11d269198aaaf3367ecfd5b6c2520d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Sep 2017 01:28:38 +0200 Subject: [PATCH 07/10] Don't mention wxPenInfoBase in the documentation This is an implementation detail, don't confuse the user with this template class which isn't supposed to be used in the user code. Also improve the example in the documentation, the old one (using pen of width 0 but with a colour) didn't make much sense. --- interface/wx/graphics.h | 15 ++++++++------- interface/wx/pen.h | 12 +++++++----- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/interface/wx/graphics.h b/interface/wx/graphics.h index fdc263c4ac..cec978d8e4 100644 --- a/interface/wx/graphics.h +++ b/interface/wx/graphics.h @@ -1541,22 +1541,23 @@ public: calls to its clearly named methods instead of passing them in the fixed order to wxGraphicsPen constructors. - Typically you would use wxGraphicsPenInfo with a wxGraphicsContext: + Typically you would use wxGraphicsPenInfo with a wxGraphicsContext, e.g. to + start drawing with a dotted blue pen slightly wider than normal you could + write the following: @code wxGraphicsContext ctx = wxGraphicsContext::Create(dc); - ctx.SetPen(wxGraphicsPenInfo(*wxBLUE, 1.25)); + ctx.SetPen(wxGraphicsPenInfo(*wxBLUE).Width(1.25).Style(wxPENSTYLE_DOT)); @endcode @since 3.1.1 */ -class wxGraphicsPenInfo : public wxPenInfoBase +class wxGraphicsPenInfo { public: - - explicit wxGraphicsPenInfo(const wxColour& colour = wxColour(), wxDouble width = 1.0, wxPenStyle style = wxPENSTYLE_SOLID); - - static wxGraphicsPenInfo CreateFromPen(const wxPen& pen); + explicit wxGraphicsPenInfo(const wxColour& colour = wxColour(), + wxDouble width = 1.0, + wxPenStyle style = wxPENSTYLE_SOLID); wxGraphicsPenInfo& Colour(const wxColour& col); diff --git a/interface/wx/pen.h b/interface/wx/pen.h index a9ecddb59c..04000e478f 100644 --- a/interface/wx/pen.h +++ b/interface/wx/pen.h @@ -109,18 +109,20 @@ enum wxPenCap calls to its clearly named methods instead of passing them in the fixed order to wxPen constructors. - For instance, to create a blue pen with a width of 0: + For instance, to create a dotted blue pen with the given join style you + could do @code - wxPen pen(wxPenInfo(*wxBLUE, 0)); + wxPen pen(wxPenInfo(*wxBLUE).Style(wxPENSTYLE_DOT).Join(wxJOIN_BEVEL)); @endcode @since 3.1.1 */ -class wxPenInfo : public wxPenInfoBase +class wxPenInfo { public: - - explicit wxPenInfo(const wxColour& colour = wxColour(), int width = 1, wxPenStyle style = wxPENSTYLE_SOLID); + explicit wxPenInfo(const wxColour& colour = wxColour(), + int width = 1, + wxPenStyle style = wxPENSTYLE_SOLID); wxPenInfo& Colour(const wxColour& col); From 76fd05b147d68bc306b4cc2c3430e0a933e88ab6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Sep 2017 01:48:30 +0200 Subject: [PATCH 08/10] Leave only wxGraphicsRenderer::CreatePen(wxGraphicsPenInfo) overload It doesn't make much sense to require all the graphics backends to create wxGraphicsPen from either wxPen or wxGraphicsPenInfo when the former can be handled just once in the common code. So do just this, leaving CreatePen() overload taking wxGraphicsPenInfo where the real pen construction takes place and implementing wxGraphicsPen creation from wxPen in the common wxGraphicsContext code. This is not 100% backwards-compatible as any code inheriting from wxGraphicsRenderer and overriding its CreatePen() will now be broken, however this should be extremely rare (there is no good reason to inherit from this class in the user code) and result in compile errors if it does happen. --- docs/changes.txt | 4 ++++ include/wx/graphics.h | 10 +++++---- interface/wx/graphics.h | 16 ++++++++++---- src/common/graphcmn.cpp | 10 ++++----- src/generic/graphicc.cpp | 25 --------------------- src/msw/graphics.cpp | 28 ------------------------ src/msw/graphicsd2d.cpp | 43 +++---------------------------------- src/osx/carbon/graphics.cpp | 27 ----------------------- 8 files changed, 29 insertions(+), 134 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index ddf132d01f..2ace518e7e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -58,6 +58,10 @@ Changes in behaviour which may result in build errors - Never documented and not always available private wxGetClipboardData() function now doesn't exist at all any more in wxMSW, use wxClipboard instead. +- wxGraphicsRenderer::CreatePen() now takes wxGraphicsPenInfo and not a wxPen. + This only affects code defining its own custom renderers, code just using + wxGraphicsContext::CreatePen() continues to compile and work as before. + 3.1.1: (not released yet) ---------------------------- diff --git a/include/wx/graphics.h b/include/wx/graphics.h index f9d7f33db2..0374f62885 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -512,9 +512,11 @@ public: wxGraphicsPath CreatePath() const; - virtual wxGraphicsPen CreatePen(const wxPen& pen) const; + wxGraphicsPen CreatePen(const wxPen& pen) const + { return DoCreatePen(wxGraphicsPenInfo::CreateFromPen(pen)); } - virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& pen) const; + wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) const + { return DoCreatePen(info); } virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) const; @@ -779,6 +781,8 @@ protected: // implementations of overloaded public functions: we use different names // for them to avoid the virtual function hiding problems in the derived // classes + virtual wxGraphicsPen DoCreatePen(const wxGraphicsPenInfo& info) const; + virtual void DoDrawText(const wxString& str, wxDouble x, wxDouble y) = 0; virtual void DoDrawRotatedText(const wxString& str, wxDouble x, wxDouble y, wxDouble angle); @@ -896,8 +900,6 @@ public: // Paints - virtual wxGraphicsPen CreatePen(const wxPen& pen) = 0; - virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) = 0; virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) = 0; diff --git a/interface/wx/graphics.h b/interface/wx/graphics.h index cec978d8e4..84ab4dd929 100644 --- a/interface/wx/graphics.h +++ b/interface/wx/graphics.h @@ -677,13 +677,19 @@ public: /** Creates a native pen from a wxPen. + + Prefer to use the overload taking wxGraphicsPenInfo unless you already + have a wxPen as constructing one only to pass it to this method is + wasteful. */ - virtual wxGraphicsPen CreatePen(const wxPen& pen) const; + wxGraphicsPen CreatePen(const wxPen& pen) const; /** Creates a native pen from a wxGraphicsPenInfo. + + @since 3.1.1 */ - virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) const; + wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) const; /** Sets the pen used for stroking. @@ -1420,9 +1426,11 @@ public: virtual wxGraphicsPath CreatePath() = 0; /** - Creates a native pen from a wxPen. + Creates a native pen from its description. + + @since 3.1.1 */ - virtual wxGraphicsPen CreatePen(const wxPen& pen) = 0; + virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) = 0; /** Creates a native brush with a radial gradient. diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index 30d136b5ca..afb24ca689 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -120,6 +120,9 @@ WXDLLIMPEXP_DATA_CORE(wxGraphicsBitmap) wxNullGraphicsBitmap; /* static */ wxGraphicsPenInfo wxGraphicsPenInfo::CreateFromPen(const wxPen& pen) { + if ( !pen.IsOk() ) + return wxGraphicsPenInfo().Style(wxPENSTYLE_TRANSPARENT); + wxDash *dashes; int nb_dashes = pen.GetDashes(&dashes); return wxGraphicsPenInfo() @@ -838,12 +841,7 @@ wxGraphicsPath wxGraphicsContext::CreatePath() const return GetRenderer()->CreatePath(); } -wxGraphicsPen wxGraphicsContext::CreatePen(const wxPen& pen) const -{ - return GetRenderer()->CreatePen(pen); -} - -wxGraphicsPen wxGraphicsContext::CreatePen(const wxGraphicsPenInfo& info) const +wxGraphicsPen wxGraphicsContext::DoCreatePen(const wxGraphicsPenInfo& info) const { return GetRenderer()->CreatePen(info); } diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index 4afacfc251..12e858d9c9 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -284,12 +284,10 @@ private: class WXDLLIMPEXP_CORE wxCairoPenData : public wxCairoPenBrushBaseData { public: - wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); wxCairoPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ); ~wxCairoPenData(); void Init(); - void InitFromPenInfo( const wxGraphicsPenInfo& info ); virtual void Apply( wxGraphicsContext* context ) wxOVERRIDE; virtual wxDouble GetWidth() { return m_width; } @@ -735,19 +733,8 @@ void wxCairoPenData::Init() m_count = 0; } -wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) - : wxCairoPenBrushBaseData(renderer, pen.GetColour(), pen.IsTransparent()) -{ - InitFromPenInfo(wxGraphicsPenInfo::CreateFromPen(pen)); -} - wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ) : wxCairoPenBrushBaseData(renderer, info.GetColour(), info.IsTransparent()) -{ - InitFromPenInfo(info); -} - -void wxCairoPenData::InitFromPenInfo( const wxGraphicsPenInfo &info ) { Init(); m_width = info.GetWidth(); @@ -2916,7 +2903,6 @@ public : wxDouble tx=0.0, wxDouble ty=0.0) wxOVERRIDE; - virtual wxGraphicsPen CreatePen(const wxPen& pen) wxOVERRIDE ; virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) wxOVERRIDE ; virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) wxOVERRIDE ; @@ -3093,17 +3079,6 @@ wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble return m; } -wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen) -{ - wxGraphicsPen p; - ENSURE_LOADED_OR_RETURN(p); - if (pen.IsOk() && pen.GetStyle() != wxPENSTYLE_TRANSPARENT) - { - p.SetRefData(new wxCairoPenData( this, pen )); - } - return p; -} - wxGraphicsPen wxCairoRenderer::CreatePen(const wxGraphicsPenInfo& info) { wxGraphicsPen p; diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index 2da964da7c..b518dbdd99 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -260,12 +260,10 @@ private: class wxGDIPlusPenData : public wxGraphicsObjectRefData { public: - wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ); ~wxGDIPlusPenData(); void Init(); - void InitFromPenInfo( const wxGraphicsPenInfo &info ); virtual wxDouble GetWidth() { return m_width; } virtual Pen* GetGDIPlusPen() { return m_pen; } @@ -575,8 +573,6 @@ public : wxDouble tx=0.0, wxDouble ty=0.0) wxOVERRIDE; - virtual wxGraphicsPen CreatePen(const wxPen& pen) wxOVERRIDE; - virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& pen) wxOVERRIDE; virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) wxOVERRIDE; @@ -647,20 +643,9 @@ void wxGDIPlusPenData::Init() m_penBrush = NULL; } -wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) -: wxGraphicsObjectRefData(renderer) -{ - InitFromPenInfo(wxGraphicsPenInfo::CreateFromPen(pen)); -} - wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo &info ) : wxGraphicsObjectRefData(renderer) -{ - InitFromPenInfo(info); -} - -void wxGDIPlusPenData::InitFromPenInfo( const wxGraphicsPenInfo &info ) { Init(); m_width = info.GetWidth(); @@ -2464,19 +2449,6 @@ wxGraphicsMatrix wxGDIPlusRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDoub return m; } -wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen) -{ - ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen); - if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT ) - return wxNullGraphicsPen; - else - { - wxGraphicsPen p; - p.SetRefData(new wxGDIPlusPenData( this, pen )); - return p; - } -} - wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxGraphicsPenInfo& info) { ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen); diff --git a/src/msw/graphicsd2d.cpp b/src/msw/graphicsd2d.cpp index b30b222cde..22306f9169 100644 --- a/src/msw/graphicsd2d.cpp +++ b/src/msw/graphicsd2d.cpp @@ -2458,11 +2458,9 @@ wxBrushStyle wxConvertPenStyleToBrushStyle(wxPenStyle penStyle) class wxD2DPenData : public wxGraphicsObjectRefData, public wxD2DManagedGraphicsData { public: - wxD2DPenData(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, const wxPen& pen); - - wxD2DPenData(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, const wxGraphicsPenInfo& info); - - void Init(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory); + wxD2DPenData(wxGraphicsRenderer* renderer, + ID2D1Factory* direct2dFactory, + const wxGraphicsPenInfo& info); void CreateStrokeStyle(ID2D1Factory* const direct2dfactory); @@ -2497,17 +2495,6 @@ private: // wxD2DPenData implementation //----------------------------------------------------------------------------- -wxD2DPenData::wxD2DPenData( - wxGraphicsRenderer* renderer, - ID2D1Factory* direct2dFactory, - const wxPen& pen) - : wxGraphicsObjectRefData(renderer), - m_penInfo(wxGraphicsPenInfo::CreateFromPen(pen)), - m_width(pen.GetWidth()) -{ - Init(renderer, direct2dFactory); -} - wxD2DPenData::wxD2DPenData( wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, @@ -2515,13 +2502,6 @@ wxD2DPenData::wxD2DPenData( : wxGraphicsObjectRefData(renderer), m_penInfo(info), m_width(info.GetWidth()) -{ - Init(renderer, direct2dFactory); -} - -void wxD2DPenData::Init( - wxGraphicsRenderer* renderer, - ID2D1Factory* direct2dFactory) { CreateStrokeStyle(direct2dFactory); @@ -4408,8 +4388,6 @@ public : wxDouble a = 1.0, wxDouble b = 0.0, wxDouble c = 0.0, wxDouble d = 1.0, wxDouble tx = 0.0, wxDouble ty = 0.0) wxOVERRIDE; - wxGraphicsPen CreatePen(const wxPen& pen) wxOVERRIDE; - wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) wxOVERRIDE; wxGraphicsBrush CreateBrush(const wxBrush& brush) wxOVERRIDE; @@ -4579,21 +4557,6 @@ wxGraphicsMatrix wxD2DRenderer::CreateMatrix( return matrix; } -wxGraphicsPen wxD2DRenderer::CreatePen(const wxPen& pen) -{ - if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT ) - { - return wxNullGraphicsPen; - } - else - { - wxGraphicsPen p; - wxD2DPenData* penData = new wxD2DPenData(this, m_direct2dFactory, pen); - p.SetRefData(penData); - return p; - } -} - wxGraphicsPen wxD2DRenderer::CreatePen(const wxGraphicsPenInfo& info) { if ( info.GetStyle() == wxPENSTYLE_TRANSPARENT ) diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index eaba7e9b22..e77c6172cf 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -304,12 +304,10 @@ protected : class wxMacCoreGraphicsPenData : public wxGraphicsObjectRefData { public: - wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ); ~wxMacCoreGraphicsPenData(); void Init(); - void InitFromPenInfo( const wxGraphicsPenInfo& info ); virtual void Apply( wxGraphicsContext* context ); virtual wxDouble GetWidth() { return m_width; } @@ -331,20 +329,9 @@ protected : CGFloat* m_patternColorComponents; }; -wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) : - wxGraphicsObjectRefData( renderer ) -{ - InitFromPenInfo(wxGraphicsPenInfo::CreateFromPen(pen)); -} - wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxGraphicsPenInfo& info ) : wxGraphicsObjectRefData( renderer ) -{ - InitFromPenInfo(info); -} - -void wxMacCoreGraphicsPenData::InitFromPenInfo( const wxGraphicsPenInfo& info ) { Init(); @@ -2550,8 +2537,6 @@ public : wxDouble tx=0.0, wxDouble ty=0.0) wxOVERRIDE; - virtual wxGraphicsPen CreatePen(const wxPen& pen) wxOVERRIDE ; - virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) wxOVERRIDE ; virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) wxOVERRIDE ; @@ -2721,18 +2706,6 @@ wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b return m; } -wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen) -{ - if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT ) - return wxNullGraphicsPen; - else - { - wxGraphicsPen p; - p.SetRefData(new wxMacCoreGraphicsPenData( this, pen )); - return p; - } -} - wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxGraphicsPenInfo& info) { if ( info.IsTransparent() ) From 8bad0e494f70ed1d62580f2b2cbbe767853f62b9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Sep 2017 01:53:02 +0200 Subject: [PATCH 09/10] Get rid of wxGraphicsPenInfo::CreateFromPen() This is a helper method used only in wxWidgets itself and only once, so just inline it at the point of use to avoid exporting an unnecessary function in the public API. --- include/wx/graphics.h | 7 +------ src/common/graphcmn.cpp | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 0374f62885..7cbe1bc52f 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -157,10 +157,6 @@ public: wxDouble GetWidth() const { return m_width; } - // This is a helper used by wxWidgets itself, it doesn't make much sense to - // use it outside of the library. - static wxGraphicsPenInfo CreateFromPen(const wxPen& pen); - private: wxDouble m_width; }; @@ -512,8 +508,7 @@ public: wxGraphicsPath CreatePath() const; - wxGraphicsPen CreatePen(const wxPen& pen) const - { return DoCreatePen(wxGraphicsPenInfo::CreateFromPen(pen)); } + wxGraphicsPen CreatePen(const wxPen& pen) const; wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) const { return DoCreatePen(info); } diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index afb24ca689..5f88faf950 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -117,24 +117,6 @@ WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush; WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont; WXDLLIMPEXP_DATA_CORE(wxGraphicsBitmap) wxNullGraphicsBitmap; -/* static */ -wxGraphicsPenInfo wxGraphicsPenInfo::CreateFromPen(const wxPen& pen) -{ - if ( !pen.IsOk() ) - return wxGraphicsPenInfo().Style(wxPENSTYLE_TRANSPARENT); - - wxDash *dashes; - int nb_dashes = pen.GetDashes(&dashes); - return wxGraphicsPenInfo() - .Colour(pen.GetColour()) - .Width(pen.GetWidth()) - .Style(pen.GetStyle()) - .Stipple(*pen.GetStipple()) - .Dashes(nb_dashes, dashes) - .Join(pen.GetJoin()) - .Cap(pen.GetCap()); -} - //----------------------------------------------------------------------------- // matrix //----------------------------------------------------------------------------- @@ -841,6 +823,24 @@ wxGraphicsPath wxGraphicsContext::CreatePath() const return GetRenderer()->CreatePath(); } +wxGraphicsPen wxGraphicsContext::CreatePen(const wxPen& pen) const +{ + if ( !pen.IsOk() ) + return wxGraphicsPen(); + + wxDash *dashes; + int nb_dashes = pen.GetDashes(&dashes); + + return DoCreatePen(wxGraphicsPenInfo() + .Colour(pen.GetColour()) + .Width(pen.GetWidth()) + .Style(pen.GetStyle()) + .Stipple(*pen.GetStipple()) + .Dashes(nb_dashes, dashes) + .Join(pen.GetJoin()) + .Cap(pen.GetCap())); +} + wxGraphicsPen wxGraphicsContext::DoCreatePen(const wxGraphicsPenInfo& info) const { return GetRenderer()->CreatePen(info); From 2bb6ac7051b409ec33dad6781eee94c98acf878a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Sep 2017 02:17:02 +0200 Subject: [PATCH 10/10] Refactor all wxPen ctors to use wxPenRefData(wxPenInfo) ctor Centralize all pen creation logic in a single place (in each port) in the new wxPenRefData ctor taking wxPenInfo. --- src/gtk/pen.cpp | 31 +++++++++++++++-------------- src/gtk1/pen.cpp | 34 ++++++++++++++++--------------- src/msw/pen.cpp | 46 +++++++++++++++--------------------------- src/osx/pen.cpp | 52 +++++++++++++++--------------------------------- src/x11/pen.cpp | 34 ++++++++++++++++--------------- 5 files changed, 84 insertions(+), 113 deletions(-) diff --git a/src/gtk/pen.cpp b/src/gtk/pen.cpp index d95f0e1ab1..e5976eb85a 100644 --- a/src/gtk/pen.cpp +++ b/src/gtk/pen.cpp @@ -46,6 +46,16 @@ public: m_dash = data.m_dash; } + wxPenRefData( const wxPenInfo& info ) + { + m_width = info.GetWidth(); + m_style = info.GetStyle(); + m_joinStyle = info.GetJoin(); + m_capStyle = info.GetCap(); + m_colour = info.GetColour(); + m_countDashes = info.GetDashes((wxDash**)&m_dash); + } + bool operator == (const wxPenRefData& data) const { if ( m_countDashes != data.m_countDashes ) @@ -89,29 +99,20 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject); wxPen::wxPen( const wxColour &colour, int width, wxPenStyle style ) { - m_refData = new wxPenRefData(); - M_PENDATA->m_width = width; - M_PENDATA->m_style = style; - M_PENDATA->m_colour = colour; + m_refData = new wxPenRefData(wxPenInfo(colour, width).Style(style)); } wxPen::wxPen(const wxColour& colour, int width, int style) { - m_refData = new wxPenRefData(); - M_PENDATA->m_width = width; - M_PENDATA->m_style = (wxPenStyle)style; - M_PENDATA->m_colour = colour; + m_refData = new wxPenRefData + ( + wxPenInfo(colour, width).Style((wxPenStyle)style) + ); } wxPen::wxPen(const wxPenInfo& info) { - m_refData = new wxPenRefData(); - M_PENDATA->m_colour = info.GetColour(); - M_PENDATA->m_width = info.GetWidth(); - M_PENDATA->m_style = info.GetStyle(); - M_PENDATA->m_joinStyle = info.GetJoin(); - M_PENDATA->m_capStyle = info.GetCap(); - M_PENDATA->m_countDashes = info.GetDashes(&M_PENDATA->m_dash); + m_refData = new wxPenRefData(info); } wxPen::~wxPen() diff --git a/src/gtk1/pen.cpp b/src/gtk1/pen.cpp index d68c6bfce9..3e145d18ec 100644 --- a/src/gtk1/pen.cpp +++ b/src/gtk1/pen.cpp @@ -46,6 +46,18 @@ public: m_dash = data.m_dash; } + wxPenRefData( const wxPenInfo& info ) + { + m_width = info.GetWidth(); + m_style = info.GetStyle(); + m_joinStyle = info.GetJoin(); + m_capStyle = info.GetCap(); + m_colour = info.GetColour(); + wxDash* dash; + m_countDashes = info.GetDashes(&dash); + m_dash = (wxGTKDash*)dash; + } + bool operator == (const wxPenRefData& data) const { if ( m_countDashes != data.m_countDashes ) @@ -89,30 +101,20 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject); wxPen::wxPen( const wxColour &colour, int width, wxPenStyle style ) { - m_refData = new wxPenRefData(); - M_PENDATA->m_width = width; - M_PENDATA->m_style = style; - M_PENDATA->m_colour = colour; + m_refData = new wxPenRefData(wxPenInfo(colour, width).Style(style)); } wxPen::wxPen(const wxColour& colour, int width, int style) { - m_refData = new wxPenRefData(); - M_PENDATA->m_width = width; - M_PENDATA->m_style = (wxPenStyle)style; - M_PENDATA->m_colour = colour; + m_refData = new wxPenRefData + ( + wxPenInfo(colour, width).Style((wxPenStyle)style) + ); } wxPen::wxPen(const wxPenInfo& info) { - m_refData = new wxPenRefData(); - - M_PENDATA->m_colour = info.GetColour(); - M_PENDATA->m_width = info.GetWidth(); - M_PENDATA->m_style = info.GetStyle(); - M_PENDATA->m_joinStyle = info.GetJoin(); - M_PENDATA->m_capStyle = info.GetCap(); - M_PENDATA->m_countDashes = info.GetDashes(&M_PENDATA->m_dash); + m_refData = new wxPenRefData(info); } wxGDIRefData *wxPen::CreateGDIRefData() const diff --git a/src/msw/pen.cpp b/src/msw/pen.cpp index cf8af1986b..61a8648d3b 100644 --- a/src/msw/pen.cpp +++ b/src/msw/pen.cpp @@ -46,8 +46,7 @@ public: wxPenRefData(); wxPenRefData(const wxPenRefData& data); - wxPenRefData(const wxColour& col, int width, wxPenStyle style); - wxPenRefData(const wxBitmap& stipple, int width); + wxPenRefData(const wxPenInfo& info); virtual ~wxPenRefData(); bool operator==(const wxPenRefData& data) const @@ -170,24 +169,17 @@ wxPenRefData::wxPenRefData(const wxPenRefData& data) m_hPen = 0; } -wxPenRefData::wxPenRefData(const wxColour& col, int width, wxPenStyle style) +wxPenRefData::wxPenRefData(const wxPenInfo& info) { Init(); - m_style = style; - m_width = width; - - m_colour = col; -} - -wxPenRefData::wxPenRefData(const wxBitmap& stipple, int width) -{ - Init(); - - m_style = wxPENSTYLE_STIPPLE; - m_width = width; - - m_stipple = stipple; + m_style = info.GetStyle(); + m_width = info.GetWidth(); + m_join = info.GetJoin(); + m_cap = info.GetCap(); + m_stipple = info.GetStipple(); + m_nbDash = info.GetDashes(&m_dash); + m_colour = info.GetColour(); } wxPenRefData::~wxPenRefData() @@ -401,31 +393,25 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject); wxPen::wxPen(const wxColour& col, int width, wxPenStyle style) { - m_refData = new wxPenRefData(col, width, style); + m_refData = new wxPenRefData(wxPenInfo(col, width).Style(style)); } wxPen::wxPen(const wxColour& colour, int width, int style) { - m_refData = new wxPenRefData(colour, width, (wxPenStyle)style); + m_refData = new wxPenRefData + ( + wxPenInfo(colour, width).Style((wxPenStyle)style) + ); } wxPen::wxPen(const wxBitmap& stipple, int width) { - m_refData = new wxPenRefData(stipple, width); + m_refData = new wxPenRefData(wxPenInfo().Stipple(stipple).Width(width)); } wxPen::wxPen(const wxPenInfo& info) { - m_refData = new wxPenRefData(); - - M_PENDATA->SetColour(info.GetColour()); - M_PENDATA->SetWidth(info.GetWidth()); - M_PENDATA->SetStyle(info.GetStyle()); - M_PENDATA->SetJoin(info.GetJoin()); - M_PENDATA->SetCap(info.GetCap()); - wxDash *dash; - int nb_dashes = info.GetDashes(&dash); - M_PENDATA->SetDashes(nb_dashes, dash); + m_refData = new wxPenRefData(info); } bool wxPen::operator==(const wxPen& pen) const diff --git a/src/osx/pen.cpp b/src/osx/pen.cpp index f6ea0a489d..40c9fba41b 100644 --- a/src/osx/pen.cpp +++ b/src/osx/pen.cpp @@ -23,6 +23,7 @@ class WXDLLEXPORT wxPenRefData : public wxGDIRefData public: wxPenRefData(); wxPenRefData(const wxPenRefData& data); + wxPenRefData(const wxPenInfo& info); virtual ~wxPenRefData(); wxPenRefData& operator=(const wxPenRefData& data); @@ -79,6 +80,16 @@ wxPenRefData::wxPenRefData(const wxPenRefData& data) m_colour = data.m_colour; } +wxPenRefData::wxPenRefData(const wxPenInfo& info) +{ + m_style = info.GetStyle(); + m_width = info.GetWidth(); + m_join = info.GetJoin(); + m_cap = info.GetCap(); + m_nbDash = info.GetDashes(&m_dash); + m_colour = info.GetColour(); +} + wxPenRefData::~wxPenRefData() { } @@ -98,59 +109,28 @@ wxPen::~wxPen() // Should implement Create wxPen::wxPen(const wxColour& col, int Width, wxPenStyle Style) { - m_refData = new wxPenRefData; - - M_PENDATA->m_colour = col; - M_PENDATA->m_width = Width; - M_PENDATA->m_style = Style; - M_PENDATA->m_join = wxJOIN_ROUND ; - M_PENDATA->m_cap = wxCAP_ROUND ; - M_PENDATA->m_nbDash = 0 ; - M_PENDATA->m_dash = 0 ; + m_refData = new wxPenRefData(wxPenInfo(col, Width).Style(Style)); RealizeResource(); } wxPen::wxPen(const wxColour& col, int Width, int Style) { - m_refData = new wxPenRefData; - - M_PENDATA->m_colour = col; - M_PENDATA->m_width = Width; - M_PENDATA->m_style = (wxPenStyle)Style; - M_PENDATA->m_join = wxJOIN_ROUND ; - M_PENDATA->m_cap = wxCAP_ROUND ; - M_PENDATA->m_nbDash = 0 ; - M_PENDATA->m_dash = 0 ; + m_refData = new wxPenRefData(wxPenInfo(col, Width).Style((wxPenStyle)Style)); RealizeResource(); } -wxPen::wxPen(const wxBitmap& stipple, int Width) +wxPen::wxPen(const wxBitmap& stipple, int width) { - m_refData = new wxPenRefData; - - M_PENDATA->m_stipple = stipple; - M_PENDATA->m_width = Width; - M_PENDATA->m_style = wxPENSTYLE_STIPPLE; - M_PENDATA->m_join = wxJOIN_ROUND ; - M_PENDATA->m_cap = wxCAP_ROUND ; - M_PENDATA->m_nbDash = 0 ; - M_PENDATA->m_dash = 0 ; + m_refData = new wxPenRefData(wxPenInfo().Stipple(stipple).Width(width)); RealizeResource(); } wxPen::wxPen(const wxPenInfo& info) { - m_refData = new wxPenRefData; - - M_PENDATA->m_colour = info.GetColour(); - M_PENDATA->m_width = info.GetWidth(); - M_PENDATA->m_style = info.GetStyle(); - M_PENDATA->m_join = info.GetJoin(); - M_PENDATA->m_cap = info.GetCap(); - M_PENDATA->m_nbDash = info.GetDashes(&M_PENDATA->m_dash); + m_refData = new wxPenRefData(info); RealizeResource(); } diff --git a/src/x11/pen.cpp b/src/x11/pen.cpp index 9960466228..007c35fe25 100644 --- a/src/x11/pen.cpp +++ b/src/x11/pen.cpp @@ -52,6 +52,18 @@ public: m_stipple = data.m_stipple; } + wxPenRefData( const wxPenInfo& info ) + { + m_width = info.GetWidth(); + m_style = info.GetStyle(); + m_joinStyle = info.GetJoin(); + m_capStyle = info.GetCap(); + m_colour = info.GetColour(); + wxDash* dash; + m_countDashes = info.GetDashes(&dash); + m_dash = (wxX11Dash*)dash; + } + bool operator == (const wxPenRefData& data) const { return (m_style == data.m_style && @@ -79,30 +91,20 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject); wxPen::wxPen( const wxColour &colour, int width, wxPenStyle style ) { - m_refData = new wxPenRefData(); - M_PENDATA->m_width = width; - M_PENDATA->m_style = style; - M_PENDATA->m_colour = colour; + m_refData = new wxPenRefData(wxPenInfo(colour, width).Style(style)); } wxPen::wxPen(const wxColour& colour, int width, int style) { - m_refData = new wxPenRefData(); - M_PENDATA->m_width = width; - M_PENDATA->m_style = (wxPenStyle)style; - M_PENDATA->m_colour = colour; + m_refData = new wxPenRefData + ( + wxPenInfo(colour, width).Style((wxPenStyle)style) + ); } wxPen::wxPen(const wxPenInfo& info) { - m_refData = new wxPenRefData(); - M_PENDATA->m_width = info.GetWidth(); - M_PENDATA->m_style = info.GetStyle(); - M_PENDATA->m_colour = info.GetColour(); - M_PENDATA->m_capStyle = info.GetCap(); - M_PENDATA->m_joinStyle = info.GetJoin(); - M_PENDATA->m_stipple = info.GetStipple(); - M_PENDATA->m_countDashes = info.GetDashes(&M_PENDATA->m_dash); + m_refData = new wxPenRefData(info); } wxPen::~wxPen()