]> git.saurik.com Git - wxWidgets.git/blob - src/x11/font.cpp
fix combobox dropdown position in RTL (patch 1623127)
[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/font.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/string.h"
36 #include "wx/utils.h" // for wxGetDisplay()
37 #include "wx/settings.h"
38 #include "wx/gdicmn.h"
39 #endif
40
41 #include "wx/fontutil.h" // for wxNativeFontInfo
42 #include "wx/tokenzr.h"
43
44 #include "wx/x11/private.h"
45
46 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // the default size (in points) for the fonts
53 static const int wxDEFAULT_FONT_SIZE = 12;
54
55
56 #if wxUSE_UNICODE
57 #else
58 // ----------------------------------------------------------------------------
59 // wxXFont
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
64 class wxXFont : public wxObject
65 {
66 public:
67 wxXFont();
68 virtual ~wxXFont();
69
70 WXFontStructPtr m_fontStruct; // XFontStruct
71 WXDisplay* m_display; // XDisplay
72 int m_scale; // Scale * 100
73 };
74
75 wxXFont::wxXFont()
76 {
77 m_fontStruct = (WXFontStructPtr) 0;
78 m_display = (WXDisplay*) 0;
79 m_scale = 100;
80 }
81
82 wxXFont::~wxXFont()
83 {
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);
88 }
89 #endif
90
91 // ----------------------------------------------------------------------------
92 // wxFontRefData
93 // ----------------------------------------------------------------------------
94
95 class wxFontRefData: public wxObjectRefData
96 {
97 friend class wxFont;
98
99 public:
100 wxFontRefData(int size = wxDEFAULT,
101 int family = wxDEFAULT,
102 int style = wxDEFAULT,
103 int weight = wxDEFAULT,
104 bool underlined = false,
105 const wxString& faceName = wxEmptyString,
106 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
107
108 // copy cstr
109 wxFontRefData(const wxFontRefData& data);
110
111 // from XFLD
112 wxFontRefData(const wxString& fontname);
113
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);
124 bool SetFaceName(const wxString& facename);
125 void SetEncoding(wxFontEncoding encoding);
126
127 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
128 bool GetNoAntiAliasing() const { return m_noAA; }
129
130 // and this one also modifies all the other font data fields
131 void SetNativeFontInfo(const wxNativeFontInfo& info);
132
133 protected:
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
143 // set all fields from (already initialized and valid) m_nativeFontInfo
144 void InitFromNative();
145
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;
153 wxFontEncoding m_encoding; // Unused in Unicode mode
154 bool m_noAA; // No anti-aliasing
155
156 wxNativeFontInfo m_nativeFontInfo;
157
158 void ClearX11Fonts();
159
160 #if wxUSE_UNICODE
161 #else
162 // A list of wxXFonts
163 wxList m_fonts;
164 #endif
165 };
166
167 #define M_FONTDATA ((wxFontRefData*)m_refData)
168
169 // ----------------------------------------------------------------------------
170 // wxFontRefData
171 // ----------------------------------------------------------------------------
172
173 void wxFontRefData::Init(int pointSize,
174 int family,
175 int style,
176 int weight,
177 bool underlined,
178 const wxString& faceName,
179 wxFontEncoding encoding)
180 {
181 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
182
183 m_faceName = faceName;
184
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;
197
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;
209 case wxFONTFAMILY_ROMAN:
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
222 void wxFontRefData::InitFromNative()
223 {
224 m_noAA = false;
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 ) );
232
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
248 // Not defined in some Pango versions
249 #define wxPANGO_WEIGHT_SEMIBOLD 600
250
251 switch (pango_font_description_get_weight( desc ))
252 {
253 case PANGO_WEIGHT_ULTRALIGHT:
254 case PANGO_WEIGHT_LIGHT:
255 m_weight = wxFONTWEIGHT_LIGHT;
256 break;
257
258 default:
259 wxFAIL_MSG(_T("unknown Pango font weight"));
260 // fall through
261
262 case PANGO_WEIGHT_NORMAL:
263 m_weight = wxFONTWEIGHT_NORMAL;
264 break;
265
266 case wxPANGO_WEIGHT_SEMIBOLD:
267 case PANGO_WEIGHT_BOLD:
268 case PANGO_WEIGHT_ULTRABOLD:
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 }
282 else
283 {
284 m_family = wxFONTFAMILY_UNKNOWN;
285 }
286
287 // Pango description are never underlined (?)
288 m_underlined = false;
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;
327
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 }
338 else
339 {
340 m_pointSize = wxDEFAULT_FONT_SIZE;
341 }
342
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...
358 m_underlined = false;
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
391 }
392
393 wxFontRefData::wxFontRefData( const wxFontRefData& data )
394 : wxObjectRefData()
395 {
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;
407
408 m_nativeFontInfo = data.m_nativeFontInfo;
409 }
410
411 wxFontRefData::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
419 wxFontRefData::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
431 void wxFontRefData::ClearX11Fonts()
432 {
433 #if wxUSE_UNICODE
434 #else
435 wxList::compatibility_iterator node = m_fonts.GetFirst();
436 while (node)
437 {
438 wxXFont* f = (wxXFont*) node->GetData();
439 delete f;
440 node = node->GetNext();
441 }
442 m_fonts.Clear();
443 #endif
444 }
445
446 wxFontRefData::~wxFontRefData()
447 {
448 ClearX11Fonts();
449 }
450
451 // ----------------------------------------------------------------------------
452 // wxFontRefData SetXXX()
453 // ----------------------------------------------------------------------------
454
455 void wxFontRefData::SetPointSize(int pointSize)
456 {
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
467 void wxFontRefData::SetFamily(int family)
468 {
469 m_family = family;
470
471 // TODO: what are we supposed to do with m_nativeFontInfo here?
472 }
473
474 void 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
500 void wxFontRefData::SetWeight(int weight)
501 {
502 m_weight = weight;
503 }
504
505 void wxFontRefData::SetUnderlined(bool underlined)
506 {
507 m_underlined = underlined;
508
509 // the XLFD doesn't have "underlined" field anyhow
510 }
511
512 bool wxFontRefData::SetFaceName(const wxString& facename)
513 {
514 m_faceName = facename;
515 return true;
516 }
517
518 void wxFontRefData::SetEncoding(wxFontEncoding encoding)
519 {
520 m_encoding = encoding;
521 }
522
523 void 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
538 wxFont::wxFont(const wxNativeFontInfo& info)
539 {
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
553 bool 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();
562
563 m_refData = new wxFontRefData(pointSize, family, style, weight,
564 underlined, faceName, encoding);
565
566 return true;
567 }
568
569 #if !wxUSE_UNICODE
570
571 bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
572 {
573 if( !fontname )
574 {
575 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT);
576 return true;
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
669 return false;
670 }
671 return true;
672 }
673 #endif // !wxUSE_UNICODE
674
675 wxFont::~wxFont()
676 {
677 }
678
679 // ----------------------------------------------------------------------------
680 // change the font attributes
681 // ----------------------------------------------------------------------------
682
683 void 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
698 // ----------------------------------------------------------------------------
699 // accessors
700 // ----------------------------------------------------------------------------
701
702 int wxFont::GetPointSize() const
703 {
704 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
705
706 return M_FONTDATA->m_pointSize;
707 }
708
709 wxString wxFont::GetFaceName() const
710 {
711 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
712
713 return M_FONTDATA->m_faceName;
714 }
715
716 int wxFont::GetFamily() const
717 {
718 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
719
720 return M_FONTDATA->m_family;
721 }
722
723 int wxFont::GetStyle() const
724 {
725 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
726
727 return M_FONTDATA->m_style;
728 }
729
730 int wxFont::GetWeight() const
731 {
732 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
733
734 return M_FONTDATA->m_weight;
735 }
736
737 bool wxFont::GetUnderlined() const
738 {
739 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
740
741 return M_FONTDATA->m_underlined;
742 }
743
744 wxFontEncoding wxFont::GetEncoding() const
745 {
746 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
747
748 return M_FONTDATA->m_encoding;
749 }
750
751 bool wxFont::GetNoAntiAliasing() const
752 {
753 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
754
755 return M_FONTDATA->m_noAA;
756 }
757
758 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
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
768 return &(M_FONTDATA->m_nativeFontInfo);
769 }
770
771 bool wxFont::IsFixedWidth() const
772 {
773 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
774
775 #if wxUSE_UNICODE
776 return wxFontBase::IsFixedWidth();
777 #else
778 // Robert, is this right? HasNativeFont doesn't exist.
779 if ( true )
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 }
788 // Unreaceable code for now
789 // return wxFontBase::IsFixedWidth();
790 #endif
791
792 }
793
794 // ----------------------------------------------------------------------------
795 // change font attributes
796 // ----------------------------------------------------------------------------
797
798 void wxFont::SetPointSize(int pointSize)
799 {
800 Unshare();
801
802 M_FONTDATA->SetPointSize(pointSize);
803 }
804
805 void wxFont::SetFamily(int family)
806 {
807 Unshare();
808
809 M_FONTDATA->SetFamily(family);
810 }
811
812 void wxFont::SetStyle(int style)
813 {
814 Unshare();
815
816 M_FONTDATA->SetStyle(style);
817 }
818
819 void wxFont::SetWeight(int weight)
820 {
821 Unshare();
822
823 M_FONTDATA->SetWeight(weight);
824 }
825
826 bool wxFont::SetFaceName(const wxString& faceName)
827 {
828 Unshare();
829
830 return M_FONTDATA->SetFaceName(faceName) &&
831 wxFontBase::SetFaceName(faceName);
832 }
833
834 void wxFont::SetUnderlined(bool underlined)
835 {
836 Unshare();
837
838 M_FONTDATA->SetUnderlined(underlined);
839 }
840
841 void wxFont::SetEncoding(wxFontEncoding encoding)
842 {
843 Unshare();
844
845 M_FONTDATA->SetEncoding(encoding);
846 }
847
848 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
849 {
850 Unshare();
851
852 M_FONTDATA->SetNativeFontInfo( info );
853 }
854
855 void wxFont::SetNoAntiAliasing( bool no )
856 {
857 Unshare();
858
859 M_FONTDATA->SetNoAntiAliasing( no );
860 }
861
862 #if wxUSE_UNICODE
863 #else
864
865 // ----------------------------------------------------------------------------
866 // X11 implementation
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.
872 wxXFont* 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
881 wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst();
882 while (node)
883 {
884 wxXFont* f = (wxXFont*) node->GetData();
885 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
886 return f;
887 node = node->GetNext();
888 }
889
890 wxString xFontName = M_FONTDATA->m_nativeFontInfo.GetXFontName();
891 if (xFontName == "-*-*-*-*-*--*-*-*-*-*-*-*-*")
892 // wxFont constructor not called with native font info parameter => take M_FONTDATA values
893 xFontName.Clear();
894
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(""),
903 M_FONTDATA->m_encoding,
904 & xFontName);
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;
917 M_FONTDATA->m_fonts.Append(f);
918
919 return f;
920 }
921
922 WXFontStructPtr 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
929 #endif