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