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;