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