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