1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFontBase class: the interface of wxFont
4 // Author: Vadim Zeitlin
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_FONT_H_BASE_
12 #define _WX_FONT_H_BASE_
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 #include "wx/defs.h" // for wxDEFAULT &c
19 #include "wx/fontenc.h" // the font encoding constants
20 #include "wx/gdiobj.h" // the base class
21 #include "wx/gdicmn.h" // for wxGDIObjListBase
23 // ----------------------------------------------------------------------------
24 // forward declarations
25 // ----------------------------------------------------------------------------
27 class WXDLLIMPEXP_FWD_CORE wxFont
;
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // standard font families: these may be used only for the font creation, it
34 // doesn't make sense to query an existing font for its font family as,
35 // especially if the font had been created from a native font description, it
39 wxFONTFAMILY_DEFAULT
= wxDEFAULT
,
40 wxFONTFAMILY_DECORATIVE
= wxDECORATIVE
,
41 wxFONTFAMILY_ROMAN
= wxROMAN
,
42 wxFONTFAMILY_SCRIPT
= wxSCRIPT
,
43 wxFONTFAMILY_SWISS
= wxSWISS
,
44 wxFONTFAMILY_MODERN
= wxMODERN
,
45 wxFONTFAMILY_TELETYPE
= wxTELETYPE
,
47 wxFONTFAMILY_UNKNOWN
= wxFONTFAMILY_MAX
53 wxFONTSTYLE_NORMAL
= wxNORMAL
,
54 wxFONTSTYLE_ITALIC
= wxITALIC
,
55 wxFONTSTYLE_SLANT
= wxSLANT
,
62 wxFONTWEIGHT_NORMAL
= wxNORMAL
,
63 wxFONTWEIGHT_LIGHT
= wxLIGHT
,
64 wxFONTWEIGHT_BOLD
= wxBOLD
,
68 // Symbolic font sizes as defined in CSS specification.
69 enum wxFontSymbolicSize
71 wxFONTSIZE_XX_SMALL
= -3,
80 // the font flag bits for the new font ctor accepting one combined flags word
83 // no special flags: font with default weight/slant/anti-aliasing
84 wxFONTFLAG_DEFAULT
= 0,
86 // slant flags (default: no slant)
87 wxFONTFLAG_ITALIC
= 1 << 0,
88 wxFONTFLAG_SLANT
= 1 << 1,
90 // weight flags (default: medium)
91 wxFONTFLAG_LIGHT
= 1 << 2,
92 wxFONTFLAG_BOLD
= 1 << 3,
94 // anti-aliasing flag: force on or off (default: the current system default)
95 wxFONTFLAG_ANTIALIASED
= 1 << 4,
96 wxFONTFLAG_NOT_ANTIALIASED
= 1 << 5,
98 // underlined/strikethrough flags (default: no lines)
99 wxFONTFLAG_UNDERLINED
= 1 << 6,
100 wxFONTFLAG_STRIKETHROUGH
= 1 << 7,
102 // the mask of all currently used flags
103 wxFONTFLAG_MASK
= wxFONTFLAG_ITALIC
|
107 wxFONTFLAG_ANTIALIASED
|
108 wxFONTFLAG_NOT_ANTIALIASED
|
109 wxFONTFLAG_UNDERLINED
|
110 wxFONTFLAG_STRIKETHROUGH
113 // ----------------------------------------------------------------------------
114 // wxFontInfo describes a wxFont
115 // ----------------------------------------------------------------------------
120 // Default ctor uses the default font size appropriate for the current
123 { InitPointSize(-1); }
125 // These ctors specify the font size, either in points or in pixels.
126 wxEXPLICIT
wxFontInfo(int pointSize
)
127 { InitPointSize(pointSize
); }
128 wxEXPLICIT
wxFontInfo(const wxSize
& pixelSize
) : m_pixelSize(pixelSize
)
131 // Setters for the various attributes. All of them return the object itself
132 // so that the calls to them could be chained.
133 wxFontInfo
& Family(wxFontFamily family
)
134 { m_family
= family
; return *this; }
135 wxFontInfo
& FaceName(const wxString
& faceName
)
136 { m_faceName
= faceName
; return *this; }
138 wxFontInfo
& Bold(bool bold
= true)
139 { SetFlag(wxFONTFLAG_BOLD
, bold
); return *this; }
140 wxFontInfo
& Light(bool light
= true)
141 { SetFlag(wxFONTFLAG_LIGHT
, light
); return *this; }
143 wxFontInfo
& Italic(bool italic
= true)
144 { SetFlag(wxFONTFLAG_ITALIC
, italic
); return *this; }
145 wxFontInfo
& Slant(bool slant
= true)
146 { SetFlag(wxFONTFLAG_SLANT
, slant
); return *this; }
148 wxFontInfo
& AntiAliased(bool antiAliased
= true)
149 { SetFlag(wxFONTFLAG_ANTIALIASED
, antiAliased
); return *this; }
150 wxFontInfo
& Underlined(bool underlined
= true)
151 { SetFlag(wxFONTFLAG_UNDERLINED
, underlined
); return *this; }
152 wxFontInfo
& Strikethrough(bool strikethrough
= true)
153 { SetFlag(wxFONTFLAG_STRIKETHROUGH
, strikethrough
); return *this; }
155 wxFontInfo
& Encoding(wxFontEncoding encoding
)
156 { m_encoding
= encoding
; return *this; }
159 // Set all flags at once.
160 wxFontInfo
& AllFlags(int flags
)
161 { m_flags
= flags
; return *this; }
164 // Accessors are mostly meant to be used by wxFont itself to extract the
165 // various pieces of the font description.
167 bool IsUsingSizeInPixels() const { return m_pixelSize
!= wxDefaultSize
; }
168 int GetPointSize() const { return m_pointSize
; }
169 wxSize
GetPixelSize() const { return m_pixelSize
; }
170 wxFontFamily
GetFamily() const { return m_family
; }
171 const wxString
& GetFaceName() const { return m_faceName
; }
173 wxFontStyle
GetStyle() const
175 return m_flags
& wxFONTFLAG_ITALIC
177 : m_flags
& wxFONTFLAG_SLANT
179 : wxFONTSTYLE_NORMAL
;
182 wxFontWeight
GetWeight() const
184 return m_flags
& wxFONTFLAG_LIGHT
186 : m_flags
& wxFONTFLAG_BOLD
188 : wxFONTWEIGHT_NORMAL
;
191 bool IsAntiAliased() const
193 return (m_flags
& wxFONTFLAG_ANTIALIASED
) != 0;
196 bool IsUnderlined() const
198 return (m_flags
& wxFONTFLAG_UNDERLINED
) != 0;
201 bool IsStrikethrough() const
203 return (m_flags
& wxFONTFLAG_STRIKETHROUGH
) != 0;
206 wxFontEncoding
GetEncoding() const { return m_encoding
; }
209 // Default copy ctor, assignment operator and dtor are OK.
212 // Common part of all ctor, initializing everything except the size (which
213 // is initialized by the ctors themselves).
216 m_family
= wxFONTFAMILY_DEFAULT
;
217 m_flags
= wxFONTFLAG_DEFAULT
;
218 m_encoding
= wxFONTENCODING_DEFAULT
;
221 void InitPointSize(int pointSize
)
225 m_pointSize
= pointSize
;
226 m_pixelSize
= wxDefaultSize
;
229 // Turn on or off the given bit in m_flags depending on the value of the
231 void SetFlag(int flag
, bool on
)
239 // The size information: if m_pixelSize is valid (!= wxDefaultSize), then
240 // it is used. Otherwise m_pointSize is used, taking into account that if
241 // it is == -1, it means that the platform dependent font size should be
246 wxFontFamily m_family
;
249 wxFontEncoding m_encoding
;
252 // ----------------------------------------------------------------------------
253 // wxFontBase represents a font object
254 // ----------------------------------------------------------------------------
256 class WXDLLIMPEXP_FWD_CORE wxNativeFontInfo
;
258 class WXDLLIMPEXP_CORE wxFontBase
: public wxGDIObject
262 derived classes should provide the following ctors:
265 wxFont(const wxFontInfo& info);
266 wxFont(const wxString& nativeFontInfoString);
267 wxFont(const wxNativeFontInfo& info);
272 bool underlined = false,
273 const wxString& face = wxEmptyString,
274 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
275 wxFont(const wxSize& pixelSize,
279 bool underlined = false,
280 const wxString& face = wxEmptyString,
281 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
285 virtual ~wxFontBase();
288 #if FUTURE_WXWIN_COMPATIBILITY_3_0
289 // from the font components
291 int pointSize
, // size of the font in points
292 int family
, // see wxFontFamily enum
293 int style
, // see wxFontStyle enum
294 int weight
, // see wxFontWeight enum
295 bool underlined
= false, // not underlined by default
296 const wxString
& face
= wxEmptyString
, // facename
297 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
) // ISO8859-X, ...
298 { return New(pointSize
, (wxFontFamily
)family
, (wxFontStyle
)style
,
299 (wxFontWeight
)weight
, underlined
, face
, encoding
); }
301 // from the font components
303 const wxSize
& pixelSize
, // size of the font in pixels
304 int family
, // see wxFontFamily enum
305 int style
, // see wxFontStyle enum
306 int weight
, // see wxFontWeight enum
307 bool underlined
= false, // not underlined by default
308 const wxString
& face
= wxEmptyString
, // facename
309 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
) // ISO8859-X, ...
310 { return New(pixelSize
, (wxFontFamily
)family
, (wxFontStyle
)style
,
311 (wxFontWeight
)weight
, underlined
, face
, encoding
); }
314 // from the font components
316 int pointSize
, // size of the font in points
317 wxFontFamily family
, // see wxFontFamily enum
318 wxFontStyle style
, // see wxFontStyle enum
319 wxFontWeight weight
, // see wxFontWeight enum
320 bool underlined
= false, // not underlined by default
321 const wxString
& face
= wxEmptyString
, // facename
322 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
); // ISO8859-X, ...
324 // from the font components
326 const wxSize
& pixelSize
, // size of the font in pixels
327 wxFontFamily family
, // see wxFontFamily enum
328 wxFontStyle style
, // see wxFontStyle enum
329 wxFontWeight weight
, // see wxFontWeight enum
330 bool underlined
= false, // not underlined by default
331 const wxString
& face
= wxEmptyString
, // facename
332 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
); // ISO8859-X, ...
334 // from the font components but using the font flags instead of separate
335 // parameters for each flag
336 static wxFont
*New(int pointSize
,
338 int flags
= wxFONTFLAG_DEFAULT
,
339 const wxString
& face
= wxEmptyString
,
340 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
343 // from the font components but using the font flags instead of separate
344 // parameters for each flag
345 static wxFont
*New(const wxSize
& pixelSize
,
347 int flags
= wxFONTFLAG_DEFAULT
,
348 const wxString
& face
= wxEmptyString
,
349 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
351 // from the (opaque) native font description object
352 static wxFont
*New(const wxNativeFontInfo
& nativeFontDesc
);
354 // from the string representation of wxNativeFontInfo
355 static wxFont
*New(const wxString
& strNativeFontDesc
);
358 bool operator==(const wxFont
& font
) const;
359 bool operator!=(const wxFont
& font
) const { return !(*this == font
); }
361 // accessors: get the font characteristics
362 virtual int GetPointSize() const = 0;
363 virtual wxSize
GetPixelSize() const;
364 virtual bool IsUsingSizeInPixels() const;
365 wxFontFamily
GetFamily() const;
366 virtual wxFontStyle
GetStyle() const = 0;
367 virtual wxFontWeight
GetWeight() const = 0;
368 virtual bool GetUnderlined() const = 0;
369 virtual bool GetStrikethrough() const { return false; }
370 virtual wxString
GetFaceName() const = 0;
371 virtual wxFontEncoding
GetEncoding() const = 0;
372 virtual const wxNativeFontInfo
*GetNativeFontInfo() const = 0;
374 virtual bool IsFixedWidth() const;
376 wxString
GetNativeFontInfoDesc() const;
377 wxString
GetNativeFontInfoUserDesc() const;
379 // change the font characteristics
380 virtual void SetPointSize( int pointSize
) = 0;
381 virtual void SetPixelSize( const wxSize
& pixelSize
);
382 virtual void SetFamily( wxFontFamily family
) = 0;
383 virtual void SetStyle( wxFontStyle style
) = 0;
384 virtual void SetWeight( wxFontWeight weight
) = 0;
386 virtual void SetUnderlined( bool underlined
) = 0;
387 virtual void SetStrikethrough( bool WXUNUSED(strikethrough
) ) {}
388 virtual void SetEncoding(wxFontEncoding encoding
) = 0;
389 virtual bool SetFaceName( const wxString
& faceName
);
390 void SetNativeFontInfo(const wxNativeFontInfo
& info
)
391 { DoSetNativeFontInfo(info
); }
393 bool SetNativeFontInfo(const wxString
& info
);
394 bool SetNativeFontInfoUserDesc(const wxString
& info
);
396 // Symbolic font sizes support: set the font size to "large" or "very
397 // small" either absolutely (i.e. compared to the default font size) or
398 // relatively to the given font size.
399 void SetSymbolicSize(wxFontSymbolicSize size
);
400 void SetSymbolicSizeRelativeTo(wxFontSymbolicSize size
, int base
)
402 SetPointSize(AdjustToSymbolicSize(size
, base
));
405 // Adjust the base size in points according to symbolic size.
406 static int AdjustToSymbolicSize(wxFontSymbolicSize size
, int base
);
409 // translate the fonts into human-readable string (i.e. GetStyleString()
410 // will return "wxITALIC" for an italic font, ...)
411 wxString
GetFamilyString() const;
412 wxString
GetStyleString() const;
413 wxString
GetWeightString() const;
415 // the default encoding is used for creating all fonts with default
416 // encoding parameter
417 static wxFontEncoding
GetDefaultEncoding() { return ms_encodingDefault
; }
418 static void SetDefaultEncoding(wxFontEncoding encoding
);
420 // this doesn't do anything and is kept for compatibility only
421 #if WXWIN_COMPATIBILITY_2_8
422 wxDEPRECATED_INLINE(void SetNoAntiAliasing(bool no
= true), wxUnusedVar(no
););
423 wxDEPRECATED_INLINE(bool GetNoAntiAliasing() const, return false;)
424 #endif // WXWIN_COMPATIBILITY_2_8
427 // the function called by both overloads of SetNativeFontInfo()
428 virtual void DoSetNativeFontInfo(const wxNativeFontInfo
& info
);
430 // The function called by public GetFamily(): it can return
431 // wxFONTFAMILY_UNKNOWN unlike the public method (see comment there).
432 virtual wxFontFamily
DoGetFamily() const = 0;
435 // Helper functions to recover wxFONTSTYLE/wxFONTWEIGHT and underlined flag
436 // values from flags containing a combination of wxFONTFLAG_XXX.
437 static wxFontStyle
GetStyleFromFlags(int flags
)
439 return flags
& wxFONTFLAG_ITALIC
441 : flags
& wxFONTFLAG_SLANT
443 : wxFONTSTYLE_NORMAL
;
446 static wxFontWeight
GetWeightFromFlags(int flags
)
448 return flags
& wxFONTFLAG_LIGHT
450 : flags
& wxFONTFLAG_BOLD
452 : wxFONTWEIGHT_NORMAL
;
455 static bool GetUnderlinedFromFlags(int flags
)
457 return (flags
& wxFONTFLAG_UNDERLINED
) != 0;
460 static bool GetStrikethroughFromFlags(int flags
)
462 return (flags
& wxFONTFLAG_STRIKETHROUGH
) != 0;
466 // the currently default encoding: by default, it's the default system
467 // encoding, but may be changed by the application using
468 // SetDefaultEncoding() to make all subsequent fonts created without
469 // specifying encoding parameter using this encoding
470 static wxFontEncoding ms_encodingDefault
;
473 // wxFontBase <-> wxString utilities, used by wxConfig
474 WXDLLIMPEXP_CORE wxString
wxToString(const wxFontBase
& font
);
475 WXDLLIMPEXP_CORE
bool wxFromString(const wxString
& str
, wxFontBase
* font
);
478 #if FUTURE_WXWIN_COMPATIBILITY_3_0
479 #define wxDECLARE_FONT_COMPAT_SETTER \
480 wxDEPRECATED_FUTURE( void SetFamily(int family) ) \
481 { SetFamily((wxFontFamily)family); } \
482 wxDEPRECATED_FUTURE( void SetStyle(int style) ) \
483 { SetStyle((wxFontStyle)style); } \
484 wxDEPRECATED_FUTURE( void SetWeight(int weight) ) \
485 { SetWeight((wxFontWeight)weight); } \
486 wxDEPRECATED_FUTURE( void SetFamily(wxDeprecatedGUIConstants family) ) \
487 { SetFamily((wxFontFamily)family); } \
488 wxDEPRECATED_FUTURE( void SetStyle(wxDeprecatedGUIConstants style) ) \
489 { SetStyle((wxFontStyle)style); } \
490 wxDEPRECATED_FUTURE( void SetWeight(wxDeprecatedGUIConstants weight) ) \
491 { SetWeight((wxFontWeight)weight); }
493 #define wxDECLARE_FONT_COMPAT_SETTER /*empty*/
496 // this macro must be used in all derived wxFont classes declarations
497 #define wxDECLARE_COMMON_FONT_METHODS() \
498 wxDECLARE_FONT_COMPAT_SETTER \
500 /* functions for modifying font in place */ \
501 wxFont& MakeBold(); \
502 wxFont& MakeItalic(); \
503 wxFont& MakeUnderlined(); \
504 wxFont& MakeStrikethrough(); \
505 wxFont& MakeLarger() { return Scale(1.2f); } \
506 wxFont& MakeSmaller() { return Scale(1/1.2f); } \
507 wxFont& Scale(float x); \
508 /* functions for creating fonts based on this one */ \
509 wxFont Bold() const; \
510 wxFont Italic() const; \
511 wxFont Underlined() const; \
512 wxFont Strikethrough() const; \
513 wxFont Larger() const { return Scaled(1.2f); } \
514 wxFont Smaller() const { return Scaled(1/1.2f); } \
515 wxFont Scaled(float x) const
517 // include the real class declaration
518 #if defined(__WXMSW__)
519 #include "wx/msw/font.h"
520 #elif defined(__WXMOTIF__)
521 #include "wx/motif/font.h"
522 #elif defined(__WXGTK20__)
523 #include "wx/gtk/font.h"
524 #elif defined(__WXGTK__)
525 #include "wx/gtk1/font.h"
526 #elif defined(__WXX11__)
527 #include "wx/x11/font.h"
528 #elif defined(__WXDFB__)
529 #include "wx/dfb/font.h"
530 #elif defined(__WXMAC__)
531 #include "wx/osx/font.h"
532 #elif defined(__WXCOCOA__)
533 #include "wx/cocoa/font.h"
534 #elif defined(__WXPM__)
535 #include "wx/os2/font.h"
538 class WXDLLIMPEXP_CORE wxFontList
: public wxGDIObjListBase
541 wxFont
*FindOrCreateFont(int pointSize
,
545 bool underline
= false,
546 const wxString
& face
= wxEmptyString
,
547 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
549 #if FUTURE_WXWIN_COMPATIBILITY_3_0
550 wxFont
*FindOrCreateFont(int pointSize
, int family
, int style
, int weight
,
551 bool underline
= false,
552 const wxString
& face
= wxEmptyString
,
553 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
554 { return FindOrCreateFont(pointSize
, (wxFontFamily
)family
, (wxFontStyle
)style
,
555 (wxFontWeight
)weight
, underline
, face
, encoding
); }
558 #if WXWIN_COMPATIBILITY_2_6
559 wxDEPRECATED( void AddFont(wxFont
*) );
560 wxDEPRECATED( void RemoveFont(wxFont
*) );
564 extern WXDLLIMPEXP_DATA_CORE(wxFontList
*) wxTheFontList
;
567 // provide comparison operators to allow code such as
569 // if ( font.GetStyle() == wxFONTSTYLE_SLANT )
571 // to compile without warnings which it would otherwise provoke from some
572 // compilers as it compares elements of different enums
573 #if FUTURE_WXWIN_COMPATIBILITY_3_0
575 // Unfortunately some compilers have ambiguity issues when enum comparisons are
576 // overloaded so we have to disable the overloads in this case, see
577 // wxCOMPILER_NO_OVERLOAD_ON_ENUM definition in wx/platform.h for more details.
578 #ifndef wxCOMPILER_NO_OVERLOAD_ON_ENUM
580 inline bool operator==(wxFontFamily s
, wxDeprecatedGUIConstants t
)
581 { return static_cast<int>(s
) == static_cast<int>(t
); }
582 inline bool operator!=(wxFontFamily s
, wxDeprecatedGUIConstants t
)
583 { return !(s
== t
); }
584 inline bool operator==(wxFontStyle s
, wxDeprecatedGUIConstants t
)
585 { return static_cast<int>(s
) == static_cast<int>(t
); }
586 inline bool operator!=(wxFontStyle s
, wxDeprecatedGUIConstants t
)
587 { return !(s
== t
); }
588 inline bool operator==(wxFontWeight s
, wxDeprecatedGUIConstants t
)
589 { return static_cast<int>(s
) == static_cast<int>(t
); }
590 inline bool operator!=(wxFontWeight s
, wxDeprecatedGUIConstants t
)
591 { return !(s
== t
); }
593 #endif // // wxCOMPILER_NO_OVERLOAD_ON_ENUM
595 #endif // FUTURE_WXWIN_COMPATIBILITY_3_0