]> git.saurik.com Git - wxWidgets.git/blame - src/x11/font.cpp
Fix compilation with MinGW -std=c++11 option.
[wxWidgets.git] / src / x11 / font.cpp
CommitLineData
83df96d6 1/////////////////////////////////////////////////////////////////////////////
788519c6 2// Name: src/x11/font.cpp
83df96d6
JS
3// Purpose: wxFont class
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
83df96d6
JS
10/////////////////////////////////////////////////////////////////////////////
11
7520f3da
WS
12// for compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
83df96d6
JS
15// ============================================================================
16// declarations
17// ============================================================================
18
19// ----------------------------------------------------------------------------
20// headers
21// ----------------------------------------------------------------------------
22
83df96d6
JS
23#ifdef __VMS
24#pragma message disable nosimpint
25#include "wx/vms_x_fix.h"
26#endif
bc797f4c 27
83df96d6
JS
28#ifdef __VMS
29#pragma message enable nosimpint
30#endif
31
83df96d6 32#include "wx/font.h"
df91131c
WS
33
34#ifndef WX_PRECOMP
35 #include "wx/string.h"
de6185e2 36 #include "wx/utils.h" // for wxGetDisplay()
9eddec69 37 #include "wx/settings.h"
dd05139a 38 #include "wx/gdicmn.h"
df91131c
WS
39#endif
40
83df96d6
JS
41#include "wx/fontutil.h" // for wxNativeFontInfo
42#include "wx/tokenzr.h"
3d63133a 43#include "wx/fontenum.h"
2b5f62a0 44
8354aa92 45#include "wx/x11/private.h"
83df96d6 46
83df96d6 47// ----------------------------------------------------------------------------
2b5f62a0
VZ
48// constants
49// ----------------------------------------------------------------------------
50
51// the default size (in points) for the fonts
52static const int wxDEFAULT_FONT_SIZE = 12;
53
54
55#if wxUSE_UNICODE
56#else
57// ----------------------------------------------------------------------------
58// wxXFont
83df96d6
JS
59// ----------------------------------------------------------------------------
60
61// For every wxFont, there must be a font for each display and scale requested.
62// So these objects are stored in wxFontRefData::m_fonts
63class wxXFont : public wxObject
64{
65public:
66 wxXFont();
d3c7fc99 67 virtual ~wxXFont();
83df96d6
JS
68
69 WXFontStructPtr m_fontStruct; // XFontStruct
83df96d6
JS
70 WXDisplay* m_display; // XDisplay
71 int m_scale; // Scale * 100
72};
73
2b5f62a0
VZ
74wxXFont::wxXFont()
75{
76 m_fontStruct = (WXFontStructPtr) 0;
77 m_display = (WXDisplay*) 0;
78 m_scale = 100;
79}
80
81wxXFont::~wxXFont()
82{
6811251b
JS
83 // Freeing the font used to produce a segv, but
84 // appears to be OK now (bug fix in X11?)
85 XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
86 XFreeFont((Display*) m_display, fontStruct);
2b5f62a0
VZ
87}
88#endif
89
90// ----------------------------------------------------------------------------
91// wxFontRefData
92// ----------------------------------------------------------------------------
93
8f884a0d 94class wxFontRefData: public wxGDIRefData
83df96d6
JS
95{
96friend class wxFont;
97
98public:
99 wxFontRefData(int size = wxDEFAULT,
0c14b6c3
FM
100 wxFontFamily family = wxFONTFAMILY_DEFAULT,
101 wxFontStyle style = wxFONTSTYLE_NORMAL,
102 wxFontWeight weight = wxFONTWEIGHT_NORMAL,
7520f3da 103 bool underlined = false,
83df96d6 104 const wxString& faceName = wxEmptyString,
2b5f62a0 105 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
83df96d6 106
2b5f62a0
VZ
107 // copy cstr
108 wxFontRefData(const wxFontRefData& data);
9045ad9d 109
2b5f62a0
VZ
110 // from XFLD
111 wxFontRefData(const wxString& fontname);
9045ad9d 112
2b5f62a0
VZ
113 // dstr
114 virtual ~wxFontRefData();
115
116 // setters: all of them also take care to modify m_nativeFontInfo if we
117 // have it so as to not lose the information not carried by our fields
118 void SetPointSize(int pointSize);
0c14b6c3
FM
119 void SetFamily(wxFontFamily family);
120 void SetStyle(wxFontStyle style);
121 void SetWeight(wxFontWeight weight);
2b5f62a0 122 void SetUnderlined(bool underlined);
85ab460e 123 bool SetFaceName(const wxString& facename);
2b5f62a0
VZ
124 void SetEncoding(wxFontEncoding encoding);
125
2b5f62a0
VZ
126 // and this one also modifies all the other font data fields
127 void SetNativeFontInfo(const wxNativeFontInfo& info);
9045ad9d 128
83df96d6
JS
129protected:
130 // common part of all ctors
131 void Init(int size,
0c14b6c3
FM
132 wxFontFamily family,
133 wxFontStyle style,
134 wxFontWeight weight,
83df96d6
JS
135 bool underlined,
136 const wxString& faceName,
137 wxFontEncoding encoding);
138
2b5f62a0
VZ
139 // set all fields from (already initialized and valid) m_nativeFontInfo
140 void InitFromNative();
9045ad9d 141
83df96d6
JS
142 // font attributes
143 int m_pointSize;
0c14b6c3
FM
144 wxFontFamily m_family;
145 wxFontStyle m_style;
146 wxFontWeight m_weight;
83df96d6
JS
147 bool m_underlined;
148 wxString m_faceName;
2b5f62a0 149 wxFontEncoding m_encoding; // Unused in Unicode mode
83df96d6
JS
150
151 wxNativeFontInfo m_nativeFontInfo;
9045ad9d 152
2b5f62a0
VZ
153 void ClearX11Fonts();
154
155#if wxUSE_UNICODE
156#else
83df96d6
JS
157 // A list of wxXFonts
158 wxList m_fonts;
2b5f62a0 159#endif
83df96d6
JS
160};
161
68c95704 162#define M_FONTDATA ((wxFontRefData*)m_refData)
873fd4af 163
83df96d6
JS
164// ----------------------------------------------------------------------------
165// wxFontRefData
166// ----------------------------------------------------------------------------
167
168void wxFontRefData::Init(int pointSize,
0c14b6c3
FM
169 wxFontFamily family,
170 wxFontStyle style,
171 wxFontWeight weight,
83df96d6
JS
172 bool underlined,
173 const wxString& faceName,
174 wxFontEncoding encoding)
175{
2b5f62a0 176 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
83df96d6
JS
177
178 m_faceName = faceName;
179
2b5f62a0
VZ
180 // we accept both wxDEFAULT and wxNORMAL here - should we?
181 m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
182 m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
183
2b5f62a0
VZ
184 m_underlined = underlined;
185 m_encoding = encoding;
9045ad9d 186
2b5f62a0 187#if wxUSE_UNICODE
3d63133a
VZ
188 if ( m_nativeFontInfo.description )
189 pango_font_description_free(m_nativeFontInfo.description);
190
2b5f62a0
VZ
191 // Create native font info
192 m_nativeFontInfo.description = pango_font_description_new();
193
3d63133a
VZ
194 // if a face name is specified, use it if it's available, otherwise use
195 // just the family
196 if ( faceName.empty() || !wxFontEnumerator::IsValidFacename(faceName) )
2b5f62a0 197 {
3d63133a
VZ
198 // TODO: scan system for valid fonts matching the given family instead
199 // of hardcoding them here
200 switch ( m_family )
201 {
202 case wxFONTFAMILY_TELETYPE:
203 m_faceName = wxT("monospace");
204 break;
205
206 case wxFONTFAMILY_ROMAN:
207 m_faceName = wxT("serif");
208 break;
209
210 default:
211 m_faceName = wxT("sans");
212 }
2b5f62a0 213 }
3d63133a
VZ
214 else // specified face name is available, use it
215 {
216 m_faceName = faceName;
217 }
218
219 m_nativeFontInfo.SetFaceName(m_faceName);
3d63133a
VZ
220 m_nativeFontInfo.SetWeight((wxFontWeight)m_weight);
221 m_nativeFontInfo.SetStyle((wxFontStyle)m_style);
222#endif // wxUSE_UNICODE
cb5ec61f
VZ
223
224 SetPointSize(pointSize);
2b5f62a0
VZ
225}
226
227void wxFontRefData::InitFromNative()
228{
2b5f62a0
VZ
229#if wxUSE_UNICODE
230 // Get native info
231 PangoFontDescription *desc = m_nativeFontInfo.description;
232
233 // init fields
234 m_faceName = wxGTK_CONV_BACK( pango_font_description_get_family( desc ) );
83df96d6 235
2b5f62a0
VZ
236 m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE;
237
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
15a187a6
JS
251// Not defined in some Pango versions
252#define wxPANGO_WEIGHT_SEMIBOLD 600
253
2b5f62a0
VZ
254 switch (pango_font_description_get_weight( desc ))
255 {
256 case PANGO_WEIGHT_ULTRALIGHT:
2b5f62a0
VZ
257 case PANGO_WEIGHT_LIGHT:
258 m_weight = wxFONTWEIGHT_LIGHT;
259 break;
51440135
VZ
260
261 default:
9a83f860 262 wxFAIL_MSG(wxT("unknown Pango font weight"));
51440135
VZ
263 // fall through
264
2b5f62a0
VZ
265 case PANGO_WEIGHT_NORMAL:
266 m_weight = wxFONTWEIGHT_NORMAL;
267 break;
51440135 268
15a187a6 269 case wxPANGO_WEIGHT_SEMIBOLD:
2b5f62a0 270 case PANGO_WEIGHT_BOLD:
2b5f62a0 271 case PANGO_WEIGHT_ULTRABOLD:
2b5f62a0
VZ
272 case PANGO_WEIGHT_HEAVY:
273 m_weight = wxFONTWEIGHT_BOLD;
274 break;
275 }
276
277 if (m_faceName == wxT("monospace"))
278 {
279 m_family = wxFONTFAMILY_TELETYPE;
280 }
281 else if (m_faceName == wxT("sans"))
282 {
283 m_family = wxFONTFAMILY_SWISS;
284 }
83df96d6 285 else
2b5f62a0
VZ
286 {
287 m_family = wxFONTFAMILY_UNKNOWN;
288 }
289
290 // Pango description are never underlined (?)
7520f3da 291 m_underlined = false;
2b5f62a0
VZ
292
293 // Cannot we choose that
294 m_encoding = wxFONTENCODING_SYSTEM;
295#else // X11
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();
9a83f860 304 if ( !w.empty() && w != wxT('*') )
2b5f62a0
VZ
305 {
306 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
307 // and BLACK
9a83f860 308 if ( ((w[0u] == wxT('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
2b5f62a0 309 !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
9a83f860 310 wxStrstr(w.c_str() + 1, wxT("BOLD")) )
2b5f62a0
VZ
311 {
312 m_weight = wxFONTWEIGHT_BOLD;
313 }
9a83f860 314 else if ( w == wxT("LIGHT") || w == wxT("THIN") )
2b5f62a0
VZ
315 {
316 m_weight = wxFONTWEIGHT_LIGHT;
317 }
318 }
319
e97e6073 320 switch ( wxToupper( m_nativeFontInfo.
03647350 321 GetXFontComponent(wxXLFD_SLANT)[0u]).GetValue() )
2b5f62a0 322 {
9a83f860 323 case wxT('I'): // italique
2b5f62a0
VZ
324 m_style = wxFONTSTYLE_ITALIC;
325 break;
326
9a83f860 327 case wxT('O'): // oblique
2b5f62a0
VZ
328 m_style = wxFONTSTYLE_SLANT;
329 break;
83df96d6 330
2b5f62a0
VZ
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 }
83df96d6 341 else
2b5f62a0
VZ
342 {
343 m_pointSize = wxDEFAULT_FONT_SIZE;
344 }
83df96d6 345
2b5f62a0
VZ
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()
9a83f860 349 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == wxT('M') )
2b5f62a0
VZ
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...
7520f3da 361 m_underlined = false;
2b5f62a0
VZ
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
9a83f860 368 if ( registry == wxT("ISO8859") )
2b5f62a0
VZ
369 {
370 int cp;
371 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
372 {
373 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
374 }
375 }
9a83f860 376 else if ( registry == wxT("MICROSOFT") )
2b5f62a0
VZ
377 {
378 int cp;
379 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
380 {
381 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
382 }
383 }
9a83f860 384 else if ( registry == wxT("KOI8") )
2b5f62a0
VZ
385 {
386 m_encoding = wxFONTENCODING_KOI8;
387 }
388 else // unknown encoding
389 {
390 // may be give a warning here? or use wxFontMapper?
391 m_encoding = wxFONTENCODING_SYSTEM;
392 }
393#endif // Pango/X11
83df96d6
JS
394}
395
2b5f62a0 396wxFontRefData::wxFontRefData( const wxFontRefData& data )
8f884a0d 397 : wxGDIRefData()
83df96d6 398{
2b5f62a0
VZ
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_nativeFontInfo = data.m_nativeFontInfo;
410}
411
0c14b6c3
FM
412wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style,
413 wxFontWeight weight, bool underlined,
2b5f62a0
VZ
414 const wxString& faceName,
415 wxFontEncoding encoding)
416{
417 Init(size, family, style, weight, underlined, faceName, encoding);
418}
419
420wxFontRefData::wxFontRefData(const wxString& fontname)
421{
422 // VZ: FromString() should really work in both cases, doesn't it?
423#if wxUSE_UNICODE
424 m_nativeFontInfo.FromString( fontname );
425#else
426 m_nativeFontInfo.SetXFontName(fontname);
427#endif
428
429 InitFromNative();
430}
431
432void wxFontRefData::ClearX11Fonts()
433{
434#if wxUSE_UNICODE
435#else
ac32ba44 436 wxList::compatibility_iterator node = m_fonts.GetFirst();
83df96d6
JS
437 while (node)
438 {
09a1dffa 439 wxXFont* f = (wxXFont*) node->GetData();
83df96d6 440 delete f;
09a1dffa 441 node = node->GetNext();
83df96d6
JS
442 }
443 m_fonts.Clear();
2b5f62a0
VZ
444#endif
445}
446
447wxFontRefData::~wxFontRefData()
448{
449 ClearX11Fonts();
83df96d6
JS
450}
451
452// ----------------------------------------------------------------------------
2b5f62a0 453// wxFontRefData SetXXX()
83df96d6
JS
454// ----------------------------------------------------------------------------
455
2b5f62a0 456void wxFontRefData::SetPointSize(int pointSize)
83df96d6 457{
cb5ec61f
VZ
458 // NB: Pango doesn't support point sizes less than 1
459 m_pointSize = pointSize == wxDEFAULT || pointSize < 1 ? wxDEFAULT_FONT_SIZE
460 : pointSize;
2b5f62a0
VZ
461
462#if wxUSE_UNICODE
cb5ec61f 463 m_nativeFontInfo.SetPointSize(m_pointSize);
2b5f62a0
VZ
464#endif
465}
466
0c14b6c3 467void wxFontRefData::SetFamily(wxFontFamily family)
2b5f62a0
VZ
468{
469 m_family = family;
470
471 // TODO: what are we supposed to do with m_nativeFontInfo here?
472}
473
0c14b6c3 474void wxFontRefData::SetStyle(wxFontStyle style)
2b5f62a0
VZ
475{
476 m_style = style;
477
478#if wxUSE_UNICODE
479 // Get native info
480 PangoFontDescription *desc = m_nativeFontInfo.description;
481
482 switch ( style )
483 {
484 case wxFONTSTYLE_ITALIC:
485 pango_font_description_set_style( desc, PANGO_STYLE_ITALIC );
486 break;
487 case wxFONTSTYLE_SLANT:
488 pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE );
489 break;
490 default:
9a83f860 491 wxFAIL_MSG( wxT("unknown font style") );
2b5f62a0
VZ
492 // fall through
493 case wxFONTSTYLE_NORMAL:
494 pango_font_description_set_style( desc, PANGO_STYLE_NORMAL );
495 break;
496 }
497#endif
498}
499
0c14b6c3 500void wxFontRefData::SetWeight(wxFontWeight weight)
2b5f62a0
VZ
501{
502 m_weight = weight;
503}
504
505void wxFontRefData::SetUnderlined(bool underlined)
506{
507 m_underlined = underlined;
83df96d6 508
2b5f62a0 509 // the XLFD doesn't have "underlined" field anyhow
83df96d6
JS
510}
511
85ab460e 512bool wxFontRefData::SetFaceName(const wxString& facename)
2b5f62a0
VZ
513{
514 m_faceName = facename;
85ab460e 515 return true;
2b5f62a0
VZ
516}
517
518void wxFontRefData::SetEncoding(wxFontEncoding encoding)
519{
520 m_encoding = encoding;
521}
522
523void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
524{
525 // previously cached fonts shouldn't be used
526 ClearX11Fonts();
527
528 m_nativeFontInfo = info;
529
530 // set all the other font parameters from the native font info
531 InitFromNative();
532}
533
534// ----------------------------------------------------------------------------
535// wxFont
536// ----------------------------------------------------------------------------
537
2b5f62a0
VZ
538wxFont::wxFont(const wxNativeFontInfo& info)
539{
2b5f62a0
VZ
540#if wxUSE_UNICODE
541 Create( info.GetPointSize(),
542 info.GetFamily(),
543 info.GetStyle(),
544 info.GetWeight(),
545 info.GetUnderlined(),
546 info.GetFaceName(),
547 info.GetEncoding() );
548#else
549 (void) Create(info.GetXFontName());
550#endif
551}
552
83df96d6 553bool wxFont::Create(int pointSize,
0c14b6c3
FM
554 wxFontFamily family,
555 wxFontStyle style,
556 wxFontWeight weight,
83df96d6
JS
557 bool underlined,
558 const wxString& faceName,
559 wxFontEncoding encoding)
560{
561 UnRef();
9045ad9d 562
83df96d6
JS
563 m_refData = new wxFontRefData(pointSize, family, style, weight,
564 underlined, faceName, encoding);
565
7520f3da 566 return true;
83df96d6
JS
567}
568
9045ad9d
VZ
569#if !wxUSE_UNICODE
570
83df96d6
JS
571bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
572{
573 if( !fontname )
574 {
575 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT);
7520f3da 576 return true;
83df96d6
JS
577 }
578
579 m_refData = new wxFontRefData();
580
581 M_FONTDATA->m_nativeFontInfo.SetXFontName(fontname); // X font name
582
583 wxString tmp;
584
585 wxStringTokenizer tn( fontname, wxT("-") );
586
587 tn.GetNextToken(); // skip initial empty token
588 tn.GetNextToken(); // foundry
589
590
591 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
592
593 tmp = tn.GetNextToken().MakeUpper(); // weight
ae391295
JJ
594 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
595 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
596 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
597 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
598 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
83df96d6 599
ae391295
JJ
600 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxFONTWEIGHT_LIGHT;
601 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxFONTWEIGHT_LIGHT;
83df96d6
JS
602
603 tmp = tn.GetNextToken().MakeUpper(); // slant
ae391295
JJ
604 if (tmp == wxT("I")) M_FONTDATA->m_style = wxFONTSTYLE_ITALIC;
605 if (tmp == wxT("O")) M_FONTDATA->m_style = wxFONTSTYLE_ITALIC;
83df96d6
JS
606
607 tn.GetNextToken(); // set width
608 tn.GetNextToken(); // add. style
609 tn.GetNextToken(); // pixel size
610
611 tmp = tn.GetNextToken(); // pointsize
612 if (tmp != wxT("*"))
613 {
614 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
615 M_FONTDATA->m_pointSize = (int)(num / 10);
616 }
617
618 tn.GetNextToken(); // x-res
619 tn.GetNextToken(); // y-res
620
621 tmp = tn.GetNextToken().MakeUpper(); // spacing
622
623 if (tmp == wxT("M"))
ae391295 624 M_FONTDATA->m_family = wxFONTFAMILY_MODERN;
83df96d6 625 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
ae391295 626 M_FONTDATA->m_family = wxFONTFAMILY_ROMAN;
83df96d6 627 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
ae391295 628 M_FONTDATA->m_family = wxFONTFAMILY_SWISS;
83df96d6 629 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
ae391295 630 M_FONTDATA->m_family = wxFONTFAMILY_TELETYPE;
83df96d6 631 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
ae391295 632 M_FONTDATA->m_family = wxFONTFAMILY_DECORATIVE;
83df96d6 633 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
ae391295 634 M_FONTDATA->m_family = wxFONTFAMILY_SCRIPT;
83df96d6
JS
635
636 tn.GetNextToken(); // avg width
637
638 // deal with font encoding
639 M_FONTDATA->m_encoding = enc;
640 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
641 {
642 wxString registry = tn.GetNextToken().MakeUpper(),
643 encoding = tn.GetNextToken().MakeUpper();
644
9a83f860 645 if ( registry == wxT("ISO8859") )
83df96d6
JS
646 {
647 int cp;
648 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
649 {
650 M_FONTDATA->m_encoding =
651 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
652 }
653 }
9a83f860 654 else if ( registry == wxT("MICROSOFT") )
83df96d6
JS
655 {
656 int cp;
657 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
658 {
659 M_FONTDATA->m_encoding =
660 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
661 }
662 }
9a83f860 663 else if ( registry == wxT("KOI8") )
83df96d6
JS
664 {
665 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
666 }
667 //else: unknown encoding - may be give a warning here?
668 else
7520f3da 669 return false;
83df96d6 670 }
7520f3da 671 return true;
83df96d6 672}
9045ad9d 673#endif // !wxUSE_UNICODE
83df96d6
JS
674
675wxFont::~wxFont()
676{
677}
678
8f884a0d
VZ
679wxGDIRefData *wxFont::CreateGDIRefData() const
680{
681 return new wxFontRefData;
682}
683
684wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
685{
5c33522f 686 return new wxFontRefData(*static_cast<const wxFontRefData *>(data));
8f884a0d
VZ
687}
688
83df96d6
JS
689// ----------------------------------------------------------------------------
690// change the font attributes
691// ----------------------------------------------------------------------------
692
693void wxFont::Unshare()
694{
695 // Don't change shared data
696 if (!m_refData)
697 {
698 m_refData = new wxFontRefData();
699 }
700 else
701 {
702 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
703 UnRef();
704 m_refData = ref;
705 }
706}
707
2b5f62a0
VZ
708// ----------------------------------------------------------------------------
709// accessors
710// ----------------------------------------------------------------------------
711
712int wxFont::GetPointSize() const
83df96d6 713{
a1b806b9 714 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
83df96d6 715
2b5f62a0 716 return M_FONTDATA->m_pointSize;
83df96d6
JS
717}
718
2b5f62a0 719wxString wxFont::GetFaceName() const
83df96d6 720{
a1b806b9 721 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
83df96d6 722
2b5f62a0 723 return M_FONTDATA->m_faceName;
83df96d6
JS
724}
725
59b7da02 726wxFontFamily wxFont::DoGetFamily() const
83df96d6 727{
2b5f62a0 728 return M_FONTDATA->m_family;
83df96d6
JS
729}
730
0c14b6c3 731wxFontStyle wxFont::GetStyle() const
83df96d6 732{
a1b806b9 733 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX, wxT("invalid font") );
83df96d6 734
2b5f62a0 735 return M_FONTDATA->m_style;
83df96d6
JS
736}
737
0c14b6c3 738wxFontWeight wxFont::GetWeight() const
83df96d6 739{
a1b806b9 740 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") );
83df96d6 741
2b5f62a0 742 return M_FONTDATA->m_weight;
83df96d6
JS
743}
744
2b5f62a0 745bool wxFont::GetUnderlined() const
83df96d6 746{
a1b806b9 747 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
83df96d6 748
2b5f62a0 749 return M_FONTDATA->m_underlined;
83df96d6
JS
750}
751
2b5f62a0 752wxFontEncoding wxFont::GetEncoding() const
83df96d6 753{
a1b806b9 754 wxCHECK_MSG( IsOk(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
83df96d6 755
2b5f62a0 756 return M_FONTDATA->m_encoding;
83df96d6
JS
757}
758
3bf5a59b 759const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
2b5f62a0 760{
a1b806b9 761 wxCHECK_MSG( IsOk(), NULL, wxT("invalid font") );
2b5f62a0
VZ
762
763#if wxUSE_UNICODE
764#else
765 if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
766 GetInternalFont();
767#endif
768
3bf5a59b 769 return &(M_FONTDATA->m_nativeFontInfo);
2b5f62a0
VZ
770}
771
772bool wxFont::IsFixedWidth() const
773{
a1b806b9 774 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
2b5f62a0
VZ
775
776#if wxUSE_UNICODE
a371f703 777 return wxFontBase::IsFixedWidth();
2b5f62a0
VZ
778#else
779 // Robert, is this right? HasNativeFont doesn't exist.
7520f3da 780 if ( true )
2b5f62a0
VZ
781 // if ( M_FONTDATA->HasNativeFont() )
782 {
783 // the monospace fonts are supposed to have "M" in the spacing field
784 wxString spacing = M_FONTDATA->
785 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
786
9a83f860 787 return spacing.Upper() == wxT('M');
2b5f62a0 788 }
a371f703
JJ
789 // Unreaceable code for now
790 // return wxFontBase::IsFixedWidth();
2b5f62a0 791#endif
83df96d6 792
83df96d6
JS
793}
794
795// ----------------------------------------------------------------------------
2b5f62a0 796// change font attributes
83df96d6
JS
797// ----------------------------------------------------------------------------
798
2b5f62a0 799void wxFont::SetPointSize(int pointSize)
83df96d6 800{
2b5f62a0
VZ
801 Unshare();
802
803 M_FONTDATA->SetPointSize(pointSize);
83df96d6
JS
804}
805
0c14b6c3 806void wxFont::SetFamily(wxFontFamily family)
83df96d6 807{
2b5f62a0 808 Unshare();
83df96d6 809
2b5f62a0 810 M_FONTDATA->SetFamily(family);
83df96d6
JS
811}
812
0c14b6c3 813void wxFont::SetStyle(wxFontStyle style)
83df96d6 814{
2b5f62a0 815 Unshare();
83df96d6 816
2b5f62a0 817 M_FONTDATA->SetStyle(style);
83df96d6
JS
818}
819
0c14b6c3 820void wxFont::SetWeight(wxFontWeight weight)
83df96d6 821{
2b5f62a0 822 Unshare();
83df96d6 823
2b5f62a0 824 M_FONTDATA->SetWeight(weight);
83df96d6
JS
825}
826
85ab460e 827bool wxFont::SetFaceName(const wxString& faceName)
83df96d6 828{
2b5f62a0 829 Unshare();
83df96d6 830
85ab460e
VZ
831 return M_FONTDATA->SetFaceName(faceName) &&
832 wxFontBase::SetFaceName(faceName);
83df96d6
JS
833}
834
2b5f62a0 835void wxFont::SetUnderlined(bool underlined)
83df96d6 836{
2b5f62a0 837 Unshare();
83df96d6 838
2b5f62a0 839 M_FONTDATA->SetUnderlined(underlined);
83df96d6
JS
840}
841
2b5f62a0 842void wxFont::SetEncoding(wxFontEncoding encoding)
83df96d6 843{
2b5f62a0 844 Unshare();
83df96d6 845
2b5f62a0 846 M_FONTDATA->SetEncoding(encoding);
83df96d6
JS
847}
848
9045ad9d 849void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
83df96d6 850{
2b5f62a0 851 Unshare();
83df96d6 852
2b5f62a0
VZ
853 M_FONTDATA->SetNativeFontInfo( info );
854}
83df96d6 855
8462a84b 856#if !wxUSE_UNICODE
2b5f62a0 857
83df96d6 858// ----------------------------------------------------------------------------
2b5f62a0 859// X11 implementation
83df96d6
JS
860// ----------------------------------------------------------------------------
861
862// Find an existing, or create a new, XFontStruct
863// based on this wxFont and the given scale. Append the
864// font to list in the private data for future reference.
865wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const
866{
a1b806b9 867 if ( !IsOk() )
d3b9f782 868 return NULL;
83df96d6
JS
869
870 long intScale = long(scale * 100.0 + 0.5); // key for wxXFont
871 int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100;
872
873 // search existing fonts first
ac32ba44 874 wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst();
83df96d6
JS
875 while (node)
876 {
09a1dffa 877 wxXFont* f = (wxXFont*) node->GetData();
83df96d6
JS
878 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
879 return f;
09a1dffa 880 node = node->GetNext();
83df96d6
JS
881 }
882
69ffc7b2 883 wxString xFontName = M_FONTDATA->m_nativeFontInfo.GetXFontName();
97bdb4c8
JS
884 if (xFontName == "-*-*-*-*-*--*-*-*-*-*-*-*-*")
885 // wxFont constructor not called with native font info parameter => take M_FONTDATA values
886 xFontName.Clear();
7520f3da 887
83df96d6
JS
888 // not found, create a new one
889 XFontStruct *font = (XFontStruct *)
890 wxLoadQueryNearestFont(pointSize,
891 M_FONTDATA->m_family,
892 M_FONTDATA->m_style,
893 M_FONTDATA->m_weight,
894 M_FONTDATA->m_underlined,
895 wxT(""),
69ffc7b2
JS
896 M_FONTDATA->m_encoding,
897 & xFontName);
83df96d6
JS
898
899 if ( !font )
900 {
901 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
902
d3b9f782 903 return NULL;
83df96d6
JS
904 }
905
906 wxXFont* f = new wxXFont;
907 f->m_fontStruct = (WXFontStructPtr)font;
908 f->m_display = ( display ? display : wxGetDisplay() );
909 f->m_scale = intScale;
83df96d6
JS
910 M_FONTDATA->m_fonts.Append(f);
911
912 return f;
913}
914
915WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const
916{
917 wxXFont* f = GetInternalFont(scale, display);
918
919 return (f ? f->m_fontStruct : (WXFontStructPtr) 0);
920}
921
8462a84b 922#endif // !wxUSE_UNICODE