]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/font.cpp
survive delete within Notify
[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 : wxObjectRefData()
178 {
179 m_pointSize = data.m_pointSize;
180 m_family = data.m_family;
181 m_style = data.m_style;
182 m_weight = data.m_weight;
183
184 m_underlined = data.m_underlined;
185
186 m_faceName = data.m_faceName;
187 m_encoding = data.m_encoding;
188
189 m_nativeFontInfo = data.m_nativeFontInfo;
190 }
191
192 wxFontRefData::wxFontRefData(int size, int family, int style,
193 int weight, bool underlined,
194 const wxString& faceName,
195 wxFontEncoding encoding)
196 {
197 Init(size, family, style, weight, underlined, faceName, encoding);
198 }
199
200 wxFontRefData::wxFontRefData(const wxString& fontname)
201 {
202 // remember the X font name
203 m_nativeFontInfo.SetXFontName(fontname);
204
205 // get the font parameters from the XLFD
206 // -------------------------------------
207
208 m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY);
209
210 m_weight = wxFONTWEIGHT_NORMAL;
211
212 wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper();
213 if ( !w.empty() && w != _T('*') )
214 {
215 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
216 // and BLACK
217 if ( ((w[0u] == _T('B') && (!strcmp(w.c_str() + 1, _T("OLD")) ||
218 !strcmp(w.c_str() + 1, _T("LACK"))))) ||
219 strstr(w.c_str() + 1, _T("BOLD")) )
220 {
221 m_weight = wxFONTWEIGHT_BOLD;
222 }
223 else if ( w == _T("LIGHT") || w == _T("THIN") )
224 {
225 m_weight = wxFONTWEIGHT_LIGHT;
226 }
227 }
228
229 switch ( wxToupper(*m_nativeFontInfo.
230 GetXFontComponent(wxXLFD_SLANT).c_str()) )
231 {
232 case _T('I'): // italique
233 m_style = wxFONTSTYLE_ITALIC;
234 break;
235
236 case _T('O'): // oblique
237 m_style = wxFONTSTYLE_SLANT;
238 break;
239
240 default:
241 m_style = wxFONTSTYLE_NORMAL;
242 }
243
244 long ptSize;
245 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
246 {
247 // size in XLFD is in 10 point units
248 m_pointSize = (int)(ptSize / 10);
249 }
250 else
251 {
252 m_pointSize = wxDEFAULT_FONT_SIZE;
253 }
254
255 // examine the spacing: if the font is monospaced, assume wxTELETYPE
256 // family for compatibility with the old code which used it instead of
257 // IsFixedWidth()
258 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') )
259 {
260 m_family = wxFONTFAMILY_TELETYPE;
261 }
262 else // not monospaceed
263 {
264 // don't even try guessing it, it doesn't work for too many fonts
265 // anyhow
266 m_family = wxFONTFAMILY_UNKNOWN;
267 }
268
269 // X fonts are never underlined...
270 m_underlined = FALSE;
271
272 // deal with font encoding
273 wxString
274 registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(),
275 encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper();
276
277 if ( registry == _T("ISO8859") )
278 {
279 int cp;
280 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
281 {
282 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
283 }
284 }
285 else if ( registry == _T("MICROSOFT") )
286 {
287 int cp;
288 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
289 {
290 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
291 }
292 }
293 else if ( registry == _T("KOI8") )
294 {
295 m_encoding = wxFONTENCODING_KOI8;
296 }
297 else // unknown encoding
298 {
299 // may be give a warning here?
300 m_encoding = wxFONTENCODING_SYSTEM;
301 }
302 }
303
304 wxFontRefData::~wxFontRefData()
305 {
306 wxNode *node = m_scaled_xfonts.First();
307 while (node)
308 {
309 GdkFont *font = (GdkFont*)node->Data();
310 wxNode *next = node->Next();
311 gdk_font_unref( font );
312 node = next;
313 }
314 }
315
316 // ----------------------------------------------------------------------------
317 // wxFontRefData SetXXX()
318 // ----------------------------------------------------------------------------
319
320 void wxFontRefData::SetPointSize(int pointSize)
321 {
322 m_pointSize = pointSize;
323
324 if ( HasNativeFont() )
325 {
326 wxString size;
327 if ( pointSize == -1 )
328 size = _T('*');
329 else
330 size.Printf(_T("%d"), 10*pointSize);
331
332 m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
333 }
334 }
335
336 void wxFontRefData::SetFamily(int family)
337 {
338 m_family = family;
339
340 // TODO: what are we supposed to do with m_nativeFontInfo here?
341 }
342
343 void wxFontRefData::SetStyle(int style)
344 {
345 m_style = style;
346
347 if ( HasNativeFont() )
348 {
349 wxString slant;
350 switch ( style )
351 {
352 case wxFONTSTYLE_ITALIC:
353 slant = _T('i');
354 break;
355
356 case wxFONTSTYLE_SLANT:
357 slant = _T('o');
358 break;
359
360 default:
361 wxFAIL_MSG( _T("unknown font style") );
362 // fall through
363
364 case wxFONTSTYLE_NORMAL:
365 slant = _T('r');
366 }
367
368 m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
369 }
370 }
371
372 void wxFontRefData::SetWeight(int weight)
373 {
374 m_weight = weight;
375
376 if ( HasNativeFont() )
377 {
378 wxString boldness;
379 switch ( weight )
380 {
381 case wxFONTWEIGHT_BOLD:
382 boldness = _T("bold");
383 break;
384
385 case wxFONTWEIGHT_LIGHT:
386 boldness = _T("light");
387 break;
388
389 default:
390 wxFAIL_MSG( _T("unknown font weight") );
391 // fall through
392
393 case wxFONTWEIGHT_NORMAL:
394 // unspecified
395 boldness = _T("medium");
396 }
397
398 m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
399 }
400 }
401
402 void wxFontRefData::SetUnderlined(bool underlined)
403 {
404 m_underlined = underlined;
405
406 // the XLFD doesn't have "underlined" field anyhow
407 }
408
409 void wxFontRefData::SetFaceName(const wxString& facename)
410 {
411 m_faceName = facename;
412
413 if ( HasNativeFont() )
414 {
415 m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
416 }
417 }
418
419 void wxFontRefData::SetEncoding(wxFontEncoding encoding)
420 {
421 m_encoding = encoding;
422
423 if ( HasNativeFont() )
424 {
425 wxNativeEncodingInfo info;
426 if ( wxGetNativeFontEncoding(encoding, &info) )
427 {
428 m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
429 m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
430 }
431 }
432 }
433
434 // ============================================================================
435 // wxFont implementation
436 // ============================================================================
437
438 // ----------------------------------------------------------------------------
439 // wxFont creation
440 // ----------------------------------------------------------------------------
441
442 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
443
444 void wxFont::Init()
445 {
446 }
447
448 wxFont::wxFont(const wxNativeFontInfo& info)
449 {
450 Init();
451
452 Create(info.GetXFontName());
453 }
454
455 bool wxFont::Create( int pointSize,
456 int family,
457 int style,
458 int weight,
459 bool underlined,
460 const wxString& face,
461 wxFontEncoding encoding)
462 {
463 m_refData = new wxFontRefData(pointSize, family, style, weight,
464 underlined, face, encoding);
465
466 return TRUE;
467 }
468
469 bool wxFont::Create(const wxString& fontname)
470 {
471 // VZ: does this really happen?
472 if ( fontname.empty() )
473 {
474 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
475
476 return TRUE;
477 }
478
479 m_refData = new wxFontRefData(fontname);
480
481 return TRUE;
482 }
483
484 void wxFont::Unshare()
485 {
486 if (!m_refData)
487 {
488 m_refData = new wxFontRefData();
489 }
490 else
491 {
492 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
493 UnRef();
494 m_refData = ref;
495 }
496 }
497
498 wxFont::~wxFont()
499 {
500 }
501
502 // ----------------------------------------------------------------------------
503 // accessors
504 // ----------------------------------------------------------------------------
505
506 // all accessors are just forwarded to wxFontRefData which has everything we
507 // need
508
509 int wxFont::GetPointSize() const
510 {
511 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
512
513 return M_FONTDATA->m_pointSize;
514 }
515
516 wxString wxFont::GetFaceName() const
517 {
518 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
519
520 return M_FONTDATA->m_faceName;
521 }
522
523 int wxFont::GetFamily() const
524 {
525 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
526
527 return M_FONTDATA->m_family;
528 }
529
530 int wxFont::GetStyle() const
531 {
532 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
533
534 return M_FONTDATA->m_style;
535 }
536
537 int wxFont::GetWeight() const
538 {
539 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
540
541 return M_FONTDATA->m_weight;
542 }
543
544 bool wxFont::GetUnderlined() const
545 {
546 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
547
548 return M_FONTDATA->m_underlined;
549 }
550
551 wxFontEncoding wxFont::GetEncoding() const
552 {
553 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
554
555 return M_FONTDATA->m_encoding;
556 }
557
558 wxNativeFontInfo *wxFont::GetNativeFontInfo() const
559 {
560 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
561
562 if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
563 GetInternalFont();
564
565 return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
566 }
567
568 bool wxFont::IsFixedWidth() const
569 {
570 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
571
572 if ( M_FONTDATA->HasNativeFont() )
573 {
574 // the monospace fonts are supposed to have "M" in the spacing field
575 wxString spacing = M_FONTDATA->
576 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
577
578 return spacing.Upper() == _T('M');
579 }
580
581 return wxFontBase::IsFixedWidth();
582 }
583
584 // ----------------------------------------------------------------------------
585 // change font attributes
586 // ----------------------------------------------------------------------------
587
588 void wxFont::SetPointSize(int pointSize)
589 {
590 Unshare();
591
592 M_FONTDATA->SetPointSize(pointSize);
593 }
594
595 void wxFont::SetFamily(int family)
596 {
597 Unshare();
598
599 M_FONTDATA->SetFamily(family);
600 }
601
602 void wxFont::SetStyle(int style)
603 {
604 Unshare();
605
606 M_FONTDATA->SetStyle(style);
607 }
608
609 void wxFont::SetWeight(int weight)
610 {
611 Unshare();
612
613 M_FONTDATA->SetWeight(weight);
614 }
615
616 void wxFont::SetFaceName(const wxString& faceName)
617 {
618 Unshare();
619
620 M_FONTDATA->SetFaceName(faceName);
621 }
622
623 void wxFont::SetUnderlined(bool underlined)
624 {
625 Unshare();
626
627 M_FONTDATA->SetUnderlined(underlined);
628 }
629
630 void wxFont::SetEncoding(wxFontEncoding encoding)
631 {
632 Unshare();
633
634 M_FONTDATA->SetEncoding(encoding);
635 }
636
637 void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
638 {
639 Unshare();
640
641 M_FONTDATA->m_nativeFontInfo = info;
642 }
643
644 // ----------------------------------------------------------------------------
645 // get internal representation of font
646 // ----------------------------------------------------------------------------
647
648 static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
649
650 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
651 extern GdkFont *GtkGetDefaultGuiFont()
652 {
653 if (!g_systemDefaultGuiFont)
654 {
655 GtkWidget *widget = gtk_button_new();
656 GtkStyle *def = gtk_rc_get_style( widget );
657 if (def)
658 {
659 g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) );
660 }
661 else
662 {
663 def = gtk_widget_get_default_style();
664 if (def)
665 g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) );
666 }
667 gtk_widget_destroy( widget );
668 }
669 else
670 {
671 // already have it, but ref it once more before returning
672 gdk_font_ref(g_systemDefaultGuiFont);
673 }
674
675 return g_systemDefaultGuiFont;
676 }
677
678 GdkFont *wxFont::GetInternalFont( float scale ) const
679 {
680 GdkFont *font = (GdkFont *) NULL;
681
682 wxCHECK_MSG( Ok(), font, wxT("invalid font") )
683
684 long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
685 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
686
687 wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
688 if (node)
689 {
690 font = (GdkFont*)node->Data();
691 }
692 else // we don't have this font in this size yet
693 {
694 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
695 {
696 font = GtkGetDefaultGuiFont();
697 }
698
699 if ( !font )
700 {
701 // do we have the XLFD?
702 if ( M_FONTDATA->HasNativeFont() )
703 {
704 font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
705 }
706
707 // no XLFD of no exact match - try the approximate one now
708 if ( !font )
709 {
710 wxString xfontname;
711 font = wxLoadQueryNearestFont( point_scale,
712 M_FONTDATA->m_family,
713 M_FONTDATA->m_style,
714 M_FONTDATA->m_weight,
715 M_FONTDATA->m_underlined,
716 M_FONTDATA->m_faceName,
717 M_FONTDATA->m_encoding,
718 &xfontname);
719 if ( font )
720 {
721 M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname);
722 }
723 }
724 }
725
726 if ( font )
727 {
728 M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
729 }
730 }
731
732 // it's quite useless to make it a wxCHECK because we're going to crash
733 // anyhow...
734 wxASSERT_MSG( font, wxT("could not load any font?") );
735
736 return font;
737 }
738