From 3674bd1c1f346b2e0905b91b2d94107f8cb61dce Mon Sep 17 00:00:00 2001 From: Vsevolod V Gromov Date: Wed, 17 Apr 2019 12:39:03 +0300 Subject: [PATCH] Add wxDCTextBgColourChanger and wxDCTextBgModeChanger helpers These classes are similar to the existing wxDCTextColourChanger and allow temporarily changing other wxDC attributes. Closes https://github.com/wxWidgets/wxWidgets/pull/1298 --- include/wx/dc.h | 72 +++++++++++++++++++++++++++++ interface/wx/dc.h | 113 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 184 insertions(+), 1 deletion(-) diff --git a/include/wx/dc.h b/include/wx/dc.h index dd5eb2251d..847b7f64e5 100644 --- a/include/wx/dc.h +++ b/include/wx/dc.h @@ -1405,6 +1405,78 @@ private: wxDECLARE_NO_COPY_CLASS(wxDCTextColourChanger); }; +// ---------------------------------------------------------------------------- +// helper class: you can use it to temporarily change the DC text background colour and +// restore it automatically when the object goes out of scope +// ---------------------------------------------------------------------------- + +class WXDLLIMPEXP_CORE wxDCTextBgColourChanger +{ +public: + wxDCTextBgColourChanger(wxDC& dc) : m_dc(dc) { } + + wxDCTextBgColourChanger(wxDC& dc, const wxColour& col) : m_dc(dc) + { + Set(col); + } + + ~wxDCTextBgColourChanger() + { + if ( m_colBgOld.IsOk() ) + m_dc.SetTextBackground(m_colBgOld); + } + + void Set(const wxColour& col) + { + if ( !m_colBgOld.IsOk() ) + m_colBgOld = m_dc.GetTextBackground(); + m_dc.SetTextBackground(col); + } + +private: + wxDC& m_dc; + + wxColour m_colBgOld; + + wxDECLARE_NO_COPY_CLASS(wxDCTextBgColourChanger); +}; + +// ---------------------------------------------------------------------------- +// helper class: you can use it to temporarily change the DC text background mode and +// restore it automatically when the object goes out of scope +// ---------------------------------------------------------------------------- + +class WXDLLIMPEXP_CORE wxDCTextBgModeChanger +{ +public: + wxDCTextBgModeChanger(wxDC& dc) : m_dc(dc), m_modeOld(wxBRUSHSTYLE_INVALID) { } + + wxDCTextBgModeChanger(wxDC& dc, int mode) : m_dc(dc) + { + Set(mode); + } + + ~wxDCTextBgModeChanger() + { + if ( m_modeOld != wxBRUSHSTYLE_INVALID ) + m_dc.SetBackgroundMode(m_modeOld); + } + + void Set(int mode) + { + if ( m_modeOld == wxBRUSHSTYLE_INVALID ) + m_modeOld = m_dc.GetBackgroundMode(); + m_dc.SetBackgroundMode(mode); + } + +private: + wxDC& m_dc; + + int m_modeOld; + + wxDECLARE_NO_COPY_CLASS(wxDCTextBgModeChanger); +}; + // ---------------------------------------------------------------------------- // helper class: you can use it to temporarily change the DC pen and // restore it automatically when the object goes out of scope diff --git a/interface/wx/dc.h b/interface/wx/dc.h index 84238b44df..ad83b31059 100644 --- a/interface/wx/dc.h +++ b/interface/wx/dc.h @@ -1799,7 +1799,7 @@ public: @category{gdi} @see wxDC::SetTextForeground(), wxDCFontChanger, wxDCPenChanger, wxDCBrushChanger, - wxDCClipper + wxDCClipper, wxDCTextBgColourChanger, wxDCBgModeChanger */ class wxDCTextColourChanger { @@ -1842,6 +1842,117 @@ public: }; +/** + @class wxDCTextBgColourChanger + + wxDCTextBgColourChanger is a small helper class for setting a background + text colour on a wxDC and unsetting it automatically in the destructor, + restoring the previous one. + + @library{wxcore} + @category{gdi} + + @see wxDC::SetTextBackground(), wxDCFontChanger, wxDCPenChanger, wxDCBrushChanger, + wxDCClipper, wxDCTextColourChanger, wxDCBgModeChanger + + @since 3.1.3 +*/ +class wxDCTextBgColourChanger +{ +public: + /** + Trivial constructor not changing anything. + + This constructor is useful if you don't know beforehand if the colour + needs to be changed or not. It simply creates the object which won't do + anything in its destructor unless Set() is called -- in which case it + would reset the previous colour. + */ + wxDCTextBgColourChanger(wxDC& dc); + + /** + Sets @a col on the given @a dc, storing the old one. + + @param dc + The DC where the colour must be temporary set. + @param col + The text background colour to set. + */ + wxDCTextBgColourChanger(wxDC& dc, const wxColour& col); + + /** + Set the background colour to use. + + This method is meant to be called once only and only on the objects + created with the constructor overload not taking wxColour argument and + has the same effect as the other constructor, i.e. sets the background colour to + the given @a col and ensures that the old value is restored when this + object is destroyed. + */ + void Set(const wxColour& col); + + /** + Restores the background colour originally selected in the DC passed to the ctor. + */ + ~wxDCTextBgColourChanger(); +}; + + +/** + @class wxDCTextBgModeChanger + + wxDCTextBgModeChanger is a small helper class for setting a background + text mode on a wxDC and unsetting it automatically in the destructor, + restoring the previous one. + + @library{wxcore} + @category{gdi} + + @see wxDC::SetBackgroundMode(), wxDCFontChanger, wxDCPenChanger, wxDCBrushChanger, + wxDCClipper, wxDCTextColourChanger, wxDCTextBgColourChanger + + @since 3.1.3 +*/ +class wxDCBgModeChanger +{ +public: + /** + Trivial constructor not changing anything. + + This constructor is useful if you don't know beforehand if the background mode + needs to be changed or not. It simply creates the object which won't do + anything in its destructor unless Set() is called -- in which case it + would reset the previous mode. + */ + wxDCBgModeChanger(wxDC& dc); + + /** + Sets @a mode on the given @a dc, storing the old one. + + @param dc + The DC where the mode must be temporary set. + @param mode + The background mode to set. + */ + wxDCBgModeChanger(wxDC& dc, int mode); + + /** + Set the text background mode to use. + + This method is meant to be called once only and only on the objects + created with the constructor overload not taking mode argument and + has the same effect as the other constructor, i.e. sets the background mode to + the given @a one, and ensures that the old value is restored when this + object is destroyed. + */ + void Set(int mode); + + /** + Restores the text background mode originally selected in the DC passed to the ctor. + */ + ~wxDCBgModeChanger(); +}; + /** @class wxDCFontChanger