From f76c075805027058f08ffba0e8c1b32372f5fd44 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 12 Jun 2009 20:11:21 +0000 Subject: [PATCH] added convenient wxFont::Make{Bold,Italic,Smaller,Larger} and Scale() methods git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61033 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/cocoa/font.h | 2 +- include/wx/dfb/font.h | 2 +- include/wx/font.h | 15 +++- include/wx/gtk/font.h | 2 +- include/wx/gtk1/font.h | 2 +- include/wx/mgl/font.h | 2 +- include/wx/motif/font.h | 2 +- include/wx/msw/font.h | 2 +- include/wx/os2/font.h | 2 +- include/wx/osx/font.h | 2 +- include/wx/palmos/font.h | 2 +- include/wx/x11/font.h | 2 +- interface/wx/font.h | 145 ++++++++++++++++++++++++++------------- src/common/fontcmn.cpp | 20 ++++++ 15 files changed, 141 insertions(+), 62 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 5fccff31e7..239b8ebde6 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -346,6 +346,7 @@ All (GUI): - Added wxMouseEventsManager. - Building OpenGL library is now enabled by default. - Improve wxTreeCtrl::ScrollTo() in generic version (Raanan Barzel). +- Added wxFont::Make{Bold,Italic,Smaller,Larger} and Scale() methods. - Added wxDC::CopyAttributes() and use it in wxBufferedDC. - Added wxTextWrapper helper class useful for wrapping lines of text. diff --git a/include/wx/cocoa/font.h b/include/wx/cocoa/font.h index acda832c69..ae5686f98d 100644 --- a/include/wx/cocoa/font.h +++ b/include/wx/cocoa/font.h @@ -119,7 +119,7 @@ public: virtual void SetUnderlined(bool underlined); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); // implementation only from now on // ------------------------------- diff --git a/include/wx/dfb/font.h b/include/wx/dfb/font.h index 941f513dee..def7085e07 100644 --- a/include/wx/dfb/font.h +++ b/include/wx/dfb/font.h @@ -88,7 +88,7 @@ public: virtual void SetUnderlined(bool underlined); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); // Unofficial API, don't use virtual void SetNoAntiAliasing(bool no = true); diff --git a/include/wx/font.h b/include/wx/font.h index ec8ed80d6c..873ba84a4a 100644 --- a/include/wx/font.h +++ b/include/wx/font.h @@ -276,7 +276,7 @@ WXDLLIMPEXP_CORE bool wxFromString(const wxString& str, wxFontBase* font); #if FUTURE_WXWIN_COMPATIBILITY_3_0 -#define WXDECLARE_COMPAT_SETTERS \ +#define wxDECLARE_FONT_COMPAT_SETTER \ wxDEPRECATED_FUTURE( void SetFamily(int family) ) \ { SetFamily((wxFontFamily)family); } \ wxDEPRECATED_FUTURE( void SetStyle(int style) ) \ @@ -290,9 +290,20 @@ WXDLLIMPEXP_CORE bool wxFromString(const wxString& str, wxFontBase* font); wxDEPRECATED_FUTURE( void SetWeight(wxDeprecatedGUIConstants weight) ) \ { SetWeight((wxFontWeight)weight); } #else -#define WXDECLARE_COMPAT_SETTERS /*empty*/ +#define wxDECLARE_FONT_COMPAT_SETTER /*empty*/ #endif +// this macro must be used in all derived wxFont classes declarations +#define wxDECLARE_COMMON_FONT_METHODS() \ + wxDECLARE_FONT_COMPAT_SETTER \ + \ + /* functions for creating fonts based on this one */ \ + wxFont MakeBold() const; \ + wxFont MakeItalic() const; \ + wxFont MakeLarger() const { return Scale(1.2f); } \ + wxFont MakeSmaller() const { return Scale(1/1.2f); } \ + wxFont Scale(float x) const + // include the real class declaration #if defined(__WXPALMOS__) #include "wx/palmos/font.h" diff --git a/include/wx/gtk/font.h b/include/wx/gtk/font.h index 4fb065741c..8e03364bca 100644 --- a/include/wx/gtk/font.h +++ b/include/wx/gtk/font.h @@ -95,7 +95,7 @@ public: virtual void SetUnderlined( bool underlined ); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); virtual void SetNoAntiAliasing( bool no = true ); virtual bool GetNoAntiAliasing() const ; diff --git a/include/wx/gtk1/font.h b/include/wx/gtk1/font.h index de10438926..0686c3934f 100644 --- a/include/wx/gtk1/font.h +++ b/include/wx/gtk1/font.h @@ -109,7 +109,7 @@ public: virtual void SetUnderlined( bool underlined ); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); virtual void SetNoAntiAliasing( bool no = true ); virtual bool GetNoAntiAliasing() const ; diff --git a/include/wx/mgl/font.h b/include/wx/mgl/font.h index 8625ccdd59..a94a28c3ea 100644 --- a/include/wx/mgl/font.h +++ b/include/wx/mgl/font.h @@ -104,7 +104,7 @@ public: virtual void SetUnderlined(bool underlined); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); // Unofficial API, don't use virtual void SetNoAntiAliasing(bool no = true); diff --git a/include/wx/motif/font.h b/include/wx/motif/font.h index 6c59225a13..80f86980ae 100644 --- a/include/wx/motif/font.h +++ b/include/wx/motif/font.h @@ -99,7 +99,7 @@ public: virtual void SetUnderlined(bool underlined); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); // Implementation diff --git a/include/wx/msw/font.h b/include/wx/msw/font.h index 4756c68eab..afadf95e08 100644 --- a/include/wx/msw/font.h +++ b/include/wx/msw/font.h @@ -135,7 +135,7 @@ public: virtual void SetUnderlined(bool underlined); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); virtual bool IsFixedWidth() const; diff --git a/include/wx/os2/font.h b/include/wx/os2/font.h index e4a2f1d608..ef692625d2 100644 --- a/include/wx/os2/font.h +++ b/include/wx/os2/font.h @@ -109,7 +109,7 @@ public: virtual void SetUnderlined(bool bUnderlined); virtual void SetEncoding(wxFontEncoding vEncoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); // // For internal use only! diff --git a/include/wx/osx/font.h b/include/wx/osx/font.h index c005e55be3..79fb78b7c4 100644 --- a/include/wx/osx/font.h +++ b/include/wx/osx/font.h @@ -113,7 +113,7 @@ public: virtual void SetUnderlined(bool underlined); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); // implementation only from now on // ------------------------------- diff --git a/include/wx/palmos/font.h b/include/wx/palmos/font.h index a800719d7c..716fd9a247 100644 --- a/include/wx/palmos/font.h +++ b/include/wx/palmos/font.h @@ -116,7 +116,7 @@ public: virtual void SetUnderlined(bool underlined); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); virtual bool IsFixedWidth() const; diff --git a/include/wx/x11/font.h b/include/wx/x11/font.h index 1093602daf..bfe33ee3e0 100644 --- a/include/wx/x11/font.h +++ b/include/wx/x11/font.h @@ -98,7 +98,7 @@ public: virtual void SetUnderlined(bool underlined); virtual void SetEncoding(wxFontEncoding encoding); - WXDECLARE_COMPAT_SETTERS + wxDECLARE_COMMON_FONT_METHODS(); virtual void SetNoAntiAliasing( bool no = TRUE ); virtual bool GetNoAntiAliasing() const ; diff --git a/interface/wx/font.h b/interface/wx/font.h index b880ee1cec..c814de2baa 100644 --- a/interface/wx/font.h +++ b/interface/wx/font.h @@ -19,23 +19,23 @@ enum wxFontFamily { wxFONTFAMILY_DEFAULT = wxDEFAULT, //!< Chooses a default font. - + wxFONTFAMILY_DECORATIVE = wxDECORATIVE, //!< A decorative font. wxFONTFAMILY_ROMAN = wxROMAN, //!< A formal, serif font. wxFONTFAMILY_SCRIPT = wxSCRIPT, //!< A handwriting font. wxFONTFAMILY_SWISS = wxSWISS, //!< A sans-serif font. - + /// A fixed pitch font. Note that wxFont currently does not make distinctions /// between @c wxFONTFAMILY_MODERN and @c wxFONTFAMILY_TELETYPE. wxFONTFAMILY_MODERN = wxMODERN, - + /// A teletype (i.e. monospaced) font. - /// Monospace fonts have a fixed width like typewriters and often have strong angular - /// or block serifs. Monospace font faces are often used code samples and have a simple, + /// Monospace fonts have a fixed width like typewriters and often have strong angular + /// or block serifs. Monospace font faces are often used code samples and have a simple, /// functional font style. /// See also wxFont::IsFixedWidth() for an easy way to test for monospace property. wxFONTFAMILY_TELETYPE = wxTELETYPE, - + /// Returned by wxFont::GetFamily() when the face name of the font cannot /// be classified into one of the previous wxFontFamily values. wxFONTFAMILY_UNKNOWN = wxFONTFAMILY_MAX, @@ -53,8 +53,8 @@ enum wxFontStyle /// The font is slanted in an italic style. wxFONTSTYLE_ITALIC = wxITALIC, - - /// The font is slanted, but in a roman style. + + /// The font is slanted, but in a roman style. /// Note that under wxMSW this style is the same as @c wxFONTSTYLE_ITALIC. wxFONTSTYLE_SLANT = wxSLANT, @@ -111,7 +111,7 @@ enum wxFontFlag /** Font encodings. - + See wxFont::SetEncoding(). */ enum wxFontEncoding @@ -298,7 +298,7 @@ public: @param style One of @c wxFONTSTYLE_NORMAL, @c wxFONTSTYLE_SLANT and @c wxFONTSTYLE_ITALIC. @param weight - Font weight, sometimes also referred to as font boldness. + Font weight, sometimes also referred to as font boldness. One of the ::wxFontWeight enumeration values. @param underline The value can be @true or @false. @@ -313,8 +313,8 @@ public: @c wxFONTENCODING_SYSTEMDefault system encoding. @c wxFONTENCODING_DEFAULT Default application encoding: this is the encoding set by calls to - SetDefaultEncoding() and which may be set to, say, KOI8 to create all - fonts by default with KOI8 encoding. Initially, the default application + SetDefaultEncoding() and which may be set to, say, KOI8 to create all + fonts by default with KOI8 encoding. Initially, the default application encoding is the same as default system encoding. @c wxFONTENCODING_ISO8859_1...15ISO8859 encodings. @c wxFONTENCODING_KOI8The standard Russian encoding for Internet. @@ -359,8 +359,8 @@ public: @c wxFONTENCODING_SYSTEMDefault system encoding. @c wxFONTENCODING_DEFAULT Default application encoding: this is the encoding set by calls to - SetDefaultEncoding() and which may be set to, say, KOI8 to create all - fonts by default with KOI8 encoding. Initially, the default application + SetDefaultEncoding() and which may be set to, say, KOI8 to create all + fonts by default with KOI8 encoding. Initially, the default application encoding is the same as default system encoding. @c wxFONTENCODING_ISO8859_1...15ISO8859 encodings. @c wxFONTENCODING_KOI8The standard Russian encoding for Internet. @@ -401,21 +401,21 @@ public: */ virtual ~wxFont(); - + /** @name Getters */ //@{ - + /** Returns the encoding of this font. - + Note that under wxGTK the returned value is always @c wxFONTENCODING_UTF8. - + @see SetEncoding() */ virtual wxFontEncoding GetEncoding() const; - + /** Returns the face name associated with the font, or the empty string if there is no face information. @@ -428,12 +428,12 @@ public: Gets the font family. As described in ::wxFontFamily docs the returned value acts as a rough, basic classification of the main font properties (look, spacing). - + If the current font face name is not recognized by wxFont or by the underlying system, @c wxFONTFAMILY_UNKNOWN is returned. - + Note that currently this function is rather unreliable (@c wxFONTFAMILY_UNKNOWN - is returned in too many cases) and not particularly useful. + is returned in too many cases) and not particularly useful. Font families mostly make sense only for font creation; see SetFamily(). @see SetFamily() @@ -454,7 +454,7 @@ public: /** Returns a user-friendly string for this font object. Returned string is always non-empty. - + The string does not encode all wxFont infos under all platforms; e.g. under wxMSW the font family is not present in the returned string. @@ -474,7 +474,7 @@ public: /** Gets the pixel size. - + Note that under wxMSW if you passed to SetPixelSize() (or to the ctor) a wxSize object with a null width value, you'll get a null width in the returned object. @@ -482,7 +482,7 @@ public: @see SetPixelSize() */ virtual wxSize GetPixelSize() const; - + /** Gets the font style. See ::wxFontStyle for a list of valid styles. @@ -507,7 +507,7 @@ public: /** Returns @true if the font is a fixed width (or monospaced) font, @false if it is a proportional one or font is invalid. - + Note that this function under some platforms is different than just testing for the font family being equal to @c wxFONTFAMILY_TELETYPE because native platform-specific functions are used for the check (resulting in a more @@ -519,14 +519,61 @@ public: Returns @true if this object is a valid font, @false otherwise. */ virtual bool IsOk() const; - + + //@} + + + /** + @name Similar fonts creation + + The functions in this section create a font similar to the given one + but with its weight, style or size changed. + */ + //@{ + + /** + Return a bold version of this font. + */ + wxFont MakeBold() const; + + /** + Return an italic version of this font. + */ + wxFont MakeItalic() const; + + /** + Return a larger version of this font. + + The font size is multiplied by CSS specification inspired factor of @c + 1.2. + + @see MakeSmaller(), Scale() + */ + wxFont MakeLarger() const; + + /** + Return a smaller version of this font. + + The font size is divided by CSS specification inspired factor of @c + 1.2. + + @see MakeLarger(), Scale() + */ + wxFont MakeSmaller() const; + + /** + Return a scaled version of this font. + + The font size is multiplied by the given factor (which may be less than + 1 to create a smaller version of the font). + */ + wxFont Scale(float x) const; + //@} - - - + /** @name Setters - + These functions internally recreate the native font object with the new specified property. */ @@ -534,10 +581,10 @@ public: /** Sets the encoding for this font. - + Note that under wxGTK this function has no effect (because the underlying Pango library always uses @c wxFONTENCODING_UTF8). - + @see GetEncoding() */ virtual void SetEncoding(wxFontEncoding encoding); @@ -552,7 +599,7 @@ public: but specify the font family instead (see ::wxFontFamily and SetFamily()). @return @true if the given face name exists; if the face name doesn't exist - in the user's system then the font is invalidated (so that IsOk() will + in the user's system then the font is invalidated (so that IsOk() will return @false) and @false is returned. @see GetFaceName(), SetFamily() @@ -561,7 +608,7 @@ public: /** Sets the font family. - + As described in ::wxFontFamily docs the given @a family value acts as a rough, basic indication of the main font properties (look, spacing). @@ -606,7 +653,7 @@ public: For more detailed information about the allowed syntaxes you can look at the documentation of the native API used for font-rendering (e.g. @c pango_font_description_from_string on GTK). - + Note that unlike SetNativeFontInfo(), this function doesn't always restore all attributes of the wxFont object under all platforms; e.g. on wxMSW the font family is not restored (because GetNativeFontInfoUserDesc doesn't return it on wxMSW). @@ -619,9 +666,9 @@ public: /** Sets the point size. - - The point size is defined as 1/72 of the anglo-Saxon inch - (25.4 mm): it is approximately 0.0139 inch or 352.8 um. + + The point size is defined as 1/72 of the anglo-Saxon inch + (25.4 mm): it is approximately 0.0139 inch or 352.8 um. @param pointSize Size in points. @@ -632,19 +679,19 @@ public: /** Sets the pixel size. - + The height parameter of @a pixelSize must be positive while the width parameter may also be zero (to indicate that you're not interested in the width of the characters: a suitable width will be chosen for best rendering). - - This feature (specifying the font pixel size) is directly supported only - under wxMSW and wxGTK currently; under other platforms a font with the + + This feature (specifying the font pixel size) is directly supported only + under wxMSW and wxGTK currently; under other platforms a font with the closest size to the given one is found using binary search (this maybe slower). @see GetPixelSize() */ virtual void SetPixelSize(const wxSize& pixelSize); - + /** Sets the font style. @@ -674,7 +721,7 @@ public: @see GetWeight() */ virtual void SetWeight(wxFontWeight weight); - + //@} @@ -698,8 +745,8 @@ public: Assignment operator, using @ref overview_refcount "reference counting". */ wxFont& operator =(const wxFont& font); - - + + // statics /** @@ -715,7 +762,7 @@ public: @see @ref overview_fontencoding, GetDefaultEncoding() */ static void SetDefaultEncoding(wxFontEncoding encoding); - + //@{ /** This function takes the same parameters as the relative @@ -754,7 +801,7 @@ wxFont wxNullFont; /** Equivalent to wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT). - + @see wxSystemSettings */ wxFont wxNORMAL_FONT; diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index ef779b3fb4..887145f0e0 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -421,6 +421,26 @@ bool wxFontBase::SetFaceName(const wxString& facename) return true; } +wxFont wxFont::MakeBold() const +{ + wxFont font(*this); + font.SetWeight(wxFONTWEIGHT_BOLD); + return font; +} + +wxFont wxFont::MakeItalic() const +{ + wxFont font(*this); + font.SetStyle(wxFONTSTYLE_ITALIC); + return font; +} + +wxFont wxFont::Scale(float x) const +{ + wxFont font(*this); + font.SetPointSize(int(x*GetPointSize() + 0.5)); + return font; +} // ---------------------------------------------------------------------------- // wxNativeFontInfo -- 2.45.2