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