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