]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/font.cpp
don't allocate system fonts/colours dynamically
[wxWidgets.git] / src / gtk1 / font.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
409d5a58 2// Name: gtk/font.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
6c9a19aa 6// Copyright: (c) 1998 Robert Roebling and Julian Smart
0c5d3e1c 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
0c5d3e1c
VZ
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
14f355c2 18#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
0c5d3e1c 19 #pragma implementation "font.h"
c801d85f
KB
20#endif
21
14f355c2
VS
22// For compilers that support precompilation, includes "wx.h".
23#include "wx/wxprec.h"
24
c801d85f 25#include "wx/font.h"
7beba2fc
VZ
26#include "wx/fontutil.h"
27#include "wx/cmndata.h"
c801d85f 28#include "wx/utils.h"
5705323e 29#include "wx/log.h"
4cb122de 30#include "wx/gdicmn.h"
8636aed8 31#include "wx/tokenzr.h"
c7985368 32#include "wx/settings.h"
0c5d3e1c 33
c801d85f
KB
34#include <strings.h>
35
9e691f46 36#include "wx/gtk/private.h"
d06b34a7 37#include <gdk/gdkprivate.h>
83624f79 38
409d5a58
VZ
39// ----------------------------------------------------------------------------
40// constants
41// ----------------------------------------------------------------------------
42
43// the default size (in points) for the fonts
44static const int wxDEFAULT_FONT_SIZE = 12;
45
46// ----------------------------------------------------------------------------
011ba5ed 47// wxScaledFontList: maps the font sizes to the GDK fonts for the given font
409d5a58
VZ
48// ----------------------------------------------------------------------------
49
011ba5ed
VZ
50WX_DECLARE_HASH_MAP(int, GdkFont *, wxIntegerHash, wxIntegerEqual,
51 wxScaledFontList);
409d5a58 52
0c5d3e1c
VZ
53// ----------------------------------------------------------------------------
54// wxFontRefData
55// ----------------------------------------------------------------------------
56
57class wxFontRefData : public wxObjectRefData
c801d85f 58{
8bbe427f 59public:
409d5a58
VZ
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,
0c5d3e1c
VZ
65 bool underlined = FALSE,
66 const wxString& faceName = wxEmptyString,
7826e2dd 67 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
409d5a58
VZ
68
69 // from XFLD
70 wxFontRefData(const wxString& fontname);
71
72 // copy ctor
358fc25c 73 wxFontRefData( const wxFontRefData& data );
409d5a58 74
0c5d3e1c
VZ
75 virtual ~wxFontRefData();
76
409d5a58
VZ
77 // do we have the native font info?
78 bool HasNativeFont() const
79 {
db16cab4 80#ifdef __WXGTK20__
011ba5ed
VZ
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
409d5a58 85 return !m_nativeFontInfo.IsDefault();
011ba5ed 86#endif // GTK 2.0/1.x
409d5a58
VZ
87 }
88
0aa1258a 89#ifndef __WXGTK20__
be5b2f9f
VZ
90 // reinitilize the font with the gived XFLD
91 void ReInit(const wxString& fontname);
0aa1258a 92#endif
be5b2f9f 93
409d5a58
VZ
94 // setters: all of them also take care to modify m_nativeFontInfo if we
95 // have it so as to not lose the information not carried by our fields
96 void SetPointSize(int pointSize);
97 void SetFamily(int family);
98 void SetStyle(int style);
99 void SetWeight(int weight);
100 void SetUnderlined(bool underlined);
101 void SetFaceName(const wxString& facename);
102 void SetEncoding(wxFontEncoding encoding);
103
2b5f62a0
VZ
104 void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; }
105 bool GetNoAntiAliasing() { return m_noAA; }
cd9a673c 106
011ba5ed
VZ
107 // and this one also modifies all the other font data fields
108 void SetNativeFontInfo(const wxNativeFontInfo& info);
109
409d5a58
VZ
110 // debugger helper: shows what the font really is
111 //
112 // VZ: I need this as my gdb either shows wildly wrong values or crashes
113 // when I ask it to "p fontRefData" :-(
db16cab4 114#if defined(__WXDEBUG__) && !defined(__WXGTK20__)
409d5a58
VZ
115 void Dump() const
116 {
117 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
118 m_faceName.c_str(),
119 m_weight == wxFONTWEIGHT_NORMAL
120 ? _T("normal")
121 : m_weight == wxFONTWEIGHT_BOLD
122 ? _T("bold")
123 : _T("light"),
124 m_style == wxFONTSTYLE_NORMAL ? _T("regular") : _T("italic"),
125 m_pointSize,
126 m_encoding);
127 }
128#endif // Debug
129
0c5d3e1c
VZ
130protected:
131 // common part of all ctors
132 void Init(int pointSize,
133 int family,
134 int style,
135 int weight,
136 bool underlined,
137 const wxString& faceName,
7826e2dd 138 wxFontEncoding encoding);
0c5d3e1c 139
011ba5ed
VZ
140 // set all fields from (already initialized and valid) m_nativeFontInfo
141 void InitFromNative();
142
0c5d3e1c 143private:
2b5f62a0 144 // clear m_scaled_xfonts if any
011ba5ed
VZ
145 void ClearGdkFonts();
146
2b5f62a0 147#ifndef __WXGTK20__
409d5a58
VZ
148 // the map of font sizes to "GdkFont *"
149 wxScaledFontList m_scaled_xfonts;
011ba5ed 150#endif // GTK 2.0/1.x
409d5a58 151
f35c2659
RR
152 int m_pointSize;
153 int m_family,
154 m_style,
155 m_weight;
156 bool m_underlined;
157 wxString m_faceName;
db16cab4 158 wxFontEncoding m_encoding; // Unused under GTK 2.0
2b5f62a0 159 bool m_noAA; // No anti-aliasing
7826e2dd 160
db16cab4
RR
161 // The native font info, basicly an XFLD under GTK 1.2 and
162 // the pango font description under GTK 2.0.
30764ab5 163 wxNativeFontInfo m_nativeFontInfo;
8bbe427f 164
f6bcfd97 165 friend class wxFont;
c801d85f
KB
166};
167
0c5d3e1c 168// ----------------------------------------------------------------------------
cd9a673c 169// wxFontRefData
0c5d3e1c
VZ
170// ----------------------------------------------------------------------------
171
172void wxFontRefData::Init(int pointSize,
173 int family,
174 int style,
175 int weight,
176 bool underlined,
177 const wxString& faceName,
7826e2dd 178 wxFontEncoding encoding)
8bbe427f 179{
409d5a58 180 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
0c5d3e1c
VZ
181
182 m_faceName = faceName;
183
409d5a58
VZ
184 // we accept both wxDEFAULT and wxNORMAL here - should we?
185 m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
186 m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
0c5d3e1c 187
409d5a58
VZ
188 // and here, do we really want to forbid creation of the font of the size
189 // 90 (the value of wxDEFAULT)??
011ba5ed
VZ
190 m_pointSize = pointSize == wxDEFAULT || pointSize == -1
191 ? wxDEFAULT_FONT_SIZE
192 : pointSize;
0c5d3e1c
VZ
193
194 m_underlined = underlined;
195 m_encoding = encoding;
cd9a673c 196
2b5f62a0 197 m_noAA = FALSE;
011ba5ed
VZ
198
199#ifdef __WXGTK20__
46eed000
RR
200 // Create native font info
201 m_nativeFontInfo.description = pango_font_description_new();
202
011ba5ed 203 // And set its values
2b5f62a0
VZ
204 if (!m_faceName.empty())
205 {
206 pango_font_description_set_family( m_nativeFontInfo.description, wxGTK_CONV(m_faceName) );
207 }
208 else
209 {
210 switch (m_family)
211 {
212 case wxFONTFAMILY_MODERN:
213 case wxFONTFAMILY_TELETYPE:
214 pango_font_description_set_family( m_nativeFontInfo.description, "monospace" );
215 break;
216 case wxFONTFAMILY_ROMAN:
217 pango_font_description_set_family( m_nativeFontInfo.description, "serif" );
218 break;
219 case wxFONTFAMILY_SWISS:
220 // SWISS = sans serif
221 default:
222 pango_font_description_set_family( m_nativeFontInfo.description, "sans" );
223 break;
224 }
46eed000 225 }
cd9a673c 226
46eed000
RR
227 SetStyle( m_style );
228 SetPointSize( m_pointSize );
229 SetWeight( m_weight );
011ba5ed 230#endif // GTK 2.0
358fc25c
RR
231}
232
011ba5ed 233void wxFontRefData::InitFromNative()
409d5a58 234{
2b5f62a0
VZ
235 m_noAA = FALSE;
236
db16cab4 237#ifdef __WXGTK20__
db16cab4
RR
238 // Get native info
239 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 240
db16cab4
RR
241 // init fields
242 m_faceName = wxGTK_CONV_BACK( pango_font_description_get_family( desc ) );
011ba5ed 243
db16cab4 244 m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE;
011ba5ed 245
db16cab4
RR
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 switch (pango_font_description_get_weight( desc ))
260 {
261 case PANGO_WEIGHT_ULTRALIGHT:
262 m_weight = wxFONTWEIGHT_LIGHT;
263 break;
264 case PANGO_WEIGHT_LIGHT:
265 m_weight = wxFONTWEIGHT_LIGHT;
266 break;
267 case PANGO_WEIGHT_NORMAL:
268 m_weight = wxFONTWEIGHT_NORMAL;
269 break;
270 case PANGO_WEIGHT_BOLD:
271 m_weight = wxFONTWEIGHT_BOLD;
272 break;
273 case PANGO_WEIGHT_ULTRABOLD:
274 m_weight = wxFONTWEIGHT_BOLD;
275 break;
276 case PANGO_WEIGHT_HEAVY:
277 m_weight = wxFONTWEIGHT_BOLD;
278 break;
279 }
011ba5ed 280
a732ef91 281 if (m_faceName == wxT("monospace"))
db16cab4
RR
282 {
283 m_family = wxFONTFAMILY_TELETYPE;
284 }
285 else if (m_faceName == wxT("sans"))
286 {
287 m_family = wxFONTFAMILY_SWISS;
288 }
2b5f62a0
VZ
289 else if (m_faceName == wxT("serif"))
290 {
291 m_family = wxFONTFAMILY_ROMAN;
292 }
db16cab4
RR
293 else
294 {
295 m_family = wxFONTFAMILY_UNKNOWN;
296 }
297
298 // Pango description are never underlined (?)
299 m_underlined = FALSE;
300
301 // Cannot we choose that
302 m_encoding = wxFONTENCODING_SYSTEM;
011ba5ed 303#else // GTK 1.x
409d5a58
VZ
304 // get the font parameters from the XLFD
305 // -------------------------------------
306
307 m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY);
308
309 m_weight = wxFONTWEIGHT_NORMAL;
310
311 wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper();
312 if ( !w.empty() && w != _T('*') )
313 {
314 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
315 // and BLACK
fab591c5
RR
316 if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
317 !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
318 wxStrstr(w.c_str() + 1, _T("BOLD")) )
409d5a58
VZ
319 {
320 m_weight = wxFONTWEIGHT_BOLD;
321 }
322 else if ( w == _T("LIGHT") || w == _T("THIN") )
323 {
324 m_weight = wxFONTWEIGHT_LIGHT;
325 }
326 }
327
328 switch ( wxToupper(*m_nativeFontInfo.
329 GetXFontComponent(wxXLFD_SLANT).c_str()) )
330 {
331 case _T('I'): // italique
332 m_style = wxFONTSTYLE_ITALIC;
333 break;
334
335 case _T('O'): // oblique
336 m_style = wxFONTSTYLE_SLANT;
337 break;
338
339 default:
340 m_style = wxFONTSTYLE_NORMAL;
341 }
342
343 long ptSize;
344 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
345 {
346 // size in XLFD is in 10 point units
347 m_pointSize = (int)(ptSize / 10);
348 }
349 else
350 {
351 m_pointSize = wxDEFAULT_FONT_SIZE;
352 }
353
354 // examine the spacing: if the font is monospaced, assume wxTELETYPE
355 // family for compatibility with the old code which used it instead of
356 // IsFixedWidth()
357 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') )
358 {
359 m_family = wxFONTFAMILY_TELETYPE;
360 }
361 else // not monospaceed
362 {
363 // don't even try guessing it, it doesn't work for too many fonts
364 // anyhow
365 m_family = wxFONTFAMILY_UNKNOWN;
366 }
367
368 // X fonts are never underlined...
369 m_underlined = FALSE;
370
371 // deal with font encoding
372 wxString
373 registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(),
374 encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper();
375
376 if ( registry == _T("ISO8859") )
377 {
378 int cp;
379 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
380 {
381 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
382 }
383 }
384 else if ( registry == _T("MICROSOFT") )
385 {
386 int cp;
387 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
388 {
389 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
390 }
391 }
392 else if ( registry == _T("KOI8") )
393 {
394 m_encoding = wxFONTENCODING_KOI8;
395 }
396 else // unknown encoding
397 {
011ba5ed 398 // may be give a warning here? or use wxFontMapper?
409d5a58
VZ
399 m_encoding = wxFONTENCODING_SYSTEM;
400 }
011ba5ed 401#endif // GTK 2.0/1.x
409d5a58
VZ
402}
403
011ba5ed
VZ
404wxFontRefData::wxFontRefData( const wxFontRefData& data )
405 : wxObjectRefData()
406{
407 m_pointSize = data.m_pointSize;
408 m_family = data.m_family;
409 m_style = data.m_style;
410 m_weight = data.m_weight;
411
412 m_underlined = data.m_underlined;
413
414 m_faceName = data.m_faceName;
415 m_encoding = data.m_encoding;
416
2b5f62a0 417 m_noAA = data.m_noAA;
cd9a673c
RD
418
419 // Forces a copy of the internal data. wxNativeFontInfo should probably
420 // have a copy ctor and assignment operator to fix this properly but that
421 // would break binary compatibility...
422 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
011ba5ed
VZ
423}
424
425wxFontRefData::wxFontRefData(int size, int family, int style,
426 int weight, bool underlined,
427 const wxString& faceName,
428 wxFontEncoding encoding)
429{
430 Init(size, family, style, weight, underlined, faceName, encoding);
431}
432
433wxFontRefData::wxFontRefData(const wxString& fontname)
8bbe427f 434{
011ba5ed
VZ
435 // VZ: FromString() should really work in both cases, doesn't it?
436#ifdef __WXGTK20__
437 m_nativeFontInfo.FromString( fontname );
438#else // GTK 1.x
439 m_nativeFontInfo.SetXFontName(fontname);
440#endif // GTK 2.0/1.x
441
442 InitFromNative();
443}
444
0aa1258a 445#ifndef __WXGTK20__
be5b2f9f
VZ
446void wxFontRefData::ReInit(const wxString& fontname)
447{
448 m_nativeFontInfo.SetXFontName(fontname);
449
450 InitFromNative();
451}
0aa1258a 452#endif
be5b2f9f 453
011ba5ed
VZ
454void wxFontRefData::ClearGdkFonts()
455{
2b5f62a0 456#ifndef __WXGTK20__
011ba5ed
VZ
457 for ( wxScaledFontList::iterator i = m_scaled_xfonts.begin();
458 i != m_scaled_xfonts.end();
459 ++i )
8bbe427f 460 {
011ba5ed 461 GdkFont *font = i->second;
8bbe427f 462 gdk_font_unref( font );
8bbe427f 463 }
011ba5ed
VZ
464
465 m_scaled_xfonts.clear();
011ba5ed 466#endif // GTK 1.x
2b5f62a0 467}
011ba5ed
VZ
468
469wxFontRefData::~wxFontRefData()
470{
471 ClearGdkFonts();
0c5d3e1c 472}
c801d85f 473
0c5d3e1c 474// ----------------------------------------------------------------------------
409d5a58 475// wxFontRefData SetXXX()
0c5d3e1c 476// ----------------------------------------------------------------------------
c801d85f 477
409d5a58 478void wxFontRefData::SetPointSize(int pointSize)
c801d85f 479{
409d5a58 480 m_pointSize = pointSize;
c801d85f 481
db16cab4
RR
482#ifdef __WXGTK20__
483 // Get native info
484 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 485
db16cab4
RR
486 pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE );
487#else
409d5a58
VZ
488 if ( HasNativeFont() )
489 {
490 wxString size;
491 if ( pointSize == -1 )
492 size = _T('*');
493 else
494 size.Printf(_T("%d"), 10*pointSize);
7826e2dd 495
409d5a58
VZ
496 m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
497 }
db16cab4 498#endif
7826e2dd
VZ
499}
500
409d5a58 501void wxFontRefData::SetFamily(int family)
7826e2dd 502{
409d5a58 503 m_family = family;
30764ab5 504
409d5a58 505 // TODO: what are we supposed to do with m_nativeFontInfo here?
30764ab5
VZ
506}
507
409d5a58 508void wxFontRefData::SetStyle(int style)
c801d85f 509{
409d5a58
VZ
510 m_style = style;
511
db16cab4
RR
512#ifdef __WXGTK20__
513 // Get native info
514 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 515
db16cab4
RR
516 switch ( style )
517 {
518 case wxFONTSTYLE_ITALIC:
519 pango_font_description_set_style( desc, PANGO_STYLE_ITALIC );
520 break;
521 case wxFONTSTYLE_SLANT:
522 pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE );
523 break;
524 default:
525 wxFAIL_MSG( _T("unknown font style") );
526 // fall through
527 case wxFONTSTYLE_NORMAL:
528 pango_font_description_set_style( desc, PANGO_STYLE_NORMAL );
529 break;
530 }
531#else
409d5a58 532 if ( HasNativeFont() )
30764ab5 533 {
409d5a58
VZ
534 wxString slant;
535 switch ( style )
536 {
537 case wxFONTSTYLE_ITALIC:
538 slant = _T('i');
539 break;
540
541 case wxFONTSTYLE_SLANT:
542 slant = _T('o');
543 break;
544
545 default:
546 wxFAIL_MSG( _T("unknown font style") );
547 // fall through
548
549 case wxFONTSTYLE_NORMAL:
550 slant = _T('r');
551 }
552
553 m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
30764ab5 554 }
db16cab4 555#endif
409d5a58 556}
7beba2fc 557
409d5a58
VZ
558void wxFontRefData::SetWeight(int weight)
559{
560 m_weight = weight;
8bbe427f 561
2b5f62a0
VZ
562#ifdef __WXGTK20__
563 PangoFontDescription *desc = m_nativeFontInfo.description;
564 switch ( weight )
565 {
566 case wxFONTWEIGHT_BOLD:
567 pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD);
568 break;
569
570 case wxFONTWEIGHT_LIGHT:
571 pango_font_description_set_weight(desc, PANGO_WEIGHT_LIGHT);
572 break;
573
574 default:
575 wxFAIL_MSG( _T("unknown font weight") );
576 // fall through
577
578 case wxFONTWEIGHT_NORMAL:
579 // unspecified
580 pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL);
581 }
582#else //!__WXGTK20__
409d5a58
VZ
583 if ( HasNativeFont() )
584 {
585 wxString boldness;
586 switch ( weight )
587 {
588 case wxFONTWEIGHT_BOLD:
589 boldness = _T("bold");
590 break;
30764ab5 591
409d5a58
VZ
592 case wxFONTWEIGHT_LIGHT:
593 boldness = _T("light");
594 break;
284b4c88 595
409d5a58
VZ
596 default:
597 wxFAIL_MSG( _T("unknown font weight") );
598 // fall through
284b4c88 599
409d5a58
VZ
600 case wxFONTWEIGHT_NORMAL:
601 // unspecified
602 boldness = _T("medium");
603 }
284b4c88 604
409d5a58
VZ
605 m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
606 }
db16cab4 607#endif
409d5a58 608}
30764ab5 609
409d5a58
VZ
610void wxFontRefData::SetUnderlined(bool underlined)
611{
612 m_underlined = underlined;
8636aed8 613
409d5a58
VZ
614 // the XLFD doesn't have "underlined" field anyhow
615}
30760ce7 616
409d5a58
VZ
617void wxFontRefData::SetFaceName(const wxString& facename)
618{
619 m_faceName = facename;
7beba2fc 620
db16cab4 621#ifndef __WXGTK20__
409d5a58
VZ
622 if ( HasNativeFont() )
623 {
624 m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
625 }
db16cab4 626#endif
409d5a58 627}
284b4c88 628
409d5a58
VZ
629void wxFontRefData::SetEncoding(wxFontEncoding encoding)
630{
631 m_encoding = encoding;
284b4c88 632
db16cab4 633#ifndef __WXGTK20__
409d5a58 634 if ( HasNativeFont() )
d06b34a7 635 {
409d5a58
VZ
636 wxNativeEncodingInfo info;
637 if ( wxGetNativeFontEncoding(encoding, &info) )
638 {
639 m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
640 m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
641 }
d06b34a7 642 }
db16cab4 643#endif
409d5a58 644}
284b4c88 645
011ba5ed
VZ
646void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
647{
648 // previously cached fonts shouldn't be used
649 ClearGdkFonts();
650
651 m_nativeFontInfo = info;
652
653 // set all the other font parameters from the native font info
654 InitFromNative();
655}
656
409d5a58
VZ
657// ----------------------------------------------------------------------------
658// wxFont creation
659// ----------------------------------------------------------------------------
36f210c8 660
409d5a58 661IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
36f210c8 662
409d5a58
VZ
663void wxFont::Init()
664{
665}
36f210c8 666
409d5a58
VZ
667wxFont::wxFont(const wxNativeFontInfo& info)
668{
669 Init();
670
db16cab4 671#ifdef __WXGTK20__
011ba5ed 672 Create( info.GetPointSize(),
db16cab4
RR
673 info.GetFamily(),
674 info.GetStyle(),
675 info.GetWeight(),
676 info.GetUnderlined(),
677 info.GetFaceName(),
678 info.GetEncoding() );
679#else
2b5f62a0 680 (void) Create(info.GetXFontName());
db16cab4 681#endif
409d5a58
VZ
682}
683
684bool wxFont::Create( int pointSize,
685 int family,
686 int style,
687 int weight,
688 bool underlined,
689 const wxString& face,
690 wxFontEncoding encoding)
691{
2b5f62a0
VZ
692 UnRef();
693
409d5a58
VZ
694 m_refData = new wxFontRefData(pointSize, family, style, weight,
695 underlined, face, encoding);
696
697 return TRUE;
698}
699
700bool wxFont::Create(const wxString& fontname)
701{
702 // VZ: does this really happen?
703 if ( fontname.empty() )
36f210c8 704 {
409d5a58 705 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7beba2fc 706
409d5a58 707 return TRUE;
36f210c8 708 }
409d5a58
VZ
709
710 m_refData = new wxFontRefData(fontname);
711
0c5d3e1c 712 return TRUE;
ff7b1510 713}
c801d85f 714
0c5d3e1c 715void wxFont::Unshare()
8bbe427f 716{
0c5d3e1c
VZ
717 if (!m_refData)
718 {
719 m_refData = new wxFontRefData();
720 }
721 else
722 {
723 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
724 UnRef();
725 m_refData = ref;
726 }
ff7b1510 727}
c801d85f 728
8bbe427f 729wxFont::~wxFont()
c801d85f 730{
ff7b1510 731}
c801d85f 732
0c5d3e1c
VZ
733// ----------------------------------------------------------------------------
734// accessors
735// ----------------------------------------------------------------------------
c801d85f 736
8bbe427f 737int wxFont::GetPointSize() const
c801d85f 738{
223d09f6 739 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
740
741 return M_FONTDATA->m_pointSize;
ff7b1510 742}
c801d85f 743
8bbe427f 744wxString wxFont::GetFaceName() const
c801d85f 745{
223d09f6 746 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
8bbe427f 747
36b3b54a 748 return M_FONTDATA->m_faceName;
ff7b1510 749}
c801d85f 750
8bbe427f 751int wxFont::GetFamily() const
c801d85f 752{
223d09f6 753 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
754
755 return M_FONTDATA->m_family;
ff7b1510 756}
c801d85f 757
8bbe427f 758int wxFont::GetStyle() const
c801d85f 759{
223d09f6 760 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
d84eb083 761
8bbe427f 762 return M_FONTDATA->m_style;
ff7b1510 763}
c801d85f 764
8bbe427f 765int wxFont::GetWeight() const
c801d85f 766{
223d09f6 767 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
768
769 return M_FONTDATA->m_weight;
770}
771
8bbe427f
VZ
772bool wxFont::GetUnderlined() const
773{
223d09f6 774 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
8bbe427f
VZ
775
776 return M_FONTDATA->m_underlined;
ff7b1510 777}
c801d85f 778
0c5d3e1c 779wxFontEncoding wxFont::GetEncoding() const
358fc25c 780{
223d09f6 781 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
0c5d3e1c
VZ
782
783 return M_FONTDATA->m_encoding;
358fc25c
RR
784}
785
2b5f62a0
VZ
786bool wxFont::GetNoAntiAliasing()
787{
788 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
789
790 return M_FONTDATA->m_noAA;
791}
792
3bf5a59b 793const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
30764ab5 794{
7826e2dd 795 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
30764ab5 796
cfcc3932 797#ifndef __WXGTK20__
409d5a58 798 if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
30764ab5 799 GetInternalFont();
db16cab4 800#endif
7826e2dd 801
3bf5a59b 802 return &(M_FONTDATA->m_nativeFontInfo);
30764ab5
VZ
803}
804
53f6aab7
VZ
805bool wxFont::IsFixedWidth() const
806{
807 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
808
db16cab4 809#ifndef __WXGTK20__
409d5a58 810 if ( M_FONTDATA->HasNativeFont() )
53f6aab7
VZ
811 {
812 // the monospace fonts are supposed to have "M" in the spacing field
813 wxString spacing = M_FONTDATA->
814 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
815
816 return spacing.Upper() == _T('M');
817 }
db16cab4 818#endif
53f6aab7
VZ
819
820 return wxFontBase::IsFixedWidth();
821}
30764ab5 822
0c5d3e1c
VZ
823// ----------------------------------------------------------------------------
824// change font attributes
825// ----------------------------------------------------------------------------
826
358fc25c
RR
827void wxFont::SetPointSize(int pointSize)
828{
829 Unshare();
011ba5ed 830
409d5a58 831 M_FONTDATA->SetPointSize(pointSize);
358fc25c
RR
832}
833
834void wxFont::SetFamily(int family)
835{
836 Unshare();
837
409d5a58 838 M_FONTDATA->SetFamily(family);
358fc25c
RR
839}
840
841void wxFont::SetStyle(int style)
842{
843 Unshare();
844
409d5a58 845 M_FONTDATA->SetStyle(style);
358fc25c
RR
846}
847
848void wxFont::SetWeight(int weight)
849{
850 Unshare();
851
409d5a58 852 M_FONTDATA->SetWeight(weight);
358fc25c
RR
853}
854
855void wxFont::SetFaceName(const wxString& faceName)
856{
857 Unshare();
858
409d5a58 859 M_FONTDATA->SetFaceName(faceName);
358fc25c
RR
860}
861
862void wxFont::SetUnderlined(bool underlined)
863{
864 Unshare();
865
409d5a58 866 M_FONTDATA->SetUnderlined(underlined);
358fc25c
RR
867}
868
0c5d3e1c
VZ
869void wxFont::SetEncoding(wxFontEncoding encoding)
870{
871 Unshare();
c801d85f 872
409d5a58 873 M_FONTDATA->SetEncoding(encoding);
30764ab5
VZ
874}
875
9045ad9d 876void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
2b5f62a0
VZ
877{
878 Unshare();
879
880 M_FONTDATA->SetNativeFontInfo( info );
881}
882
883void wxFont::SetNoAntiAliasing( bool no )
30764ab5
VZ
884{
885 Unshare();
886
2b5f62a0 887 M_FONTDATA->SetNoAntiAliasing( no );
0c5d3e1c
VZ
888}
889
890// ----------------------------------------------------------------------------
891// get internal representation of font
892// ----------------------------------------------------------------------------
c801d85f 893
cfcc3932 894#ifndef __WXGTK20__
c7985368
RR
895static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
896
409d5a58
VZ
897// this is also used from tbargtk.cpp and tooltip.cpp, hence extern
898extern GdkFont *GtkGetDefaultGuiFont()
c7985368
RR
899{
900 if (!g_systemDefaultGuiFont)
901 {
902 GtkWidget *widget = gtk_button_new();
903 GtkStyle *def = gtk_rc_get_style( widget );
e6527f9d
RR
904 if (def)
905 {
464f1a1d 906 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d
RR
907 }
908 else
909 {
910 def = gtk_widget_get_default_style();
911 if (def)
464f1a1d 912 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d 913 }
c7985368
RR
914 gtk_widget_destroy( widget );
915 }
b1d1dc51
VZ
916 else
917 {
918 // already have it, but ref it once more before returning
919 gdk_font_ref(g_systemDefaultGuiFont);
920 }
921
c7985368
RR
922 return g_systemDefaultGuiFont;
923}
924
36b3b54a 925GdkFont *wxFont::GetInternalFont( float scale ) const
c801d85f 926{
409d5a58 927 GdkFont *font = (GdkFont *) NULL;
0c5d3e1c 928
409d5a58 929 wxCHECK_MSG( Ok(), font, wxT("invalid font") )
8bbe427f 930
db16cab4 931 long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
b02da6b1 932 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
8bbe427f 933
011ba5ed
VZ
934 wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts;
935 wxScaledFontList::iterator i = list.find(int_scale);
936 if ( i != list.end() )
8bbe427f 937 {
011ba5ed 938 font = i->second;
8bbe427f 939 }
409d5a58 940 else // we don't have this font in this size yet
8bbe427f 941 {
a756f210 942 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
8bbe427f 943 {
c7985368 944 font = GtkGetDefaultGuiFont();
8bbe427f 945 }
409d5a58
VZ
946
947 if ( !font )
8bbe427f 948 {
409d5a58
VZ
949 // do we have the XLFD?
950 if ( M_FONTDATA->HasNativeFont() )
951 {
952 font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
953 }
954
955 // no XLFD of no exact match - try the approximate one now
956 if ( !font )
957 {
958 wxString xfontname;
959 font = wxLoadQueryNearestFont( point_scale,
960 M_FONTDATA->m_family,
961 M_FONTDATA->m_style,
962 M_FONTDATA->m_weight,
963 M_FONTDATA->m_underlined,
964 M_FONTDATA->m_faceName,
965 M_FONTDATA->m_encoding,
966 &xfontname);
967 if ( font )
968 {
395470d9 969 M_FONTDATA->ReInit(xfontname);
409d5a58
VZ
970 }
971 }
8bbe427f 972 }
0c5d3e1c 973
409d5a58
VZ
974 if ( font )
975 {
011ba5ed 976 list[int_scale] = font;
409d5a58 977 }
8bbe427f 978 }
284b4c88 979
7beba2fc
VZ
980 // it's quite useless to make it a wxCHECK because we're going to crash
981 // anyhow...
982 wxASSERT_MSG( font, wxT("could not load any font?") );
284b4c88 983
8bbe427f 984 return font;
ff7b1510 985}
cfcc3932 986#endif // not GTK 2.0
c801d85f 987