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