From 399371921fc54524c460047b138cdb207b7dfb4a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 18 Mar 2014 17:23:13 +0000 Subject: [PATCH] Add font colour support to wxFontPickerCtrl. Currently this is only really implemented under Windows, just as the colour support in wxFontDialog, but make the API available under all platforms for consistency. Closes #11614. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76160 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/fontpicker.h | 13 ++++++++++++- include/wx/generic/fontpickerg.h | 6 ++++++ include/wx/gtk/fontpicker.h | 22 ++++++++++++++++++++-- interface/wx/fontpicker.h | 21 +++++++++++++++++++++ src/generic/fontpickerg.cpp | 3 ++- tests/controls/pickertest.cpp | 16 ++++++++++++++++ 7 files changed, 78 insertions(+), 4 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index ccf30a330a..fab4fadc2f 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -54,6 +54,7 @@ wxMSW: - Fix loading of bitmap with non-pre-multiplied alpha (Artur Wieczorek). - Support multiline strings in wxDC::DrawRotatedText() (Artur Wieczorek). - Fix stretchable spacers in vertical toolbars (Artur Wieczorek). +- Add font colour support to wxFontPickerCtrl (Pana Alexandru). - Add wxEnhMetaFile::Detach() (Luca Bacci). - Add support for saving 256*256 32bpp ICOs in PNG format (Artur Wieczorek). diff --git a/include/wx/fontpicker.h b/include/wx/fontpicker.h index 6df6f950b5..7761a30807 100644 --- a/include/wx/fontpicker.h +++ b/include/wx/fontpicker.h @@ -41,6 +41,9 @@ public: virtual void SetSelectedFont(const wxFont &f) { m_selectedFont = f; UpdateFont(); } + virtual wxColour GetSelectedColour() const = 0; + virtual void SetSelectedColour(const wxColour &colour) = 0; + protected: virtual void UpdateFont() = 0; @@ -138,7 +141,15 @@ public: // public API // sets currently displayed font void SetSelectedFont(const wxFont& f); - // set/get the max pointsize + // returns the selected color + wxColour GetSelectedColour() const + { return GetPickerWidget()->GetSelectedColour(); } + + // sets the currently selected color + void SetSelectedColour(const wxColour& colour) + { GetPickerWidget()->SetSelectedColour(colour); } + + // set/get the max point size void SetMaxPointSize(unsigned int max) { m_nMaxPointSize=max; } unsigned int GetMaxPointSize() const diff --git a/include/wx/generic/fontpickerg.h b/include/wx/generic/fontpickerg.h index 8090450038..7bb4dee0e0 100644 --- a/include/wx/generic/fontpickerg.h +++ b/include/wx/generic/fontpickerg.h @@ -35,6 +35,12 @@ public: Create(parent, id, initial, pos, size, style, validator, name); } + virtual wxColour GetSelectedColour() const + { return m_data.GetColour(); } + + virtual void SetSelectedColour(const wxColour &colour) + { m_data.SetColour(colour); UpdateFont(); } + virtual ~wxGenericFontButton() {} diff --git a/include/wx/gtk/fontpicker.h b/include/wx/gtk/fontpicker.h index 65bb5c9593..c991137cab 100644 --- a/include/wx/gtk/fontpicker.h +++ b/include/wx/gtk/fontpicker.h @@ -21,7 +21,7 @@ class WXDLLIMPEXP_CORE wxFontButton : public wxButton, public wxFontPickerWidgetBase { public: - wxFontButton() {} + wxFontButton() { Init(); } wxFontButton(wxWindow *parent, wxWindowID id, const wxFont& initial = wxNullFont, @@ -31,7 +31,9 @@ public: const wxValidator& validator = wxDefaultValidator, const wxString& name = wxFontPickerWidgetNameStr) { - Create(parent, id, initial, pos, size, style, validator, name); + Init(); + + Create(parent, id, initial, pos, size, style, validator, name); } bool Create(wxWindow *parent, @@ -43,6 +45,12 @@ public: const wxValidator& validator = wxDefaultValidator, const wxString& name = wxFontPickerWidgetNameStr); + virtual wxColour GetSelectedColour() const + { return m_selectedColour; } + + void SetSelectedColour(const wxColour &colour) + { m_selectedColour = colour; } + virtual ~wxFontButton(); protected: @@ -55,6 +63,16 @@ public: // used by the GTK callback only { m_selectedFont.SetNativeFontInfo(wxString::FromAscii(gtkdescription)); } private: + // Common part of both ctors. + void Init() + { + m_selectedColour = *wxBLACK; + } + + // This can't be changed by the user, but is provided to + // satisfy the wxFontPickerWidgetBase interface. + wxColour m_selectedColour; + DECLARE_DYNAMIC_CLASS(wxFontButton) }; diff --git a/interface/wx/fontpicker.h b/interface/wx/fontpicker.h index 1d3bd77e51..61bcae95f6 100644 --- a/interface/wx/fontpicker.h +++ b/interface/wx/fontpicker.h @@ -109,6 +109,17 @@ public: */ unsigned int GetMaxPointSize() const; + /** + Returns the currently selected colour. + + Note that the colour of the font can only be set by the user under + Windows currently, elsewhere this method simply returns the colour + previously set by SetSelectedColour() or black if it hadn't been called. + + @since 3.1.0 + */ + wxColour GetSelectedColour() const; + /** Returns the currently selected font. Note that this function is completely different from wxWindow::GetFont. @@ -126,6 +137,16 @@ public: */ void SetMaxPointSize(unsigned int max); + /** + Sets the font colour. + + The font colour is actually only used under Windows currently, but this + function is available under all platforms for consistency. + + @since 3.1.0 + */ + void SetSelectedColour(const wxColour& colour); + /** Sets the currently selected font. Note that this function is completely different from wxWindow::SetFont. diff --git a/src/generic/fontpickerg.cpp b/src/generic/fontpickerg.cpp index 62599b7892..3636c82598 100644 --- a/src/generic/fontpickerg.cpp +++ b/src/generic/fontpickerg.cpp @@ -62,9 +62,10 @@ bool wxGenericFontButton::Create( wxWindow *parent, wxWindowID id, wxCommandEventHandler(wxGenericFontButton::OnButtonClick), NULL, this); + InitFontData(); + m_selectedFont = initial.IsOk() ? initial : *wxNORMAL_FONT; UpdateFont(); - InitFontData(); return true; } diff --git a/tests/controls/pickertest.cpp b/tests/controls/pickertest.cpp index 22bff2960d..c7ca967478 100644 --- a/tests/controls/pickertest.cpp +++ b/tests/controls/pickertest.cpp @@ -20,6 +20,7 @@ #include "wx/filepicker.h" #include "wx/fontpicker.h" #include "pickerbasetest.h" +#include "asserthelper.h" #if wxUSE_COLOURPICKERCTRL @@ -172,8 +173,11 @@ private: CPPUNIT_TEST_SUITE( FontPickerCtrlTestCase ); wxPICKER_BASE_TESTS(); + CPPUNIT_TEST( ColourSelection ); CPPUNIT_TEST_SUITE_END(); + void ColourSelection(); + wxFontPickerCtrl *m_font; DECLARE_NO_COPY_CLASS(FontPickerCtrlTestCase) @@ -198,4 +202,16 @@ void FontPickerCtrlTestCase::tearDown() wxDELETE(m_font); } +void FontPickerCtrlTestCase::ColourSelection() +{ + wxColour selectedColour(0xFF4269UL); + + CPPUNIT_ASSERT_EQUAL_MESSAGE("Default font picker color must be black", + m_font->GetSelectedColour(), wxColour(*wxBLACK)); + + m_font->SetSelectedColour(selectedColour); + + CPPUNIT_ASSERT_EQUAL_MESSAGE("Font picker did not react to color selection", + m_font->GetSelectedColour(), selectedColour); +} #endif //wxUSE_FONTPICKERCTRL