]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/font.cpp
Mark a couple of labels for translation.
[wxWidgets.git] / src / gtk1 / font.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
dd05139a 2// Name: src/gtk1/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
VS
18// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
c801d85f 21#include "wx/font.h"
e4db172a
WS
22
23#ifndef WX_PRECOMP
24 #include "wx/log.h"
9eddec69 25 #include "wx/settings.h"
ce5d92e1 26 #include "wx/cmndata.h"
dd05139a 27 #include "wx/gdicmn.h"
e4db172a
WS
28#endif
29
7beba2fc 30#include "wx/fontutil.h"
c801d85f 31#include "wx/utils.h"
8636aed8 32#include "wx/tokenzr.h"
0c5d3e1c 33
c801d85f
KB
34#include <strings.h>
35
3cbab641 36#include "wx/gtk1/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
8f884a0d 57class wxFontRefData : public wxGDIRefData
c801d85f 58{
8bbe427f 59public:
409d5a58
VZ
60 // from broken down font parameters, also default ctor
61 wxFontRefData(int size = -1,
0c14b6c3
FM
62 wxFontFamily family = wxFONTFAMILY_DEFAULT,
63 wxFontStyle style = wxFONTSTYLE_NORMAL,
64 wxFontWeight weight = wxFONTWEIGHT_NORMAL,
9eddec69 65 bool underlined = false,
0c5d3e1c 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 {
011ba5ed 80 // only use m_nativeFontInfo if it had been initialized
409d5a58
VZ
81 return !m_nativeFontInfo.IsDefault();
82 }
83
84 // setters: all of them also take care to modify m_nativeFontInfo if we
85 // have it so as to not lose the information not carried by our fields
86 void SetPointSize(int pointSize);
0c14b6c3
FM
87 void SetFamily(wxFontFamily family);
88 void SetStyle(wxFontStyle style);
89 void SetWeight(wxFontWeight weight);
409d5a58 90 void SetUnderlined(bool underlined);
85ab460e 91 bool SetFaceName(const wxString& facename);
409d5a58
VZ
92 void SetEncoding(wxFontEncoding encoding);
93
9eddec69 94 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
5ac2e80c 95 bool GetNoAntiAliasing() const { return m_noAA; }
cd9a673c 96
011ba5ed
VZ
97 // and this one also modifies all the other font data fields
98 void SetNativeFontInfo(const wxNativeFontInfo& info);
99
0c5d3e1c
VZ
100protected:
101 // common part of all ctors
102 void Init(int pointSize,
0c14b6c3
FM
103 wxFontFamily family,
104 wxFontStyle style,
105 wxFontWeight weight,
0c5d3e1c
VZ
106 bool underlined,
107 const wxString& faceName,
7826e2dd 108 wxFontEncoding encoding);
0c5d3e1c 109
011ba5ed
VZ
110 // set all fields from (already initialized and valid) m_nativeFontInfo
111 void InitFromNative();
112
0c5d3e1c 113private:
2b5f62a0 114 // clear m_scaled_xfonts if any
011ba5ed
VZ
115 void ClearGdkFonts();
116
409d5a58
VZ
117 // the map of font sizes to "GdkFont *"
118 wxScaledFontList m_scaled_xfonts;
119
f35c2659 120 int m_pointSize;
0c14b6c3
FM
121 wxFontFamily m_family;
122 wxFontStyle m_style;
123 wxFontWeight m_weight;
f35c2659
RR
124 bool m_underlined;
125 wxString m_faceName;
db16cab4 126 wxFontEncoding m_encoding; // Unused under GTK 2.0
2b5f62a0 127 bool m_noAA; // No anti-aliasing
7826e2dd 128
db16cab4
RR
129 // The native font info, basicly an XFLD under GTK 1.2 and
130 // the pango font description under GTK 2.0.
30764ab5 131 wxNativeFontInfo m_nativeFontInfo;
8bbe427f 132
f6bcfd97 133 friend class wxFont;
c801d85f
KB
134};
135
68c95704 136#define M_FONTDATA ((wxFontRefData*)m_refData)
873fd4af 137
0c5d3e1c 138// ----------------------------------------------------------------------------
cd9a673c 139// wxFontRefData
0c5d3e1c
VZ
140// ----------------------------------------------------------------------------
141
142void wxFontRefData::Init(int pointSize,
0c14b6c3
FM
143 wxFontFamily family,
144 wxFontStyle style,
145 wxFontWeight weight,
0c5d3e1c
VZ
146 bool underlined,
147 const wxString& faceName,
7826e2dd 148 wxFontEncoding encoding)
8bbe427f 149{
409d5a58 150 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
0c5d3e1c
VZ
151
152 m_faceName = faceName;
153
409d5a58
VZ
154 // we accept both wxDEFAULT and wxNORMAL here - should we?
155 m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
156 m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
0c5d3e1c 157
409d5a58
VZ
158 // and here, do we really want to forbid creation of the font of the size
159 // 90 (the value of wxDEFAULT)??
011ba5ed
VZ
160 m_pointSize = pointSize == wxDEFAULT || pointSize == -1
161 ? wxDEFAULT_FONT_SIZE
162 : pointSize;
0c5d3e1c
VZ
163
164 m_underlined = underlined;
165 m_encoding = encoding;
cd9a673c 166
9eddec69 167 m_noAA = false;
358fc25c
RR
168}
169
011ba5ed 170void wxFontRefData::InitFromNative()
409d5a58 171{
9eddec69 172 m_noAA = false;
2b5f62a0 173
409d5a58
VZ
174 // get the font parameters from the XLFD
175 // -------------------------------------
176
177 m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY);
178
179 m_weight = wxFONTWEIGHT_NORMAL;
180
181 wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper();
9a83f860 182 if ( !w.empty() && w != wxT('*') )
409d5a58
VZ
183 {
184 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
185 // and BLACK
9a83f860 186 if ( ((w[0u] == wxT('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
fab591c5 187 !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
9a83f860 188 wxStrstr(w.c_str() + 1, wxT("BOLD")) )
409d5a58
VZ
189 {
190 m_weight = wxFONTWEIGHT_BOLD;
191 }
9a83f860 192 else if ( w == wxT("LIGHT") || w == wxT("THIN") )
409d5a58
VZ
193 {
194 m_weight = wxFONTWEIGHT_LIGHT;
195 }
196 }
197
63415a83
VS
198 switch ( wxToupper(m_nativeFontInfo.
199 GetXFontComponent(wxXLFD_SLANT)[0u]).GetValue() )
409d5a58 200 {
9a83f860 201 case wxT('I'): // italique
409d5a58
VZ
202 m_style = wxFONTSTYLE_ITALIC;
203 break;
204
9a83f860 205 case wxT('O'): // oblique
409d5a58
VZ
206 m_style = wxFONTSTYLE_SLANT;
207 break;
208
209 default:
210 m_style = wxFONTSTYLE_NORMAL;
211 }
212
213 long ptSize;
214 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
215 {
216 // size in XLFD is in 10 point units
217 m_pointSize = (int)(ptSize / 10);
218 }
219 else
220 {
221 m_pointSize = wxDEFAULT_FONT_SIZE;
222 }
223
224 // examine the spacing: if the font is monospaced, assume wxTELETYPE
225 // family for compatibility with the old code which used it instead of
226 // IsFixedWidth()
9a83f860 227 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == wxT('M') )
409d5a58
VZ
228 {
229 m_family = wxFONTFAMILY_TELETYPE;
230 }
231 else // not monospaceed
232 {
233 // don't even try guessing it, it doesn't work for too many fonts
234 // anyhow
235 m_family = wxFONTFAMILY_UNKNOWN;
236 }
237
238 // X fonts are never underlined...
9eddec69 239 m_underlined = false;
409d5a58
VZ
240
241 // deal with font encoding
242 wxString
243 registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(),
244 encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper();
245
9a83f860 246 if ( registry == wxT("ISO8859") )
409d5a58
VZ
247 {
248 int cp;
249 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
250 {
251 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
252 }
253 }
9a83f860 254 else if ( registry == wxT("MICROSOFT") )
409d5a58
VZ
255 {
256 int cp;
257 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
258 {
259 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
260 }
261 }
9a83f860 262 else if ( registry == wxT("KOI8") )
409d5a58
VZ
263 {
264 m_encoding = wxFONTENCODING_KOI8;
265 }
266 else // unknown encoding
267 {
011ba5ed 268 // may be give a warning here? or use wxFontMapper?
409d5a58
VZ
269 m_encoding = wxFONTENCODING_SYSTEM;
270 }
271}
272
011ba5ed 273wxFontRefData::wxFontRefData( const wxFontRefData& data )
8f884a0d 274 : wxGDIRefData()
011ba5ed
VZ
275{
276 m_pointSize = data.m_pointSize;
277 m_family = data.m_family;
278 m_style = data.m_style;
279 m_weight = data.m_weight;
280
281 m_underlined = data.m_underlined;
282
283 m_faceName = data.m_faceName;
284 m_encoding = data.m_encoding;
285
2b5f62a0 286 m_noAA = data.m_noAA;
cd9a673c
RD
287
288 // Forces a copy of the internal data. wxNativeFontInfo should probably
289 // have a copy ctor and assignment operator to fix this properly but that
290 // would break binary compatibility...
291 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
011ba5ed
VZ
292}
293
0c14b6c3
FM
294wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style,
295 wxFontWeight weight, bool underlined,
011ba5ed
VZ
296 const wxString& faceName,
297 wxFontEncoding encoding)
298{
299 Init(size, family, style, weight, underlined, faceName, encoding);
300}
301
302wxFontRefData::wxFontRefData(const wxString& fontname)
8bbe427f 303{
3cbab641 304 // FromString() should really work in GTK1 too, doesn't it?
011ba5ed 305 m_nativeFontInfo.SetXFontName(fontname);
011ba5ed
VZ
306
307 InitFromNative();
308}
309
011ba5ed
VZ
310void wxFontRefData::ClearGdkFonts()
311{
312 for ( wxScaledFontList::iterator i = m_scaled_xfonts.begin();
313 i != m_scaled_xfonts.end();
314 ++i )
8bbe427f 315 {
011ba5ed 316 GdkFont *font = i->second;
8bbe427f 317 gdk_font_unref( font );
8bbe427f 318 }
011ba5ed
VZ
319
320 m_scaled_xfonts.clear();
2b5f62a0 321}
011ba5ed
VZ
322
323wxFontRefData::~wxFontRefData()
324{
325 ClearGdkFonts();
0c5d3e1c 326}
c801d85f 327
0c5d3e1c 328// ----------------------------------------------------------------------------
409d5a58 329// wxFontRefData SetXXX()
0c5d3e1c 330// ----------------------------------------------------------------------------
c801d85f 331
409d5a58 332void wxFontRefData::SetPointSize(int pointSize)
c801d85f 333{
409d5a58 334 m_pointSize = pointSize;
c801d85f 335
409d5a58
VZ
336 if ( HasNativeFont() )
337 {
338 wxString size;
339 if ( pointSize == -1 )
9a83f860 340 size = wxT('*');
409d5a58 341 else
9a83f860 342 size.Printf(wxT("%d"), 10*pointSize);
7826e2dd 343
409d5a58
VZ
344 m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
345 }
7826e2dd
VZ
346}
347
0c14b6c3 348void wxFontRefData::SetFamily(wxFontFamily family)
7826e2dd 349{
409d5a58 350 m_family = family;
30764ab5 351
409d5a58 352 // TODO: what are we supposed to do with m_nativeFontInfo here?
30764ab5
VZ
353}
354
0c14b6c3 355void wxFontRefData::SetStyle(wxFontStyle style)
c801d85f 356{
409d5a58
VZ
357 m_style = style;
358
359 if ( HasNativeFont() )
30764ab5 360 {
409d5a58
VZ
361 wxString slant;
362 switch ( style )
363 {
364 case wxFONTSTYLE_ITALIC:
9a83f860 365 slant = wxT('i');
409d5a58
VZ
366 break;
367
368 case wxFONTSTYLE_SLANT:
9a83f860 369 slant = wxT('o');
409d5a58
VZ
370 break;
371
372 default:
9a83f860 373 wxFAIL_MSG( wxT("unknown font style") );
409d5a58
VZ
374 // fall through
375
376 case wxFONTSTYLE_NORMAL:
9a83f860 377 slant = wxT('r');
409d5a58
VZ
378 }
379
380 m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
30764ab5 381 }
409d5a58 382}
7beba2fc 383
0c14b6c3 384void wxFontRefData::SetWeight(wxFontWeight weight)
409d5a58
VZ
385{
386 m_weight = weight;
8bbe427f 387
409d5a58
VZ
388 if ( HasNativeFont() )
389 {
390 wxString boldness;
391 switch ( weight )
392 {
393 case wxFONTWEIGHT_BOLD:
9a83f860 394 boldness = wxT("bold");
409d5a58 395 break;
30764ab5 396
409d5a58 397 case wxFONTWEIGHT_LIGHT:
9a83f860 398 boldness = wxT("light");
409d5a58 399 break;
284b4c88 400
409d5a58 401 default:
9a83f860 402 wxFAIL_MSG( wxT("unknown font weight") );
409d5a58 403 // fall through
284b4c88 404
409d5a58
VZ
405 case wxFONTWEIGHT_NORMAL:
406 // unspecified
9a83f860 407 boldness = wxT("medium");
409d5a58 408 }
284b4c88 409
409d5a58
VZ
410 m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
411 }
412}
30764ab5 413
409d5a58
VZ
414void wxFontRefData::SetUnderlined(bool underlined)
415{
416 m_underlined = underlined;
8636aed8 417
409d5a58
VZ
418 // the XLFD doesn't have "underlined" field anyhow
419}
30760ce7 420
85ab460e 421bool wxFontRefData::SetFaceName(const wxString& facename)
409d5a58
VZ
422{
423 m_faceName = facename;
7beba2fc 424
409d5a58
VZ
425 if ( HasNativeFont() )
426 {
427 m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
428 }
85ab460e
VZ
429
430 return true;
409d5a58 431}
284b4c88 432
409d5a58
VZ
433void wxFontRefData::SetEncoding(wxFontEncoding encoding)
434{
435 m_encoding = encoding;
284b4c88 436
409d5a58 437 if ( HasNativeFont() )
d06b34a7 438 {
409d5a58
VZ
439 wxNativeEncodingInfo info;
440 if ( wxGetNativeFontEncoding(encoding, &info) )
441 {
442 m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
443 m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
444 }
d06b34a7 445 }
409d5a58 446}
284b4c88 447
011ba5ed
VZ
448void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
449{
450 // previously cached fonts shouldn't be used
451 ClearGdkFonts();
452
453 m_nativeFontInfo = info;
454
455 // set all the other font parameters from the native font info
456 InitFromNative();
457}
458
409d5a58
VZ
459// ----------------------------------------------------------------------------
460// wxFont creation
461// ----------------------------------------------------------------------------
36f210c8 462
409d5a58 463IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
36f210c8 464
409d5a58
VZ
465wxFont::wxFont(const wxNativeFontInfo& info)
466{
2b5f62a0 467 (void) Create(info.GetXFontName());
409d5a58
VZ
468}
469
470bool wxFont::Create( int pointSize,
0c14b6c3
FM
471 wxFontFamily family,
472 wxFontStyle style,
473 wxFontWeight weight,
409d5a58
VZ
474 bool underlined,
475 const wxString& face,
476 wxFontEncoding encoding)
477{
2b5f62a0
VZ
478 UnRef();
479
409d5a58
VZ
480 m_refData = new wxFontRefData(pointSize, family, style, weight,
481 underlined, face, encoding);
482
9eddec69 483 return true;
409d5a58
VZ
484}
485
486bool wxFont::Create(const wxString& fontname)
487{
488 // VZ: does this really happen?
489 if ( fontname.empty() )
36f210c8 490 {
409d5a58 491 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7beba2fc 492
9eddec69 493 return true;
36f210c8 494 }
409d5a58
VZ
495
496 m_refData = new wxFontRefData(fontname);
497
9eddec69 498 return true;
ff7b1510 499}
c801d85f 500
0c5d3e1c 501void wxFont::Unshare()
8bbe427f 502{
0c5d3e1c
VZ
503 if (!m_refData)
504 {
505 m_refData = new wxFontRefData();
506 }
507 else
508 {
509 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
510 UnRef();
511 m_refData = ref;
512 }
ff7b1510 513}
c801d85f 514
8bbe427f 515wxFont::~wxFont()
c801d85f 516{
ff7b1510 517}
c801d85f 518
8f884a0d
VZ
519wxGDIRefData *wxFont::CreateGDIRefData() const
520{
521 return new wxFontRefData;
522}
523
524wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
525{
5c33522f 526 return new wxFontRefData(*static_cast<const wxFontRefData *>(data));
8f884a0d
VZ
527}
528
0c5d3e1c
VZ
529// ----------------------------------------------------------------------------
530// accessors
531// ----------------------------------------------------------------------------
c801d85f 532
8bbe427f 533int wxFont::GetPointSize() const
c801d85f 534{
223d09f6 535 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
536
537 return M_FONTDATA->m_pointSize;
ff7b1510 538}
c801d85f 539
8bbe427f 540wxString wxFont::GetFaceName() const
c801d85f 541{
e4db172a 542 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
8bbe427f 543
36b3b54a 544 return M_FONTDATA->m_faceName;
ff7b1510 545}
c801d85f 546
0c14b6c3 547wxFontFamily wxFont::GetFamily() const
c801d85f 548{
0c14b6c3 549 wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
8bbe427f
VZ
550
551 return M_FONTDATA->m_family;
ff7b1510 552}
c801d85f 553
0c14b6c3 554wxFontStyle wxFont::GetStyle() const
c801d85f 555{
0c14b6c3 556 wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX, wxT("invalid font") );
d84eb083 557
8bbe427f 558 return M_FONTDATA->m_style;
ff7b1510 559}
c801d85f 560
0c14b6c3 561wxFontWeight wxFont::GetWeight() const
c801d85f 562{
0c14b6c3 563 wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX, wxT("invalid font") );
8bbe427f
VZ
564
565 return M_FONTDATA->m_weight;
566}
567
8bbe427f
VZ
568bool wxFont::GetUnderlined() const
569{
9eddec69 570 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
8bbe427f
VZ
571
572 return M_FONTDATA->m_underlined;
ff7b1510 573}
c801d85f 574
0c5d3e1c 575wxFontEncoding wxFont::GetEncoding() const
358fc25c 576{
223d09f6 577 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
0c5d3e1c 578
02d9204c 579 // m_encoding is unused in wxGTK2, return encoding that the user set.
0c5d3e1c 580 return M_FONTDATA->m_encoding;
358fc25c
RR
581}
582
5ac2e80c 583bool wxFont::GetNoAntiAliasing() const
2b5f62a0
VZ
584{
585 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
586
587 return M_FONTDATA->m_noAA;
588}
589
3bf5a59b 590const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
30764ab5 591{
d3b9f782 592 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
30764ab5 593
38de9427
VS
594 if ( !M_FONTDATA->HasNativeFont() )
595 {
596 // NB: this call has important side-effect: it not only finds
597 // GdkFont representation, it also initializes m_nativeFontInfo
598 // by calling its SetXFontName method
30764ab5 599 GetInternalFont();
38de9427 600 }
7826e2dd 601
3bf5a59b 602 return &(M_FONTDATA->m_nativeFontInfo);
30764ab5
VZ
603}
604
53f6aab7
VZ
605bool wxFont::IsFixedWidth() const
606{
9eddec69 607 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
53f6aab7 608
409d5a58 609 if ( M_FONTDATA->HasNativeFont() )
53f6aab7
VZ
610 {
611 // the monospace fonts are supposed to have "M" in the spacing field
612 wxString spacing = M_FONTDATA->
613 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
614
9a83f860 615 return spacing.Upper() == wxT('M');
53f6aab7
VZ
616 }
617
618 return wxFontBase::IsFixedWidth();
619}
30764ab5 620
0c5d3e1c
VZ
621// ----------------------------------------------------------------------------
622// change font attributes
623// ----------------------------------------------------------------------------
624
358fc25c
RR
625void wxFont::SetPointSize(int pointSize)
626{
627 Unshare();
011ba5ed 628
409d5a58 629 M_FONTDATA->SetPointSize(pointSize);
358fc25c
RR
630}
631
0c14b6c3 632void wxFont::SetFamily(wxFontFamily family)
358fc25c
RR
633{
634 Unshare();
635
409d5a58 636 M_FONTDATA->SetFamily(family);
358fc25c
RR
637}
638
0c14b6c3 639void wxFont::SetStyle(wxFontStyle style)
358fc25c
RR
640{
641 Unshare();
642
409d5a58 643 M_FONTDATA->SetStyle(style);
358fc25c
RR
644}
645
0c14b6c3 646void wxFont::SetWeight(wxFontWeight weight)
358fc25c
RR
647{
648 Unshare();
649
409d5a58 650 M_FONTDATA->SetWeight(weight);
358fc25c
RR
651}
652
85ab460e 653bool wxFont::SetFaceName(const wxString& faceName)
358fc25c
RR
654{
655 Unshare();
656
85ab460e
VZ
657 return M_FONTDATA->SetFaceName(faceName) &&
658 wxFontBase::SetFaceName(faceName);
358fc25c
RR
659}
660
661void wxFont::SetUnderlined(bool underlined)
662{
663 Unshare();
664
409d5a58 665 M_FONTDATA->SetUnderlined(underlined);
358fc25c
RR
666}
667
0c5d3e1c
VZ
668void wxFont::SetEncoding(wxFontEncoding encoding)
669{
670 Unshare();
c801d85f 671
409d5a58 672 M_FONTDATA->SetEncoding(encoding);
30764ab5
VZ
673}
674
9045ad9d 675void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
2b5f62a0
VZ
676{
677 Unshare();
678
679 M_FONTDATA->SetNativeFontInfo( info );
680}
681
682void wxFont::SetNoAntiAliasing( bool no )
30764ab5
VZ
683{
684 Unshare();
685
2b5f62a0 686 M_FONTDATA->SetNoAntiAliasing( no );
0c5d3e1c
VZ
687}
688
689// ----------------------------------------------------------------------------
690// get internal representation of font
691// ----------------------------------------------------------------------------
c801d85f 692
d3b9f782 693static GdkFont *g_systemDefaultGuiFont = NULL;
c7985368 694
409d5a58
VZ
695// this is also used from tbargtk.cpp and tooltip.cpp, hence extern
696extern GdkFont *GtkGetDefaultGuiFont()
c7985368
RR
697{
698 if (!g_systemDefaultGuiFont)
699 {
700 GtkWidget *widget = gtk_button_new();
701 GtkStyle *def = gtk_rc_get_style( widget );
e6527f9d
RR
702 if (def)
703 {
464f1a1d 704 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d
RR
705 }
706 else
707 {
708 def = gtk_widget_get_default_style();
709 if (def)
464f1a1d 710 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d 711 }
c7985368
RR
712 gtk_widget_destroy( widget );
713 }
b1d1dc51
VZ
714 else
715 {
716 // already have it, but ref it once more before returning
717 gdk_font_ref(g_systemDefaultGuiFont);
718 }
719
c7985368
RR
720 return g_systemDefaultGuiFont;
721}
722
36b3b54a 723GdkFont *wxFont::GetInternalFont( float scale ) const
c801d85f 724{
d3b9f782 725 GdkFont *font = NULL;
0c5d3e1c 726
637b7e4f 727 wxCHECK_MSG( Ok(), font, wxT("invalid font") );
8bbe427f 728
db16cab4 729 long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
b02da6b1 730 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
8bbe427f 731
011ba5ed
VZ
732 wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts;
733 wxScaledFontList::iterator i = list.find(int_scale);
734 if ( i != list.end() )
8bbe427f 735 {
011ba5ed 736 font = i->second;
8bbe427f 737 }
409d5a58 738 else // we don't have this font in this size yet
8bbe427f 739 {
a756f210 740 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
8bbe427f 741 {
c7985368 742 font = GtkGetDefaultGuiFont();
8bbe427f 743 }
409d5a58
VZ
744
745 if ( !font )
8bbe427f 746 {
409d5a58 747 // do we have the XLFD?
38de9427 748 if ( int_scale == 100 && M_FONTDATA->HasNativeFont() )
409d5a58
VZ
749 {
750 font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
751 }
752
753 // no XLFD of no exact match - try the approximate one now
754 if ( !font )
755 {
756 wxString xfontname;
757 font = wxLoadQueryNearestFont( point_scale,
758 M_FONTDATA->m_family,
759 M_FONTDATA->m_style,
760 M_FONTDATA->m_weight,
761 M_FONTDATA->m_underlined,
762 M_FONTDATA->m_faceName,
763 M_FONTDATA->m_encoding,
764 &xfontname);
0f6858b6 765 // NB: wxFont::GetNativeFontInfo relies on this
38de9427
VS
766 // side-effect of GetInternalFont
767 if ( int_scale == 100 )
768 M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname);
409d5a58 769 }
8bbe427f 770 }
0c5d3e1c 771
409d5a58
VZ
772 if ( font )
773 {
011ba5ed 774 list[int_scale] = font;
409d5a58 775 }
8bbe427f 776 }
284b4c88 777
7beba2fc
VZ
778 // it's quite useless to make it a wxCHECK because we're going to crash
779 // anyhow...
780 wxASSERT_MSG( font, wxT("could not load any font?") );
284b4c88 781
8bbe427f 782 return font;
ff7b1510 783}