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 // wxFontInfo describes a wxFont
116 // ----------------------------------------------------------------------------
121 // Default ctor uses the default font size appropriate for the current
124 { InitPointSize(-1); }
126 // These ctors specify the font size, either in points or in pixels.
127 wxEXPLICIT
wxFontInfo(int pointSize
)
128 { InitPointSize(pointSize
); }
129 wxEXPLICIT
wxFontInfo(const wxSize
& pixelSize
) : m_pixelSize(pixelSize
)
132 // Setters for the various attributes. All of them return the object itself
133 // so that the calls to them could be chained.
134 wxFontInfo
& Family(wxFontFamily family
)
135 { m_family
= family
; return *this; }
136 wxFontInfo
& FaceName(const wxString
& faceName
)
137 { m_faceName
= faceName
; return *this; }
139 wxFontInfo
& Bold(bool bold
= true)
140 { SetFlag(wxFONTFLAG_BOLD
, bold
); return *this; }
141 wxFontInfo
& Light(bool light
= true)
142 { SetFlag(wxFONTFLAG_LIGHT
, light
); return *this; }
144 wxFontInfo
& Italic(bool italic
= true)
145 { SetFlag(wxFONTFLAG_ITALIC
, italic
); return *this; }
146 wxFontInfo
& Slant(bool slant
= true)
147 { SetFlag(wxFONTFLAG_SLANT
, slant
); return *this; }
149 wxFontInfo
& AntiAliased(bool antiAliased
= true)
150 { SetFlag(wxFONTFLAG_ANTIALIASED
, antiAliased
); return *this; }
151 wxFontInfo
& Underlined(bool underlined
= true)
152 { SetFlag(wxFONTFLAG_UNDERLINED
, underlined
); return *this; }
153 wxFontInfo
& Strikethrough(bool strikethrough
= true)
154 { SetFlag(wxFONTFLAG_STRIKETHROUGH
, strikethrough
); return *this; }
156 wxFontInfo
& Encoding(wxFontEncoding encoding
)
157 { m_encoding
= encoding
; return *this; }
160 // Set all flags at once.
161 wxFontInfo
& AllFlags(int flags
)
162 { m_flags
= flags
; return *this; }
165 // Accessors are mostly meant to be used by wxFont itself to extract the
166 // various pieces of the font description.
168 bool IsUsingSizeInPixels() const { return m_pixelSize
!= wxDefaultSize
; }
169 int GetPointSize() const { return m_pointSize
; }
170 wxSize
GetPixelSize() const { return m_pixelSize
; }
171 wxFontFamily
GetFamily() const { return m_family
; }
172 const wxString
& GetFaceName() const { return m_faceName
; }
174 wxFontStyle
GetStyle() const
176 return m_flags
& wxFONTFLAG_ITALIC
178 : m_flags
& wxFONTFLAG_SLANT
180 : wxFONTSTYLE_NORMAL
;
183 wxFontWeight
GetWeight() const
185 return m_flags
& wxFONTFLAG_LIGHT
187 : m_flags
& wxFONTFLAG_BOLD
189 : wxFONTWEIGHT_NORMAL
;
192 bool IsAntiAliased() const
194 return (m_flags
& wxFONTFLAG_ANTIALIASED
) != 0;
197 bool IsUnderlined() const
199 return (m_flags
& wxFONTFLAG_UNDERLINED
) != 0;
202 bool IsStrikethrough() const
204 return (m_flags
& wxFONTFLAG_STRIKETHROUGH
) != 0;
207 wxFontEncoding
GetEncoding() const { return m_encoding
; }
210 // Default copy ctor, assignment operator and dtor are OK.
213 // Common part of all ctor, initializing everything except the size (which
214 // is initialized by the ctors themselves).
217 m_family
= wxFONTFAMILY_DEFAULT
;
218 m_flags
= wxFONTFLAG_DEFAULT
;
219 m_encoding
= wxFONTENCODING_DEFAULT
;
222 void InitPointSize(int pointSize
)
226 m_pointSize
= pointSize
;
227 m_pixelSize
= wxDefaultSize
;
230 // Turn on or off the given bit in m_flags depending on the value of the
232 void SetFlag(int flag
, bool on
)
240 // The size information: if m_pixelSize is valid (!= wxDefaultSize), then
241 // it is used. Otherwise m_pointSize is used, taking into account that if
242 // it is == -1, it means that the platform dependent font size should be
247 wxFontFamily m_family
;
250 wxFontEncoding m_encoding
;
253 // ----------------------------------------------------------------------------
254 // wxFontBase represents a font object
255 // ----------------------------------------------------------------------------
257 class WXDLLIMPEXP_FWD_CORE wxNativeFontInfo
;
259 class WXDLLIMPEXP_CORE wxFontBase
: public wxGDIObject
263 derived classes should provide the following ctors:
266 wxFont(const wxFontInfo& info);
267 wxFont(const wxString& nativeFontInfoString);
268 wxFont(const wxNativeFontInfo& info);
273 bool underlined = false,
274 const wxString& face = wxEmptyString,
275 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
276 wxFont(const wxSize& pixelSize,
280 bool underlined = false,
281 const wxString& face = wxEmptyString,
282 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
286 virtual ~wxFontBase();
289 #if FUTURE_WXWIN_COMPATIBILITY_3_0
290 // from the font components
292 int pointSize
, // size of the font in points
293 int family
, // see wxFontFamily enum
294 int style
, // see wxFontStyle enum
295 int weight
, // see wxFontWeight enum
296 bool underlined
= false, // not underlined by default
297 const wxString
& face
= wxEmptyString
, // facename
298 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
) // ISO8859-X, ...
299 { return New(pointSize
, (wxFontFamily
)family
, (wxFontStyle
)style
,
300 (wxFontWeight
)weight
, underlined
, face
, encoding
); }
302 // from the font components
304 const wxSize
& pixelSize
, // size of the font in pixels
305 int family
, // see wxFontFamily enum
306 int style
, // see wxFontStyle enum
307 int weight
, // see wxFontWeight enum
308 bool underlined
= false, // not underlined by default
309 const wxString
& face
= wxEmptyString
, // facename
310 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
) // ISO8859-X, ...
311 { return New(pixelSize
, (wxFontFamily
)family
, (wxFontStyle
)style
,
312 (wxFontWeight
)weight
, underlined
, face
, encoding
); }
315 // from the font components
317 int pointSize
, // size of the font in points
318 wxFontFamily family
, // see wxFontFamily enum
319 wxFontStyle style
, // see wxFontStyle enum
320 wxFontWeight weight
, // see wxFontWeight enum
321 bool underlined
= false, // not underlined by default
322 const wxString
& face
= wxEmptyString
, // facename
323 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
); // ISO8859-X, ...
325 // from the font components
327 const wxSize
& pixelSize
, // size of the font in pixels
328 wxFontFamily family
, // see wxFontFamily enum
329 wxFontStyle style
, // see wxFontStyle enum
330 wxFontWeight weight
, // see wxFontWeight enum
331 bool underlined
= false, // not underlined by default
332 const wxString
& face
= wxEmptyString
, // facename
333 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
); // ISO8859-X, ...
335 // from the font components but using the font flags instead of separate
336 // parameters for each flag
337 static wxFont
*New(int pointSize
,
339 int flags
= wxFONTFLAG_DEFAULT
,
340 const wxString
& face
= wxEmptyString
,
341 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
344 // from the font components but using the font flags instead of separate
345 // parameters for each flag
346 static wxFont
*New(const wxSize
& pixelSize
,
348 int flags
= wxFONTFLAG_DEFAULT
,
349 const wxString
& face
= wxEmptyString
,
350 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
352 // from the (opaque) native font description object
353 static wxFont
*New(const wxNativeFontInfo
& nativeFontDesc
);
355 // from the string representation of wxNativeFontInfo
356 static wxFont
*New(const wxString
& strNativeFontDesc
);
359 bool operator==(const wxFont
& font
) const;
360 bool operator!=(const wxFont
& font
) const { return !(*this == font
); }
362 // accessors: get the font characteristics
363 virtual int GetPointSize() const = 0;
364 virtual wxSize
GetPixelSize() const;
365 virtual bool IsUsingSizeInPixels() const;
366 wxFontFamily
GetFamily() const;
367 virtual wxFontStyle
GetStyle() const = 0;
368 virtual wxFontWeight
GetWeight() const = 0;
369 virtual bool GetUnderlined() const = 0;
370 virtual bool GetStrikethrough() const { return false; }
371 virtual wxString
GetFaceName() const = 0;
372 virtual wxFontEncoding
GetEncoding() const = 0;
373 virtual const wxNativeFontInfo
*GetNativeFontInfo() const = 0;
375 virtual bool IsFixedWidth() const;
377 wxString
GetNativeFontInfoDesc() const;
378 wxString
GetNativeFontInfoUserDesc() const;
380 // change the font characteristics
381 virtual void SetPointSize( int pointSize
) = 0;
382 virtual void SetPixelSize( const wxSize
& pixelSize
);
383 virtual void SetFamily( wxFontFamily family
) = 0;
384 virtual void SetStyle( wxFontStyle style
) = 0;
385 virtual void SetWeight( wxFontWeight weight
) = 0;
387 virtual void SetUnderlined( bool underlined
) = 0;
388 virtual void SetStrikethrough( bool WXUNUSED(strikethrough
) ) {}
389 virtual void SetEncoding(wxFontEncoding encoding
) = 0;
390 virtual bool SetFaceName( const wxString
& faceName
);
391 void SetNativeFontInfo(const wxNativeFontInfo
& info
)
392 { DoSetNativeFontInfo(info
); }
394 bool SetNativeFontInfo(const wxString
& info
);
395 bool SetNativeFontInfoUserDesc(const wxString
& info
);
397 // Symbolic font sizes support: set the font size to "large" or "very
398 // small" either absolutely (i.e. compared to the default font size) or
399 // relatively to the given font size.
400 void SetSymbolicSize(wxFontSymbolicSize size
);
401 void SetSymbolicSizeRelativeTo(wxFontSymbolicSize size
, int base
)
403 SetPointSize(AdjustToSymbolicSize(size
, base
));
406 // Adjust the base size in points according to symbolic size.
407 static int AdjustToSymbolicSize(wxFontSymbolicSize size
, int base
);
410 // translate the fonts into human-readable string (i.e. GetStyleString()
411 // will return "wxITALIC" for an italic font, ...)
412 wxString
GetFamilyString() const;
413 wxString
GetStyleString() const;
414 wxString
GetWeightString() const;
416 // the default encoding is used for creating all fonts with default
417 // encoding parameter
418 static wxFontEncoding
GetDefaultEncoding() { return ms_encodingDefault
; }
419 static void SetDefaultEncoding(wxFontEncoding encoding
);
421 // this doesn't do anything and is kept for compatibility only
422 #if WXWIN_COMPATIBILITY_2_8
423 wxDEPRECATED_INLINE(void SetNoAntiAliasing(bool no
= true), wxUnusedVar(no
););
424 wxDEPRECATED_INLINE(bool GetNoAntiAliasing() const, return false;)
425 #endif // WXWIN_COMPATIBILITY_2_8
428 // the function called by both overloads of SetNativeFontInfo()
429 virtual void DoSetNativeFontInfo(const wxNativeFontInfo
& info
);
431 // The function called by public GetFamily(): it can return
432 // wxFONTFAMILY_UNKNOWN unlike the public method (see comment there).
433 virtual wxFontFamily
DoGetFamily() const = 0;
436 // Helper functions to recover wxFONTSTYLE/wxFONTWEIGHT and underlined flag
437 // values from flags containing a combination of wxFONTFLAG_XXX.
438 static wxFontStyle
GetStyleFromFlags(int flags
)
440 return flags
& wxFONTFLAG_ITALIC
442 : flags
& wxFONTFLAG_SLANT
444 : wxFONTSTYLE_NORMAL
;
447 static wxFontWeight
GetWeightFromFlags(int flags
)
449 return flags
& wxFONTFLAG_LIGHT
451 : flags
& wxFONTFLAG_BOLD
453 : wxFONTWEIGHT_NORMAL
;
456 static bool GetUnderlinedFromFlags(int flags
)
458 return (flags
& wxFONTFLAG_UNDERLINED
) != 0;
461 static bool GetStrikethroughFromFlags(int flags
)
463 return (flags
& wxFONTFLAG_STRIKETHROUGH
) != 0;
467 // the currently default encoding: by default, it's the default system
468 // encoding, but may be changed by the application using
469 // SetDefaultEncoding() to make all subsequent fonts created without
470 // specifying encoding parameter using this encoding
471 static wxFontEncoding ms_encodingDefault
;
474 // wxFontBase <-> wxString utilities, used by wxConfig
475 WXDLLIMPEXP_CORE wxString
wxToString(const wxFontBase
& font
);
476 WXDLLIMPEXP_CORE
bool wxFromString(const wxString
& str
, wxFontBase
* font
);
479 #if FUTURE_WXWIN_COMPATIBILITY_3_0
480 #define wxDECLARE_FONT_COMPAT_SETTER \
481 wxDEPRECATED_FUTURE( void SetFamily(int family) ) \
482 { SetFamily((wxFontFamily)family); } \
483 wxDEPRECATED_FUTURE( void SetStyle(int style) ) \
484 { SetStyle((wxFontStyle)style); } \
485 wxDEPRECATED_FUTURE( void SetWeight(int weight) ) \
486 { SetWeight((wxFontWeight)weight); } \
487 wxDEPRECATED_FUTURE( void SetFamily(wxDeprecatedGUIConstants family) ) \
488 { SetFamily((wxFontFamily)family); } \
489 wxDEPRECATED_FUTURE( void SetStyle(wxDeprecatedGUIConstants style) ) \
490 { SetStyle((wxFontStyle)style); } \
491 wxDEPRECATED_FUTURE( void SetWeight(wxDeprecatedGUIConstants weight) ) \
492 { SetWeight((wxFontWeight)weight); }
494 #define wxDECLARE_FONT_COMPAT_SETTER /*empty*/
497 // this macro must be used in all derived wxFont classes declarations
498 #define wxDECLARE_COMMON_FONT_METHODS() \
499 wxDECLARE_FONT_COMPAT_SETTER \
501 /* functions for modifying font in place */ \
502 wxFont& MakeBold(); \
503 wxFont& MakeItalic(); \
504 wxFont& MakeUnderlined(); \
505 wxFont& MakeStrikethrough(); \
506 wxFont& MakeLarger() { return Scale(1.2f); } \
507 wxFont& MakeSmaller() { return Scale(1/1.2f); } \
508 wxFont& Scale(float x); \
509 /* functions for creating fonts based on this one */ \
510 wxFont Bold() const; \
511 wxFont Italic() const; \
512 wxFont Underlined() const; \
513 wxFont Strikethrough() const; \
514 wxFont Larger() const { return Scaled(1.2f); } \
515 wxFont Smaller() const { return Scaled(1/1.2f); } \
516 wxFont Scaled(float x) const
518 // include the real class declaration
519 #if defined(__WXMSW__)
520 #include "wx/msw/font.h"
521 #elif defined(__WXMOTIF__)
522 #include "wx/motif/font.h"
523 #elif defined(__WXGTK20__)
524 #include "wx/gtk/font.h"
525 #elif defined(__WXGTK__)
526 #include "wx/gtk1/font.h"
527 #elif defined(__WXX11__)
528 #include "wx/x11/font.h"
529 #elif defined(__WXDFB__)
530 #include "wx/dfb/font.h"
531 #elif defined(__WXMAC__)
532 #include "wx/osx/font.h"
533 #elif defined(__WXCOCOA__)
534 #include "wx/cocoa/font.h"
535 #elif defined(__WXPM__)
536 #include "wx/os2/font.h"
539 class WXDLLIMPEXP_CORE wxFontList
: public wxGDIObjListBase
542 wxFont
*FindOrCreateFont(int pointSize
,
546 bool underline
= false,
547 const wxString
& face
= wxEmptyString
,
548 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
550 #if FUTURE_WXWIN_COMPATIBILITY_3_0
551 wxFont
*FindOrCreateFont(int pointSize
, int family
, int style
, int weight
,
552 bool underline
= false,
553 const wxString
& face
= wxEmptyString
,
554 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
555 { return FindOrCreateFont(pointSize
, (wxFontFamily
)family
, (wxFontStyle
)style
,
556 (wxFontWeight
)weight
, underline
, face
, encoding
); }
559 #if WXWIN_COMPATIBILITY_2_6
560 wxDEPRECATED( void AddFont(wxFont
*) );
561 wxDEPRECATED( void RemoveFont(wxFont
*) );
565 extern WXDLLIMPEXP_DATA_CORE(wxFontList
*) wxTheFontList
;
568 // provide comparison operators to allow code such as
570 // if ( font.GetStyle() == wxFONTSTYLE_SLANT )
572 // to compile without warnings which it would otherwise provoke from some
573 // compilers as it compares elements of different enums
574 #if FUTURE_WXWIN_COMPATIBILITY_3_0
576 // Unfortunately some compilers have ambiguity issues when enum comparisons are
577 // overloaded so we have to disable the overloads in this case, see
578 // wxCOMPILER_NO_OVERLOAD_ON_ENUM definition in wx/platform.h for more details.
579 #ifndef wxCOMPILER_NO_OVERLOAD_ON_ENUM
581 inline bool operator==(wxFontFamily s
, wxDeprecatedGUIConstants t
)
582 { return static_cast<int>(s
) == static_cast<int>(t
); }
583 inline bool operator!=(wxFontFamily s
, wxDeprecatedGUIConstants t
)
584 { return !(s
== t
); }
585 inline bool operator==(wxFontStyle s
, wxDeprecatedGUIConstants t
)
586 { return static_cast<int>(s
) == static_cast<int>(t
); }
587 inline bool operator!=(wxFontStyle s
, wxDeprecatedGUIConstants t
)
588 { return !(s
== t
); }
589 inline bool operator==(wxFontWeight s
, wxDeprecatedGUIConstants t
)
590 { return static_cast<int>(s
) == static_cast<int>(t
); }
591 inline bool operator!=(wxFontWeight s
, wxDeprecatedGUIConstants t
)
592 { return !(s
== t
); }
594 #endif // // wxCOMPILER_NO_OVERLOAD_ON_ENUM
596 #endif // FUTURE_WXWIN_COMPATIBILITY_3_0