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