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