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