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