From b356d1d3c7a63ffe73f78706b17c5d939597d5bf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 11 Mar 2014 20:48:20 +0000 Subject: [PATCH] Add wxFont::GetBaseFont(). This can be used to "undo" the result of Bold() ,Underlined() or Italic() methods and returns an unadorned version of the font. Closes #11815. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76120 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/font.h | 1 + interface/wx/font.h | 8 ++++++++ samples/font/font.cpp | 6 ++++++ src/common/fontcmn.cpp | 10 ++++++++++ tests/font/fonttest.cpp | 7 +++++++ 6 files changed, 33 insertions(+) diff --git a/docs/changes.txt b/docs/changes.txt index 86e1ed27a8..d01b6e501a 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -37,6 +37,7 @@ All (GUI): - Fix wxRearrangeList::Check() which asserted and misbehaved before. - Optimized wxRTC insertion and deletion when floating objects are present. - Added on-demand image loading option to wxRTC. +- Add wxFont::GetBaseFont() (Melroy Tellis). wxGTK: diff --git a/include/wx/font.h b/include/wx/font.h index d7e529fef2..c2117237d2 100644 --- a/include/wx/font.h +++ b/include/wx/font.h @@ -507,6 +507,7 @@ WXDLLIMPEXP_CORE bool wxFromString(const wxString& str, wxFontBase* font); wxFont& Scale(float x); \ /* functions for creating fonts based on this one */ \ wxFont Bold() const; \ + wxFont GetBaseFont() const; \ wxFont Italic() const; \ wxFont Underlined() const; \ wxFont Strikethrough() const; \ diff --git a/interface/wx/font.h b/interface/wx/font.h index d0a0b368a9..9d41f496c6 100644 --- a/interface/wx/font.h +++ b/interface/wx/font.h @@ -607,6 +607,14 @@ public: */ //@{ + /** + Returns a font with the same face/size as the given one but with normal + weight and style and not underlined nor stricken through. + + @since 3.1.0 + */ + wxFont GetBaseFont() const; + /** Returns the encoding of this font. diff --git a/samples/font/font.cpp b/samples/font/font.cpp index 7852c2f343..7136e9557f 100644 --- a/samples/font/font.cpp +++ b/samples/font/font.cpp @@ -96,6 +96,8 @@ public: void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); + void OnGetBaseFont(wxCommandEvent& WXUNUSED(event)) + { DoChangeFont(m_canvas->GetTextFont().GetBaseFont()); } void OnIncFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(+2); } void OnDecFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(-2); } @@ -171,6 +173,8 @@ enum Font_IncSize, Font_DecSize, + Font_GetBaseFont, + Font_Bold, Font_Light, @@ -222,6 +226,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Font_ViewMsg, MyFrame::OnViewMsg) EVT_MENU(Font_About, MyFrame::OnAbout) + EVT_MENU(Font_GetBaseFont, MyFrame::OnGetBaseFont) EVT_MENU(Font_IncSize, MyFrame::OnIncFont) EVT_MENU(Font_DecSize, MyFrame::OnDecFont) @@ -320,6 +325,7 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) wxMenu *menuFont = new wxMenu; menuFont->Append(Font_IncSize, wxT("&Increase font size by 2 points\tCtrl-I")); menuFont->Append(Font_DecSize, wxT("&Decrease font size by 2 points\tCtrl-D")); + menuFont->Append(Font_GetBaseFont, wxT("Use &base version of the font\tCtrl-0")); menuFont->AppendSeparator(); menuFont->AppendCheckItem(Font_Bold, wxT("&Bold\tCtrl-B"), wxT("Toggle bold state")); menuFont->AppendCheckItem(Font_Light, wxT("&Light\tCtrl-L"), wxT("Toggle light state")); diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index 715e2c05f6..b8f46fef70 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -510,6 +510,16 @@ wxFont wxFont::Bold() const return font; } +wxFont wxFont::GetBaseFont() const +{ + wxFont font(*this); + font.SetStyle(wxFONTSTYLE_NORMAL); + font.SetWeight(wxFONTWEIGHT_NORMAL ); + font.SetUnderlined(false); + font.SetStrikethrough(false); + return font; +} + wxFont& wxFont::MakeItalic() { SetStyle(wxFONTSTYLE_ITALIC); diff --git a/tests/font/fonttest.cpp b/tests/font/fonttest.cpp index 7d1416509f..7e6197a33f 100644 --- a/tests/font/fonttest.cpp +++ b/tests/font/fonttest.cpp @@ -202,6 +202,13 @@ void FontTestCase::GetSet() CPPUNIT_ASSERT( test.IsOk() ); CPPUNIT_ASSERT_EQUAL( true, test.GetUnderlined() ); + const wxFont fontBase = test.GetBaseFont(); + CPPUNIT_ASSERT( fontBase.IsOk() ); + CPPUNIT_ASSERT( !fontBase.GetUnderlined() ); + CPPUNIT_ASSERT( !fontBase.GetStrikethrough() ); + CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_NORMAL, fontBase.GetWeight() ); + CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_NORMAL, fontBase.GetStyle() ); + // test Get/SetStrikethrough() // Strike through support not implemented in wxOSX currently.