]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/font.cpp
Calculate correct client size for windows that are using deferred sizing.
[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
VS
18// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
c801d85f 21#include "wx/font.h"
7beba2fc
VZ
22#include "wx/fontutil.h"
23#include "wx/cmndata.h"
c801d85f 24#include "wx/utils.h"
5705323e 25#include "wx/log.h"
4cb122de 26#include "wx/gdicmn.h"
8636aed8 27#include "wx/tokenzr.h"
c7985368 28#include "wx/settings.h"
0c5d3e1c 29
c801d85f
KB
30#include <strings.h>
31
3cbab641 32#include "wx/gtk1/private.h"
d06b34a7 33#include <gdk/gdkprivate.h>
83624f79 34
409d5a58
VZ
35// ----------------------------------------------------------------------------
36// constants
37// ----------------------------------------------------------------------------
38
39// the default size (in points) for the fonts
40static const int wxDEFAULT_FONT_SIZE = 12;
41
42// ----------------------------------------------------------------------------
011ba5ed 43// wxScaledFontList: maps the font sizes to the GDK fonts for the given font
409d5a58
VZ
44// ----------------------------------------------------------------------------
45
011ba5ed
VZ
46WX_DECLARE_HASH_MAP(int, GdkFont *, wxIntegerHash, wxIntegerEqual,
47 wxScaledFontList);
409d5a58 48
0c5d3e1c
VZ
49// ----------------------------------------------------------------------------
50// wxFontRefData
51// ----------------------------------------------------------------------------
52
53class wxFontRefData : public wxObjectRefData
c801d85f 54{
8bbe427f 55public:
409d5a58
VZ
56 // from broken down font parameters, also default ctor
57 wxFontRefData(int size = -1,
58 int family = wxFONTFAMILY_DEFAULT,
59 int style = wxFONTSTYLE_NORMAL,
60 int weight = wxFONTWEIGHT_NORMAL,
0c5d3e1c
VZ
61 bool underlined = FALSE,
62 const wxString& faceName = wxEmptyString,
7826e2dd 63 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
409d5a58
VZ
64
65 // from XFLD
66 wxFontRefData(const wxString& fontname);
67
68 // copy ctor
358fc25c 69 wxFontRefData( const wxFontRefData& data );
409d5a58 70
0c5d3e1c
VZ
71 virtual ~wxFontRefData();
72
409d5a58
VZ
73 // do we have the native font info?
74 bool HasNativeFont() const
75 {
011ba5ed 76 // only use m_nativeFontInfo if it had been initialized
409d5a58
VZ
77 return !m_nativeFontInfo.IsDefault();
78 }
79
80 // setters: all of them also take care to modify m_nativeFontInfo if we
81 // have it so as to not lose the information not carried by our fields
82 void SetPointSize(int pointSize);
83 void SetFamily(int family);
84 void SetStyle(int style);
85 void SetWeight(int weight);
86 void SetUnderlined(bool underlined);
87 void SetFaceName(const wxString& facename);
88 void SetEncoding(wxFontEncoding encoding);
89
2b5f62a0 90 void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; }
5ac2e80c 91 bool GetNoAntiAliasing() const { return m_noAA; }
cd9a673c 92
011ba5ed
VZ
93 // and this one also modifies all the other font data fields
94 void SetNativeFontInfo(const wxNativeFontInfo& info);
95
409d5a58
VZ
96 // debugger helper: shows what the font really is
97 //
98 // VZ: I need this as my gdb either shows wildly wrong values or crashes
99 // when I ask it to "p fontRefData" :-(
3cbab641 100#if defined(__WXDEBUG__)
409d5a58
VZ
101 void Dump() const
102 {
103 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
104 m_faceName.c_str(),
105 m_weight == wxFONTWEIGHT_NORMAL
106 ? _T("normal")
107 : m_weight == wxFONTWEIGHT_BOLD
108 ? _T("bold")
109 : _T("light"),
110 m_style == wxFONTSTYLE_NORMAL ? _T("regular") : _T("italic"),
111 m_pointSize,
112 m_encoding);
113 }
114#endif // Debug
115
0c5d3e1c
VZ
116protected:
117 // common part of all ctors
118 void Init(int pointSize,
119 int family,
120 int style,
121 int weight,
122 bool underlined,
123 const wxString& faceName,
7826e2dd 124 wxFontEncoding encoding);
0c5d3e1c 125
011ba5ed
VZ
126 // set all fields from (already initialized and valid) m_nativeFontInfo
127 void InitFromNative();
128
0c5d3e1c 129private:
2b5f62a0 130 // clear m_scaled_xfonts if any
011ba5ed
VZ
131 void ClearGdkFonts();
132
409d5a58
VZ
133 // the map of font sizes to "GdkFont *"
134 wxScaledFontList m_scaled_xfonts;
135
f35c2659
RR
136 int m_pointSize;
137 int m_family,
138 m_style,
139 m_weight;
140 bool m_underlined;
141 wxString m_faceName;
db16cab4 142 wxFontEncoding m_encoding; // Unused under GTK 2.0
2b5f62a0 143 bool m_noAA; // No anti-aliasing
7826e2dd 144
db16cab4
RR
145 // The native font info, basicly an XFLD under GTK 1.2 and
146 // the pango font description under GTK 2.0.
30764ab5 147 wxNativeFontInfo m_nativeFontInfo;
8bbe427f 148
f6bcfd97 149 friend class wxFont;
c801d85f
KB
150};
151
0c5d3e1c 152// ----------------------------------------------------------------------------
cd9a673c 153// wxFontRefData
0c5d3e1c
VZ
154// ----------------------------------------------------------------------------
155
156void wxFontRefData::Init(int pointSize,
157 int family,
158 int style,
159 int weight,
160 bool underlined,
161 const wxString& faceName,
7826e2dd 162 wxFontEncoding encoding)
8bbe427f 163{
409d5a58 164 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
0c5d3e1c
VZ
165
166 m_faceName = faceName;
167
409d5a58
VZ
168 // we accept both wxDEFAULT and wxNORMAL here - should we?
169 m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
170 m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
0c5d3e1c 171
409d5a58
VZ
172 // and here, do we really want to forbid creation of the font of the size
173 // 90 (the value of wxDEFAULT)??
011ba5ed
VZ
174 m_pointSize = pointSize == wxDEFAULT || pointSize == -1
175 ? wxDEFAULT_FONT_SIZE
176 : pointSize;
0c5d3e1c
VZ
177
178 m_underlined = underlined;
179 m_encoding = encoding;
cd9a673c 180
2b5f62a0 181 m_noAA = FALSE;
358fc25c
RR
182}
183
011ba5ed 184void wxFontRefData::InitFromNative()
409d5a58 185{
2b5f62a0
VZ
186 m_noAA = FALSE;
187
409d5a58
VZ
188 // get the font parameters from the XLFD
189 // -------------------------------------
190
191 m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY);
192
193 m_weight = wxFONTWEIGHT_NORMAL;
194
195 wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper();
196 if ( !w.empty() && w != _T('*') )
197 {
198 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
199 // and BLACK
fab591c5
RR
200 if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
201 !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
202 wxStrstr(w.c_str() + 1, _T("BOLD")) )
409d5a58
VZ
203 {
204 m_weight = wxFONTWEIGHT_BOLD;
205 }
206 else if ( w == _T("LIGHT") || w == _T("THIN") )
207 {
208 m_weight = wxFONTWEIGHT_LIGHT;
209 }
210 }
211
212 switch ( wxToupper(*m_nativeFontInfo.
213 GetXFontComponent(wxXLFD_SLANT).c_str()) )
214 {
215 case _T('I'): // italique
216 m_style = wxFONTSTYLE_ITALIC;
217 break;
218
219 case _T('O'): // oblique
220 m_style = wxFONTSTYLE_SLANT;
221 break;
222
223 default:
224 m_style = wxFONTSTYLE_NORMAL;
225 }
226
227 long ptSize;
228 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
229 {
230 // size in XLFD is in 10 point units
231 m_pointSize = (int)(ptSize / 10);
232 }
233 else
234 {
235 m_pointSize = wxDEFAULT_FONT_SIZE;
236 }
237
238 // examine the spacing: if the font is monospaced, assume wxTELETYPE
239 // family for compatibility with the old code which used it instead of
240 // IsFixedWidth()
241 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') )
242 {
243 m_family = wxFONTFAMILY_TELETYPE;
244 }
245 else // not monospaceed
246 {
247 // don't even try guessing it, it doesn't work for too many fonts
248 // anyhow
249 m_family = wxFONTFAMILY_UNKNOWN;
250 }
251
252 // X fonts are never underlined...
253 m_underlined = FALSE;
254
255 // deal with font encoding
256 wxString
257 registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(),
258 encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper();
259
260 if ( registry == _T("ISO8859") )
261 {
262 int cp;
263 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
264 {
265 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
266 }
267 }
268 else if ( registry == _T("MICROSOFT") )
269 {
270 int cp;
271 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
272 {
273 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
274 }
275 }
276 else if ( registry == _T("KOI8") )
277 {
278 m_encoding = wxFONTENCODING_KOI8;
279 }
280 else // unknown encoding
281 {
011ba5ed 282 // may be give a warning here? or use wxFontMapper?
409d5a58
VZ
283 m_encoding = wxFONTENCODING_SYSTEM;
284 }
285}
286
011ba5ed
VZ
287wxFontRefData::wxFontRefData( const wxFontRefData& data )
288 : wxObjectRefData()
289{
290 m_pointSize = data.m_pointSize;
291 m_family = data.m_family;
292 m_style = data.m_style;
293 m_weight = data.m_weight;
294
295 m_underlined = data.m_underlined;
296
297 m_faceName = data.m_faceName;
298 m_encoding = data.m_encoding;
299
2b5f62a0 300 m_noAA = data.m_noAA;
cd9a673c
RD
301
302 // Forces a copy of the internal data. wxNativeFontInfo should probably
303 // have a copy ctor and assignment operator to fix this properly but that
304 // would break binary compatibility...
305 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
011ba5ed
VZ
306}
307
308wxFontRefData::wxFontRefData(int size, int family, int style,
309 int weight, bool underlined,
310 const wxString& faceName,
311 wxFontEncoding encoding)
312{
313 Init(size, family, style, weight, underlined, faceName, encoding);
314}
315
316wxFontRefData::wxFontRefData(const wxString& fontname)
8bbe427f 317{
3cbab641 318 // FromString() should really work in GTK1 too, doesn't it?
011ba5ed 319 m_nativeFontInfo.SetXFontName(fontname);
011ba5ed
VZ
320
321 InitFromNative();
322}
323
011ba5ed
VZ
324void wxFontRefData::ClearGdkFonts()
325{
326 for ( wxScaledFontList::iterator i = m_scaled_xfonts.begin();
327 i != m_scaled_xfonts.end();
328 ++i )
8bbe427f 329 {
011ba5ed 330 GdkFont *font = i->second;
8bbe427f 331 gdk_font_unref( font );
8bbe427f 332 }
011ba5ed
VZ
333
334 m_scaled_xfonts.clear();
2b5f62a0 335}
011ba5ed
VZ
336
337wxFontRefData::~wxFontRefData()
338{
339 ClearGdkFonts();
0c5d3e1c 340}
c801d85f 341
0c5d3e1c 342// ----------------------------------------------------------------------------
409d5a58 343// wxFontRefData SetXXX()
0c5d3e1c 344// ----------------------------------------------------------------------------
c801d85f 345
409d5a58 346void wxFontRefData::SetPointSize(int pointSize)
c801d85f 347{
409d5a58 348 m_pointSize = pointSize;
c801d85f 349
409d5a58
VZ
350 if ( HasNativeFont() )
351 {
352 wxString size;
353 if ( pointSize == -1 )
354 size = _T('*');
355 else
356 size.Printf(_T("%d"), 10*pointSize);
7826e2dd 357
409d5a58
VZ
358 m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
359 }
7826e2dd
VZ
360}
361
409d5a58 362void wxFontRefData::SetFamily(int family)
7826e2dd 363{
409d5a58 364 m_family = family;
30764ab5 365
409d5a58 366 // TODO: what are we supposed to do with m_nativeFontInfo here?
30764ab5
VZ
367}
368
409d5a58 369void wxFontRefData::SetStyle(int style)
c801d85f 370{
409d5a58
VZ
371 m_style = style;
372
373 if ( HasNativeFont() )
30764ab5 374 {
409d5a58
VZ
375 wxString slant;
376 switch ( style )
377 {
378 case wxFONTSTYLE_ITALIC:
379 slant = _T('i');
380 break;
381
382 case wxFONTSTYLE_SLANT:
383 slant = _T('o');
384 break;
385
386 default:
387 wxFAIL_MSG( _T("unknown font style") );
388 // fall through
389
390 case wxFONTSTYLE_NORMAL:
391 slant = _T('r');
392 }
393
394 m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
30764ab5 395 }
409d5a58 396}
7beba2fc 397
409d5a58
VZ
398void wxFontRefData::SetWeight(int weight)
399{
400 m_weight = weight;
8bbe427f 401
409d5a58
VZ
402 if ( HasNativeFont() )
403 {
404 wxString boldness;
405 switch ( weight )
406 {
407 case wxFONTWEIGHT_BOLD:
408 boldness = _T("bold");
409 break;
30764ab5 410
409d5a58
VZ
411 case wxFONTWEIGHT_LIGHT:
412 boldness = _T("light");
413 break;
284b4c88 414
409d5a58
VZ
415 default:
416 wxFAIL_MSG( _T("unknown font weight") );
417 // fall through
284b4c88 418
409d5a58
VZ
419 case wxFONTWEIGHT_NORMAL:
420 // unspecified
421 boldness = _T("medium");
422 }
284b4c88 423
409d5a58
VZ
424 m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
425 }
426}
30764ab5 427
409d5a58
VZ
428void wxFontRefData::SetUnderlined(bool underlined)
429{
430 m_underlined = underlined;
8636aed8 431
409d5a58
VZ
432 // the XLFD doesn't have "underlined" field anyhow
433}
30760ce7 434
409d5a58
VZ
435void wxFontRefData::SetFaceName(const wxString& facename)
436{
437 m_faceName = facename;
7beba2fc 438
409d5a58
VZ
439 if ( HasNativeFont() )
440 {
441 m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
442 }
443}
284b4c88 444
409d5a58
VZ
445void wxFontRefData::SetEncoding(wxFontEncoding encoding)
446{
447 m_encoding = encoding;
284b4c88 448
409d5a58 449 if ( HasNativeFont() )
d06b34a7 450 {
409d5a58
VZ
451 wxNativeEncodingInfo info;
452 if ( wxGetNativeFontEncoding(encoding, &info) )
453 {
454 m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
455 m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
456 }
d06b34a7 457 }
409d5a58 458}
284b4c88 459
011ba5ed
VZ
460void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
461{
462 // previously cached fonts shouldn't be used
463 ClearGdkFonts();
464
465 m_nativeFontInfo = info;
466
467 // set all the other font parameters from the native font info
468 InitFromNative();
469}
470
409d5a58
VZ
471// ----------------------------------------------------------------------------
472// wxFont creation
473// ----------------------------------------------------------------------------
36f210c8 474
409d5a58 475IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
36f210c8 476
409d5a58
VZ
477void wxFont::Init()
478{
479}
36f210c8 480
409d5a58
VZ
481wxFont::wxFont(const wxNativeFontInfo& info)
482{
483 Init();
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
501 return TRUE;
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
409d5a58 511 return TRUE;
36f210c8 512 }
409d5a58
VZ
513
514 m_refData = new wxFontRefData(fontname);
515
0c5d3e1c 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{
223d09f6 555 wxCHECK_MSG( Ok(), wxT(""), 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{
223d09f6 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{
647 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
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
693void wxFont::SetFaceName(const wxString& faceName)
694{
695 Unshare();
696
409d5a58 697 M_FONTDATA->SetFaceName(faceName);
358fc25c
RR
698}
699
700void wxFont::SetUnderlined(bool underlined)
701{
702 Unshare();
703
409d5a58 704 M_FONTDATA->SetUnderlined(underlined);
358fc25c
RR
705}
706
0c5d3e1c
VZ
707void wxFont::SetEncoding(wxFontEncoding encoding)
708{
709 Unshare();
c801d85f 710
409d5a58 711 M_FONTDATA->SetEncoding(encoding);
30764ab5
VZ
712}
713
9045ad9d 714void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
2b5f62a0
VZ
715{
716 Unshare();
717
718 M_FONTDATA->SetNativeFontInfo( info );
719}
720
721void wxFont::SetNoAntiAliasing( bool no )
30764ab5
VZ
722{
723 Unshare();
724
2b5f62a0 725 M_FONTDATA->SetNoAntiAliasing( no );
0c5d3e1c
VZ
726}
727
728// ----------------------------------------------------------------------------
729// get internal representation of font
730// ----------------------------------------------------------------------------
c801d85f 731
c7985368
RR
732static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
733
409d5a58
VZ
734// this is also used from tbargtk.cpp and tooltip.cpp, hence extern
735extern GdkFont *GtkGetDefaultGuiFont()
c7985368
RR
736{
737 if (!g_systemDefaultGuiFont)
738 {
739 GtkWidget *widget = gtk_button_new();
740 GtkStyle *def = gtk_rc_get_style( widget );
e6527f9d
RR
741 if (def)
742 {
464f1a1d 743 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d
RR
744 }
745 else
746 {
747 def = gtk_widget_get_default_style();
748 if (def)
464f1a1d 749 g_systemDefaultGuiFont = gdk_font_ref( def->font );
e6527f9d 750 }
c7985368
RR
751 gtk_widget_destroy( widget );
752 }
b1d1dc51
VZ
753 else
754 {
755 // already have it, but ref it once more before returning
756 gdk_font_ref(g_systemDefaultGuiFont);
757 }
758
c7985368
RR
759 return g_systemDefaultGuiFont;
760}
761
36b3b54a 762GdkFont *wxFont::GetInternalFont( float scale ) const
c801d85f 763{
409d5a58 764 GdkFont *font = (GdkFont *) NULL;
0c5d3e1c 765
409d5a58 766 wxCHECK_MSG( Ok(), font, wxT("invalid font") )
8bbe427f 767
db16cab4 768 long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
b02da6b1 769 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
8bbe427f 770
011ba5ed
VZ
771 wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts;
772 wxScaledFontList::iterator i = list.find(int_scale);
773 if ( i != list.end() )
8bbe427f 774 {
011ba5ed 775 font = i->second;
8bbe427f 776 }
409d5a58 777 else // we don't have this font in this size yet
8bbe427f 778 {
a756f210 779 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
8bbe427f 780 {
c7985368 781 font = GtkGetDefaultGuiFont();
8bbe427f 782 }
409d5a58
VZ
783
784 if ( !font )
8bbe427f 785 {
409d5a58 786 // do we have the XLFD?
38de9427 787 if ( int_scale == 100 && M_FONTDATA->HasNativeFont() )
409d5a58
VZ
788 {
789 font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
790 }
791
792 // no XLFD of no exact match - try the approximate one now
793 if ( !font )
794 {
795 wxString xfontname;
796 font = wxLoadQueryNearestFont( point_scale,
797 M_FONTDATA->m_family,
798 M_FONTDATA->m_style,
799 M_FONTDATA->m_weight,
800 M_FONTDATA->m_underlined,
801 M_FONTDATA->m_faceName,
802 M_FONTDATA->m_encoding,
803 &xfontname);
0f6858b6 804 // NB: wxFont::GetNativeFontInfo relies on this
38de9427
VS
805 // side-effect of GetInternalFont
806 if ( int_scale == 100 )
807 M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname);
409d5a58 808 }
8bbe427f 809 }
0c5d3e1c 810
409d5a58
VZ
811 if ( font )
812 {
011ba5ed 813 list[int_scale] = font;
409d5a58 814 }
8bbe427f 815 }
284b4c88 816
7beba2fc
VZ
817 // it's quite useless to make it a wxCHECK because we're going to crash
818 // anyhow...
819 wxASSERT_MSG( font, wxT("could not load any font?") );
284b4c88 820
8bbe427f 821 return font;
ff7b1510 822}