1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFontBase class: the interface of wxFont
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_FONT_H_BASE_
13 #define _WX_FONT_H_BASE_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/defs.h" // for wxDEFAULT &c
20 #include "wx/fontenc.h" // the font encoding constants
21 #include "wx/gdiobj.h" // the base class
22 #include "wx/gdicmn.h" // for wxGDIObjListBase
24 // ----------------------------------------------------------------------------
25 // forward declarations
26 // ----------------------------------------------------------------------------
28 class WXDLLIMPEXP_FWD_CORE wxFont
;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // standard font families: these may be used only for the font creation, it
35 // doesn't make sense to query an existing font for its font family as,
36 // especially if the font had been created from a native font description, it
40 wxFONTFAMILY_DEFAULT
= wxDEFAULT
,
41 wxFONTFAMILY_DECORATIVE
= wxDECORATIVE
,
42 wxFONTFAMILY_ROMAN
= wxROMAN
,
43 wxFONTFAMILY_SCRIPT
= wxSCRIPT
,
44 wxFONTFAMILY_SWISS
= wxSWISS
,
45 wxFONTFAMILY_MODERN
= wxMODERN
,
46 wxFONTFAMILY_TELETYPE
= wxTELETYPE
,
48 wxFONTFAMILY_UNKNOWN
= wxFONTFAMILY_MAX
54 wxFONTSTYLE_NORMAL
= wxNORMAL
,
55 wxFONTSTYLE_ITALIC
= wxITALIC
,
56 wxFONTSTYLE_SLANT
= wxSLANT
,
63 wxFONTWEIGHT_NORMAL
= wxNORMAL
,
64 wxFONTWEIGHT_LIGHT
= wxLIGHT
,
65 wxFONTWEIGHT_BOLD
= wxBOLD
,
69 // Symbolic font sizes as defined in CSS specification.
70 enum wxFontSymbolicSize
72 wxFONTSIZE_XX_SMALL
= -3,
81 // the font flag bits for the new font ctor accepting one combined flags word
84 // no special flags: font with default weight/slant/anti-aliasing
85 wxFONTFLAG_DEFAULT
= 0,
87 // slant flags (default: no slant)
88 wxFONTFLAG_ITALIC
= 1 << 0,
89 wxFONTFLAG_SLANT
= 1 << 1,
91 // weight flags (default: medium)
92 wxFONTFLAG_LIGHT
= 1 << 2,
93 wxFONTFLAG_BOLD
= 1 << 3,
95 // anti-aliasing flag: force on or off (default: the current system default)
96 wxFONTFLAG_ANTIALIASED
= 1 << 4,
97 wxFONTFLAG_NOT_ANTIALIASED
= 1 << 5,
99 // underlined/strikethrough flags (default: no lines)
100 wxFONTFLAG_UNDERLINED
= 1 << 6,
101 wxFONTFLAG_STRIKETHROUGH
= 1 << 7,
103 // the mask of all currently used flags
104 wxFONTFLAG_MASK
= wxFONTFLAG_ITALIC
|
108 wxFONTFLAG_ANTIALIASED
|
109 wxFONTFLAG_NOT_ANTIALIASED
|
110 wxFONTFLAG_UNDERLINED
|
111 wxFONTFLAG_STRIKETHROUGH
114 // ----------------------------------------------------------------------------
115 // wxFontBase represents a font object
116 // ----------------------------------------------------------------------------
118 class WXDLLIMPEXP_FWD_CORE wxNativeFontInfo
;
120 class WXDLLIMPEXP_CORE wxFontBase
: public wxGDIObject
124 derived classes should provide the following ctors:
127 wxFont(const wxString& nativeFontInfoString);
128 wxFont(const wxNativeFontInfo& info);
133 bool underlined = false,
134 const wxString& face = wxEmptyString,
135 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
136 wxFont(const wxSize& pixelSize,
140 bool underlined = false,
141 const wxString& face = wxEmptyString,
142 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
146 virtual ~wxFontBase();
149 #if FUTURE_WXWIN_COMPATIBILITY_3_0
150 // from the font components
152 int pointSize
, // size of the font in points
153 int family
, // see wxFontFamily enum
154 int style
, // see wxFontStyle enum
155 int weight
, // see wxFontWeight enum
156 bool underlined
= false, // not underlined by default
157 const wxString
& face
= wxEmptyString
, // facename
158 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
) // ISO8859-X, ...
159 { return New(pointSize
, (wxFontFamily
)family
, (wxFontStyle
)style
,
160 (wxFontWeight
)weight
, underlined
, face
, encoding
); }
162 // from the font components
164 const wxSize
& pixelSize
, // size of the font in pixels
165 int family
, // see wxFontFamily enum
166 int style
, // see wxFontStyle enum
167 int weight
, // see wxFontWeight enum
168 bool underlined
= false, // not underlined by default
169 const wxString
& face
= wxEmptyString
, // facename
170 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
) // ISO8859-X, ...
171 { return New(pixelSize
, (wxFontFamily
)family
, (wxFontStyle
)style
,
172 (wxFontWeight
)weight
, underlined
, face
, encoding
); }
175 // from the font components
177 int pointSize
, // size of the font in points
178 wxFontFamily family
, // see wxFontFamily enum
179 wxFontStyle style
, // see wxFontStyle enum
180 wxFontWeight weight
, // see wxFontWeight enum
181 bool underlined
= false, // not underlined by default
182 const wxString
& face
= wxEmptyString
, // facename
183 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
); // ISO8859-X, ...
185 // from the font components
187 const wxSize
& pixelSize
, // size of the font in pixels
188 wxFontFamily family
, // see wxFontFamily enum
189 wxFontStyle style
, // see wxFontStyle enum
190 wxFontWeight weight
, // see wxFontWeight enum
191 bool underlined
= false, // not underlined by default
192 const wxString
& face
= wxEmptyString
, // facename
193 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
); // ISO8859-X, ...
195 // from the font components but using the font flags instead of separate
196 // parameters for each flag
197 static wxFont
*New(int pointSize
,
199 int flags
= wxFONTFLAG_DEFAULT
,
200 const wxString
& face
= wxEmptyString
,
201 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
204 // from the font components but using the font flags instead of separate
205 // parameters for each flag
206 static wxFont
*New(const wxSize
& pixelSize
,
208 int flags
= wxFONTFLAG_DEFAULT
,
209 const wxString
& face
= wxEmptyString
,
210 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
212 // from the (opaque) native font description object
213 static wxFont
*New(const wxNativeFontInfo
& nativeFontDesc
);
215 // from the string representation of wxNativeFontInfo
216 static wxFont
*New(const wxString
& strNativeFontDesc
);
219 bool operator==(const wxFont
& font
) const;
220 bool operator!=(const wxFont
& font
) const { return !(*this == font
); }
222 // accessors: get the font characteristics
223 virtual int GetPointSize() const = 0;
224 virtual wxSize
GetPixelSize() const;
225 virtual bool IsUsingSizeInPixels() const;
226 wxFontFamily
GetFamily() const;
227 virtual wxFontStyle
GetStyle() const = 0;
228 virtual wxFontWeight
GetWeight() const = 0;
229 virtual bool GetUnderlined() const = 0;
230 virtual bool GetStrikethrough() const { return false; }
231 virtual wxString
GetFaceName() const = 0;
232 virtual wxFontEncoding
GetEncoding() const = 0;
233 virtual const wxNativeFontInfo
*GetNativeFontInfo() const = 0;
235 virtual bool IsFixedWidth() const;
237 wxString
GetNativeFontInfoDesc() const;
238 wxString
GetNativeFontInfoUserDesc() const;
240 // change the font characteristics
241 virtual void SetPointSize( int pointSize
) = 0;
242 virtual void SetPixelSize( const wxSize
& pixelSize
);
243 virtual void SetFamily( wxFontFamily family
) = 0;
244 virtual void SetStyle( wxFontStyle style
) = 0;
245 virtual void SetWeight( wxFontWeight weight
) = 0;
247 virtual void SetUnderlined( bool underlined
) = 0;
248 virtual void SetStrikethrough( bool WXUNUSED(strikethrough
) ) {}
249 virtual void SetEncoding(wxFontEncoding encoding
) = 0;
250 virtual bool SetFaceName( const wxString
& faceName
);
251 void SetNativeFontInfo(const wxNativeFontInfo
& info
)
252 { DoSetNativeFontInfo(info
); }
254 bool SetNativeFontInfo(const wxString
& info
);
255 bool SetNativeFontInfoUserDesc(const wxString
& info
);
257 // Symbolic font sizes support: set the font size to "large" or "very
258 // small" either absolutely (i.e. compared to the default font size) or
259 // relatively to the given font size.
260 void SetSymbolicSize(wxFontSymbolicSize size
);
261 void SetSymbolicSizeRelativeTo(wxFontSymbolicSize size
, int base
)
263 SetPointSize(AdjustToSymbolicSize(size
, base
));
266 // Adjust the base size in points according to symbolic size.
267 static int AdjustToSymbolicSize(wxFontSymbolicSize size
, int base
);
270 // translate the fonts into human-readable string (i.e. GetStyleString()
271 // will return "wxITALIC" for an italic font, ...)
272 wxString
GetFamilyString() const;
273 wxString
GetStyleString() const;
274 wxString
GetWeightString() const;
276 // the default encoding is used for creating all fonts with default
277 // encoding parameter
278 static wxFontEncoding
GetDefaultEncoding() { return ms_encodingDefault
; }
279 static void SetDefaultEncoding(wxFontEncoding encoding
);
281 // this doesn't do anything and is kept for compatibility only
282 #if WXWIN_COMPATIBILITY_2_8
283 wxDEPRECATED_INLINE(void SetNoAntiAliasing(bool no
= true), wxUnusedVar(no
););
284 wxDEPRECATED_INLINE(bool GetNoAntiAliasing() const, return false;)
285 #endif // WXWIN_COMPATIBILITY_2_8
288 // the function called by both overloads of SetNativeFontInfo()
289 virtual void DoSetNativeFontInfo(const wxNativeFontInfo
& info
);
291 // The function called by public GetFamily(): it can return
292 // wxFONTFAMILY_UNKNOWN unlike the public method (see comment there).
293 virtual wxFontFamily
DoGetFamily() const = 0;
296 // Helper functions to recover wxFONTSTYLE/wxFONTWEIGHT and underlined flg
297 // values from flags containing a combination of wxFONTFLAG_XXX.
298 static wxFontStyle
GetStyleFromFlags(int flags
)
300 return flags
& wxFONTFLAG_ITALIC
302 : flags
& wxFONTFLAG_SLANT
304 : wxFONTSTYLE_NORMAL
;
307 static wxFontWeight
GetWeightFromFlags(int flags
)
309 return flags
& wxFONTFLAG_LIGHT
311 : flags
& wxFONTFLAG_BOLD
313 : wxFONTWEIGHT_NORMAL
;
316 static bool GetUnderlinedFromFlags(int flags
)
318 return (flags
& wxFONTFLAG_UNDERLINED
) != 0;
323 // the currently default encoding: by default, it's the default system
324 // encoding, but may be changed by the application using
325 // SetDefaultEncoding() to make all subsequent fonts created without
326 // specifying encoding parameter using this encoding
327 static wxFontEncoding ms_encodingDefault
;
330 // wxFontBase <-> wxString utilities, used by wxConfig
331 WXDLLIMPEXP_CORE wxString
wxToString(const wxFontBase
& font
);
332 WXDLLIMPEXP_CORE
bool wxFromString(const wxString
& str
, wxFontBase
* font
);
335 #if FUTURE_WXWIN_COMPATIBILITY_3_0
336 #define wxDECLARE_FONT_COMPAT_SETTER \
337 wxDEPRECATED_FUTURE( void SetFamily(int family) ) \
338 { SetFamily((wxFontFamily)family); } \
339 wxDEPRECATED_FUTURE( void SetStyle(int style) ) \
340 { SetStyle((wxFontStyle)style); } \
341 wxDEPRECATED_FUTURE( void SetWeight(int weight) ) \
342 { SetWeight((wxFontWeight)weight); } \
343 wxDEPRECATED_FUTURE( void SetFamily(wxDeprecatedGUIConstants family) ) \
344 { SetFamily((wxFontFamily)family); } \
345 wxDEPRECATED_FUTURE( void SetStyle(wxDeprecatedGUIConstants style) ) \
346 { SetStyle((wxFontStyle)style); } \
347 wxDEPRECATED_FUTURE( void SetWeight(wxDeprecatedGUIConstants weight) ) \
348 { SetWeight((wxFontWeight)weight); }
350 #define wxDECLARE_FONT_COMPAT_SETTER /*empty*/
353 // this macro must be used in all derived wxFont classes declarations
354 #define wxDECLARE_COMMON_FONT_METHODS() \
355 wxDECLARE_FONT_COMPAT_SETTER \
357 /* functions for modifying font in place */ \
358 wxFont& MakeBold(); \
359 wxFont& MakeItalic(); \
360 wxFont& MakeUnderlined(); \
361 wxFont& MakeStrikethrough(); \
362 wxFont& MakeLarger() { return Scale(1.2f); } \
363 wxFont& MakeSmaller() { return Scale(1/1.2f); } \
364 wxFont& Scale(float x); \
365 /* functions for creating fonts based on this one */ \
366 wxFont Bold() const; \
367 wxFont Italic() const; \
368 wxFont Underlined() const; \
369 wxFont Strikethrough() const; \
370 wxFont Larger() const { return Scaled(1.2f); } \
371 wxFont Smaller() const { return Scaled(1/1.2f); } \
372 wxFont Scaled(float x) const
374 // include the real class declaration
375 #if defined(__WXMSW__)
376 #include "wx/msw/font.h"
377 #elif defined(__WXMOTIF__)
378 #include "wx/motif/font.h"
379 #elif defined(__WXGTK20__)
380 #include "wx/gtk/font.h"
381 #elif defined(__WXGTK__)
382 #include "wx/gtk1/font.h"
383 #elif defined(__WXX11__)
384 #include "wx/x11/font.h"
385 #elif defined(__WXDFB__)
386 #include "wx/dfb/font.h"
387 #elif defined(__WXMAC__)
388 #include "wx/osx/font.h"
389 #elif defined(__WXCOCOA__)
390 #include "wx/cocoa/font.h"
391 #elif defined(__WXPM__)
392 #include "wx/os2/font.h"
395 class WXDLLIMPEXP_CORE wxFontList
: public wxGDIObjListBase
398 wxFont
*FindOrCreateFont(int pointSize
,
402 bool underline
= false,
403 const wxString
& face
= wxEmptyString
,
404 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
406 #if FUTURE_WXWIN_COMPATIBILITY_3_0
407 wxFont
*FindOrCreateFont(int pointSize
, int family
, int style
, int weight
,
408 bool underline
= false,
409 const wxString
& face
= wxEmptyString
,
410 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
411 { return FindOrCreateFont(pointSize
, (wxFontFamily
)family
, (wxFontStyle
)style
,
412 (wxFontWeight
)weight
, underline
, face
, encoding
); }
415 #if WXWIN_COMPATIBILITY_2_6
416 wxDEPRECATED( void AddFont(wxFont
*) );
417 wxDEPRECATED( void RemoveFont(wxFont
*) );
421 extern WXDLLIMPEXP_DATA_CORE(wxFontList
*) wxTheFontList
;
424 // provide comparison operators to allow code such as
426 // if ( font.GetStyle() == wxFONTSTYLE_SLANT )
428 // to compile without warnings which it would otherwise provoke from some
429 // compilers as it compares elements of different enums
430 #if FUTURE_WXWIN_COMPATIBILITY_3_0
432 // Unfortunately some compilers have ambiguity issues when enum comparisons are
433 // overloaded so we have to disable the overloads in this case, see
434 // wxCOMPILER_NO_OVERLOAD_ON_ENUM definition in wx/platform.h for more details.
435 #ifndef wxCOMPILER_NO_OVERLOAD_ON_ENUM
437 inline bool operator==(wxFontFamily s
, wxDeprecatedGUIConstants t
)
438 { return static_cast<int>(s
) == static_cast<int>(t
); }
439 inline bool operator!=(wxFontFamily s
, wxDeprecatedGUIConstants t
)
440 { return !(s
== t
); }
441 inline bool operator==(wxFontStyle s
, wxDeprecatedGUIConstants t
)
442 { return static_cast<int>(s
) == static_cast<int>(t
); }
443 inline bool operator!=(wxFontStyle s
, wxDeprecatedGUIConstants t
)
444 { return !(s
== t
); }
445 inline bool operator==(wxFontWeight s
, wxDeprecatedGUIConstants t
)
446 { return static_cast<int>(s
) == static_cast<int>(t
); }
447 inline bool operator!=(wxFontWeight s
, wxDeprecatedGUIConstants t
)
448 { return !(s
== t
); }
450 #endif // // wxCOMPILER_NO_OVERLOAD_ON_ENUM
452 #endif // FUTURE_WXWIN_COMPATIBILITY_3_0