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