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