]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/font.cpp
regenerated after #error changes
[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 // Get native info
498 PangoFontDescription *desc = m_nativeFontInfo.description;
499
500 switch ( style )
501 {
502 case wxFONTSTYLE_ITALIC:
503 pango_font_description_set_style( desc, PANGO_STYLE_ITALIC );
504 break;
505 case wxFONTSTYLE_SLANT:
506 pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE );
507 break;
508 default:
509 wxFAIL_MSG( _T("unknown font style") );
510 // fall through
511 case wxFONTSTYLE_NORMAL:
512 pango_font_description_set_style( desc, PANGO_STYLE_NORMAL );
513 break;
514 }
515 #else
516 if ( HasNativeFont() )
517 {
518 wxString slant;
519 switch ( style )
520 {
521 case wxFONTSTYLE_ITALIC:
522 slant = _T('i');
523 break;
524
525 case wxFONTSTYLE_SLANT:
526 slant = _T('o');
527 break;
528
529 default:
530 wxFAIL_MSG( _T("unknown font style") );
531 // fall through
532
533 case wxFONTSTYLE_NORMAL:
534 slant = _T('r');
535 }
536
537 m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
538 }
539 #endif
540 }
541
542 void wxFontRefData::SetWeight(int weight)
543 {
544 m_weight = weight;
545
546 #ifdef __WXGTK20__
547 PangoFontDescription *desc = m_nativeFontInfo.description;
548 switch ( weight )
549 {
550 case wxFONTWEIGHT_BOLD:
551 pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD);
552 break;
553
554 case wxFONTWEIGHT_LIGHT:
555 pango_font_description_set_weight(desc, PANGO_WEIGHT_LIGHT);
556 break;
557
558 default:
559 wxFAIL_MSG( _T("unknown font weight") );
560 // fall through
561
562 case wxFONTWEIGHT_NORMAL:
563 // unspecified
564 pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL);
565 }
566 #else //!__WXGTK20__
567 if ( HasNativeFont() )
568 {
569 wxString boldness;
570 switch ( weight )
571 {
572 case wxFONTWEIGHT_BOLD:
573 boldness = _T("bold");
574 break;
575
576 case wxFONTWEIGHT_LIGHT:
577 boldness = _T("light");
578 break;
579
580 default:
581 wxFAIL_MSG( _T("unknown font weight") );
582 // fall through
583
584 case wxFONTWEIGHT_NORMAL:
585 // unspecified
586 boldness = _T("medium");
587 }
588
589 m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
590 }
591 #endif
592 }
593
594 void wxFontRefData::SetUnderlined(bool underlined)
595 {
596 m_underlined = underlined;
597
598 // the XLFD doesn't have "underlined" field anyhow
599 }
600
601 void wxFontRefData::SetFaceName(const wxString& facename)
602 {
603 m_faceName = facename;
604
605 #ifndef __WXGTK20__
606 if ( HasNativeFont() )
607 {
608 m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
609 }
610 #endif
611 }
612
613 void wxFontRefData::SetEncoding(wxFontEncoding encoding)
614 {
615 m_encoding = encoding;
616
617 #ifndef __WXGTK20__
618 if ( HasNativeFont() )
619 {
620 wxNativeEncodingInfo info;
621 if ( wxGetNativeFontEncoding(encoding, &info) )
622 {
623 m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
624 m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
625 }
626 }
627 #endif
628 }
629
630 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
631 {
632 // previously cached fonts shouldn't be used
633 ClearGdkFonts();
634
635 m_nativeFontInfo = info;
636
637 // set all the other font parameters from the native font info
638 InitFromNative();
639 }
640
641 // ----------------------------------------------------------------------------
642 // wxFont creation
643 // ----------------------------------------------------------------------------
644
645 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
646
647 void wxFont::Init()
648 {
649 }
650
651 wxFont::wxFont(const wxNativeFontInfo& info)
652 {
653 Init();
654
655 #ifdef __WXGTK20__
656 Create( info.GetPointSize(),
657 info.GetFamily(),
658 info.GetStyle(),
659 info.GetWeight(),
660 info.GetUnderlined(),
661 info.GetFaceName(),
662 info.GetEncoding() );
663 #else
664 (void) Create(info.GetXFontName());
665 #endif
666 }
667
668 bool wxFont::Create( int pointSize,
669 int family,
670 int style,
671 int weight,
672 bool underlined,
673 const wxString& face,
674 wxFontEncoding encoding)
675 {
676 UnRef();
677
678 m_refData = new wxFontRefData(pointSize, family, style, weight,
679 underlined, face, encoding);
680
681 return TRUE;
682 }
683
684 bool wxFont::Create(const wxString& fontname)
685 {
686 // VZ: does this really happen?
687 if ( fontname.empty() )
688 {
689 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
690
691 return TRUE;
692 }
693
694 m_refData = new wxFontRefData(fontname);
695
696 return TRUE;
697 }
698
699 void wxFont::Unshare()
700 {
701 if (!m_refData)
702 {
703 m_refData = new wxFontRefData();
704 }
705 else
706 {
707 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
708 UnRef();
709 m_refData = ref;
710 }
711 }
712
713 wxFont::~wxFont()
714 {
715 }
716
717 // ----------------------------------------------------------------------------
718 // accessors
719 // ----------------------------------------------------------------------------
720
721 int wxFont::GetPointSize() const
722 {
723 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
724
725 #if wxUSE_PANGO
726 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetPointSize()
727 : M_FONTDATA->m_pointSize;
728 #else
729 return M_FONTDATA->m_pointSize;
730 #endif
731 }
732
733 wxString wxFont::GetFaceName() const
734 {
735 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
736
737 #if wxUSE_PANGO
738 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetFaceName()
739 : M_FONTDATA->m_faceName;
740 #else
741 return M_FONTDATA->m_faceName;
742 #endif
743 }
744
745 int wxFont::GetFamily() const
746 {
747 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
748
749 #if wxUSE_PANGO
750 int ret = M_FONTDATA->m_family;
751 if (M_FONTDATA->HasNativeFont())
752 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
753 ret = M_FONTDATA->m_nativeFontInfo.GetFamily();
754
755 if (ret == wxFONTFAMILY_DEFAULT)
756 ret = M_FONTDATA->m_family;
757
758 return ret;
759 #else
760 return M_FONTDATA->m_family;
761 #endif
762 }
763
764 int wxFont::GetStyle() const
765 {
766 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
767
768 #if wxUSE_PANGO
769 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetStyle()
770 : M_FONTDATA->m_style;
771 #else
772 return M_FONTDATA->m_style;
773 #endif
774 }
775
776 int wxFont::GetWeight() const
777 {
778 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
779
780 #if wxUSE_PANGO
781 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetWeight()
782 : M_FONTDATA->m_weight;
783 #else
784 return M_FONTDATA->m_weight;
785 #endif
786 }
787
788 bool wxFont::GetUnderlined() const
789 {
790 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
791
792 return M_FONTDATA->m_underlined;
793 }
794
795 wxFontEncoding wxFont::GetEncoding() const
796 {
797 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
798
799 // m_encoding is unused in wxGTK2, return encoding that the user set.
800 return M_FONTDATA->m_encoding;
801 }
802
803 bool wxFont::GetNoAntiAliasing() const
804 {
805 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
806
807 return M_FONTDATA->m_noAA;
808 }
809
810 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
811 {
812 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
813
814 #ifndef __WXGTK20__
815 if ( !M_FONTDATA->HasNativeFont() )
816 {
817 // NB: this call has important side-effect: it not only finds
818 // GdkFont representation, it also initializes m_nativeFontInfo
819 // by calling its SetXFontName method
820 GetInternalFont();
821 }
822 #endif
823
824 return &(M_FONTDATA->m_nativeFontInfo);
825 }
826
827 bool wxFont::IsFixedWidth() const
828 {
829 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
830
831 #ifndef __WXGTK20__
832 if ( M_FONTDATA->HasNativeFont() )
833 {
834 // the monospace fonts are supposed to have "M" in the spacing field
835 wxString spacing = M_FONTDATA->
836 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
837
838 return spacing.Upper() == _T('M');
839 }
840 #endif
841
842 return wxFontBase::IsFixedWidth();
843 }
844
845 // ----------------------------------------------------------------------------
846 // change font attributes
847 // ----------------------------------------------------------------------------
848
849 void wxFont::SetPointSize(int pointSize)
850 {
851 Unshare();
852
853 M_FONTDATA->SetPointSize(pointSize);
854 }
855
856 void wxFont::SetFamily(int family)
857 {
858 Unshare();
859
860 M_FONTDATA->SetFamily(family);
861 }
862
863 void wxFont::SetStyle(int style)
864 {
865 Unshare();
866
867 M_FONTDATA->SetStyle(style);
868 }
869
870 void wxFont::SetWeight(int weight)
871 {
872 Unshare();
873
874 M_FONTDATA->SetWeight(weight);
875 }
876
877 void wxFont::SetFaceName(const wxString& faceName)
878 {
879 Unshare();
880
881 M_FONTDATA->SetFaceName(faceName);
882 }
883
884 void wxFont::SetUnderlined(bool underlined)
885 {
886 Unshare();
887
888 M_FONTDATA->SetUnderlined(underlined);
889 }
890
891 void wxFont::SetEncoding(wxFontEncoding encoding)
892 {
893 Unshare();
894
895 M_FONTDATA->SetEncoding(encoding);
896 }
897
898 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
899 {
900 Unshare();
901
902 M_FONTDATA->SetNativeFontInfo( info );
903 }
904
905 void wxFont::SetNoAntiAliasing( bool no )
906 {
907 Unshare();
908
909 M_FONTDATA->SetNoAntiAliasing( no );
910 }
911
912 // ----------------------------------------------------------------------------
913 // get internal representation of font
914 // ----------------------------------------------------------------------------
915
916 #ifndef __WXGTK20__
917 static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
918
919 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
920 extern GdkFont *GtkGetDefaultGuiFont()
921 {
922 if (!g_systemDefaultGuiFont)
923 {
924 GtkWidget *widget = gtk_button_new();
925 GtkStyle *def = gtk_rc_get_style( widget );
926 if (def)
927 {
928 g_systemDefaultGuiFont = gdk_font_ref( def->font );
929 }
930 else
931 {
932 def = gtk_widget_get_default_style();
933 if (def)
934 g_systemDefaultGuiFont = gdk_font_ref( def->font );
935 }
936 gtk_widget_destroy( widget );
937 }
938 else
939 {
940 // already have it, but ref it once more before returning
941 gdk_font_ref(g_systemDefaultGuiFont);
942 }
943
944 return g_systemDefaultGuiFont;
945 }
946
947 GdkFont *wxFont::GetInternalFont( float scale ) const
948 {
949 GdkFont *font = (GdkFont *) NULL;
950
951 wxCHECK_MSG( Ok(), font, wxT("invalid font") )
952
953 long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
954 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
955
956 wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts;
957 wxScaledFontList::iterator i = list.find(int_scale);
958 if ( i != list.end() )
959 {
960 font = i->second;
961 }
962 else // we don't have this font in this size yet
963 {
964 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
965 {
966 font = GtkGetDefaultGuiFont();
967 }
968
969 if ( !font )
970 {
971 // do we have the XLFD?
972 if ( int_scale == 100 && M_FONTDATA->HasNativeFont() )
973 {
974 font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
975 }
976
977 // no XLFD of no exact match - try the approximate one now
978 if ( !font )
979 {
980 wxString xfontname;
981 font = wxLoadQueryNearestFont( point_scale,
982 M_FONTDATA->m_family,
983 M_FONTDATA->m_style,
984 M_FONTDATA->m_weight,
985 M_FONTDATA->m_underlined,
986 M_FONTDATA->m_faceName,
987 M_FONTDATA->m_encoding,
988 &xfontname);
989 // NB: wxFont::GetNativeFontInfo relies on this
990 // side-effect of GetInternalFont
991 if ( int_scale == 100 )
992 M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname);
993 }
994 }
995
996 if ( font )
997 {
998 list[int_scale] = font;
999 }
1000 }
1001
1002 // it's quite useless to make it a wxCHECK because we're going to crash
1003 // anyhow...
1004 wxASSERT_MSG( font, wxT("could not load any font?") );
1005
1006 return font;
1007 }
1008 #endif // not GTK 2.0
1009