]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/font.cpp
Work around "possibly uninitialized variable" warning in wxFileName.
[wxWidgets.git] / src / gtk1 / font.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk1/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// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
21#include "wx/font.h"
22
23#ifndef WX_PRECOMP
24 #include "wx/log.h"
25 #include "wx/settings.h"
26 #include "wx/cmndata.h"
27 #include "wx/gdicmn.h"
28#endif
29
30#include "wx/fontutil.h"
31#include "wx/utils.h"
32#include "wx/tokenzr.h"
33
34#include <strings.h>
35
36#include "wx/gtk1/private.h"
37#include <gdk/gdkprivate.h>
38
39// ----------------------------------------------------------------------------
40// constants
41// ----------------------------------------------------------------------------
42
43// the default size (in points) for the fonts
44static const int wxDEFAULT_FONT_SIZE = 12;
45
46// ----------------------------------------------------------------------------
47// wxScaledFontList: maps the font sizes to the GDK fonts for the given font
48// ----------------------------------------------------------------------------
49
50WX_DECLARE_HASH_MAP(int, GdkFont *, wxIntegerHash, wxIntegerEqual,
51 wxScaledFontList);
52
53// ----------------------------------------------------------------------------
54// wxFontRefData
55// ----------------------------------------------------------------------------
56
57class wxFontRefData : public wxGDIRefData
58{
59public:
60 // from broken down font parameters, also default ctor
61 wxFontRefData(int size = -1,
62 wxFontFamily family = wxFONTFAMILY_DEFAULT,
63 wxFontStyle style = wxFONTSTYLE_NORMAL,
64 wxFontWeight 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 // only use m_nativeFontInfo if it had been initialized
81 return !m_nativeFontInfo.IsDefault();
82 }
83
84 // setters: all of them also take care to modify m_nativeFontInfo if we
85 // have it so as to not lose the information not carried by our fields
86 void SetPointSize(int pointSize);
87 void SetFamily(wxFontFamily family);
88 void SetStyle(wxFontStyle style);
89 void SetWeight(wxFontWeight weight);
90 void SetUnderlined(bool underlined);
91 bool SetFaceName(const wxString& facename);
92 void SetEncoding(wxFontEncoding encoding);
93
94 // and this one also modifies all the other font data fields
95 void SetNativeFontInfo(const wxNativeFontInfo& info);
96
97protected:
98 // common part of all ctors
99 void Init(int pointSize,
100 wxFontFamily family,
101 wxFontStyle style,
102 wxFontWeight weight,
103 bool underlined,
104 const wxString& faceName,
105 wxFontEncoding encoding);
106
107 // set all fields from (already initialized and valid) m_nativeFontInfo
108 void InitFromNative();
109
110private:
111 // clear m_scaled_xfonts if any
112 void ClearGdkFonts();
113
114 // the map of font sizes to "GdkFont *"
115 wxScaledFontList m_scaled_xfonts;
116
117 int m_pointSize;
118 wxFontFamily m_family;
119 wxFontStyle m_style;
120 wxFontWeight m_weight;
121 bool m_underlined;
122 wxString m_faceName;
123 wxFontEncoding m_encoding; // Unused under GTK 2.0
124
125 // The native font info, basicly an XFLD under GTK 1.2 and
126 // the pango font description under GTK 2.0.
127 wxNativeFontInfo m_nativeFontInfo;
128
129 friend class wxFont;
130};
131
132#define M_FONTDATA ((wxFontRefData*)m_refData)
133
134// ----------------------------------------------------------------------------
135// wxFontRefData
136// ----------------------------------------------------------------------------
137
138void wxFontRefData::Init(int pointSize,
139 wxFontFamily family,
140 wxFontStyle style,
141 wxFontWeight weight,
142 bool underlined,
143 const wxString& faceName,
144 wxFontEncoding encoding)
145{
146 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
147
148 m_faceName = faceName;
149
150 // we accept both wxDEFAULT and wxNORMAL here - should we?
151 m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
152 m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
153
154 // and here, do we really want to forbid creation of the font of the size
155 // 90 (the value of wxDEFAULT)??
156 m_pointSize = pointSize == wxDEFAULT || pointSize == -1
157 ? wxDEFAULT_FONT_SIZE
158 : pointSize;
159
160 m_underlined = underlined;
161 m_encoding = encoding;
162}
163
164void wxFontRefData::InitFromNative()
165{
166 // get the font parameters from the XLFD
167 // -------------------------------------
168
169 m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY);
170
171 m_weight = wxFONTWEIGHT_NORMAL;
172
173 wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper();
174 if ( !w.empty() && w != wxT('*') )
175 {
176 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
177 // and BLACK
178 if ( ((w[0u] == wxT('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
179 !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
180 wxStrstr(w.c_str() + 1, wxT("BOLD")) )
181 {
182 m_weight = wxFONTWEIGHT_BOLD;
183 }
184 else if ( w == wxT("LIGHT") || w == wxT("THIN") )
185 {
186 m_weight = wxFONTWEIGHT_LIGHT;
187 }
188 }
189
190 switch ( wxToupper(m_nativeFontInfo.
191 GetXFontComponent(wxXLFD_SLANT)[0u]).GetValue() )
192 {
193 case wxT('I'): // italique
194 m_style = wxFONTSTYLE_ITALIC;
195 break;
196
197 case wxT('O'): // oblique
198 m_style = wxFONTSTYLE_SLANT;
199 break;
200
201 default:
202 m_style = wxFONTSTYLE_NORMAL;
203 }
204
205 long ptSize;
206 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
207 {
208 // size in XLFD is in 10 point units
209 m_pointSize = (int)(ptSize / 10);
210 }
211 else
212 {
213 m_pointSize = wxDEFAULT_FONT_SIZE;
214 }
215
216 // examine the spacing: if the font is monospaced, assume wxTELETYPE
217 // family for compatibility with the old code which used it instead of
218 // IsFixedWidth()
219 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == wxT('M') )
220 {
221 m_family = wxFONTFAMILY_TELETYPE;
222 }
223 else // not monospaceed
224 {
225 // don't even try guessing it, it doesn't work for too many fonts
226 // anyhow
227 m_family = wxFONTFAMILY_UNKNOWN;
228 }
229
230 // X fonts are never underlined...
231 m_underlined = false;
232
233 // deal with font encoding
234 wxString
235 registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(),
236 encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper();
237
238 if ( registry == wxT("ISO8859") )
239 {
240 int cp;
241 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
242 {
243 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
244 }
245 }
246 else if ( registry == wxT("MICROSOFT") )
247 {
248 int cp;
249 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
250 {
251 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
252 }
253 }
254 else if ( registry == wxT("KOI8") )
255 {
256 m_encoding = wxFONTENCODING_KOI8;
257 }
258 else // unknown encoding
259 {
260 // may be give a warning here? or use wxFontMapper?
261 m_encoding = wxFONTENCODING_SYSTEM;
262 }
263}
264
265wxFontRefData::wxFontRefData( const wxFontRefData& data )
266 : wxGDIRefData()
267{
268 m_pointSize = data.m_pointSize;
269 m_family = data.m_family;
270 m_style = data.m_style;
271 m_weight = data.m_weight;
272
273 m_underlined = data.m_underlined;
274
275 m_faceName = data.m_faceName;
276 m_encoding = data.m_encoding;
277
278 // Forces a copy of the internal data. wxNativeFontInfo should probably
279 // have a copy ctor and assignment operator to fix this properly but that
280 // would break binary compatibility...
281 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
282}
283
284wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style,
285 wxFontWeight weight, bool underlined,
286 const wxString& faceName,
287 wxFontEncoding encoding)
288{
289 Init(size, family, style, weight, underlined, faceName, encoding);
290}
291
292wxFontRefData::wxFontRefData(const wxString& fontname)
293{
294 // FromString() should really work in GTK1 too, doesn't it?
295 m_nativeFontInfo.SetXFontName(fontname);
296
297 InitFromNative();
298}
299
300void wxFontRefData::ClearGdkFonts()
301{
302 for ( wxScaledFontList::iterator i = m_scaled_xfonts.begin();
303 i != m_scaled_xfonts.end();
304 ++i )
305 {
306 GdkFont *font = i->second;
307 gdk_font_unref( font );
308 }
309
310 m_scaled_xfonts.clear();
311}
312
313wxFontRefData::~wxFontRefData()
314{
315 ClearGdkFonts();
316}
317
318// ----------------------------------------------------------------------------
319// wxFontRefData SetXXX()
320// ----------------------------------------------------------------------------
321
322void wxFontRefData::SetPointSize(int pointSize)
323{
324 m_pointSize = pointSize;
325
326 if ( HasNativeFont() )
327 {
328 wxString size;
329 if ( pointSize == -1 )
330 size = wxT('*');
331 else
332 size.Printf(wxT("%d"), 10*pointSize);
333
334 m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
335 }
336}
337
338void wxFontRefData::SetFamily(wxFontFamily family)
339{
340 m_family = family;
341
342 // TODO: what are we supposed to do with m_nativeFontInfo here?
343}
344
345void wxFontRefData::SetStyle(wxFontStyle style)
346{
347 m_style = style;
348
349 if ( HasNativeFont() )
350 {
351 wxString slant;
352 switch ( style )
353 {
354 case wxFONTSTYLE_ITALIC:
355 slant = wxT('i');
356 break;
357
358 case wxFONTSTYLE_SLANT:
359 slant = wxT('o');
360 break;
361
362 default:
363 wxFAIL_MSG( wxT("unknown font style") );
364 // fall through
365
366 case wxFONTSTYLE_NORMAL:
367 slant = wxT('r');
368 }
369
370 m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
371 }
372}
373
374void wxFontRefData::SetWeight(wxFontWeight weight)
375{
376 m_weight = weight;
377
378 if ( HasNativeFont() )
379 {
380 wxString boldness;
381 switch ( weight )
382 {
383 case wxFONTWEIGHT_BOLD:
384 boldness = wxT("bold");
385 break;
386
387 case wxFONTWEIGHT_LIGHT:
388 boldness = wxT("light");
389 break;
390
391 default:
392 wxFAIL_MSG( wxT("unknown font weight") );
393 // fall through
394
395 case wxFONTWEIGHT_NORMAL:
396 // unspecified
397 boldness = wxT("medium");
398 }
399
400 m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
401 }
402}
403
404void wxFontRefData::SetUnderlined(bool underlined)
405{
406 m_underlined = underlined;
407
408 // the XLFD doesn't have "underlined" field anyhow
409}
410
411bool wxFontRefData::SetFaceName(const wxString& facename)
412{
413 m_faceName = facename;
414
415 if ( HasNativeFont() )
416 {
417 m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
418 }
419
420 return true;
421}
422
423void wxFontRefData::SetEncoding(wxFontEncoding encoding)
424{
425 m_encoding = encoding;
426
427 if ( HasNativeFont() )
428 {
429 wxNativeEncodingInfo info;
430 if ( wxGetNativeFontEncoding(encoding, &info) )
431 {
432 m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
433 m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
434 }
435 }
436}
437
438void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
439{
440 // previously cached fonts shouldn't be used
441 ClearGdkFonts();
442
443 m_nativeFontInfo = info;
444
445 // set all the other font parameters from the native font info
446 InitFromNative();
447}
448
449// ----------------------------------------------------------------------------
450// wxFont creation
451// ----------------------------------------------------------------------------
452
453IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
454
455wxFont::wxFont(const wxNativeFontInfo& info)
456{
457 (void) Create(info.GetXFontName());
458}
459
460bool wxFont::Create( int pointSize,
461 wxFontFamily family,
462 wxFontStyle style,
463 wxFontWeight weight,
464 bool underlined,
465 const wxString& face,
466 wxFontEncoding encoding)
467{
468 UnRef();
469
470 m_refData = new wxFontRefData(pointSize, family, style, weight,
471 underlined, face, encoding);
472
473 return true;
474}
475
476bool wxFont::Create(const wxString& fontname)
477{
478 // VZ: does this really happen?
479 if ( fontname.empty() )
480 {
481 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
482
483 return true;
484 }
485
486 m_refData = new wxFontRefData(fontname);
487
488 return true;
489}
490
491void wxFont::Unshare()
492{
493 if (!m_refData)
494 {
495 m_refData = new wxFontRefData();
496 }
497 else
498 {
499 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
500 UnRef();
501 m_refData = ref;
502 }
503}
504
505wxFont::~wxFont()
506{
507}
508
509wxGDIRefData *wxFont::CreateGDIRefData() const
510{
511 return new wxFontRefData;
512}
513
514wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
515{
516 return new wxFontRefData(*static_cast<const wxFontRefData *>(data));
517}
518
519// ----------------------------------------------------------------------------
520// accessors
521// ----------------------------------------------------------------------------
522
523int wxFont::GetPointSize() const
524{
525 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
526
527 return M_FONTDATA->m_pointSize;
528}
529
530wxString wxFont::GetFaceName() const
531{
532 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
533
534 return M_FONTDATA->m_faceName;
535}
536
537wxFontFamily wxFont::GetFamily() const
538{
539 wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
540
541 return M_FONTDATA->m_family;
542}
543
544wxFontStyle wxFont::GetStyle() const
545{
546 wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX, wxT("invalid font") );
547
548 return M_FONTDATA->m_style;
549}
550
551wxFontWeight wxFont::GetWeight() const
552{
553 wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX, wxT("invalid font") );
554
555 return M_FONTDATA->m_weight;
556}
557
558bool wxFont::GetUnderlined() const
559{
560 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
561
562 return M_FONTDATA->m_underlined;
563}
564
565wxFontEncoding wxFont::GetEncoding() const
566{
567 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
568
569 // m_encoding is unused in wxGTK2, return encoding that the user set.
570 return M_FONTDATA->m_encoding;
571}
572
573const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
574{
575 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
576
577 if ( !M_FONTDATA->HasNativeFont() )
578 {
579 // NB: this call has important side-effect: it not only finds
580 // GdkFont representation, it also initializes m_nativeFontInfo
581 // by calling its SetXFontName method
582 GetInternalFont();
583 }
584
585 return &(M_FONTDATA->m_nativeFontInfo);
586}
587
588bool wxFont::IsFixedWidth() const
589{
590 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
591
592 if ( M_FONTDATA->HasNativeFont() )
593 {
594 // the monospace fonts are supposed to have "M" in the spacing field
595 wxString spacing = M_FONTDATA->
596 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
597
598 return spacing.Upper() == wxT('M');
599 }
600
601 return wxFontBase::IsFixedWidth();
602}
603
604// ----------------------------------------------------------------------------
605// change font attributes
606// ----------------------------------------------------------------------------
607
608void wxFont::SetPointSize(int pointSize)
609{
610 Unshare();
611
612 M_FONTDATA->SetPointSize(pointSize);
613}
614
615void wxFont::SetFamily(wxFontFamily family)
616{
617 Unshare();
618
619 M_FONTDATA->SetFamily(family);
620}
621
622void wxFont::SetStyle(wxFontStyle style)
623{
624 Unshare();
625
626 M_FONTDATA->SetStyle(style);
627}
628
629void wxFont::SetWeight(wxFontWeight weight)
630{
631 Unshare();
632
633 M_FONTDATA->SetWeight(weight);
634}
635
636bool wxFont::SetFaceName(const wxString& faceName)
637{
638 Unshare();
639
640 return M_FONTDATA->SetFaceName(faceName) &&
641 wxFontBase::SetFaceName(faceName);
642}
643
644void wxFont::SetUnderlined(bool underlined)
645{
646 Unshare();
647
648 M_FONTDATA->SetUnderlined(underlined);
649}
650
651void wxFont::SetEncoding(wxFontEncoding encoding)
652{
653 Unshare();
654
655 M_FONTDATA->SetEncoding(encoding);
656}
657
658void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
659{
660 Unshare();
661
662 M_FONTDATA->SetNativeFontInfo( info );
663}
664
665// ----------------------------------------------------------------------------
666// get internal representation of font
667// ----------------------------------------------------------------------------
668
669static GdkFont *g_systemDefaultGuiFont = NULL;
670
671// this is also used from toolbar.cpp and tooltip.cpp, hence extern
672extern GdkFont *GtkGetDefaultGuiFont()
673{
674 if (!g_systemDefaultGuiFont)
675 {
676 GtkWidget *widget = gtk_button_new();
677 GtkStyle *def = gtk_rc_get_style( widget );
678 if (def)
679 {
680 g_systemDefaultGuiFont = gdk_font_ref( def->font );
681 }
682 else
683 {
684 def = gtk_widget_get_default_style();
685 if (def)
686 g_systemDefaultGuiFont = gdk_font_ref( def->font );
687 }
688 gtk_widget_destroy( widget );
689 }
690 else
691 {
692 // already have it, but ref it once more before returning
693 gdk_font_ref(g_systemDefaultGuiFont);
694 }
695
696 return g_systemDefaultGuiFont;
697}
698
699GdkFont *wxFont::GetInternalFont( float scale ) const
700{
701 GdkFont *font = NULL;
702
703 wxCHECK_MSG( Ok(), font, wxT("invalid font") );
704
705 long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
706 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
707
708 wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts;
709 wxScaledFontList::iterator i = list.find(int_scale);
710 if ( i != list.end() )
711 {
712 font = i->second;
713 }
714 else // we don't have this font in this size yet
715 {
716 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
717 {
718 font = GtkGetDefaultGuiFont();
719 }
720
721 if ( !font )
722 {
723 // do we have the XLFD?
724 if ( int_scale == 100 && M_FONTDATA->HasNativeFont() )
725 {
726 font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
727 }
728
729 // no XLFD of no exact match - try the approximate one now
730 if ( !font )
731 {
732 wxString xfontname;
733 font = wxLoadQueryNearestFont( point_scale,
734 M_FONTDATA->m_family,
735 M_FONTDATA->m_style,
736 M_FONTDATA->m_weight,
737 M_FONTDATA->m_underlined,
738 M_FONTDATA->m_faceName,
739 M_FONTDATA->m_encoding,
740 &xfontname);
741 // NB: wxFont::GetNativeFontInfo relies on this
742 // side-effect of GetInternalFont
743 if ( int_scale == 100 )
744 M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname);
745 }
746 }
747
748 if ( font )
749 {
750 list[int_scale] = font;
751 }
752 }
753
754 // it's quite useless to make it a wxCHECK because we're going to crash
755 // anyhow...
756 wxASSERT_MSG( font, wxT("could not load any font?") );
757
758 return font;
759}