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