]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/font.cpp
add the wxFont(const wxSize& pixelSize, ...) ctor to all ports; add some wxCHECK_MSGs...
[wxWidgets.git] / src / gtk / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/font.cpp
3 // Purpose: wxFont for wxGTK
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #include "wx/font.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/log.h"
25 #include "wx/utils.h"
26 #include "wx/settings.h"
27 #include "wx/cmndata.h"
28 #include "wx/gdicmn.h"
29 #endif
30
31 #include "wx/fontutil.h"
32 #include "wx/tokenzr.h"
33
34 #include "wx/gtk/private.h"
35
36 // ----------------------------------------------------------------------------
37 // constants
38 // ----------------------------------------------------------------------------
39
40 // the default size (in points) for the fonts
41 static const int wxDEFAULT_FONT_SIZE = 12;
42
43 // ----------------------------------------------------------------------------
44 // wxFontRefData
45 // ----------------------------------------------------------------------------
46
47 class wxFontRefData : public wxGDIRefData
48 {
49 public:
50 // from broken down font parameters, also default ctor
51 wxFontRefData(int size = -1,
52 wxFontFamily family = wxFONTFAMILY_DEFAULT,
53 wxFontStyle style = wxFONTSTYLE_NORMAL,
54 wxFontWeight weight = wxFONTWEIGHT_NORMAL,
55 bool underlined = false,
56 const wxString& faceName = wxEmptyString,
57 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
58
59 // from XFLD
60 wxFontRefData(const wxString& fontname);
61
62 // copy ctor
63 wxFontRefData( const wxFontRefData& data );
64
65 virtual ~wxFontRefData();
66
67 // do we have the native font info?
68 bool HasNativeFont() const
69 {
70 // we always have a Pango font description
71 return true;
72 }
73
74 // setters: all of them also take care to modify m_nativeFontInfo if we
75 // have it so as to not lose the information not carried by our fields
76 void SetPointSize(int pointSize);
77 void SetFamily(wxFontFamily family);
78 void SetStyle(wxFontStyle style);
79 void SetWeight(wxFontWeight weight);
80 void SetUnderlined(bool underlined);
81 bool SetFaceName(const wxString& facename);
82 void SetEncoding(wxFontEncoding encoding);
83
84 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
85 bool GetNoAntiAliasing() const { return m_noAA; }
86
87 // and this one also modifies all the other font data fields
88 void SetNativeFontInfo(const wxNativeFontInfo& info);
89
90 protected:
91 // common part of all ctors
92 void Init(int pointSize,
93 wxFontFamily family,
94 wxFontStyle style,
95 wxFontWeight weight,
96 bool underlined,
97 const wxString& faceName,
98 wxFontEncoding encoding);
99
100 // set all fields from (already initialized and valid) m_nativeFontInfo
101 void InitFromNative();
102
103 private:
104 // clear m_scaled_xfonts if any
105 void ClearGdkFonts();
106
107 int m_pointSize;
108 wxFontFamily m_family;
109 wxFontStyle m_style;
110 wxFontWeight m_weight;
111 bool m_underlined;
112 wxString m_faceName;
113 wxFontEncoding m_encoding;
114 bool m_noAA; // No anti-aliasing
115
116 // The native font info: basically a PangoFontDescription
117 wxNativeFontInfo m_nativeFontInfo;
118
119 friend class wxFont;
120 };
121
122 #define M_FONTDATA ((wxFontRefData*)m_refData)
123
124 // ----------------------------------------------------------------------------
125 // wxFontRefData
126 // ----------------------------------------------------------------------------
127
128 void wxFontRefData::Init(int pointSize,
129 wxFontFamily family,
130 wxFontStyle style,
131 wxFontWeight weight,
132 bool underlined,
133 const wxString& faceName,
134 wxFontEncoding encoding)
135 {
136 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
137
138 m_faceName = faceName;
139
140 // we accept both wxDEFAULT and wxNORMAL here - should we?
141 m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
142 m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
143
144 // and here, do we really want to forbid creation of the font of the size
145 // 90 (the value of wxDEFAULT)??
146 m_pointSize = pointSize == wxDEFAULT || pointSize == -1
147 ? wxDEFAULT_FONT_SIZE
148 : pointSize;
149
150 m_underlined = underlined;
151 m_encoding = encoding;
152 if ( m_encoding == wxFONTENCODING_DEFAULT )
153 m_encoding = wxFont::GetDefaultEncoding();
154
155 m_noAA = false;
156
157 // Create native font info
158 m_nativeFontInfo.description = pango_font_description_new();
159
160 // And set its values
161 if (!m_faceName.empty())
162 {
163 pango_font_description_set_family( m_nativeFontInfo.description,
164 wxGTK_CONV_SYS(m_faceName) );
165 }
166 else
167 {
168 switch (m_family)
169 {
170 case wxFONTFAMILY_MODERN:
171 case wxFONTFAMILY_TELETYPE:
172 pango_font_description_set_family( m_nativeFontInfo.description, "monospace" );
173 break;
174 case wxFONTFAMILY_ROMAN:
175 pango_font_description_set_family( m_nativeFontInfo.description, "serif" );
176 break;
177 case wxFONTFAMILY_SWISS:
178 // SWISS = sans serif
179 default:
180 pango_font_description_set_family( m_nativeFontInfo.description, "sans" );
181 break;
182 }
183 }
184
185 SetStyle( m_style );
186 SetPointSize( m_pointSize );
187 SetWeight( m_weight );
188 }
189
190 void wxFontRefData::InitFromNative()
191 {
192 m_noAA = false;
193
194 // Get native info
195 PangoFontDescription *desc = m_nativeFontInfo.description;
196
197 // init fields
198 m_faceName = wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc));
199
200 // Pango sometimes needs to have a size
201 int pango_size = pango_font_description_get_size( desc );
202 if (pango_size == 0)
203 m_nativeFontInfo.SetPointSize(12);
204
205 m_pointSize = m_nativeFontInfo.GetPointSize();
206 m_style = m_nativeFontInfo.GetStyle();
207 m_weight = m_nativeFontInfo.GetWeight();
208
209 if (m_faceName == wxT("monospace"))
210 {
211 m_family = wxFONTFAMILY_TELETYPE;
212 }
213 else if (m_faceName == wxT("sans"))
214 {
215 m_family = wxFONTFAMILY_SWISS;
216 }
217 else if (m_faceName == wxT("serif"))
218 {
219 m_family = wxFONTFAMILY_ROMAN;
220 }
221 else
222 {
223 m_family = wxFONTFAMILY_UNKNOWN;
224 }
225
226 // Pango description are never underlined (?)
227 m_underlined = false;
228
229 // always with GTK+ 2
230 m_encoding = wxFONTENCODING_UTF8;
231 }
232
233 wxFontRefData::wxFontRefData( const wxFontRefData& data )
234 : wxGDIRefData()
235 {
236 m_pointSize = data.m_pointSize;
237 m_family = data.m_family;
238 m_style = data.m_style;
239 m_weight = data.m_weight;
240
241 m_underlined = data.m_underlined;
242
243 m_faceName = data.m_faceName;
244 m_encoding = data.m_encoding;
245
246 m_noAA = data.m_noAA;
247
248 // Forces a copy of the internal data. wxNativeFontInfo should probably
249 // have a copy ctor and assignment operator to fix this properly but that
250 // would break binary compatibility...
251 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
252 }
253
254 wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style,
255 wxFontWeight weight, bool underlined,
256 const wxString& faceName,
257 wxFontEncoding encoding)
258 {
259 Init(size, family, style, weight, underlined, faceName, encoding);
260 }
261
262 wxFontRefData::wxFontRefData(const wxString& fontname)
263 {
264 m_nativeFontInfo.FromString( fontname );
265
266 InitFromNative();
267 }
268
269 void wxFontRefData::ClearGdkFonts()
270 {
271 }
272
273 wxFontRefData::~wxFontRefData()
274 {
275 ClearGdkFonts();
276 }
277
278 // ----------------------------------------------------------------------------
279 // wxFontRefData SetXXX()
280 // ----------------------------------------------------------------------------
281
282 void wxFontRefData::SetPointSize(int pointSize)
283 {
284 m_pointSize = pointSize;
285
286 m_nativeFontInfo.SetPointSize(pointSize);
287 }
288
289 /*
290 NOTE: disabled because pango_font_description_set_absolute_size() and
291 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
292 size of "30" makes the latter return 36...
293 Besides, we need to return GetPointSize() a point size value even if
294 SetPixelSize() was used and this would require further changes
295 (and use of pango_font_description_get_size_is_absolute in some places).
296
297 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
298 {
299 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
300 "Negative values for the pixel size or zero pixel height are not allowed" );
301
302 if (wx_pango_version_check(1,8,0) != NULL ||
303 pixelSize.GetWidth() != 0)
304 {
305 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
306 // if the user set the pixel width of the font explicitly or the pango
307 // library is too old, we cannot proceed
308 return false;
309 }
310
311 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
312 pixelSize.GetHeight() * PANGO_SCALE );
313
314 return true;
315 }
316
317 */
318
319 void wxFontRefData::SetFamily(wxFontFamily family)
320 {
321 m_family = family;
322
323 // TODO: what are we supposed to do with m_nativeFontInfo here?
324 }
325
326 void wxFontRefData::SetStyle(wxFontStyle style)
327 {
328 m_style = style;
329
330 m_nativeFontInfo.SetStyle((wxFontStyle)style);
331 }
332
333 void wxFontRefData::SetWeight(wxFontWeight weight)
334 {
335 m_weight = weight;
336
337 m_nativeFontInfo.SetWeight((wxFontWeight)weight);
338 }
339
340 void wxFontRefData::SetUnderlined(bool underlined)
341 {
342 m_underlined = underlined;
343
344 // the XLFD doesn't have "underlined" field anyhow
345 }
346
347 bool wxFontRefData::SetFaceName(const wxString& facename)
348 {
349 m_faceName = facename;
350
351 return m_nativeFontInfo.SetFaceName(facename);
352 }
353
354 void wxFontRefData::SetEncoding(wxFontEncoding encoding)
355 {
356 m_encoding = encoding;
357 }
358
359 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
360 {
361 // previously cached fonts shouldn't be used
362 ClearGdkFonts();
363
364 m_nativeFontInfo = info;
365
366 // set all the other font parameters from the native font info
367 InitFromNative();
368 }
369
370 // ----------------------------------------------------------------------------
371 // wxFont creation
372 // ----------------------------------------------------------------------------
373
374 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
375
376 wxFont::wxFont(const wxNativeFontInfo& info)
377 {
378 Create( info.GetPointSize(),
379 info.GetFamily(),
380 info.GetStyle(),
381 info.GetWeight(),
382 info.GetUnderlined(),
383 info.GetFaceName(),
384 info.GetEncoding() );
385 }
386
387 bool wxFont::Create( int pointSize,
388 wxFontFamily family,
389 wxFontStyle style,
390 wxFontWeight weight,
391 bool underlined,
392 const wxString& face,
393 wxFontEncoding encoding )
394 {
395 UnRef();
396
397 m_refData = new wxFontRefData(pointSize, family, style, weight,
398 underlined, face, encoding);
399
400 return true;
401 }
402
403 bool wxFont::Create(const wxString& fontname)
404 {
405 // VZ: does this really happen?
406 if ( fontname.empty() )
407 {
408 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
409
410 return true;
411 }
412
413 m_refData = new wxFontRefData(fontname);
414
415 return true;
416 }
417
418 wxFont::~wxFont()
419 {
420 }
421
422 // ----------------------------------------------------------------------------
423 // accessors
424 // ----------------------------------------------------------------------------
425
426 int wxFont::GetPointSize() const
427 {
428 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
429
430 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetPointSize()
431 : M_FONTDATA->m_pointSize;
432 }
433
434 wxString wxFont::GetFaceName() const
435 {
436 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
437
438 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetFaceName()
439 : M_FONTDATA->m_faceName;
440 }
441
442 wxFontFamily wxFont::GetFamily() const
443 {
444 wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
445
446 wxFontFamily ret = M_FONTDATA->m_family;
447 if (M_FONTDATA->HasNativeFont())
448 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
449 ret = M_FONTDATA->m_nativeFontInfo.GetFamily();
450
451 if (ret == wxFONTFAMILY_DEFAULT)
452 ret = M_FONTDATA->m_family;
453
454 return ret;
455 }
456
457 wxFontStyle wxFont::GetStyle() const
458 {
459 wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX, wxT("invalid font") );
460
461 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetStyle()
462 : M_FONTDATA->m_style;
463 }
464
465 wxFontWeight wxFont::GetWeight() const
466 {
467 wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX, wxT("invalid font") );
468
469 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetWeight()
470 : M_FONTDATA->m_weight;
471 }
472
473 bool wxFont::GetUnderlined() const
474 {
475 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
476
477 return M_FONTDATA->m_underlined;
478 }
479
480 wxFontEncoding wxFont::GetEncoding() const
481 {
482 wxCHECK_MSG( Ok(), wxFONTENCODING_SYSTEM, wxT("invalid font") );
483
484 return M_FONTDATA->m_encoding;
485 }
486
487 bool wxFont::GetNoAntiAliasing() const
488 {
489 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
490
491 return M_FONTDATA->m_noAA;
492 }
493
494 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
495 {
496 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
497
498 return &(M_FONTDATA->m_nativeFontInfo);
499 }
500
501 bool wxFont::IsFixedWidth() const
502 {
503 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
504
505 return wxFontBase::IsFixedWidth();
506 }
507
508 // ----------------------------------------------------------------------------
509 // change font attributes
510 // ----------------------------------------------------------------------------
511
512 void wxFont::SetPointSize(int pointSize)
513 {
514 AllocExclusive();
515
516 M_FONTDATA->SetPointSize(pointSize);
517 }
518
519 void wxFont::SetFamily(wxFontFamily family)
520 {
521 AllocExclusive();
522
523 M_FONTDATA->SetFamily(family);
524 }
525
526 void wxFont::SetStyle(wxFontStyle style)
527 {
528 AllocExclusive();
529
530 M_FONTDATA->SetStyle(style);
531 }
532
533 void wxFont::SetWeight(wxFontWeight weight)
534 {
535 AllocExclusive();
536
537 M_FONTDATA->SetWeight(weight);
538 }
539
540 bool wxFont::SetFaceName(const wxString& faceName)
541 {
542 AllocExclusive();
543
544 return M_FONTDATA->SetFaceName(faceName) &&
545 wxFontBase::SetFaceName(faceName);
546 }
547
548 void wxFont::SetUnderlined(bool underlined)
549 {
550 AllocExclusive();
551
552 M_FONTDATA->SetUnderlined(underlined);
553 }
554
555 void wxFont::SetEncoding(wxFontEncoding encoding)
556 {
557 AllocExclusive();
558
559 M_FONTDATA->SetEncoding(encoding);
560 }
561
562 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
563 {
564 AllocExclusive();
565
566 M_FONTDATA->SetNativeFontInfo( info );
567 }
568
569 void wxFont::SetNoAntiAliasing( bool no )
570 {
571 AllocExclusive();
572
573 M_FONTDATA->SetNoAntiAliasing( no );
574 }
575
576 wxGDIRefData* wxFont::CreateGDIRefData() const
577 {
578 return new wxFontRefData;
579 }
580
581 wxGDIRefData* wxFont::CloneGDIRefData(const wxGDIRefData* data) const
582 {
583 return new wxFontRefData(*static_cast<const wxFontRefData*>(data));
584 }