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