]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/font.cpp
Need to use the same instance handles
[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
b6b579bd
RR
244 // Pango sometimes needs to have a size
245 int pango_size = pango_font_description_get_size( desc );
246 if (pango_size == 0)
247 pango_font_description_set_size( desc, 12 * PANGO_SCALE);
248
db16cab4 249 m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE;
011ba5ed 250
db16cab4
RR
251 switch (pango_font_description_get_style( desc ))
252 {
253 case PANGO_STYLE_NORMAL:
254 m_style = wxFONTSTYLE_NORMAL;
255 break;
256 case PANGO_STYLE_ITALIC:
257 m_style = wxFONTSTYLE_ITALIC;
258 break;
259 case PANGO_STYLE_OBLIQUE:
260 m_style = wxFONTSTYLE_SLANT;
261 break;
262 }
263
264 switch (pango_font_description_get_weight( desc ))
265 {
266 case PANGO_WEIGHT_ULTRALIGHT:
267 m_weight = wxFONTWEIGHT_LIGHT;
268 break;
269 case PANGO_WEIGHT_LIGHT:
270 m_weight = wxFONTWEIGHT_LIGHT;
271 break;
272 case PANGO_WEIGHT_NORMAL:
273 m_weight = wxFONTWEIGHT_NORMAL;
274 break;
275 case PANGO_WEIGHT_BOLD:
276 m_weight = wxFONTWEIGHT_BOLD;
277 break;
278 case PANGO_WEIGHT_ULTRABOLD:
279 m_weight = wxFONTWEIGHT_BOLD;
280 break;
281 case PANGO_WEIGHT_HEAVY:
282 m_weight = wxFONTWEIGHT_BOLD;
283 break;
284 }
011ba5ed 285
a732ef91 286 if (m_faceName == wxT("monospace"))
db16cab4
RR
287 {
288 m_family = wxFONTFAMILY_TELETYPE;
289 }
290 else if (m_faceName == wxT("sans"))
291 {
292 m_family = wxFONTFAMILY_SWISS;
293 }
2b5f62a0
VZ
294 else if (m_faceName == wxT("serif"))
295 {
296 m_family = wxFONTFAMILY_ROMAN;
297 }
db16cab4
RR
298 else
299 {
300 m_family = wxFONTFAMILY_UNKNOWN;
301 }
302
303 // Pango description are never underlined (?)
304 m_underlined = FALSE;
305
306 // Cannot we choose that
307 m_encoding = wxFONTENCODING_SYSTEM;
011ba5ed 308#else // GTK 1.x
409d5a58
VZ
309 // get the font parameters from the XLFD
310 // -------------------------------------
311
312 m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY);
313
314 m_weight = wxFONTWEIGHT_NORMAL;
315
316 wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper();
317 if ( !w.empty() && w != _T('*') )
318 {
319 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
320 // and BLACK
fab591c5
RR
321 if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
322 !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
323 wxStrstr(w.c_str() + 1, _T("BOLD")) )
409d5a58
VZ
324 {
325 m_weight = wxFONTWEIGHT_BOLD;
326 }
327 else if ( w == _T("LIGHT") || w == _T("THIN") )
328 {
329 m_weight = wxFONTWEIGHT_LIGHT;
330 }
331 }
332
333 switch ( wxToupper(*m_nativeFontInfo.
334 GetXFontComponent(wxXLFD_SLANT).c_str()) )
335 {
336 case _T('I'): // italique
337 m_style = wxFONTSTYLE_ITALIC;
338 break;
339
340 case _T('O'): // oblique
341 m_style = wxFONTSTYLE_SLANT;
342 break;
343
344 default:
345 m_style = wxFONTSTYLE_NORMAL;
346 }
347
348 long ptSize;
349 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
350 {
351 // size in XLFD is in 10 point units
352 m_pointSize = (int)(ptSize / 10);
353 }
354 else
355 {
356 m_pointSize = wxDEFAULT_FONT_SIZE;
357 }
358
359 // examine the spacing: if the font is monospaced, assume wxTELETYPE
360 // family for compatibility with the old code which used it instead of
361 // IsFixedWidth()
362 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') )
363 {
364 m_family = wxFONTFAMILY_TELETYPE;
365 }
366 else // not monospaceed
367 {
368 // don't even try guessing it, it doesn't work for too many fonts
369 // anyhow
370 m_family = wxFONTFAMILY_UNKNOWN;
371 }
372
373 // X fonts are never underlined...
374 m_underlined = FALSE;
375
376 // deal with font encoding
377 wxString
378 registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(),
379 encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper();
380
381 if ( registry == _T("ISO8859") )
382 {
383 int cp;
384 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
385 {
386 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
387 }
388 }
389 else if ( registry == _T("MICROSOFT") )
390 {
391 int cp;
392 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
393 {
394 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
395 }
396 }
397 else if ( registry == _T("KOI8") )
398 {
399 m_encoding = wxFONTENCODING_KOI8;
400 }
401 else // unknown encoding
402 {
011ba5ed 403 // may be give a warning here? or use wxFontMapper?
409d5a58
VZ
404 m_encoding = wxFONTENCODING_SYSTEM;
405 }
011ba5ed 406#endif // GTK 2.0/1.x
409d5a58
VZ
407}
408
011ba5ed
VZ
409wxFontRefData::wxFontRefData( const wxFontRefData& data )
410 : wxObjectRefData()
411{
412 m_pointSize = data.m_pointSize;
413 m_family = data.m_family;
414 m_style = data.m_style;
415 m_weight = data.m_weight;
416
417 m_underlined = data.m_underlined;
418
419 m_faceName = data.m_faceName;
420 m_encoding = data.m_encoding;
421
2b5f62a0 422 m_noAA = data.m_noAA;
cd9a673c
RD
423
424 // Forces a copy of the internal data. wxNativeFontInfo should probably
425 // have a copy ctor and assignment operator to fix this properly but that
426 // would break binary compatibility...
427 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
011ba5ed
VZ
428}
429
430wxFontRefData::wxFontRefData(int size, int family, int style,
431 int weight, bool underlined,
432 const wxString& faceName,
433 wxFontEncoding encoding)
434{
435 Init(size, family, style, weight, underlined, faceName, encoding);
436}
437
438wxFontRefData::wxFontRefData(const wxString& fontname)
8bbe427f 439{
011ba5ed
VZ
440 // VZ: FromString() should really work in both cases, doesn't it?
441#ifdef __WXGTK20__
442 m_nativeFontInfo.FromString( fontname );
443#else // GTK 1.x
444 m_nativeFontInfo.SetXFontName(fontname);
445#endif // GTK 2.0/1.x
446
447 InitFromNative();
448}
449
0aa1258a 450#ifndef __WXGTK20__
a0af0d98 451
be5b2f9f
VZ
452void wxFontRefData::ReInit(const wxString& fontname)
453{
a0af0d98
VZ
454 // calling InitFromNative() resets m_underlined flag as X11 fonts are never
455 // underlined, but we don't want to lose its old value here so save it ...
456 bool underlined = m_underlined;
457
be5b2f9f
VZ
458 m_nativeFontInfo.SetXFontName(fontname);
459
460 InitFromNative();
a0af0d98
VZ
461
462 // ... and restore it now
463 m_underlined = underlined;
be5b2f9f 464}
a0af0d98
VZ
465
466#endif // !__WXGTK20__
be5b2f9f 467
011ba5ed
VZ
468void wxFontRefData::ClearGdkFonts()
469{
2b5f62a0 470#ifndef __WXGTK20__
011ba5ed
VZ
471 for ( wxScaledFontList::iterator i = m_scaled_xfonts.begin();
472 i != m_scaled_xfonts.end();
473 ++i )
8bbe427f 474 {
011ba5ed 475 GdkFont *font = i->second;
8bbe427f 476 gdk_font_unref( font );
8bbe427f 477 }
011ba5ed
VZ
478
479 m_scaled_xfonts.clear();
011ba5ed 480#endif // GTK 1.x
2b5f62a0 481}
011ba5ed
VZ
482
483wxFontRefData::~wxFontRefData()
484{
485 ClearGdkFonts();
0c5d3e1c 486}
c801d85f 487
0c5d3e1c 488// ----------------------------------------------------------------------------
409d5a58 489// wxFontRefData SetXXX()
0c5d3e1c 490// ----------------------------------------------------------------------------
c801d85f 491
409d5a58 492void wxFontRefData::SetPointSize(int pointSize)
c801d85f 493{
409d5a58 494 m_pointSize = pointSize;
c801d85f 495
db16cab4
RR
496#ifdef __WXGTK20__
497 // Get native info
498 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 499
db16cab4
RR
500 pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE );
501#else
409d5a58
VZ
502 if ( HasNativeFont() )
503 {
504 wxString size;
505 if ( pointSize == -1 )
506 size = _T('*');
507 else
508 size.Printf(_T("%d"), 10*pointSize);
7826e2dd 509
409d5a58
VZ
510 m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
511 }
db16cab4 512#endif
7826e2dd
VZ
513}
514
409d5a58 515void wxFontRefData::SetFamily(int family)
7826e2dd 516{
409d5a58 517 m_family = family;
30764ab5 518
409d5a58 519 // TODO: what are we supposed to do with m_nativeFontInfo here?
30764ab5
VZ
520}
521
409d5a58 522void wxFontRefData::SetStyle(int style)
c801d85f 523{
409d5a58
VZ
524 m_style = style;
525
db16cab4
RR
526#ifdef __WXGTK20__
527 // Get native info
528 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 529
db16cab4
RR
530 switch ( style )
531 {
532 case wxFONTSTYLE_ITALIC:
533 pango_font_description_set_style( desc, PANGO_STYLE_ITALIC );
534 break;
535 case wxFONTSTYLE_SLANT:
536 pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE );
537 break;
538 default:
539 wxFAIL_MSG( _T("unknown font style") );
540 // fall through
541 case wxFONTSTYLE_NORMAL:
542 pango_font_description_set_style( desc, PANGO_STYLE_NORMAL );
543 break;
544 }
545#else
409d5a58 546 if ( HasNativeFont() )
30764ab5 547 {
409d5a58
VZ
548 wxString slant;
549 switch ( style )
550 {
551 case wxFONTSTYLE_ITALIC:
552 slant = _T('i');
553 break;
554
555 case wxFONTSTYLE_SLANT:
556 slant = _T('o');
557 break;
558
559 default:
560 wxFAIL_MSG( _T("unknown font style") );
561 // fall through
562
563 case wxFONTSTYLE_NORMAL:
564 slant = _T('r');
565 }
566
567 m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
30764ab5 568 }
db16cab4 569#endif
409d5a58 570}
7beba2fc 571
409d5a58
VZ
572void wxFontRefData::SetWeight(int weight)
573{
574 m_weight = weight;
8bbe427f 575
2b5f62a0
VZ
576#ifdef __WXGTK20__
577 PangoFontDescription *desc = m_nativeFontInfo.description;
578 switch ( weight )
579 {
580 case wxFONTWEIGHT_BOLD:
581 pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD);
582 break;
583
584 case wxFONTWEIGHT_LIGHT:
585 pango_font_description_set_weight(desc, PANGO_WEIGHT_LIGHT);
586 break;
587
588 default:
589 wxFAIL_MSG( _T("unknown font weight") );
590 // fall through
591
592 case wxFONTWEIGHT_NORMAL:
593 // unspecified
594 pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL);
595 }
596#else //!__WXGTK20__
409d5a58
VZ
597 if ( HasNativeFont() )
598 {
599 wxString boldness;
600 switch ( weight )
601 {
602 case wxFONTWEIGHT_BOLD:
603 boldness = _T("bold");
604 break;
30764ab5 605
409d5a58
VZ
606 case wxFONTWEIGHT_LIGHT:
607 boldness = _T("light");
608 break;
284b4c88 609
409d5a58
VZ
610 default:
611 wxFAIL_MSG( _T("unknown font weight") );
612 // fall through
284b4c88 613
409d5a58
VZ
614 case wxFONTWEIGHT_NORMAL:
615 // unspecified
616 boldness = _T("medium");
617 }
284b4c88 618
409d5a58
VZ
619 m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
620 }
db16cab4 621#endif
409d5a58 622}
30764ab5 623
409d5a58
VZ
624void wxFontRefData::SetUnderlined(bool underlined)
625{
626 m_underlined = underlined;
8636aed8 627
409d5a58
VZ
628 // the XLFD doesn't have "underlined" field anyhow
629}
30760ce7 630
409d5a58
VZ
631void wxFontRefData::SetFaceName(const wxString& facename)
632{
633 m_faceName = facename;
7beba2fc 634
db16cab4 635#ifndef __WXGTK20__
409d5a58
VZ
636 if ( HasNativeFont() )
637 {
638 m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
639 }
db16cab4 640#endif
409d5a58 641}
284b4c88 642
409d5a58
VZ
643void wxFontRefData::SetEncoding(wxFontEncoding encoding)
644{
645 m_encoding = encoding;
284b4c88 646
db16cab4 647#ifndef __WXGTK20__
409d5a58 648 if ( HasNativeFont() )
d06b34a7 649 {
409d5a58
VZ
650 wxNativeEncodingInfo info;
651 if ( wxGetNativeFontEncoding(encoding, &info) )
652 {
653 m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
654 m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
655 }
d06b34a7 656 }
db16cab4 657#endif
409d5a58 658}
284b4c88 659
011ba5ed
VZ
660void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
661{
662 // previously cached fonts shouldn't be used
663 ClearGdkFonts();
664
665 m_nativeFontInfo = info;
666
667 // set all the other font parameters from the native font info
668 InitFromNative();
669}
670
409d5a58
VZ
671// ----------------------------------------------------------------------------
672// wxFont creation
673// ----------------------------------------------------------------------------
36f210c8 674
409d5a58 675IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
36f210c8 676
409d5a58
VZ
677void wxFont::Init()
678{
679}
36f210c8 680
409d5a58
VZ
681wxFont::wxFont(const wxNativeFontInfo& info)
682{
683 Init();
684
db16cab4 685#ifdef __WXGTK20__
011ba5ed 686 Create( info.GetPointSize(),
db16cab4
RR
687 info.GetFamily(),
688 info.GetStyle(),
689 info.GetWeight(),
690 info.GetUnderlined(),
691 info.GetFaceName(),
692 info.GetEncoding() );
693#else
2b5f62a0 694 (void) Create(info.GetXFontName());
db16cab4 695#endif
409d5a58
VZ
696}
697
698bool wxFont::Create( int pointSize,
699 int family,
700 int style,
701 int weight,
702 bool underlined,
703 const wxString& face,
704 wxFontEncoding encoding)
705{
2b5f62a0
VZ
706 UnRef();
707
409d5a58
VZ
708 m_refData = new wxFontRefData(pointSize, family, style, weight,
709 underlined, face, encoding);
710
711 return TRUE;
712}
713
714bool wxFont::Create(const wxString& fontname)
715{
716 // VZ: does this really happen?
717 if ( fontname.empty() )
36f210c8 718 {
409d5a58 719 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7beba2fc 720
409d5a58 721 return TRUE;
36f210c8 722 }
409d5a58
VZ
723
724 m_refData = new wxFontRefData(fontname);
725
0c5d3e1c 726 return TRUE;
ff7b1510 727}
c801d85f 728
0c5d3e1c 729void wxFont::Unshare()
8bbe427f 730{
0c5d3e1c
VZ
731 if (!m_refData)
732 {
733 m_refData = new wxFontRefData();
734 }
735 else
736 {
737 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
738 UnRef();
739 m_refData = ref;
740 }
ff7b1510 741}
c801d85f 742
8bbe427f 743wxFont::~wxFont()
c801d85f 744{
ff7b1510 745}
c801d85f 746
0c5d3e1c
VZ
747// ----------------------------------------------------------------------------
748// accessors
749// ----------------------------------------------------------------------------
c801d85f 750
8bbe427f 751int wxFont::GetPointSize() const
c801d85f 752{
223d09f6 753 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
754
755 return M_FONTDATA->m_pointSize;
ff7b1510 756}
c801d85f 757
8bbe427f 758wxString wxFont::GetFaceName() const
c801d85f 759{
223d09f6 760 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
8bbe427f 761
36b3b54a 762 return M_FONTDATA->m_faceName;
ff7b1510 763}
c801d85f 764
8bbe427f 765int wxFont::GetFamily() const
c801d85f 766{
223d09f6 767 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
768
769 return M_FONTDATA->m_family;
ff7b1510 770}
c801d85f 771
8bbe427f 772int wxFont::GetStyle() const
c801d85f 773{
223d09f6 774 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
d84eb083 775
8bbe427f 776 return M_FONTDATA->m_style;
ff7b1510 777}
c801d85f 778
8bbe427f 779int wxFont::GetWeight() const
c801d85f 780{
223d09f6 781 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
782
783 return M_FONTDATA->m_weight;
784}
785
8bbe427f
VZ
786bool wxFont::GetUnderlined() const
787{
223d09f6 788 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
8bbe427f
VZ
789
790 return M_FONTDATA->m_underlined;
ff7b1510 791}
c801d85f 792
0c5d3e1c 793wxFontEncoding wxFont::GetEncoding() const
358fc25c 794{
223d09f6 795 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
0c5d3e1c
VZ
796
797 return M_FONTDATA->m_encoding;
358fc25c
RR
798}
799
2b5f62a0
VZ
800bool wxFont::GetNoAntiAliasing()
801{
802 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
803
804 return M_FONTDATA->m_noAA;
805}
806
3bf5a59b 807const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
30764ab5 808{
7826e2dd 809 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
30764ab5 810
cfcc3932 811#ifndef __WXGTK20__
409d5a58 812 if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
30764ab5 813 GetInternalFont();
db16cab4 814#endif
7826e2dd 815
3bf5a59b 816 return &(M_FONTDATA->m_nativeFontInfo);
30764ab5
VZ
817}
818
53f6aab7
VZ
819bool wxFont::IsFixedWidth() const
820{
821 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
822
db16cab4 823#ifndef __WXGTK20__
409d5a58 824 if ( M_FONTDATA->HasNativeFont() )
53f6aab7
VZ
825 {
826 // the monospace fonts are supposed to have "M" in the spacing field
827 wxString spacing = M_FONTDATA->
828 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
829
830 return spacing.Upper() == _T('M');
831 }
db16cab4 832#endif
53f6aab7
VZ
833
834 return wxFontBase::IsFixedWidth();
835}
30764ab5 836
0c5d3e1c
VZ
837// ----------------------------------------------------------------------------
838// change font attributes
839// ----------------------------------------------------------------------------
840
358fc25c
RR
841void wxFont::SetPointSize(int pointSize)
842{
843 Unshare();
011ba5ed 844
409d5a58 845 M_FONTDATA->SetPointSize(pointSize);
358fc25c
RR
846}
847
848void wxFont::SetFamily(int family)
849{
850 Unshare();
851
409d5a58 852 M_FONTDATA->SetFamily(family);
358fc25c
RR
853}
854
855void wxFont::SetStyle(int style)
856{
857 Unshare();
858
409d5a58 859 M_FONTDATA->SetStyle(style);
358fc25c
RR
860}
861
862void wxFont::SetWeight(int weight)
863{
864 Unshare();
865
409d5a58 866 M_FONTDATA->SetWeight(weight);
358fc25c
RR
867}
868
869void wxFont::SetFaceName(const wxString& faceName)
870{
871 Unshare();
872
409d5a58 873 M_FONTDATA->SetFaceName(faceName);
358fc25c
RR
874}
875
876void wxFont::SetUnderlined(bool underlined)
877{
878 Unshare();
879
409d5a58 880 M_FONTDATA->SetUnderlined(underlined);
358fc25c
RR
881}
882
0c5d3e1c
VZ
883void wxFont::SetEncoding(wxFontEncoding encoding)
884{
885 Unshare();
c801d85f 886
409d5a58 887 M_FONTDATA->SetEncoding(encoding);
30764ab5
VZ
888}
889
9045ad9d 890void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
2b5f62a0
VZ
891{
892 Unshare();
893
894 M_FONTDATA->SetNativeFontInfo( info );
895}
896
897void wxFont::SetNoAntiAliasing( bool no )
30764ab5
VZ
898{
899 Unshare();
900
2b5f62a0 901 M_FONTDATA->SetNoAntiAliasing( no );
0c5d3e1c
VZ
902}
903
904// ----------------------------------------------------------------------------
905// get internal representation of font
906// ----------------------------------------------------------------------------
c801d85f 907
cfcc3932 908#ifndef __WXGTK20__
c7985368
RR
909static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
910
409d5a58
VZ
911// this is also used from tbargtk.cpp and tooltip.cpp, hence extern
912extern GdkFont *GtkGetDefaultGuiFont()
c7985368
RR
913{
914 if (!g_systemDefaultGuiFont)
915 {
916 GtkWidget *widget = gtk_button_new();
917 GtkStyle *def = gtk_rc_get_style( widget );
e6527f9d
RR
918 if (def)
919 {
464f1a1d 920 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d
RR
921 }
922 else
923 {
924 def = gtk_widget_get_default_style();
925 if (def)
464f1a1d 926 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d 927 }
c7985368
RR
928 gtk_widget_destroy( widget );
929 }
b1d1dc51
VZ
930 else
931 {
932 // already have it, but ref it once more before returning
933 gdk_font_ref(g_systemDefaultGuiFont);
934 }
935
c7985368
RR
936 return g_systemDefaultGuiFont;
937}
938
36b3b54a 939GdkFont *wxFont::GetInternalFont( float scale ) const
c801d85f 940{
409d5a58 941 GdkFont *font = (GdkFont *) NULL;
0c5d3e1c 942
409d5a58 943 wxCHECK_MSG( Ok(), font, wxT("invalid font") )
8bbe427f 944
db16cab4 945 long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
b02da6b1 946 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
8bbe427f 947
011ba5ed
VZ
948 wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts;
949 wxScaledFontList::iterator i = list.find(int_scale);
950 if ( i != list.end() )
8bbe427f 951 {
011ba5ed 952 font = i->second;
8bbe427f 953 }
409d5a58 954 else // we don't have this font in this size yet
8bbe427f 955 {
a756f210 956 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
8bbe427f 957 {
c7985368 958 font = GtkGetDefaultGuiFont();
8bbe427f 959 }
409d5a58
VZ
960
961 if ( !font )
8bbe427f 962 {
409d5a58
VZ
963 // do we have the XLFD?
964 if ( M_FONTDATA->HasNativeFont() )
965 {
966 font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
967 }
968
969 // no XLFD of no exact match - try the approximate one now
970 if ( !font )
971 {
972 wxString xfontname;
973 font = wxLoadQueryNearestFont( point_scale,
974 M_FONTDATA->m_family,
975 M_FONTDATA->m_style,
976 M_FONTDATA->m_weight,
977 M_FONTDATA->m_underlined,
978 M_FONTDATA->m_faceName,
979 M_FONTDATA->m_encoding,
980 &xfontname);
981 if ( font )
982 {
395470d9 983 M_FONTDATA->ReInit(xfontname);
409d5a58
VZ
984 }
985 }
8bbe427f 986 }
0c5d3e1c 987
409d5a58
VZ
988 if ( font )
989 {
011ba5ed 990 list[int_scale] = font;
409d5a58 991 }
8bbe427f 992 }
284b4c88 993
7beba2fc
VZ
994 // it's quite useless to make it a wxCHECK because we're going to crash
995 // anyhow...
996 wxASSERT_MSG( font, wxT("could not load any font?") );
284b4c88 997
8bbe427f 998 return font;
ff7b1510 999}
cfcc3932 1000#endif // not GTK 2.0
c801d85f 1001