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