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