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