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