Further code for Pango fonts (work now) and
[wxWidgets.git] / src / gtk1 / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/font.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 #ifdef __GNUG__
19 #pragma implementation "font.h"
20 #endif
21
22 #include "wx/font.h"
23 #include "wx/fontutil.h"
24 #include "wx/cmndata.h"
25 #include "wx/utils.h"
26 #include "wx/log.h"
27 #include "wx/gdicmn.h"
28 #include "wx/tokenzr.h"
29 #include "wx/settings.h"
30
31 #include <strings.h>
32
33 #include "wx/gtk/private.h"
34 #include <gdk/gdkprivate.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 // wxScaledFontList
45 // ----------------------------------------------------------------------------
46
47 // TODO: replace this with a type safe list or hash!!
48 class wxScaledFontList : public wxList
49 {
50 public:
51 wxScaledFontList() : wxList(wxKEY_INTEGER) { }
52 };
53
54 // ----------------------------------------------------------------------------
55 // wxFontRefData
56 // ----------------------------------------------------------------------------
57
58 class wxFontRefData : public wxObjectRefData
59 {
60 public:
61 // from broken down font parameters, also default ctor
62 wxFontRefData(int size = -1,
63 int family = wxFONTFAMILY_DEFAULT,
64 int style = wxFONTSTYLE_NORMAL,
65 int weight = wxFONTWEIGHT_NORMAL,
66 bool underlined = FALSE,
67 const wxString& faceName = wxEmptyString,
68 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
69
70 // from XFLD
71 wxFontRefData(const wxString& fontname);
72
73 // copy ctor
74 wxFontRefData( const wxFontRefData& data );
75
76 virtual ~wxFontRefData();
77
78 // do we have the native font info?
79 bool HasNativeFont() const
80 {
81 #ifdef __WXGTK20__
82 return TRUE; // ?
83 #else
84 return !m_nativeFontInfo.IsDefault();
85 #endif
86 }
87
88 // setters: all of them also take care to modify m_nativeFontInfo if we
89 // have it so as to not lose the information not carried by our fields
90 void SetPointSize(int pointSize);
91 void SetFamily(int family);
92 void SetStyle(int style);
93 void SetWeight(int weight);
94 void SetUnderlined(bool underlined);
95 void SetFaceName(const wxString& facename);
96 void SetEncoding(wxFontEncoding encoding);
97
98 // debugger helper: shows what the font really is
99 //
100 // VZ: I need this as my gdb either shows wildly wrong values or crashes
101 // when I ask it to "p fontRefData" :-(
102 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
103 void Dump() const
104 {
105 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
106 m_faceName.c_str(),
107 m_weight == wxFONTWEIGHT_NORMAL
108 ? _T("normal")
109 : m_weight == wxFONTWEIGHT_BOLD
110 ? _T("bold")
111 : _T("light"),
112 m_style == wxFONTSTYLE_NORMAL ? _T("regular") : _T("italic"),
113 m_pointSize,
114 m_encoding);
115 }
116 #endif // Debug
117
118 protected:
119 // common part of all ctors
120 void Init(int pointSize,
121 int family,
122 int style,
123 int weight,
124 bool underlined,
125 const wxString& faceName,
126 wxFontEncoding encoding);
127
128 private:
129 #ifndef __WXGTK20__
130 // the map of font sizes to "GdkFont *"
131 wxScaledFontList m_scaled_xfonts;
132 #endif
133
134 // the broken down font parameters
135 int m_pointSize;
136 int m_family,
137 m_style,
138 m_weight;
139 bool m_underlined;
140 wxString m_faceName;
141 wxFontEncoding m_encoding; // Unused under GTK 2.0
142
143 // The native font info, basicly an XFLD under GTK 1.2 and
144 // the pango font description under GTK 2.0.
145 wxNativeFontInfo m_nativeFontInfo;
146
147 friend class wxFont;
148 };
149
150 // ============================================================================
151 // wxFontRefData implementation
152 // ============================================================================
153
154 // ----------------------------------------------------------------------------
155 // wxFontRefData creation
156 // ----------------------------------------------------------------------------
157
158 void wxFontRefData::Init(int pointSize,
159 int family,
160 int style,
161 int weight,
162 bool underlined,
163 const wxString& faceName,
164 wxFontEncoding encoding)
165 {
166 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
167
168 m_faceName = faceName;
169
170 // we accept both wxDEFAULT and wxNORMAL here - should we?
171 m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
172 m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
173
174 // and here, do we really want to forbid creation of the font of the size
175 // 90 (the value of wxDEFAULT)??
176 m_pointSize = pointSize == wxDEFAULT ||
177 pointSize == -1 ? wxDEFAULT_FONT_SIZE : pointSize;
178
179 m_underlined = underlined;
180 m_encoding = encoding;
181
182
183 // Create native font info
184 m_nativeFontInfo.description = pango_font_description_new();
185
186 // And set its values
187 switch (m_family)
188 {
189 case wxFONTFAMILY_TELETYPE:
190 pango_font_description_set_family( m_nativeFontInfo.description, "monospaced" );
191 break;
192 case wxFONTFAMILY_SWISS:
193 pango_font_description_set_family( m_nativeFontInfo.description, "serif" );
194 break;
195 default:
196 pango_font_description_set_family( m_nativeFontInfo.description, "sans" );
197 break;
198 }
199 SetStyle( m_style );
200 SetPointSize( m_pointSize );
201 SetWeight( m_weight );
202 }
203
204 wxFontRefData::wxFontRefData( const wxFontRefData& data )
205 : wxObjectRefData()
206 {
207 m_pointSize = data.m_pointSize;
208 m_family = data.m_family;
209 m_style = data.m_style;
210 m_weight = data.m_weight;
211
212 m_underlined = data.m_underlined;
213
214 m_faceName = data.m_faceName;
215 m_encoding = data.m_encoding;
216
217 m_nativeFontInfo = data.m_nativeFontInfo;
218 }
219
220 wxFontRefData::wxFontRefData(int size, int family, int style,
221 int weight, bool underlined,
222 const wxString& faceName,
223 wxFontEncoding encoding)
224 {
225 Init(size, family, style, weight, underlined, faceName, encoding);
226 }
227
228 wxFontRefData::wxFontRefData(const wxString& fontname)
229 {
230 #ifdef __WXGTK20__
231 m_nativeFontInfo.FromString( fontname );
232
233 // Get native info
234 PangoFontDescription *desc = m_nativeFontInfo.description;
235
236 // init fields
237 m_faceName = wxGTK_CONV_BACK( pango_font_description_get_family( desc ) );
238
239 m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE;
240
241 switch (pango_font_description_get_style( desc ))
242 {
243 case PANGO_STYLE_NORMAL:
244 m_style = wxFONTSTYLE_NORMAL;
245 break;
246 case PANGO_STYLE_ITALIC:
247 m_style = wxFONTSTYLE_ITALIC;
248 break;
249 case PANGO_STYLE_OBLIQUE:
250 m_style = wxFONTSTYLE_SLANT;
251 break;
252 }
253
254 switch (pango_font_description_get_weight( desc ))
255 {
256 case PANGO_WEIGHT_ULTRALIGHT:
257 m_weight = wxFONTWEIGHT_LIGHT;
258 break;
259 case PANGO_WEIGHT_LIGHT:
260 m_weight = wxFONTWEIGHT_LIGHT;
261 break;
262 case PANGO_WEIGHT_NORMAL:
263 m_weight = wxFONTWEIGHT_NORMAL;
264 break;
265 case PANGO_WEIGHT_BOLD:
266 m_weight = wxFONTWEIGHT_BOLD;
267 break;
268 case PANGO_WEIGHT_ULTRABOLD:
269 m_weight = wxFONTWEIGHT_BOLD;
270 break;
271 case PANGO_WEIGHT_HEAVY:
272 m_weight = wxFONTWEIGHT_BOLD;
273 break;
274 }
275
276 if (m_faceName == wxT("monospaced"))
277 {
278 m_family = wxFONTFAMILY_TELETYPE;
279 }
280 else if (m_faceName == wxT("sans"))
281 {
282 m_family = wxFONTFAMILY_SWISS;
283 }
284 else
285 {
286 m_family = wxFONTFAMILY_UNKNOWN;
287 }
288
289 // Pango description are never underlined (?)
290 m_underlined = FALSE;
291
292 // Cannot we choose that
293 m_encoding = wxFONTENCODING_SYSTEM;
294 #else
295 // remember the X font name
296 m_nativeFontInfo.SetXFontName(fontname);
297
298 // get the font parameters from the XLFD
299 // -------------------------------------
300
301 m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY);
302
303 m_weight = wxFONTWEIGHT_NORMAL;
304
305 wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper();
306 if ( !w.empty() && w != _T('*') )
307 {
308 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
309 // and BLACK
310 if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
311 !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
312 wxStrstr(w.c_str() + 1, _T("BOLD")) )
313 {
314 m_weight = wxFONTWEIGHT_BOLD;
315 }
316 else if ( w == _T("LIGHT") || w == _T("THIN") )
317 {
318 m_weight = wxFONTWEIGHT_LIGHT;
319 }
320 }
321
322 switch ( wxToupper(*m_nativeFontInfo.
323 GetXFontComponent(wxXLFD_SLANT).c_str()) )
324 {
325 case _T('I'): // italique
326 m_style = wxFONTSTYLE_ITALIC;
327 break;
328
329 case _T('O'): // oblique
330 m_style = wxFONTSTYLE_SLANT;
331 break;
332
333 default:
334 m_style = wxFONTSTYLE_NORMAL;
335 }
336
337 long ptSize;
338 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
339 {
340 // size in XLFD is in 10 point units
341 m_pointSize = (int)(ptSize / 10);
342 }
343 else
344 {
345 m_pointSize = wxDEFAULT_FONT_SIZE;
346 }
347
348 // examine the spacing: if the font is monospaced, assume wxTELETYPE
349 // family for compatibility with the old code which used it instead of
350 // IsFixedWidth()
351 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') )
352 {
353 m_family = wxFONTFAMILY_TELETYPE;
354 }
355 else // not monospaceed
356 {
357 // don't even try guessing it, it doesn't work for too many fonts
358 // anyhow
359 m_family = wxFONTFAMILY_UNKNOWN;
360 }
361
362 // X fonts are never underlined...
363 m_underlined = FALSE;
364
365 // deal with font encoding
366 wxString
367 registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(),
368 encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper();
369
370 if ( registry == _T("ISO8859") )
371 {
372 int cp;
373 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
374 {
375 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
376 }
377 }
378 else if ( registry == _T("MICROSOFT") )
379 {
380 int cp;
381 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
382 {
383 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
384 }
385 }
386 else if ( registry == _T("KOI8") )
387 {
388 m_encoding = wxFONTENCODING_KOI8;
389 }
390 else // unknown encoding
391 {
392 // may be give a warning here?
393 m_encoding = wxFONTENCODING_SYSTEM;
394 }
395 #endif
396 }
397
398 wxFontRefData::~wxFontRefData()
399 {
400 #ifndef __WXGTK20__
401 wxNode *node = m_scaled_xfonts.First();
402 while (node)
403 {
404 GdkFont *font = (GdkFont*)node->Data();
405 wxNode *next = node->Next();
406 gdk_font_unref( font );
407 node = next;
408 }
409 #endif
410 }
411
412 // ----------------------------------------------------------------------------
413 // wxFontRefData SetXXX()
414 // ----------------------------------------------------------------------------
415
416 void wxFontRefData::SetPointSize(int pointSize)
417 {
418 m_pointSize = pointSize;
419
420 #ifdef __WXGTK20__
421 // Get native info
422 PangoFontDescription *desc = m_nativeFontInfo.description;
423
424 pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE );
425 #else
426 if ( HasNativeFont() )
427 {
428 wxString size;
429 if ( pointSize == -1 )
430 size = _T('*');
431 else
432 size.Printf(_T("%d"), 10*pointSize);
433
434 m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
435 }
436 #endif
437 }
438
439 void wxFontRefData::SetFamily(int family)
440 {
441 m_family = family;
442
443 // TODO: what are we supposed to do with m_nativeFontInfo here?
444 }
445
446 void wxFontRefData::SetStyle(int style)
447 {
448 m_style = style;
449
450 #ifdef __WXGTK20__
451 // Get native info
452 PangoFontDescription *desc = m_nativeFontInfo.description;
453
454 switch ( style )
455 {
456 case wxFONTSTYLE_ITALIC:
457 pango_font_description_set_style( desc, PANGO_STYLE_ITALIC );
458 break;
459 case wxFONTSTYLE_SLANT:
460 pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE );
461 break;
462 default:
463 wxFAIL_MSG( _T("unknown font style") );
464 // fall through
465 case wxFONTSTYLE_NORMAL:
466 pango_font_description_set_style( desc, PANGO_STYLE_NORMAL );
467 break;
468 }
469 #else
470 if ( HasNativeFont() )
471 {
472 wxString slant;
473 switch ( style )
474 {
475 case wxFONTSTYLE_ITALIC:
476 slant = _T('i');
477 break;
478
479 case wxFONTSTYLE_SLANT:
480 slant = _T('o');
481 break;
482
483 default:
484 wxFAIL_MSG( _T("unknown font style") );
485 // fall through
486
487 case wxFONTSTYLE_NORMAL:
488 slant = _T('r');
489 }
490
491 m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
492 }
493 #endif
494 }
495
496 void wxFontRefData::SetWeight(int weight)
497 {
498 m_weight = weight;
499
500 #ifndef __WXGTK20__
501 if ( HasNativeFont() )
502 {
503 wxString boldness;
504 switch ( weight )
505 {
506 case wxFONTWEIGHT_BOLD:
507 boldness = _T("bold");
508 break;
509
510 case wxFONTWEIGHT_LIGHT:
511 boldness = _T("light");
512 break;
513
514 default:
515 wxFAIL_MSG( _T("unknown font weight") );
516 // fall through
517
518 case wxFONTWEIGHT_NORMAL:
519 // unspecified
520 boldness = _T("medium");
521 }
522
523 m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
524 }
525 #endif
526 }
527
528 void wxFontRefData::SetUnderlined(bool underlined)
529 {
530 m_underlined = underlined;
531
532 // the XLFD doesn't have "underlined" field anyhow
533 }
534
535 void wxFontRefData::SetFaceName(const wxString& facename)
536 {
537 m_faceName = facename;
538
539 #ifndef __WXGTK20__
540 if ( HasNativeFont() )
541 {
542 m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
543 }
544 #endif
545 }
546
547 void wxFontRefData::SetEncoding(wxFontEncoding encoding)
548 {
549 m_encoding = encoding;
550
551 #ifndef __WXGTK20__
552 if ( HasNativeFont() )
553 {
554 wxNativeEncodingInfo info;
555 if ( wxGetNativeFontEncoding(encoding, &info) )
556 {
557 m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
558 m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
559 }
560 }
561 #endif
562 }
563
564 // ============================================================================
565 // wxFont implementation
566 // ============================================================================
567
568 // ----------------------------------------------------------------------------
569 // wxFont creation
570 // ----------------------------------------------------------------------------
571
572 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
573
574 void wxFont::Init()
575 {
576 }
577
578 wxFont::wxFont(const wxNativeFontInfo& info)
579 {
580 Init();
581
582 #ifdef __WXGTK20__
583 Create( info.GetPointSize(),
584 info.GetFamily(),
585 info.GetStyle(),
586 info.GetWeight(),
587 info.GetUnderlined(),
588 info.GetFaceName(),
589 info.GetEncoding() );
590 #else
591 Create(info.GetXFontName());
592 #endif
593 }
594
595 bool wxFont::Create( int pointSize,
596 int family,
597 int style,
598 int weight,
599 bool underlined,
600 const wxString& face,
601 wxFontEncoding encoding)
602 {
603 m_refData = new wxFontRefData(pointSize, family, style, weight,
604 underlined, face, encoding);
605
606 return TRUE;
607 }
608
609 bool wxFont::Create(const wxString& fontname)
610 {
611 // VZ: does this really happen?
612 if ( fontname.empty() )
613 {
614 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
615
616 return TRUE;
617 }
618
619 m_refData = new wxFontRefData(fontname);
620
621 return TRUE;
622 }
623
624 void wxFont::Unshare()
625 {
626 if (!m_refData)
627 {
628 m_refData = new wxFontRefData();
629 }
630 else
631 {
632 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
633 UnRef();
634 m_refData = ref;
635 }
636 }
637
638 wxFont::~wxFont()
639 {
640 }
641
642 // ----------------------------------------------------------------------------
643 // accessors
644 // ----------------------------------------------------------------------------
645
646 // all accessors are just forwarded to wxFontRefData which has everything we
647 // need
648
649 int wxFont::GetPointSize() const
650 {
651 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
652
653 return M_FONTDATA->m_pointSize;
654 }
655
656 wxString wxFont::GetFaceName() const
657 {
658 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
659
660 return M_FONTDATA->m_faceName;
661 }
662
663 int wxFont::GetFamily() const
664 {
665 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
666
667 return M_FONTDATA->m_family;
668 }
669
670 int wxFont::GetStyle() const
671 {
672 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
673
674 return M_FONTDATA->m_style;
675 }
676
677 int wxFont::GetWeight() const
678 {
679 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
680
681 return M_FONTDATA->m_weight;
682 }
683
684 bool wxFont::GetUnderlined() const
685 {
686 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
687
688 return M_FONTDATA->m_underlined;
689 }
690
691 wxFontEncoding wxFont::GetEncoding() const
692 {
693 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
694
695 return M_FONTDATA->m_encoding;
696 }
697
698 wxNativeFontInfo *wxFont::GetNativeFontInfo() const
699 {
700 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
701
702 #ifndef __WXGTK20__ // ???
703 if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
704 GetInternalFont();
705 #endif
706
707 return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
708 }
709
710 bool wxFont::IsFixedWidth() const
711 {
712 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
713
714 #ifndef __WXGTK20__
715 if ( M_FONTDATA->HasNativeFont() )
716 {
717 // the monospace fonts are supposed to have "M" in the spacing field
718 wxString spacing = M_FONTDATA->
719 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
720
721 return spacing.Upper() == _T('M');
722 }
723 #endif
724
725 return wxFontBase::IsFixedWidth();
726 }
727
728 // ----------------------------------------------------------------------------
729 // change font attributes
730 // ----------------------------------------------------------------------------
731
732 void wxFont::SetPointSize(int pointSize)
733 {
734 Unshare();
735
736 M_FONTDATA->SetPointSize(pointSize);
737 }
738
739 void wxFont::SetFamily(int family)
740 {
741 Unshare();
742
743 M_FONTDATA->SetFamily(family);
744 }
745
746 void wxFont::SetStyle(int style)
747 {
748 Unshare();
749
750 M_FONTDATA->SetStyle(style);
751 }
752
753 void wxFont::SetWeight(int weight)
754 {
755 Unshare();
756
757 M_FONTDATA->SetWeight(weight);
758 }
759
760 void wxFont::SetFaceName(const wxString& faceName)
761 {
762 Unshare();
763
764 M_FONTDATA->SetFaceName(faceName);
765 }
766
767 void wxFont::SetUnderlined(bool underlined)
768 {
769 Unshare();
770
771 M_FONTDATA->SetUnderlined(underlined);
772 }
773
774 void wxFont::SetEncoding(wxFontEncoding encoding)
775 {
776 Unshare();
777
778 M_FONTDATA->SetEncoding(encoding);
779 }
780
781 void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
782 {
783 Unshare();
784
785 M_FONTDATA->m_nativeFontInfo = info;
786 }
787
788 // ----------------------------------------------------------------------------
789 // get internal representation of font
790 // ----------------------------------------------------------------------------
791
792 static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
793
794 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
795 extern GdkFont *GtkGetDefaultGuiFont()
796 {
797 if (!g_systemDefaultGuiFont)
798 {
799 GtkWidget *widget = gtk_button_new();
800 GtkStyle *def = gtk_rc_get_style( widget );
801 if (def)
802 {
803 g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) );
804 }
805 else
806 {
807 def = gtk_widget_get_default_style();
808 if (def)
809 g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) );
810 }
811 gtk_widget_destroy( widget );
812 }
813 else
814 {
815 // already have it, but ref it once more before returning
816 gdk_font_ref(g_systemDefaultGuiFont);
817 }
818
819 return g_systemDefaultGuiFont;
820 }
821
822 GdkFont *wxFont::GetInternalFont( float scale ) const
823 {
824 GdkFont *font = (GdkFont *) NULL;
825
826 wxCHECK_MSG( Ok(), font, wxT("invalid font") )
827
828 #ifdef __WXGTK20__
829 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
830 {
831 font = GtkGetDefaultGuiFont();
832 }
833 else
834 {
835 PangoFontDescription *font_description = GetNativeFontInfo()->description;
836
837 font = gdk_font_from_description( font_description );
838 }
839 #else
840 long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
841 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
842
843 wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
844 if (node)
845 {
846 font = (GdkFont*)node->Data();
847 }
848 else // we don't have this font in this size yet
849 {
850 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
851 {
852 font = GtkGetDefaultGuiFont();
853 }
854
855 if ( !font )
856 {
857 // do we have the XLFD?
858 if ( M_FONTDATA->HasNativeFont() )
859 {
860 font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
861 }
862
863 // no XLFD of no exact match - try the approximate one now
864 if ( !font )
865 {
866 wxString xfontname;
867 font = wxLoadQueryNearestFont( point_scale,
868 M_FONTDATA->m_family,
869 M_FONTDATA->m_style,
870 M_FONTDATA->m_weight,
871 M_FONTDATA->m_underlined,
872 M_FONTDATA->m_faceName,
873 M_FONTDATA->m_encoding,
874 &xfontname);
875 if ( font )
876 {
877 M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname);
878 }
879 }
880 }
881
882 if ( font )
883 {
884 M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
885 }
886 }
887 #endif // GTK 2.0
888
889 // it's quite useless to make it a wxCHECK because we're going to crash
890 // anyhow...
891 wxASSERT_MSG( font, wxT("could not load any font?") );
892
893 return font;
894 }
895