]> git.saurik.com Git - wxWidgets.git/blame - src/x11/font.cpp
compilation fix for WXWIN_COMPATIBILITY_2_6==0
[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();
68 ~wxXFont();
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:
249 m_weight = wxFONTWEIGHT_LIGHT;
250 break;
251 case PANGO_WEIGHT_LIGHT:
252 m_weight = wxFONTWEIGHT_LIGHT;
253 break;
254 case PANGO_WEIGHT_NORMAL:
255 m_weight = wxFONTWEIGHT_NORMAL;
256 break;
257 case PANGO_WEIGHT_BOLD:
258 m_weight = wxFONTWEIGHT_BOLD;
259 break;
260 case PANGO_WEIGHT_ULTRABOLD:
261 m_weight = wxFONTWEIGHT_BOLD;
262 break;
263 case PANGO_WEIGHT_HEAVY:
264 m_weight = wxFONTWEIGHT_BOLD;
265 break;
266 }
267
268 if (m_faceName == wxT("monospace"))
269 {
270 m_family = wxFONTFAMILY_TELETYPE;
271 }
272 else if (m_faceName == wxT("sans"))
273 {
274 m_family = wxFONTFAMILY_SWISS;
275 }
83df96d6 276 else
2b5f62a0
VZ
277 {
278 m_family = wxFONTFAMILY_UNKNOWN;
279 }
280
281 // Pango description are never underlined (?)
7520f3da 282 m_underlined = false;
2b5f62a0
VZ
283
284 // Cannot we choose that
285 m_encoding = wxFONTENCODING_SYSTEM;
286#else // X11
287 // get the font parameters from the XLFD
288 // -------------------------------------
289
290 m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY);
291
292 m_weight = wxFONTWEIGHT_NORMAL;
293
294 wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper();
295 if ( !w.empty() && w != _T('*') )
296 {
297 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
298 // and BLACK
299 if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
300 !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
301 wxStrstr(w.c_str() + 1, _T("BOLD")) )
302 {
303 m_weight = wxFONTWEIGHT_BOLD;
304 }
305 else if ( w == _T("LIGHT") || w == _T("THIN") )
306 {
307 m_weight = wxFONTWEIGHT_LIGHT;
308 }
309 }
310
311 switch ( wxToupper(*m_nativeFontInfo.
312 GetXFontComponent(wxXLFD_SLANT).c_str()) )
313 {
314 case _T('I'): // italique
315 m_style = wxFONTSTYLE_ITALIC;
316 break;
317
318 case _T('O'): // oblique
319 m_style = wxFONTSTYLE_SLANT;
320 break;
83df96d6 321
2b5f62a0
VZ
322 default:
323 m_style = wxFONTSTYLE_NORMAL;
324 }
325
326 long ptSize;
327 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
328 {
329 // size in XLFD is in 10 point units
330 m_pointSize = (int)(ptSize / 10);
331 }
83df96d6 332 else
2b5f62a0
VZ
333 {
334 m_pointSize = wxDEFAULT_FONT_SIZE;
335 }
83df96d6 336
2b5f62a0
VZ
337 // examine the spacing: if the font is monospaced, assume wxTELETYPE
338 // family for compatibility with the old code which used it instead of
339 // IsFixedWidth()
340 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') )
341 {
342 m_family = wxFONTFAMILY_TELETYPE;
343 }
344 else // not monospaceed
345 {
346 // don't even try guessing it, it doesn't work for too many fonts
347 // anyhow
348 m_family = wxFONTFAMILY_UNKNOWN;
349 }
350
351 // X fonts are never underlined...
7520f3da 352 m_underlined = false;
2b5f62a0
VZ
353
354 // deal with font encoding
355 wxString
356 registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(),
357 encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper();
358
359 if ( registry == _T("ISO8859") )
360 {
361 int cp;
362 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
363 {
364 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
365 }
366 }
367 else if ( registry == _T("MICROSOFT") )
368 {
369 int cp;
370 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
371 {
372 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
373 }
374 }
375 else if ( registry == _T("KOI8") )
376 {
377 m_encoding = wxFONTENCODING_KOI8;
378 }
379 else // unknown encoding
380 {
381 // may be give a warning here? or use wxFontMapper?
382 m_encoding = wxFONTENCODING_SYSTEM;
383 }
384#endif // Pango/X11
83df96d6
JS
385}
386
2b5f62a0
VZ
387wxFontRefData::wxFontRefData( const wxFontRefData& data )
388 : wxObjectRefData()
83df96d6 389{
2b5f62a0
VZ
390 m_pointSize = data.m_pointSize;
391 m_family = data.m_family;
392 m_style = data.m_style;
393 m_weight = data.m_weight;
394
395 m_underlined = data.m_underlined;
396
397 m_faceName = data.m_faceName;
398 m_encoding = data.m_encoding;
399
400 m_noAA = data.m_noAA;
9045ad9d 401
2b5f62a0
VZ
402 m_nativeFontInfo = data.m_nativeFontInfo;
403}
404
405wxFontRefData::wxFontRefData(int size, int family, int style,
406 int weight, bool underlined,
407 const wxString& faceName,
408 wxFontEncoding encoding)
409{
410 Init(size, family, style, weight, underlined, faceName, encoding);
411}
412
413wxFontRefData::wxFontRefData(const wxString& fontname)
414{
415 // VZ: FromString() should really work in both cases, doesn't it?
416#if wxUSE_UNICODE
417 m_nativeFontInfo.FromString( fontname );
418#else
419 m_nativeFontInfo.SetXFontName(fontname);
420#endif
421
422 InitFromNative();
423}
424
425void wxFontRefData::ClearX11Fonts()
426{
427#if wxUSE_UNICODE
428#else
ac32ba44 429 wxList::compatibility_iterator node = m_fonts.GetFirst();
83df96d6
JS
430 while (node)
431 {
09a1dffa 432 wxXFont* f = (wxXFont*) node->GetData();
83df96d6 433 delete f;
09a1dffa 434 node = node->GetNext();
83df96d6
JS
435 }
436 m_fonts.Clear();
2b5f62a0
VZ
437#endif
438}
439
440wxFontRefData::~wxFontRefData()
441{
442 ClearX11Fonts();
83df96d6
JS
443}
444
445// ----------------------------------------------------------------------------
2b5f62a0 446// wxFontRefData SetXXX()
83df96d6
JS
447// ----------------------------------------------------------------------------
448
2b5f62a0 449void wxFontRefData::SetPointSize(int pointSize)
83df96d6 450{
2b5f62a0
VZ
451 m_pointSize = pointSize;
452
453#if wxUSE_UNICODE
454 // Get native info
455 PangoFontDescription *desc = m_nativeFontInfo.description;
456
457 pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE );
458#endif
459}
460
461void wxFontRefData::SetFamily(int family)
462{
463 m_family = family;
464
465 // TODO: what are we supposed to do with m_nativeFontInfo here?
466}
467
468void wxFontRefData::SetStyle(int style)
469{
470 m_style = style;
471
472#if wxUSE_UNICODE
473 // Get native info
474 PangoFontDescription *desc = m_nativeFontInfo.description;
475
476 switch ( style )
477 {
478 case wxFONTSTYLE_ITALIC:
479 pango_font_description_set_style( desc, PANGO_STYLE_ITALIC );
480 break;
481 case wxFONTSTYLE_SLANT:
482 pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE );
483 break;
484 default:
485 wxFAIL_MSG( _T("unknown font style") );
486 // fall through
487 case wxFONTSTYLE_NORMAL:
488 pango_font_description_set_style( desc, PANGO_STYLE_NORMAL );
489 break;
490 }
491#endif
492}
493
494void wxFontRefData::SetWeight(int weight)
495{
496 m_weight = weight;
497}
498
499void wxFontRefData::SetUnderlined(bool underlined)
500{
501 m_underlined = underlined;
83df96d6 502
2b5f62a0 503 // the XLFD doesn't have "underlined" field anyhow
83df96d6
JS
504}
505
85ab460e 506bool wxFontRefData::SetFaceName(const wxString& facename)
2b5f62a0
VZ
507{
508 m_faceName = facename;
85ab460e 509 return true;
2b5f62a0
VZ
510}
511
512void wxFontRefData::SetEncoding(wxFontEncoding encoding)
513{
514 m_encoding = encoding;
515}
516
517void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
518{
519 // previously cached fonts shouldn't be used
520 ClearX11Fonts();
521
522 m_nativeFontInfo = info;
523
524 // set all the other font parameters from the native font info
525 InitFromNative();
526}
527
528// ----------------------------------------------------------------------------
529// wxFont
530// ----------------------------------------------------------------------------
531
2b5f62a0
VZ
532wxFont::wxFont(const wxNativeFontInfo& info)
533{
2b5f62a0
VZ
534#if wxUSE_UNICODE
535 Create( info.GetPointSize(),
536 info.GetFamily(),
537 info.GetStyle(),
538 info.GetWeight(),
539 info.GetUnderlined(),
540 info.GetFaceName(),
541 info.GetEncoding() );
542#else
543 (void) Create(info.GetXFontName());
544#endif
545}
546
83df96d6
JS
547bool wxFont::Create(int pointSize,
548 int family,
549 int style,
550 int weight,
551 bool underlined,
552 const wxString& faceName,
553 wxFontEncoding encoding)
554{
555 UnRef();
9045ad9d 556
83df96d6
JS
557 m_refData = new wxFontRefData(pointSize, family, style, weight,
558 underlined, faceName, encoding);
559
7520f3da 560 return true;
83df96d6
JS
561}
562
9045ad9d
VZ
563#if !wxUSE_UNICODE
564
83df96d6
JS
565bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
566{
567 if( !fontname )
568 {
569 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT);
7520f3da 570 return true;
83df96d6
JS
571 }
572
573 m_refData = new wxFontRefData();
574
575 M_FONTDATA->m_nativeFontInfo.SetXFontName(fontname); // X font name
576
577 wxString tmp;
578
579 wxStringTokenizer tn( fontname, wxT("-") );
580
581 tn.GetNextToken(); // skip initial empty token
582 tn.GetNextToken(); // foundry
583
584
585 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
586
587 tmp = tn.GetNextToken().MakeUpper(); // weight
588 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
589 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD;
590 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
591 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD;
592 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
593
594 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
595 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
596
597 tmp = tn.GetNextToken().MakeUpper(); // slant
598 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
599 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
600
601 tn.GetNextToken(); // set width
602 tn.GetNextToken(); // add. style
603 tn.GetNextToken(); // pixel size
604
605 tmp = tn.GetNextToken(); // pointsize
606 if (tmp != wxT("*"))
607 {
608 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
609 M_FONTDATA->m_pointSize = (int)(num / 10);
610 }
611
612 tn.GetNextToken(); // x-res
613 tn.GetNextToken(); // y-res
614
615 tmp = tn.GetNextToken().MakeUpper(); // spacing
616
617 if (tmp == wxT("M"))
618 M_FONTDATA->m_family = wxMODERN;
619 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
620 M_FONTDATA->m_family = wxROMAN;
621 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
622 M_FONTDATA->m_family = wxSWISS;
623 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
624 M_FONTDATA->m_family = wxTELETYPE;
625 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
626 M_FONTDATA->m_family = wxDECORATIVE;
627 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
628 M_FONTDATA->m_family = wxSCRIPT;
629
630 tn.GetNextToken(); // avg width
631
632 // deal with font encoding
633 M_FONTDATA->m_encoding = enc;
634 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
635 {
636 wxString registry = tn.GetNextToken().MakeUpper(),
637 encoding = tn.GetNextToken().MakeUpper();
638
639 if ( registry == _T("ISO8859") )
640 {
641 int cp;
642 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
643 {
644 M_FONTDATA->m_encoding =
645 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
646 }
647 }
648 else if ( registry == _T("MICROSOFT") )
649 {
650 int cp;
651 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
652 {
653 M_FONTDATA->m_encoding =
654 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
655 }
656 }
657 else if ( registry == _T("KOI8") )
658 {
659 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
660 }
661 //else: unknown encoding - may be give a warning here?
662 else
7520f3da 663 return false;
83df96d6 664 }
7520f3da 665 return true;
83df96d6 666}
9045ad9d 667#endif // !wxUSE_UNICODE
83df96d6
JS
668
669wxFont::~wxFont()
670{
671}
672
673// ----------------------------------------------------------------------------
674// change the font attributes
675// ----------------------------------------------------------------------------
676
677void wxFont::Unshare()
678{
679 // Don't change shared data
680 if (!m_refData)
681 {
682 m_refData = new wxFontRefData();
683 }
684 else
685 {
686 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
687 UnRef();
688 m_refData = ref;
689 }
690}
691
2b5f62a0
VZ
692// ----------------------------------------------------------------------------
693// accessors
694// ----------------------------------------------------------------------------
695
696int wxFont::GetPointSize() const
83df96d6 697{
2b5f62a0 698 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
83df96d6 699
2b5f62a0 700 return M_FONTDATA->m_pointSize;
83df96d6
JS
701}
702
2b5f62a0 703wxString wxFont::GetFaceName() const
83df96d6 704{
7520f3da 705 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
83df96d6 706
2b5f62a0 707 return M_FONTDATA->m_faceName;
83df96d6
JS
708}
709
2b5f62a0 710int wxFont::GetFamily() const
83df96d6 711{
2b5f62a0 712 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
83df96d6 713
2b5f62a0 714 return M_FONTDATA->m_family;
83df96d6
JS
715}
716
2b5f62a0 717int wxFont::GetStyle() const
83df96d6 718{
2b5f62a0 719 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
83df96d6 720
2b5f62a0 721 return M_FONTDATA->m_style;
83df96d6
JS
722}
723
2b5f62a0 724int wxFont::GetWeight() const
83df96d6 725{
2b5f62a0 726 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
83df96d6 727
2b5f62a0 728 return M_FONTDATA->m_weight;
83df96d6
JS
729}
730
2b5f62a0 731bool wxFont::GetUnderlined() const
83df96d6 732{
7520f3da 733 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
83df96d6 734
2b5f62a0 735 return M_FONTDATA->m_underlined;
83df96d6
JS
736}
737
2b5f62a0 738wxFontEncoding wxFont::GetEncoding() const
83df96d6 739{
2b5f62a0 740 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
83df96d6 741
2b5f62a0 742 return M_FONTDATA->m_encoding;
83df96d6
JS
743}
744
5ac2e80c 745bool wxFont::GetNoAntiAliasing() const
83df96d6 746{
2b5f62a0
VZ
747 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
748
749 return M_FONTDATA->m_noAA;
750}
751
3bf5a59b 752const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
2b5f62a0
VZ
753{
754 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
755
756#if wxUSE_UNICODE
757#else
758 if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
759 GetInternalFont();
760#endif
761
3bf5a59b 762 return &(M_FONTDATA->m_nativeFontInfo);
2b5f62a0
VZ
763}
764
765bool wxFont::IsFixedWidth() const
766{
7520f3da 767 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
2b5f62a0
VZ
768
769#if wxUSE_UNICODE
a371f703 770 return wxFontBase::IsFixedWidth();
2b5f62a0
VZ
771#else
772 // Robert, is this right? HasNativeFont doesn't exist.
7520f3da 773 if ( true )
2b5f62a0
VZ
774 // if ( M_FONTDATA->HasNativeFont() )
775 {
776 // the monospace fonts are supposed to have "M" in the spacing field
777 wxString spacing = M_FONTDATA->
778 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
779
780 return spacing.Upper() == _T('M');
781 }
a371f703
JJ
782 // Unreaceable code for now
783 // return wxFontBase::IsFixedWidth();
2b5f62a0 784#endif
83df96d6 785
83df96d6
JS
786}
787
788// ----------------------------------------------------------------------------
2b5f62a0 789// change font attributes
83df96d6
JS
790// ----------------------------------------------------------------------------
791
2b5f62a0 792void wxFont::SetPointSize(int pointSize)
83df96d6 793{
2b5f62a0
VZ
794 Unshare();
795
796 M_FONTDATA->SetPointSize(pointSize);
83df96d6
JS
797}
798
2b5f62a0 799void wxFont::SetFamily(int family)
83df96d6 800{
2b5f62a0 801 Unshare();
83df96d6 802
2b5f62a0 803 M_FONTDATA->SetFamily(family);
83df96d6
JS
804}
805
2b5f62a0 806void wxFont::SetStyle(int style)
83df96d6 807{
2b5f62a0 808 Unshare();
83df96d6 809
2b5f62a0 810 M_FONTDATA->SetStyle(style);
83df96d6
JS
811}
812
2b5f62a0 813void wxFont::SetWeight(int weight)
83df96d6 814{
2b5f62a0 815 Unshare();
83df96d6 816
2b5f62a0 817 M_FONTDATA->SetWeight(weight);
83df96d6
JS
818}
819
85ab460e 820bool wxFont::SetFaceName(const wxString& faceName)
83df96d6 821{
2b5f62a0 822 Unshare();
83df96d6 823
85ab460e
VZ
824 return M_FONTDATA->SetFaceName(faceName) &&
825 wxFontBase::SetFaceName(faceName);
83df96d6
JS
826}
827
2b5f62a0 828void wxFont::SetUnderlined(bool underlined)
83df96d6 829{
2b5f62a0 830 Unshare();
83df96d6 831
2b5f62a0 832 M_FONTDATA->SetUnderlined(underlined);
83df96d6
JS
833}
834
2b5f62a0 835void wxFont::SetEncoding(wxFontEncoding encoding)
83df96d6 836{
2b5f62a0 837 Unshare();
83df96d6 838
2b5f62a0 839 M_FONTDATA->SetEncoding(encoding);
83df96d6
JS
840}
841
9045ad9d 842void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
83df96d6 843{
2b5f62a0 844 Unshare();
83df96d6 845
2b5f62a0
VZ
846 M_FONTDATA->SetNativeFontInfo( info );
847}
83df96d6 848
2b5f62a0
VZ
849void wxFont::SetNoAntiAliasing( bool no )
850{
851 Unshare();
852
853 M_FONTDATA->SetNoAntiAliasing( no );
83df96d6
JS
854}
855
2b5f62a0
VZ
856#if wxUSE_UNICODE
857#else
858
83df96d6 859// ----------------------------------------------------------------------------
2b5f62a0 860// X11 implementation
83df96d6
JS
861// ----------------------------------------------------------------------------
862
863// Find an existing, or create a new, XFontStruct
864// based on this wxFont and the given scale. Append the
865// font to list in the private data for future reference.
866wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const
867{
868 if ( !Ok() )
869 return (wxXFont *)NULL;
870
871 long intScale = long(scale * 100.0 + 0.5); // key for wxXFont
872 int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100;
873
874 // search existing fonts first
ac32ba44 875 wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst();
83df96d6
JS
876 while (node)
877 {
09a1dffa 878 wxXFont* f = (wxXFont*) node->GetData();
83df96d6
JS
879 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
880 return f;
09a1dffa 881 node = node->GetNext();
83df96d6
JS
882 }
883
69ffc7b2 884 wxString xFontName = M_FONTDATA->m_nativeFontInfo.GetXFontName();
97bdb4c8
JS
885 if (xFontName == "-*-*-*-*-*--*-*-*-*-*-*-*-*")
886 // wxFont constructor not called with native font info parameter => take M_FONTDATA values
887 xFontName.Clear();
7520f3da 888
83df96d6
JS
889 // not found, create a new one
890 XFontStruct *font = (XFontStruct *)
891 wxLoadQueryNearestFont(pointSize,
892 M_FONTDATA->m_family,
893 M_FONTDATA->m_style,
894 M_FONTDATA->m_weight,
895 M_FONTDATA->m_underlined,
896 wxT(""),
69ffc7b2
JS
897 M_FONTDATA->m_encoding,
898 & xFontName);
83df96d6
JS
899
900 if ( !font )
901 {
902 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
903
904 return (wxXFont*) NULL;
905 }
906
907 wxXFont* f = new wxXFont;
908 f->m_fontStruct = (WXFontStructPtr)font;
909 f->m_display = ( display ? display : wxGetDisplay() );
910 f->m_scale = intScale;
83df96d6
JS
911 M_FONTDATA->m_fonts.Append(f);
912
913 return f;
914}
915
916WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const
917{
918 wxXFont* f = GetInternalFont(scale, display);
919
920 return (f ? f->m_fontStruct : (WXFontStructPtr) 0);
921}
922
2b5f62a0 923#endif