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