]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/font.cpp
wxHTML not used
[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__
be5b2f9f
VZ
451void wxFontRefData::ReInit(const wxString& fontname)
452{
453 m_nativeFontInfo.SetXFontName(fontname);
454
455 InitFromNative();
456}
0aa1258a 457#endif
be5b2f9f 458
011ba5ed
VZ
459void wxFontRefData::ClearGdkFonts()
460{
2b5f62a0 461#ifndef __WXGTK20__
011ba5ed
VZ
462 for ( wxScaledFontList::iterator i = m_scaled_xfonts.begin();
463 i != m_scaled_xfonts.end();
464 ++i )
8bbe427f 465 {
011ba5ed 466 GdkFont *font = i->second;
8bbe427f 467 gdk_font_unref( font );
8bbe427f 468 }
011ba5ed
VZ
469
470 m_scaled_xfonts.clear();
011ba5ed 471#endif // GTK 1.x
2b5f62a0 472}
011ba5ed
VZ
473
474wxFontRefData::~wxFontRefData()
475{
476 ClearGdkFonts();
0c5d3e1c 477}
c801d85f 478
0c5d3e1c 479// ----------------------------------------------------------------------------
409d5a58 480// wxFontRefData SetXXX()
0c5d3e1c 481// ----------------------------------------------------------------------------
c801d85f 482
409d5a58 483void wxFontRefData::SetPointSize(int pointSize)
c801d85f 484{
409d5a58 485 m_pointSize = pointSize;
c801d85f 486
db16cab4
RR
487#ifdef __WXGTK20__
488 // Get native info
489 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 490
db16cab4
RR
491 pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE );
492#else
409d5a58
VZ
493 if ( HasNativeFont() )
494 {
495 wxString size;
496 if ( pointSize == -1 )
497 size = _T('*');
498 else
499 size.Printf(_T("%d"), 10*pointSize);
7826e2dd 500
409d5a58
VZ
501 m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
502 }
db16cab4 503#endif
7826e2dd
VZ
504}
505
409d5a58 506void wxFontRefData::SetFamily(int family)
7826e2dd 507{
409d5a58 508 m_family = family;
30764ab5 509
409d5a58 510 // TODO: what are we supposed to do with m_nativeFontInfo here?
30764ab5
VZ
511}
512
409d5a58 513void wxFontRefData::SetStyle(int style)
c801d85f 514{
409d5a58
VZ
515 m_style = style;
516
db16cab4
RR
517#ifdef __WXGTK20__
518 // Get native info
519 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 520
db16cab4
RR
521 switch ( style )
522 {
523 case wxFONTSTYLE_ITALIC:
524 pango_font_description_set_style( desc, PANGO_STYLE_ITALIC );
525 break;
526 case wxFONTSTYLE_SLANT:
527 pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE );
528 break;
529 default:
530 wxFAIL_MSG( _T("unknown font style") );
531 // fall through
532 case wxFONTSTYLE_NORMAL:
533 pango_font_description_set_style( desc, PANGO_STYLE_NORMAL );
534 break;
535 }
536#else
409d5a58 537 if ( HasNativeFont() )
30764ab5 538 {
409d5a58
VZ
539 wxString slant;
540 switch ( style )
541 {
542 case wxFONTSTYLE_ITALIC:
543 slant = _T('i');
544 break;
545
546 case wxFONTSTYLE_SLANT:
547 slant = _T('o');
548 break;
549
550 default:
551 wxFAIL_MSG( _T("unknown font style") );
552 // fall through
553
554 case wxFONTSTYLE_NORMAL:
555 slant = _T('r');
556 }
557
558 m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
30764ab5 559 }
db16cab4 560#endif
409d5a58 561}
7beba2fc 562
409d5a58
VZ
563void wxFontRefData::SetWeight(int weight)
564{
565 m_weight = weight;
8bbe427f 566
2b5f62a0
VZ
567#ifdef __WXGTK20__
568 PangoFontDescription *desc = m_nativeFontInfo.description;
569 switch ( weight )
570 {
571 case wxFONTWEIGHT_BOLD:
572 pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD);
573 break;
574
575 case wxFONTWEIGHT_LIGHT:
576 pango_font_description_set_weight(desc, PANGO_WEIGHT_LIGHT);
577 break;
578
579 default:
580 wxFAIL_MSG( _T("unknown font weight") );
581 // fall through
582
583 case wxFONTWEIGHT_NORMAL:
584 // unspecified
585 pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL);
586 }
587#else //!__WXGTK20__
409d5a58
VZ
588 if ( HasNativeFont() )
589 {
590 wxString boldness;
591 switch ( weight )
592 {
593 case wxFONTWEIGHT_BOLD:
594 boldness = _T("bold");
595 break;
30764ab5 596
409d5a58
VZ
597 case wxFONTWEIGHT_LIGHT:
598 boldness = _T("light");
599 break;
284b4c88 600
409d5a58
VZ
601 default:
602 wxFAIL_MSG( _T("unknown font weight") );
603 // fall through
284b4c88 604
409d5a58
VZ
605 case wxFONTWEIGHT_NORMAL:
606 // unspecified
607 boldness = _T("medium");
608 }
284b4c88 609
409d5a58
VZ
610 m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
611 }
db16cab4 612#endif
409d5a58 613}
30764ab5 614
409d5a58
VZ
615void wxFontRefData::SetUnderlined(bool underlined)
616{
617 m_underlined = underlined;
8636aed8 618
409d5a58
VZ
619 // the XLFD doesn't have "underlined" field anyhow
620}
30760ce7 621
409d5a58
VZ
622void wxFontRefData::SetFaceName(const wxString& facename)
623{
624 m_faceName = facename;
7beba2fc 625
db16cab4 626#ifndef __WXGTK20__
409d5a58
VZ
627 if ( HasNativeFont() )
628 {
629 m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
630 }
db16cab4 631#endif
409d5a58 632}
284b4c88 633
409d5a58
VZ
634void wxFontRefData::SetEncoding(wxFontEncoding encoding)
635{
636 m_encoding = encoding;
284b4c88 637
db16cab4 638#ifndef __WXGTK20__
409d5a58 639 if ( HasNativeFont() )
d06b34a7 640 {
409d5a58
VZ
641 wxNativeEncodingInfo info;
642 if ( wxGetNativeFontEncoding(encoding, &info) )
643 {
644 m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
645 m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
646 }
d06b34a7 647 }
db16cab4 648#endif
409d5a58 649}
284b4c88 650
011ba5ed
VZ
651void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
652{
653 // previously cached fonts shouldn't be used
654 ClearGdkFonts();
655
656 m_nativeFontInfo = info;
657
658 // set all the other font parameters from the native font info
659 InitFromNative();
660}
661
409d5a58
VZ
662// ----------------------------------------------------------------------------
663// wxFont creation
664// ----------------------------------------------------------------------------
36f210c8 665
409d5a58 666IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
36f210c8 667
409d5a58
VZ
668void wxFont::Init()
669{
670}
36f210c8 671
409d5a58
VZ
672wxFont::wxFont(const wxNativeFontInfo& info)
673{
674 Init();
675
db16cab4 676#ifdef __WXGTK20__
011ba5ed 677 Create( info.GetPointSize(),
db16cab4
RR
678 info.GetFamily(),
679 info.GetStyle(),
680 info.GetWeight(),
681 info.GetUnderlined(),
682 info.GetFaceName(),
683 info.GetEncoding() );
684#else
2b5f62a0 685 (void) Create(info.GetXFontName());
db16cab4 686#endif
409d5a58
VZ
687}
688
689bool wxFont::Create( int pointSize,
690 int family,
691 int style,
692 int weight,
693 bool underlined,
694 const wxString& face,
695 wxFontEncoding encoding)
696{
2b5f62a0
VZ
697 UnRef();
698
409d5a58
VZ
699 m_refData = new wxFontRefData(pointSize, family, style, weight,
700 underlined, face, encoding);
701
702 return TRUE;
703}
704
705bool wxFont::Create(const wxString& fontname)
706{
707 // VZ: does this really happen?
708 if ( fontname.empty() )
36f210c8 709 {
409d5a58 710 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7beba2fc 711
409d5a58 712 return TRUE;
36f210c8 713 }
409d5a58
VZ
714
715 m_refData = new wxFontRefData(fontname);
716
0c5d3e1c 717 return TRUE;
ff7b1510 718}
c801d85f 719
0c5d3e1c 720void wxFont::Unshare()
8bbe427f 721{
0c5d3e1c
VZ
722 if (!m_refData)
723 {
724 m_refData = new wxFontRefData();
725 }
726 else
727 {
728 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
729 UnRef();
730 m_refData = ref;
731 }
ff7b1510 732}
c801d85f 733
8bbe427f 734wxFont::~wxFont()
c801d85f 735{
ff7b1510 736}
c801d85f 737
0c5d3e1c
VZ
738// ----------------------------------------------------------------------------
739// accessors
740// ----------------------------------------------------------------------------
c801d85f 741
8bbe427f 742int wxFont::GetPointSize() const
c801d85f 743{
223d09f6 744 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
745
746 return M_FONTDATA->m_pointSize;
ff7b1510 747}
c801d85f 748
8bbe427f 749wxString wxFont::GetFaceName() const
c801d85f 750{
223d09f6 751 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
8bbe427f 752
36b3b54a 753 return M_FONTDATA->m_faceName;
ff7b1510 754}
c801d85f 755
8bbe427f 756int wxFont::GetFamily() const
c801d85f 757{
223d09f6 758 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
759
760 return M_FONTDATA->m_family;
ff7b1510 761}
c801d85f 762
8bbe427f 763int wxFont::GetStyle() const
c801d85f 764{
223d09f6 765 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
d84eb083 766
8bbe427f 767 return M_FONTDATA->m_style;
ff7b1510 768}
c801d85f 769
8bbe427f 770int wxFont::GetWeight() const
c801d85f 771{
223d09f6 772 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
773
774 return M_FONTDATA->m_weight;
775}
776
8bbe427f
VZ
777bool wxFont::GetUnderlined() const
778{
223d09f6 779 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
8bbe427f
VZ
780
781 return M_FONTDATA->m_underlined;
ff7b1510 782}
c801d85f 783
0c5d3e1c 784wxFontEncoding wxFont::GetEncoding() const
358fc25c 785{
223d09f6 786 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
0c5d3e1c
VZ
787
788 return M_FONTDATA->m_encoding;
358fc25c
RR
789}
790
2b5f62a0
VZ
791bool wxFont::GetNoAntiAliasing()
792{
793 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
794
795 return M_FONTDATA->m_noAA;
796}
797
3bf5a59b 798const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
30764ab5 799{
7826e2dd 800 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
30764ab5 801
cfcc3932 802#ifndef __WXGTK20__
409d5a58 803 if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
30764ab5 804 GetInternalFont();
db16cab4 805#endif
7826e2dd 806
3bf5a59b 807 return &(M_FONTDATA->m_nativeFontInfo);
30764ab5
VZ
808}
809
53f6aab7
VZ
810bool wxFont::IsFixedWidth() const
811{
812 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
813
db16cab4 814#ifndef __WXGTK20__
409d5a58 815 if ( M_FONTDATA->HasNativeFont() )
53f6aab7
VZ
816 {
817 // the monospace fonts are supposed to have "M" in the spacing field
818 wxString spacing = M_FONTDATA->
819 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
820
821 return spacing.Upper() == _T('M');
822 }
db16cab4 823#endif
53f6aab7
VZ
824
825 return wxFontBase::IsFixedWidth();
826}
30764ab5 827
0c5d3e1c
VZ
828// ----------------------------------------------------------------------------
829// change font attributes
830// ----------------------------------------------------------------------------
831
358fc25c
RR
832void wxFont::SetPointSize(int pointSize)
833{
834 Unshare();
011ba5ed 835
409d5a58 836 M_FONTDATA->SetPointSize(pointSize);
358fc25c
RR
837}
838
839void wxFont::SetFamily(int family)
840{
841 Unshare();
842
409d5a58 843 M_FONTDATA->SetFamily(family);
358fc25c
RR
844}
845
846void wxFont::SetStyle(int style)
847{
848 Unshare();
849
409d5a58 850 M_FONTDATA->SetStyle(style);
358fc25c
RR
851}
852
853void wxFont::SetWeight(int weight)
854{
855 Unshare();
856
409d5a58 857 M_FONTDATA->SetWeight(weight);
358fc25c
RR
858}
859
860void wxFont::SetFaceName(const wxString& faceName)
861{
862 Unshare();
863
409d5a58 864 M_FONTDATA->SetFaceName(faceName);
358fc25c
RR
865}
866
867void wxFont::SetUnderlined(bool underlined)
868{
869 Unshare();
870
409d5a58 871 M_FONTDATA->SetUnderlined(underlined);
358fc25c
RR
872}
873
0c5d3e1c
VZ
874void wxFont::SetEncoding(wxFontEncoding encoding)
875{
876 Unshare();
c801d85f 877
409d5a58 878 M_FONTDATA->SetEncoding(encoding);
30764ab5
VZ
879}
880
9045ad9d 881void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
2b5f62a0
VZ
882{
883 Unshare();
884
885 M_FONTDATA->SetNativeFontInfo( info );
886}
887
888void wxFont::SetNoAntiAliasing( bool no )
30764ab5
VZ
889{
890 Unshare();
891
2b5f62a0 892 M_FONTDATA->SetNoAntiAliasing( no );
0c5d3e1c
VZ
893}
894
895// ----------------------------------------------------------------------------
896// get internal representation of font
897// ----------------------------------------------------------------------------
c801d85f 898
cfcc3932 899#ifndef __WXGTK20__
c7985368
RR
900static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
901
409d5a58
VZ
902// this is also used from tbargtk.cpp and tooltip.cpp, hence extern
903extern GdkFont *GtkGetDefaultGuiFont()
c7985368
RR
904{
905 if (!g_systemDefaultGuiFont)
906 {
907 GtkWidget *widget = gtk_button_new();
908 GtkStyle *def = gtk_rc_get_style( widget );
e6527f9d
RR
909 if (def)
910 {
464f1a1d 911 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d
RR
912 }
913 else
914 {
915 def = gtk_widget_get_default_style();
916 if (def)
464f1a1d 917 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d 918 }
c7985368
RR
919 gtk_widget_destroy( widget );
920 }
b1d1dc51
VZ
921 else
922 {
923 // already have it, but ref it once more before returning
924 gdk_font_ref(g_systemDefaultGuiFont);
925 }
926
c7985368
RR
927 return g_systemDefaultGuiFont;
928}
929
36b3b54a 930GdkFont *wxFont::GetInternalFont( float scale ) const
c801d85f 931{
409d5a58 932 GdkFont *font = (GdkFont *) NULL;
0c5d3e1c 933
409d5a58 934 wxCHECK_MSG( Ok(), font, wxT("invalid font") )
8bbe427f 935
db16cab4 936 long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
b02da6b1 937 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
8bbe427f 938
011ba5ed
VZ
939 wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts;
940 wxScaledFontList::iterator i = list.find(int_scale);
941 if ( i != list.end() )
8bbe427f 942 {
011ba5ed 943 font = i->second;
8bbe427f 944 }
409d5a58 945 else // we don't have this font in this size yet
8bbe427f 946 {
a756f210 947 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
8bbe427f 948 {
c7985368 949 font = GtkGetDefaultGuiFont();
8bbe427f 950 }
409d5a58
VZ
951
952 if ( !font )
8bbe427f 953 {
409d5a58
VZ
954 // do we have the XLFD?
955 if ( M_FONTDATA->HasNativeFont() )
956 {
957 font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
958 }
959
960 // no XLFD of no exact match - try the approximate one now
961 if ( !font )
962 {
963 wxString xfontname;
964 font = wxLoadQueryNearestFont( point_scale,
965 M_FONTDATA->m_family,
966 M_FONTDATA->m_style,
967 M_FONTDATA->m_weight,
968 M_FONTDATA->m_underlined,
969 M_FONTDATA->m_faceName,
970 M_FONTDATA->m_encoding,
971 &xfontname);
972 if ( font )
973 {
395470d9 974 M_FONTDATA->ReInit(xfontname);
409d5a58
VZ
975 }
976 }
8bbe427f 977 }
0c5d3e1c 978
409d5a58
VZ
979 if ( font )
980 {
011ba5ed 981 list[int_scale] = font;
409d5a58 982 }
8bbe427f 983 }
284b4c88 984
7beba2fc
VZ
985 // it's quite useless to make it a wxCHECK because we're going to crash
986 // anyhow...
987 wxASSERT_MSG( font, wxT("could not load any font?") );
284b4c88 988
8bbe427f 989 return font;
ff7b1510 990}
cfcc3932 991#endif // not GTK 2.0
c801d85f 992