]> git.saurik.com Git - wxWidgets.git/blob - include/wx/font.h
0a7623ee49b9861fe0088a7ead9dea26a9b430cc
[wxWidgets.git] / include / wx / font.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/font.h
3 // Purpose: wxFontBase class: the interface of wxFont
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 20.09.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_FONT_H_BASE_
13 #define _WX_FONT_H_BASE_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
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
23
24 // ----------------------------------------------------------------------------
25 // forward declarations
26 // ----------------------------------------------------------------------------
27
28 class WXDLLIMPEXP_FWD_CORE wxFont;
29
30 // ----------------------------------------------------------------------------
31 // font constants
32 // ----------------------------------------------------------------------------
33
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
37 // may be unknown
38 enum wxFontFamily
39 {
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,
47 wxFONTFAMILY_MAX,
48 wxFONTFAMILY_UNKNOWN = wxFONTFAMILY_MAX
49 };
50
51 // font styles
52 enum wxFontStyle
53 {
54 wxFONTSTYLE_NORMAL = wxNORMAL,
55 wxFONTSTYLE_ITALIC = wxITALIC,
56 wxFONTSTYLE_SLANT = wxSLANT,
57 wxFONTSTYLE_MAX
58 };
59
60 // font weights
61 enum wxFontWeight
62 {
63 wxFONTWEIGHT_NORMAL = wxNORMAL,
64 wxFONTWEIGHT_LIGHT = wxLIGHT,
65 wxFONTWEIGHT_BOLD = wxBOLD,
66 wxFONTWEIGHT_MAX
67 };
68
69 // Symbolic font sizes as defined in CSS specification.
70 enum wxFontSymbolicSize
71 {
72 wxFONTSIZE_XX_SMALL = -3,
73 wxFONTSIZE_X_SMALL,
74 wxFONTSIZE_SMALL,
75 wxFONTSIZE_MEDIUM,
76 wxFONTSIZE_LARGE,
77 wxFONTSIZE_X_LARGE,
78 wxFONTSIZE_XX_LARGE
79 };
80
81 // the font flag bits for the new font ctor accepting one combined flags word
82 enum wxFontFlag
83 {
84 // no special flags: font with default weight/slant/anti-aliasing
85 wxFONTFLAG_DEFAULT = 0,
86
87 // slant flags (default: no slant)
88 wxFONTFLAG_ITALIC = 1 << 0,
89 wxFONTFLAG_SLANT = 1 << 1,
90
91 // weight flags (default: medium)
92 wxFONTFLAG_LIGHT = 1 << 2,
93 wxFONTFLAG_BOLD = 1 << 3,
94
95 // anti-aliasing flag: force on or off (default: the current system default)
96 wxFONTFLAG_ANTIALIASED = 1 << 4,
97 wxFONTFLAG_NOT_ANTIALIASED = 1 << 5,
98
99 // underlined/strikethrough flags (default: no lines)
100 wxFONTFLAG_UNDERLINED = 1 << 6,
101 wxFONTFLAG_STRIKETHROUGH = 1 << 7,
102
103 // the mask of all currently used flags
104 wxFONTFLAG_MASK = wxFONTFLAG_ITALIC |
105 wxFONTFLAG_SLANT |
106 wxFONTFLAG_LIGHT |
107 wxFONTFLAG_BOLD |
108 wxFONTFLAG_ANTIALIASED |
109 wxFONTFLAG_NOT_ANTIALIASED |
110 wxFONTFLAG_UNDERLINED |
111 wxFONTFLAG_STRIKETHROUGH
112 };
113
114 // ----------------------------------------------------------------------------
115 // wxFontBase represents a font object
116 // ----------------------------------------------------------------------------
117
118 class WXDLLIMPEXP_FWD_CORE wxNativeFontInfo;
119
120 class WXDLLIMPEXP_CORE wxFontBase : public wxGDIObject
121 {
122 public:
123 /*
124 derived classes should provide the following ctors:
125
126 wxFont();
127 wxFont(const wxString& nativeFontInfoString);
128 wxFont(const wxNativeFontInfo& info);
129 wxFont(int size,
130 wxFontFamily family,
131 wxFontStyle style,
132 wxFontWeight weight,
133 bool underlined = false,
134 const wxString& face = wxEmptyString,
135 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
136 wxFont(const wxSize& pixelSize,
137 wxFontFamily family,
138 wxFontStyle style,
139 wxFontWeight weight,
140 bool underlined = false,
141 const wxString& face = wxEmptyString,
142 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
143 */
144
145 // creator function
146 virtual ~wxFontBase();
147
148
149 #if FUTURE_WXWIN_COMPATIBILITY_3_0
150 // from the font components
151 static wxFont *New(
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); }
161
162 // from the font components
163 static wxFont *New(
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); }
173 #endif
174
175 // from the font components
176 static wxFont *New(
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, ...
184
185 // from the font components
186 static wxFont *New(
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, ...
194
195 // from the font components but using the font flags instead of separate
196 // parameters for each flag
197 static wxFont *New(int pointSize,
198 wxFontFamily family,
199 int flags = wxFONTFLAG_DEFAULT,
200 const wxString& face = wxEmptyString,
201 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
202
203
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,
207 wxFontFamily family,
208 int flags = wxFONTFLAG_DEFAULT,
209 const wxString& face = wxEmptyString,
210 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
211
212 // from the (opaque) native font description object
213 static wxFont *New(const wxNativeFontInfo& nativeFontDesc);
214
215 // from the string representation of wxNativeFontInfo
216 static wxFont *New(const wxString& strNativeFontDesc);
217
218 // comparison
219 bool operator==(const wxFont& font) const;
220 bool operator!=(const wxFont& font) const { return !(*this == font); }
221
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 wxString GetFaceName() const = 0;
231 virtual wxFontEncoding GetEncoding() const = 0;
232 virtual const wxNativeFontInfo *GetNativeFontInfo() const = 0;
233
234 virtual bool IsFixedWidth() const;
235
236 wxString GetNativeFontInfoDesc() const;
237 wxString GetNativeFontInfoUserDesc() const;
238
239 // change the font characteristics
240 virtual void SetPointSize( int pointSize ) = 0;
241 virtual void SetPixelSize( const wxSize& pixelSize );
242 virtual void SetFamily( wxFontFamily family ) = 0;
243 virtual void SetStyle( wxFontStyle style ) = 0;
244 virtual void SetWeight( wxFontWeight weight ) = 0;
245
246 virtual void SetUnderlined( bool underlined ) = 0;
247 virtual void SetEncoding(wxFontEncoding encoding) = 0;
248 virtual bool SetFaceName( const wxString& faceName );
249 void SetNativeFontInfo(const wxNativeFontInfo& info)
250 { DoSetNativeFontInfo(info); }
251
252 bool SetNativeFontInfo(const wxString& info);
253 bool SetNativeFontInfoUserDesc(const wxString& info);
254
255 // Symbolic font sizes support: set the font size to "large" or "very
256 // small" either absolutely (i.e. compared to the default font size) or
257 // relatively to the given font size.
258 void SetSymbolicSize(wxFontSymbolicSize size);
259 void SetSymbolicSizeRelativeTo(wxFontSymbolicSize size, int base)
260 {
261 SetPointSize(AdjustToSymbolicSize(size, base));
262 }
263
264 // Adjust the base size in points according to symbolic size.
265 static int AdjustToSymbolicSize(wxFontSymbolicSize size, int base);
266
267
268 // translate the fonts into human-readable string (i.e. GetStyleString()
269 // will return "wxITALIC" for an italic font, ...)
270 wxString GetFamilyString() const;
271 wxString GetStyleString() const;
272 wxString GetWeightString() const;
273
274 // the default encoding is used for creating all fonts with default
275 // encoding parameter
276 static wxFontEncoding GetDefaultEncoding() { return ms_encodingDefault; }
277 static void SetDefaultEncoding(wxFontEncoding encoding);
278
279 // this doesn't do anything and is kept for compatibility only
280 #if WXWIN_COMPATIBILITY_2_8
281 wxDEPRECATED_INLINE(void SetNoAntiAliasing(bool no = true), wxUnusedVar(no););
282 wxDEPRECATED_INLINE(bool GetNoAntiAliasing() const, return false;)
283 #endif // WXWIN_COMPATIBILITY_2_8
284
285 protected:
286 // the function called by both overloads of SetNativeFontInfo()
287 virtual void DoSetNativeFontInfo(const wxNativeFontInfo& info);
288
289 // The function called by public GetFamily(): it can return
290 // wxFONTFAMILY_UNKNOWN unlike the public method (see comment there).
291 virtual wxFontFamily DoGetFamily() const = 0;
292
293
294 // Helper functions to recover wxFONTSTYLE/wxFONTWEIGHT and underlined flg
295 // values from flags containing a combination of wxFONTFLAG_XXX.
296 static wxFontStyle GetStyleFromFlags(int flags)
297 {
298 return flags & wxFONTFLAG_ITALIC
299 ? wxFONTSTYLE_ITALIC
300 : flags & wxFONTFLAG_SLANT
301 ? wxFONTSTYLE_SLANT
302 : wxFONTSTYLE_NORMAL;
303 }
304
305 static wxFontWeight GetWeightFromFlags(int flags)
306 {
307 return flags & wxFONTFLAG_LIGHT
308 ? wxFONTWEIGHT_LIGHT
309 : flags & wxFONTFLAG_BOLD
310 ? wxFONTWEIGHT_BOLD
311 : wxFONTWEIGHT_NORMAL;
312 }
313
314 static bool GetUnderlinedFromFlags(int flags)
315 {
316 return (flags & wxFONTFLAG_UNDERLINED) != 0;
317 }
318
319
320 private:
321 // the currently default encoding: by default, it's the default system
322 // encoding, but may be changed by the application using
323 // SetDefaultEncoding() to make all subsequent fonts created without
324 // specifying encoding parameter using this encoding
325 static wxFontEncoding ms_encodingDefault;
326 };
327
328 // wxFontBase <-> wxString utilities, used by wxConfig
329 WXDLLIMPEXP_CORE wxString wxToString(const wxFontBase& font);
330 WXDLLIMPEXP_CORE bool wxFromString(const wxString& str, wxFontBase* font);
331
332
333 #if FUTURE_WXWIN_COMPATIBILITY_3_0
334 #define wxDECLARE_FONT_COMPAT_SETTER \
335 wxDEPRECATED_FUTURE( void SetFamily(int family) ) \
336 { SetFamily((wxFontFamily)family); } \
337 wxDEPRECATED_FUTURE( void SetStyle(int style) ) \
338 { SetStyle((wxFontStyle)style); } \
339 wxDEPRECATED_FUTURE( void SetWeight(int weight) ) \
340 { SetWeight((wxFontWeight)weight); } \
341 wxDEPRECATED_FUTURE( void SetFamily(wxDeprecatedGUIConstants family) ) \
342 { SetFamily((wxFontFamily)family); } \
343 wxDEPRECATED_FUTURE( void SetStyle(wxDeprecatedGUIConstants style) ) \
344 { SetStyle((wxFontStyle)style); } \
345 wxDEPRECATED_FUTURE( void SetWeight(wxDeprecatedGUIConstants weight) ) \
346 { SetWeight((wxFontWeight)weight); }
347 #else
348 #define wxDECLARE_FONT_COMPAT_SETTER /*empty*/
349 #endif
350
351 // this macro must be used in all derived wxFont classes declarations
352 #define wxDECLARE_COMMON_FONT_METHODS() \
353 wxDECLARE_FONT_COMPAT_SETTER \
354 \
355 /* functions for modifying font in place */ \
356 wxFont& MakeBold(); \
357 wxFont& MakeItalic(); \
358 wxFont& MakeUnderlined(); \
359 wxFont& MakeLarger() { return Scale(1.2f); } \
360 wxFont& MakeSmaller() { return Scale(1/1.2f); } \
361 wxFont& Scale(float x); \
362 /* functions for creating fonts based on this one */ \
363 wxFont Bold() const; \
364 wxFont Italic() const; \
365 wxFont Underlined() const; \
366 wxFont Larger() const { return Scaled(1.2f); } \
367 wxFont Smaller() const { return Scaled(1/1.2f); } \
368 wxFont Scaled(float x) const
369
370 // include the real class declaration
371 #if defined(__WXMSW__)
372 #include "wx/msw/font.h"
373 #elif defined(__WXMOTIF__)
374 #include "wx/motif/font.h"
375 #elif defined(__WXGTK20__)
376 #include "wx/gtk/font.h"
377 #elif defined(__WXGTK__)
378 #include "wx/gtk1/font.h"
379 #elif defined(__WXX11__)
380 #include "wx/x11/font.h"
381 #elif defined(__WXDFB__)
382 #include "wx/dfb/font.h"
383 #elif defined(__WXMAC__)
384 #include "wx/osx/font.h"
385 #elif defined(__WXCOCOA__)
386 #include "wx/cocoa/font.h"
387 #elif defined(__WXPM__)
388 #include "wx/os2/font.h"
389 #endif
390
391 class WXDLLIMPEXP_CORE wxFontList: public wxGDIObjListBase
392 {
393 public:
394 wxFont *FindOrCreateFont(int pointSize,
395 wxFontFamily family,
396 wxFontStyle style,
397 wxFontWeight weight,
398 bool underline = false,
399 const wxString& face = wxEmptyString,
400 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
401
402 #if FUTURE_WXWIN_COMPATIBILITY_3_0
403 wxFont *FindOrCreateFont(int pointSize, int family, int style, int weight,
404 bool underline = false,
405 const wxString& face = wxEmptyString,
406 wxFontEncoding encoding = wxFONTENCODING_DEFAULT)
407 { return FindOrCreateFont(pointSize, (wxFontFamily)family, (wxFontStyle)style,
408 (wxFontWeight)weight, underline, face, encoding); }
409 #endif
410
411 #if WXWIN_COMPATIBILITY_2_6
412 wxDEPRECATED( void AddFont(wxFont*) );
413 wxDEPRECATED( void RemoveFont(wxFont*) );
414 #endif
415 };
416
417 extern WXDLLIMPEXP_DATA_CORE(wxFontList*) wxTheFontList;
418
419
420 // provide comparison operators to allow code such as
421 //
422 // if ( font.GetStyle() == wxFONTSTYLE_SLANT )
423 //
424 // to compile without warnings which it would otherwise provoke from some
425 // compilers as it compares elements of different enums
426 #if FUTURE_WXWIN_COMPATIBILITY_3_0
427
428 // Unfortunately some compilers have ambiguity issues when enum comparisons are
429 // overloaded so we have to disable the overloads in this case, see
430 // wxCOMPILER_NO_OVERLOAD_ON_ENUM definition in wx/platform.h for more details.
431 #ifndef wxCOMPILER_NO_OVERLOAD_ON_ENUM
432
433 inline bool operator==(wxFontFamily s, wxDeprecatedGUIConstants t)
434 { return static_cast<int>(s) == static_cast<int>(t); }
435 inline bool operator!=(wxFontFamily s, wxDeprecatedGUIConstants t)
436 { return !(s == t); }
437 inline bool operator==(wxFontStyle s, wxDeprecatedGUIConstants t)
438 { return static_cast<int>(s) == static_cast<int>(t); }
439 inline bool operator!=(wxFontStyle s, wxDeprecatedGUIConstants t)
440 { return !(s == t); }
441 inline bool operator==(wxFontWeight s, wxDeprecatedGUIConstants t)
442 { return static_cast<int>(s) == static_cast<int>(t); }
443 inline bool operator!=(wxFontWeight s, wxDeprecatedGUIConstants t)
444 { return !(s == t); }
445
446 #endif // // wxCOMPILER_NO_OVERLOAD_ON_ENUM
447
448 #endif // FUTURE_WXWIN_COMPATIBILITY_3_0
449
450 #endif
451 // _WX_FONT_H_BASE_