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