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