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