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