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