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