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