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