wx/wxprec.h already includes wx/defs.h (with other minor cleaning).
[wxWidgets.git] / src / x11 / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/font.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 // ============================================================================
16 // declarations
17 // ============================================================================
18
19 // ----------------------------------------------------------------------------
20 // headers
21 // ----------------------------------------------------------------------------
22
23 #ifdef __VMS
24 #pragma message disable nosimpint
25 #include "wx/vms_x_fix.h"
26 #endif
27
28 #ifdef __VMS
29 #pragma message enable nosimpint
30 #endif
31
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"
39
40 #include "wx/x11/private.h"
41
42 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
43
44 // ----------------------------------------------------------------------------
45 // constants
46 // ----------------------------------------------------------------------------
47
48 // the default size (in points) for the fonts
49 static const int wxDEFAULT_FONT_SIZE = 12;
50
51
52 #if wxUSE_UNICODE
53 #else
54 // ----------------------------------------------------------------------------
55 // wxXFont
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
60 class wxXFont : public wxObject
61 {
62 public:
63 wxXFont();
64 ~wxXFont();
65
66 WXFontStructPtr m_fontStruct; // XFontStruct
67 WXDisplay* m_display; // XDisplay
68 int m_scale; // Scale * 100
69 };
70
71 wxXFont::wxXFont()
72 {
73 m_fontStruct = (WXFontStructPtr) 0;
74 m_display = (WXDisplay*) 0;
75 m_scale = 100;
76 }
77
78 wxXFont::~wxXFont()
79 {
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);
84 }
85 #endif
86
87 // ----------------------------------------------------------------------------
88 // wxFontRefData
89 // ----------------------------------------------------------------------------
90
91 class wxFontRefData: public wxObjectRefData
92 {
93 friend class wxFont;
94
95 public:
96 wxFontRefData(int size = wxDEFAULT,
97 int family = wxDEFAULT,
98 int style = wxDEFAULT,
99 int weight = wxDEFAULT,
100 bool underlined = false,
101 const wxString& faceName = wxEmptyString,
102 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
103
104 // copy cstr
105 wxFontRefData(const wxFontRefData& data);
106
107 // from XFLD
108 wxFontRefData(const wxString& fontname);
109
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
123 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
124 bool GetNoAntiAliasing() const { return m_noAA; }
125
126 // and this one also modifies all the other font data fields
127 void SetNativeFontInfo(const wxNativeFontInfo& info);
128
129 protected:
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
139 // set all fields from (already initialized and valid) m_nativeFontInfo
140 void InitFromNative();
141
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;
149 wxFontEncoding m_encoding; // Unused in Unicode mode
150 bool m_noAA; // No anti-aliasing
151
152 wxNativeFontInfo m_nativeFontInfo;
153
154 void ClearX11Fonts();
155
156 #if wxUSE_UNICODE
157 #else
158 // A list of wxXFonts
159 wxList m_fonts;
160 #endif
161 };
162
163 // ----------------------------------------------------------------------------
164 // wxFontRefData
165 // ----------------------------------------------------------------------------
166
167 void wxFontRefData::Init(int pointSize,
168 int family,
169 int style,
170 int weight,
171 bool underlined,
172 const wxString& faceName,
173 wxFontEncoding encoding)
174 {
175 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
176
177 m_faceName = faceName;
178
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;
191
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;
203 case wxFONTFAMILY_ROMAN:
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
216 void wxFontRefData::InitFromNative()
217 {
218 m_noAA = false;
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 ) );
226
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 }
272 else
273 {
274 m_family = wxFONTFAMILY_UNKNOWN;
275 }
276
277 // Pango description are never underlined (?)
278 m_underlined = false;
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;
317
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 }
328 else
329 {
330 m_pointSize = wxDEFAULT_FONT_SIZE;
331 }
332
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...
348 m_underlined = false;
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
381 }
382
383 wxFontRefData::wxFontRefData( const wxFontRefData& data )
384 : wxObjectRefData()
385 {
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;
397
398 m_nativeFontInfo = data.m_nativeFontInfo;
399 }
400
401 wxFontRefData::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
409 wxFontRefData::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
421 void wxFontRefData::ClearX11Fonts()
422 {
423 #if wxUSE_UNICODE
424 #else
425 wxList::compatibility_iterator node = m_fonts.GetFirst();
426 while (node)
427 {
428 wxXFont* f = (wxXFont*) node->GetData();
429 delete f;
430 node = node->GetNext();
431 }
432 m_fonts.Clear();
433 #endif
434 }
435
436 wxFontRefData::~wxFontRefData()
437 {
438 ClearX11Fonts();
439 }
440
441 // ----------------------------------------------------------------------------
442 // wxFontRefData SetXXX()
443 // ----------------------------------------------------------------------------
444
445 void wxFontRefData::SetPointSize(int pointSize)
446 {
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
457 void wxFontRefData::SetFamily(int family)
458 {
459 m_family = family;
460
461 // TODO: what are we supposed to do with m_nativeFontInfo here?
462 }
463
464 void 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
490 void wxFontRefData::SetWeight(int weight)
491 {
492 m_weight = weight;
493 }
494
495 void wxFontRefData::SetUnderlined(bool underlined)
496 {
497 m_underlined = underlined;
498
499 // the XLFD doesn't have "underlined" field anyhow
500 }
501
502 void wxFontRefData::SetFaceName(const wxString& facename)
503 {
504 m_faceName = facename;
505 }
506
507 void wxFontRefData::SetEncoding(wxFontEncoding encoding)
508 {
509 m_encoding = encoding;
510 }
511
512 void 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
527 wxFont::wxFont(const wxNativeFontInfo& info)
528 {
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
542 bool 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();
551
552 m_refData = new wxFontRefData(pointSize, family, style, weight,
553 underlined, faceName, encoding);
554
555 return true;
556 }
557
558 #if !wxUSE_UNICODE
559
560 bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
561 {
562 if( !fontname )
563 {
564 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT);
565 return true;
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
658 return false;
659 }
660 return true;
661 }
662 #endif // !wxUSE_UNICODE
663
664 wxFont::~wxFont()
665 {
666 }
667
668 // ----------------------------------------------------------------------------
669 // change the font attributes
670 // ----------------------------------------------------------------------------
671
672 void 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
687 // ----------------------------------------------------------------------------
688 // accessors
689 // ----------------------------------------------------------------------------
690
691 int wxFont::GetPointSize() const
692 {
693 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
694
695 return M_FONTDATA->m_pointSize;
696 }
697
698 wxString wxFont::GetFaceName() const
699 {
700 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
701
702 return M_FONTDATA->m_faceName;
703 }
704
705 int wxFont::GetFamily() const
706 {
707 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
708
709 return M_FONTDATA->m_family;
710 }
711
712 int wxFont::GetStyle() const
713 {
714 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
715
716 return M_FONTDATA->m_style;
717 }
718
719 int wxFont::GetWeight() const
720 {
721 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
722
723 return M_FONTDATA->m_weight;
724 }
725
726 bool wxFont::GetUnderlined() const
727 {
728 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
729
730 return M_FONTDATA->m_underlined;
731 }
732
733 wxFontEncoding wxFont::GetEncoding() const
734 {
735 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
736
737 return M_FONTDATA->m_encoding;
738 }
739
740 bool wxFont::GetNoAntiAliasing() const
741 {
742 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
743
744 return M_FONTDATA->m_noAA;
745 }
746
747 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
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
757 return &(M_FONTDATA->m_nativeFontInfo);
758 }
759
760 bool wxFont::IsFixedWidth() const
761 {
762 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
763
764 #if wxUSE_UNICODE
765 return wxFontBase::IsFixedWidth();
766 #else
767 // Robert, is this right? HasNativeFont doesn't exist.
768 if ( true )
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 }
777 // Unreaceable code for now
778 // return wxFontBase::IsFixedWidth();
779 #endif
780
781 }
782
783 // ----------------------------------------------------------------------------
784 // change font attributes
785 // ----------------------------------------------------------------------------
786
787 void wxFont::SetPointSize(int pointSize)
788 {
789 Unshare();
790
791 M_FONTDATA->SetPointSize(pointSize);
792 }
793
794 void wxFont::SetFamily(int family)
795 {
796 Unshare();
797
798 M_FONTDATA->SetFamily(family);
799 }
800
801 void wxFont::SetStyle(int style)
802 {
803 Unshare();
804
805 M_FONTDATA->SetStyle(style);
806 }
807
808 void wxFont::SetWeight(int weight)
809 {
810 Unshare();
811
812 M_FONTDATA->SetWeight(weight);
813 }
814
815 void wxFont::SetFaceName(const wxString& faceName)
816 {
817 Unshare();
818
819 M_FONTDATA->SetFaceName(faceName);
820 }
821
822 void wxFont::SetUnderlined(bool underlined)
823 {
824 Unshare();
825
826 M_FONTDATA->SetUnderlined(underlined);
827 }
828
829 void wxFont::SetEncoding(wxFontEncoding encoding)
830 {
831 Unshare();
832
833 M_FONTDATA->SetEncoding(encoding);
834 }
835
836 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
837 {
838 Unshare();
839
840 M_FONTDATA->SetNativeFontInfo( info );
841 }
842
843 void wxFont::SetNoAntiAliasing( bool no )
844 {
845 Unshare();
846
847 M_FONTDATA->SetNoAntiAliasing( no );
848 }
849
850 #if wxUSE_UNICODE
851 #else
852
853 // ----------------------------------------------------------------------------
854 // X11 implementation
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.
860 wxXFont* 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
869 wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst();
870 while (node)
871 {
872 wxXFont* f = (wxXFont*) node->GetData();
873 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
874 return f;
875 node = node->GetNext();
876 }
877
878 wxString xFontName = M_FONTDATA->m_nativeFontInfo.GetXFontName();
879 if (xFontName == "-*-*-*-*-*--*-*-*-*-*-*-*-*")
880 // wxFont constructor not called with native font info parameter => take M_FONTDATA values
881 xFontName.Clear();
882
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(""),
891 M_FONTDATA->m_encoding,
892 & xFontName);
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;
905 M_FONTDATA->m_fonts.Append(f);
906
907 return f;
908 }
909
910 WXFontStructPtr 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
917 #endif