]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: gtk/font.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ============================================================================ | |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
18 | #ifdef __GNUG__ | |
19 | #pragma implementation "font.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/font.h" | |
23 | #include "wx/fontutil.h" | |
24 | #include "wx/cmndata.h" | |
25 | #include "wx/utils.h" | |
26 | #include "wx/log.h" | |
27 | #include "wx/gdicmn.h" | |
28 | #include "wx/tokenzr.h" | |
29 | #include "wx/settings.h" | |
30 | ||
31 | #include <strings.h> | |
32 | ||
33 | #include "wx/gtk/private.h" | |
34 | #include <gdk/gdkprivate.h> | |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // constants | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | // the default size (in points) for the fonts | |
41 | static const int wxDEFAULT_FONT_SIZE = 12; | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // wxScaledFontList | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | // TODO: replace this with a type safe list or hash!! | |
48 | class wxScaledFontList : public wxList | |
49 | { | |
50 | public: | |
51 | wxScaledFontList() : wxList(wxKEY_INTEGER) { } | |
52 | }; | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // wxFontRefData | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | class wxFontRefData : public wxObjectRefData | |
59 | { | |
60 | public: | |
61 | // from broken down font parameters, also default ctor | |
62 | wxFontRefData(int size = -1, | |
63 | int family = wxFONTFAMILY_DEFAULT, | |
64 | int style = wxFONTSTYLE_NORMAL, | |
65 | int weight = wxFONTWEIGHT_NORMAL, | |
66 | bool underlined = FALSE, | |
67 | const wxString& faceName = wxEmptyString, | |
68 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); | |
69 | ||
70 | // from XFLD | |
71 | wxFontRefData(const wxString& fontname); | |
72 | ||
73 | // copy ctor | |
74 | wxFontRefData( const wxFontRefData& data ); | |
75 | ||
76 | virtual ~wxFontRefData(); | |
77 | ||
78 | // do we have the native font info? | |
79 | bool HasNativeFont() const | |
80 | { | |
81 | #ifdef __WXGTK20__ | |
82 | return TRUE; // ? | |
83 | #else | |
84 | return !m_nativeFontInfo.IsDefault(); | |
85 | #endif | |
86 | } | |
87 | ||
88 | // setters: all of them also take care to modify m_nativeFontInfo if we | |
89 | // have it so as to not lose the information not carried by our fields | |
90 | void SetPointSize(int pointSize); | |
91 | void SetFamily(int family); | |
92 | void SetStyle(int style); | |
93 | void SetWeight(int weight); | |
94 | void SetUnderlined(bool underlined); | |
95 | void SetFaceName(const wxString& facename); | |
96 | void SetEncoding(wxFontEncoding encoding); | |
97 | ||
98 | // debugger helper: shows what the font really is | |
99 | // | |
100 | // VZ: I need this as my gdb either shows wildly wrong values or crashes | |
101 | // when I ask it to "p fontRefData" :-( | |
102 | #if defined(__WXDEBUG__) && !defined(__WXGTK20__) | |
103 | void Dump() const | |
104 | { | |
105 | wxPrintf(_T("%s-%s-%s-%d-%d\n"), | |
106 | m_faceName.c_str(), | |
107 | m_weight == wxFONTWEIGHT_NORMAL | |
108 | ? _T("normal") | |
109 | : m_weight == wxFONTWEIGHT_BOLD | |
110 | ? _T("bold") | |
111 | : _T("light"), | |
112 | m_style == wxFONTSTYLE_NORMAL ? _T("regular") : _T("italic"), | |
113 | m_pointSize, | |
114 | m_encoding); | |
115 | } | |
116 | #endif // Debug | |
117 | ||
118 | protected: | |
119 | // common part of all ctors | |
120 | void Init(int pointSize, | |
121 | int family, | |
122 | int style, | |
123 | int weight, | |
124 | bool underlined, | |
125 | const wxString& faceName, | |
126 | wxFontEncoding encoding); | |
127 | ||
128 | private: | |
129 | #ifndef __WXGTK20__ | |
130 | // the map of font sizes to "GdkFont *" | |
131 | wxScaledFontList m_scaled_xfonts; | |
132 | #endif | |
133 | ||
134 | // the broken down font parameters | |
135 | int m_pointSize; | |
136 | int m_family, | |
137 | m_style, | |
138 | m_weight; | |
139 | bool m_underlined; | |
140 | wxString m_faceName; | |
141 | wxFontEncoding m_encoding; // Unused under GTK 2.0 | |
142 | ||
143 | // The native font info, basicly an XFLD under GTK 1.2 and | |
144 | // the pango font description under GTK 2.0. | |
145 | wxNativeFontInfo m_nativeFontInfo; | |
146 | ||
147 | friend class wxFont; | |
148 | }; | |
149 | ||
150 | // ============================================================================ | |
151 | // wxFontRefData implementation | |
152 | // ============================================================================ | |
153 | ||
154 | // ---------------------------------------------------------------------------- | |
155 | // wxFontRefData creation | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | void wxFontRefData::Init(int pointSize, | |
159 | int family, | |
160 | int style, | |
161 | int weight, | |
162 | bool underlined, | |
163 | const wxString& faceName, | |
164 | wxFontEncoding encoding) | |
165 | { | |
166 | m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family; | |
167 | ||
168 | m_faceName = faceName; | |
169 | ||
170 | // we accept both wxDEFAULT and wxNORMAL here - should we? | |
171 | m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style; | |
172 | m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight; | |
173 | ||
174 | // and here, do we really want to forbid creation of the font of the size | |
175 | // 90 (the value of wxDEFAULT)?? | |
176 | m_pointSize = pointSize == wxDEFAULT || | |
177 | pointSize == -1 ? wxDEFAULT_FONT_SIZE : pointSize; | |
178 | ||
179 | m_underlined = underlined; | |
180 | m_encoding = encoding; | |
181 | ||
182 | #ifdef __WXGTK20__ | |
183 | // Create native font info | |
184 | m_nativeFontInfo.description = pango_font_description_new(); | |
185 | ||
186 | // And set its values | |
187 | switch (m_family) | |
188 | { | |
189 | case wxFONTFAMILY_TELETYPE: | |
190 | pango_font_description_set_family( m_nativeFontInfo.description, "monospace" ); | |
191 | break; | |
192 | case wxFONTFAMILY_SWISS: | |
193 | pango_font_description_set_family( m_nativeFontInfo.description, "serif" ); | |
194 | break; | |
195 | default: | |
196 | pango_font_description_set_family( m_nativeFontInfo.description, "sans" ); | |
197 | break; | |
198 | } | |
199 | SetStyle( m_style ); | |
200 | SetPointSize( m_pointSize ); | |
201 | SetWeight( m_weight ); | |
202 | #endif | |
203 | } | |
204 | ||
205 | wxFontRefData::wxFontRefData( const wxFontRefData& data ) | |
206 | : wxObjectRefData() | |
207 | { | |
208 | m_pointSize = data.m_pointSize; | |
209 | m_family = data.m_family; | |
210 | m_style = data.m_style; | |
211 | m_weight = data.m_weight; | |
212 | ||
213 | m_underlined = data.m_underlined; | |
214 | ||
215 | m_faceName = data.m_faceName; | |
216 | m_encoding = data.m_encoding; | |
217 | ||
218 | m_nativeFontInfo = data.m_nativeFontInfo; | |
219 | } | |
220 | ||
221 | wxFontRefData::wxFontRefData(int size, int family, int style, | |
222 | int weight, bool underlined, | |
223 | const wxString& faceName, | |
224 | wxFontEncoding encoding) | |
225 | { | |
226 | Init(size, family, style, weight, underlined, faceName, encoding); | |
227 | } | |
228 | ||
229 | wxFontRefData::wxFontRefData(const wxString& fontname) | |
230 | { | |
231 | #ifdef __WXGTK20__ | |
232 | m_nativeFontInfo.FromString( fontname ); | |
233 | ||
234 | // Get native info | |
235 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
236 | ||
237 | // init fields | |
238 | m_faceName = wxGTK_CONV_BACK( pango_font_description_get_family( desc ) ); | |
239 | ||
240 | m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE; | |
241 | ||
242 | switch (pango_font_description_get_style( desc )) | |
243 | { | |
244 | case PANGO_STYLE_NORMAL: | |
245 | m_style = wxFONTSTYLE_NORMAL; | |
246 | break; | |
247 | case PANGO_STYLE_ITALIC: | |
248 | m_style = wxFONTSTYLE_ITALIC; | |
249 | break; | |
250 | case PANGO_STYLE_OBLIQUE: | |
251 | m_style = wxFONTSTYLE_SLANT; | |
252 | break; | |
253 | } | |
254 | ||
255 | switch (pango_font_description_get_weight( desc )) | |
256 | { | |
257 | case PANGO_WEIGHT_ULTRALIGHT: | |
258 | m_weight = wxFONTWEIGHT_LIGHT; | |
259 | break; | |
260 | case PANGO_WEIGHT_LIGHT: | |
261 | m_weight = wxFONTWEIGHT_LIGHT; | |
262 | break; | |
263 | case PANGO_WEIGHT_NORMAL: | |
264 | m_weight = wxFONTWEIGHT_NORMAL; | |
265 | break; | |
266 | case PANGO_WEIGHT_BOLD: | |
267 | m_weight = wxFONTWEIGHT_BOLD; | |
268 | break; | |
269 | case PANGO_WEIGHT_ULTRABOLD: | |
270 | m_weight = wxFONTWEIGHT_BOLD; | |
271 | break; | |
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 | |
296 | // remember the X font name | |
297 | m_nativeFontInfo.SetXFontName(fontname); | |
298 | ||
299 | // get the font parameters from the XLFD | |
300 | // ------------------------------------- | |
301 | ||
302 | m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY); | |
303 | ||
304 | m_weight = wxFONTWEIGHT_NORMAL; | |
305 | ||
306 | wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper(); | |
307 | if ( !w.empty() && w != _T('*') ) | |
308 | { | |
309 | // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD | |
310 | // and BLACK | |
311 | if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) || | |
312 | !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) || | |
313 | wxStrstr(w.c_str() + 1, _T("BOLD")) ) | |
314 | { | |
315 | m_weight = wxFONTWEIGHT_BOLD; | |
316 | } | |
317 | else if ( w == _T("LIGHT") || w == _T("THIN") ) | |
318 | { | |
319 | m_weight = wxFONTWEIGHT_LIGHT; | |
320 | } | |
321 | } | |
322 | ||
323 | switch ( wxToupper(*m_nativeFontInfo. | |
324 | GetXFontComponent(wxXLFD_SLANT).c_str()) ) | |
325 | { | |
326 | case _T('I'): // italique | |
327 | m_style = wxFONTSTYLE_ITALIC; | |
328 | break; | |
329 | ||
330 | case _T('O'): // oblique | |
331 | m_style = wxFONTSTYLE_SLANT; | |
332 | break; | |
333 | ||
334 | default: | |
335 | m_style = wxFONTSTYLE_NORMAL; | |
336 | } | |
337 | ||
338 | long ptSize; | |
339 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) ) | |
340 | { | |
341 | // size in XLFD is in 10 point units | |
342 | m_pointSize = (int)(ptSize / 10); | |
343 | } | |
344 | else | |
345 | { | |
346 | m_pointSize = wxDEFAULT_FONT_SIZE; | |
347 | } | |
348 | ||
349 | // examine the spacing: if the font is monospaced, assume wxTELETYPE | |
350 | // family for compatibility with the old code which used it instead of | |
351 | // IsFixedWidth() | |
352 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') ) | |
353 | { | |
354 | m_family = wxFONTFAMILY_TELETYPE; | |
355 | } | |
356 | else // not monospaceed | |
357 | { | |
358 | // don't even try guessing it, it doesn't work for too many fonts | |
359 | // anyhow | |
360 | m_family = wxFONTFAMILY_UNKNOWN; | |
361 | } | |
362 | ||
363 | // X fonts are never underlined... | |
364 | m_underlined = FALSE; | |
365 | ||
366 | // deal with font encoding | |
367 | wxString | |
368 | registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(), | |
369 | encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper(); | |
370 | ||
371 | if ( registry == _T("ISO8859") ) | |
372 | { | |
373 | int cp; | |
374 | if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 ) | |
375 | { | |
376 | m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1); | |
377 | } | |
378 | } | |
379 | else if ( registry == _T("MICROSOFT") ) | |
380 | { | |
381 | int cp; | |
382 | if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 ) | |
383 | { | |
384 | m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp); | |
385 | } | |
386 | } | |
387 | else if ( registry == _T("KOI8") ) | |
388 | { | |
389 | m_encoding = wxFONTENCODING_KOI8; | |
390 | } | |
391 | else // unknown encoding | |
392 | { | |
393 | // may be give a warning here? | |
394 | m_encoding = wxFONTENCODING_SYSTEM; | |
395 | } | |
396 | #endif | |
397 | } | |
398 | ||
399 | wxFontRefData::~wxFontRefData() | |
400 | { | |
401 | #ifndef __WXGTK20__ | |
402 | wxNode *node = m_scaled_xfonts.First(); | |
403 | while (node) | |
404 | { | |
405 | GdkFont *font = (GdkFont*)node->Data(); | |
406 | wxNode *next = node->Next(); | |
407 | gdk_font_unref( font ); | |
408 | node = next; | |
409 | } | |
410 | #endif | |
411 | } | |
412 | ||
413 | // ---------------------------------------------------------------------------- | |
414 | // wxFontRefData SetXXX() | |
415 | // ---------------------------------------------------------------------------- | |
416 | ||
417 | void wxFontRefData::SetPointSize(int pointSize) | |
418 | { | |
419 | m_pointSize = pointSize; | |
420 | ||
421 | #ifdef __WXGTK20__ | |
422 | // Get native info | |
423 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
424 | ||
425 | pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE ); | |
426 | #else | |
427 | if ( HasNativeFont() ) | |
428 | { | |
429 | wxString size; | |
430 | if ( pointSize == -1 ) | |
431 | size = _T('*'); | |
432 | else | |
433 | size.Printf(_T("%d"), 10*pointSize); | |
434 | ||
435 | m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size); | |
436 | } | |
437 | #endif | |
438 | } | |
439 | ||
440 | void wxFontRefData::SetFamily(int family) | |
441 | { | |
442 | m_family = family; | |
443 | ||
444 | // TODO: what are we supposed to do with m_nativeFontInfo here? | |
445 | } | |
446 | ||
447 | void wxFontRefData::SetStyle(int style) | |
448 | { | |
449 | m_style = style; | |
450 | ||
451 | #ifdef __WXGTK20__ | |
452 | // Get native info | |
453 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
454 | ||
455 | switch ( style ) | |
456 | { | |
457 | case wxFONTSTYLE_ITALIC: | |
458 | pango_font_description_set_style( desc, PANGO_STYLE_ITALIC ); | |
459 | break; | |
460 | case wxFONTSTYLE_SLANT: | |
461 | pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE ); | |
462 | break; | |
463 | default: | |
464 | wxFAIL_MSG( _T("unknown font style") ); | |
465 | // fall through | |
466 | case wxFONTSTYLE_NORMAL: | |
467 | pango_font_description_set_style( desc, PANGO_STYLE_NORMAL ); | |
468 | break; | |
469 | } | |
470 | #else | |
471 | if ( HasNativeFont() ) | |
472 | { | |
473 | wxString slant; | |
474 | switch ( style ) | |
475 | { | |
476 | case wxFONTSTYLE_ITALIC: | |
477 | slant = _T('i'); | |
478 | break; | |
479 | ||
480 | case wxFONTSTYLE_SLANT: | |
481 | slant = _T('o'); | |
482 | break; | |
483 | ||
484 | default: | |
485 | wxFAIL_MSG( _T("unknown font style") ); | |
486 | // fall through | |
487 | ||
488 | case wxFONTSTYLE_NORMAL: | |
489 | slant = _T('r'); | |
490 | } | |
491 | ||
492 | m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant); | |
493 | } | |
494 | #endif | |
495 | } | |
496 | ||
497 | void wxFontRefData::SetWeight(int weight) | |
498 | { | |
499 | m_weight = weight; | |
500 | ||
501 | #ifndef __WXGTK20__ | |
502 | if ( HasNativeFont() ) | |
503 | { | |
504 | wxString boldness; | |
505 | switch ( weight ) | |
506 | { | |
507 | case wxFONTWEIGHT_BOLD: | |
508 | boldness = _T("bold"); | |
509 | break; | |
510 | ||
511 | case wxFONTWEIGHT_LIGHT: | |
512 | boldness = _T("light"); | |
513 | break; | |
514 | ||
515 | default: | |
516 | wxFAIL_MSG( _T("unknown font weight") ); | |
517 | // fall through | |
518 | ||
519 | case wxFONTWEIGHT_NORMAL: | |
520 | // unspecified | |
521 | boldness = _T("medium"); | |
522 | } | |
523 | ||
524 | m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness); | |
525 | } | |
526 | #endif | |
527 | } | |
528 | ||
529 | void wxFontRefData::SetUnderlined(bool underlined) | |
530 | { | |
531 | m_underlined = underlined; | |
532 | ||
533 | // the XLFD doesn't have "underlined" field anyhow | |
534 | } | |
535 | ||
536 | void wxFontRefData::SetFaceName(const wxString& facename) | |
537 | { | |
538 | m_faceName = facename; | |
539 | ||
540 | #ifndef __WXGTK20__ | |
541 | if ( HasNativeFont() ) | |
542 | { | |
543 | m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename); | |
544 | } | |
545 | #endif | |
546 | } | |
547 | ||
548 | void wxFontRefData::SetEncoding(wxFontEncoding encoding) | |
549 | { | |
550 | m_encoding = encoding; | |
551 | ||
552 | #ifndef __WXGTK20__ | |
553 | if ( HasNativeFont() ) | |
554 | { | |
555 | wxNativeEncodingInfo info; | |
556 | if ( wxGetNativeFontEncoding(encoding, &info) ) | |
557 | { | |
558 | m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry); | |
559 | m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding); | |
560 | } | |
561 | } | |
562 | #endif | |
563 | } | |
564 | ||
565 | // ============================================================================ | |
566 | // wxFont implementation | |
567 | // ============================================================================ | |
568 | ||
569 | // ---------------------------------------------------------------------------- | |
570 | // wxFont creation | |
571 | // ---------------------------------------------------------------------------- | |
572 | ||
573 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
574 | ||
575 | void wxFont::Init() | |
576 | { | |
577 | } | |
578 | ||
579 | wxFont::wxFont(const wxNativeFontInfo& info) | |
580 | { | |
581 | Init(); | |
582 | ||
583 | #ifdef __WXGTK20__ | |
584 | Create( info.GetPointSize(), | |
585 | info.GetFamily(), | |
586 | info.GetStyle(), | |
587 | info.GetWeight(), | |
588 | info.GetUnderlined(), | |
589 | info.GetFaceName(), | |
590 | info.GetEncoding() ); | |
591 | #else | |
592 | Create(info.GetXFontName()); | |
593 | #endif | |
594 | } | |
595 | ||
596 | bool wxFont::Create( int pointSize, | |
597 | int family, | |
598 | int style, | |
599 | int weight, | |
600 | bool underlined, | |
601 | const wxString& face, | |
602 | wxFontEncoding encoding) | |
603 | { | |
604 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
605 | underlined, face, encoding); | |
606 | ||
607 | return TRUE; | |
608 | } | |
609 | ||
610 | bool wxFont::Create(const wxString& fontname) | |
611 | { | |
612 | // VZ: does this really happen? | |
613 | if ( fontname.empty() ) | |
614 | { | |
615 | *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
616 | ||
617 | return TRUE; | |
618 | } | |
619 | ||
620 | m_refData = new wxFontRefData(fontname); | |
621 | ||
622 | return TRUE; | |
623 | } | |
624 | ||
625 | void wxFont::Unshare() | |
626 | { | |
627 | if (!m_refData) | |
628 | { | |
629 | m_refData = new wxFontRefData(); | |
630 | } | |
631 | else | |
632 | { | |
633 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); | |
634 | UnRef(); | |
635 | m_refData = ref; | |
636 | } | |
637 | } | |
638 | ||
639 | wxFont::~wxFont() | |
640 | { | |
641 | } | |
642 | ||
643 | // ---------------------------------------------------------------------------- | |
644 | // accessors | |
645 | // ---------------------------------------------------------------------------- | |
646 | ||
647 | // all accessors are just forwarded to wxFontRefData which has everything we | |
648 | // need | |
649 | ||
650 | int wxFont::GetPointSize() const | |
651 | { | |
652 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
653 | ||
654 | return M_FONTDATA->m_pointSize; | |
655 | } | |
656 | ||
657 | wxString wxFont::GetFaceName() const | |
658 | { | |
659 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); | |
660 | ||
661 | return M_FONTDATA->m_faceName; | |
662 | } | |
663 | ||
664 | int wxFont::GetFamily() const | |
665 | { | |
666 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
667 | ||
668 | return M_FONTDATA->m_family; | |
669 | } | |
670 | ||
671 | int wxFont::GetStyle() const | |
672 | { | |
673 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
674 | ||
675 | return M_FONTDATA->m_style; | |
676 | } | |
677 | ||
678 | int wxFont::GetWeight() const | |
679 | { | |
680 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
681 | ||
682 | return M_FONTDATA->m_weight; | |
683 | } | |
684 | ||
685 | bool wxFont::GetUnderlined() const | |
686 | { | |
687 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
688 | ||
689 | return M_FONTDATA->m_underlined; | |
690 | } | |
691 | ||
692 | wxFontEncoding wxFont::GetEncoding() const | |
693 | { | |
694 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
695 | ||
696 | return M_FONTDATA->m_encoding; | |
697 | } | |
698 | ||
699 | wxNativeFontInfo *wxFont::GetNativeFontInfo() const | |
700 | { | |
701 | wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") ); | |
702 | ||
703 | #ifndef __WXGTK20__ // ??? | |
704 | if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() ) | |
705 | GetInternalFont(); | |
706 | #endif | |
707 | ||
708 | return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo); | |
709 | } | |
710 | ||
711 | bool wxFont::IsFixedWidth() const | |
712 | { | |
713 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
714 | ||
715 | #ifndef __WXGTK20__ | |
716 | if ( M_FONTDATA->HasNativeFont() ) | |
717 | { | |
718 | // the monospace fonts are supposed to have "M" in the spacing field | |
719 | wxString spacing = M_FONTDATA-> | |
720 | m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING); | |
721 | ||
722 | return spacing.Upper() == _T('M'); | |
723 | } | |
724 | #endif | |
725 | ||
726 | return wxFontBase::IsFixedWidth(); | |
727 | } | |
728 | ||
729 | // ---------------------------------------------------------------------------- | |
730 | // change font attributes | |
731 | // ---------------------------------------------------------------------------- | |
732 | ||
733 | void wxFont::SetPointSize(int pointSize) | |
734 | { | |
735 | Unshare(); | |
736 | ||
737 | M_FONTDATA->SetPointSize(pointSize); | |
738 | } | |
739 | ||
740 | void wxFont::SetFamily(int family) | |
741 | { | |
742 | Unshare(); | |
743 | ||
744 | M_FONTDATA->SetFamily(family); | |
745 | } | |
746 | ||
747 | void wxFont::SetStyle(int style) | |
748 | { | |
749 | Unshare(); | |
750 | ||
751 | M_FONTDATA->SetStyle(style); | |
752 | } | |
753 | ||
754 | void wxFont::SetWeight(int weight) | |
755 | { | |
756 | Unshare(); | |
757 | ||
758 | M_FONTDATA->SetWeight(weight); | |
759 | } | |
760 | ||
761 | void wxFont::SetFaceName(const wxString& faceName) | |
762 | { | |
763 | Unshare(); | |
764 | ||
765 | M_FONTDATA->SetFaceName(faceName); | |
766 | } | |
767 | ||
768 | void wxFont::SetUnderlined(bool underlined) | |
769 | { | |
770 | Unshare(); | |
771 | ||
772 | M_FONTDATA->SetUnderlined(underlined); | |
773 | } | |
774 | ||
775 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
776 | { | |
777 | Unshare(); | |
778 | ||
779 | M_FONTDATA->SetEncoding(encoding); | |
780 | } | |
781 | ||
782 | void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info) | |
783 | { | |
784 | Unshare(); | |
785 | ||
786 | M_FONTDATA->m_nativeFontInfo = info; | |
787 | } | |
788 | ||
789 | // ---------------------------------------------------------------------------- | |
790 | // get internal representation of font | |
791 | // ---------------------------------------------------------------------------- | |
792 | ||
793 | static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL; | |
794 | ||
795 | // this is also used from tbargtk.cpp and tooltip.cpp, hence extern | |
796 | extern GdkFont *GtkGetDefaultGuiFont() | |
797 | { | |
798 | if (!g_systemDefaultGuiFont) | |
799 | { | |
800 | GtkWidget *widget = gtk_button_new(); | |
801 | GtkStyle *def = gtk_rc_get_style( widget ); | |
802 | if (def) | |
803 | { | |
804 | g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) ); | |
805 | } | |
806 | else | |
807 | { | |
808 | def = gtk_widget_get_default_style(); | |
809 | if (def) | |
810 | g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) ); | |
811 | } | |
812 | gtk_widget_destroy( widget ); | |
813 | } | |
814 | else | |
815 | { | |
816 | // already have it, but ref it once more before returning | |
817 | gdk_font_ref(g_systemDefaultGuiFont); | |
818 | } | |
819 | ||
820 | return g_systemDefaultGuiFont; | |
821 | } | |
822 | ||
823 | GdkFont *wxFont::GetInternalFont( float scale ) const | |
824 | { | |
825 | GdkFont *font = (GdkFont *) NULL; | |
826 | ||
827 | wxCHECK_MSG( Ok(), font, wxT("invalid font") ) | |
828 | ||
829 | #ifdef __WXGTK20__ | |
830 | if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT)) | |
831 | { | |
832 | font = GtkGetDefaultGuiFont(); | |
833 | } | |
834 | else | |
835 | { | |
836 | PangoFontDescription *font_description = GetNativeFontInfo()->description; | |
837 | ||
838 | font = gdk_font_from_description( font_description ); | |
839 | } | |
840 | #else | |
841 | long int_scale = long(scale * 100.0 + 0.5); // key for fontlist | |
842 | int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100); | |
843 | ||
844 | wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale); | |
845 | if (node) | |
846 | { | |
847 | font = (GdkFont*)node->Data(); | |
848 | } | |
849 | else // we don't have this font in this size yet | |
850 | { | |
851 | if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT)) | |
852 | { | |
853 | font = GtkGetDefaultGuiFont(); | |
854 | } | |
855 | ||
856 | if ( !font ) | |
857 | { | |
858 | // do we have the XLFD? | |
859 | if ( M_FONTDATA->HasNativeFont() ) | |
860 | { | |
861 | font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName()); | |
862 | } | |
863 | ||
864 | // no XLFD of no exact match - try the approximate one now | |
865 | if ( !font ) | |
866 | { | |
867 | wxString xfontname; | |
868 | font = wxLoadQueryNearestFont( point_scale, | |
869 | M_FONTDATA->m_family, | |
870 | M_FONTDATA->m_style, | |
871 | M_FONTDATA->m_weight, | |
872 | M_FONTDATA->m_underlined, | |
873 | M_FONTDATA->m_faceName, | |
874 | M_FONTDATA->m_encoding, | |
875 | &xfontname); | |
876 | if ( font ) | |
877 | { | |
878 | M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname); | |
879 | } | |
880 | } | |
881 | } | |
882 | ||
883 | if ( font ) | |
884 | { | |
885 | M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font ); | |
886 | } | |
887 | } | |
888 | #endif // GTK 2.0 | |
889 | ||
890 | // it's quite useless to make it a wxCHECK because we're going to crash | |
891 | // anyhow... | |
892 | wxASSERT_MSG( font, wxT("could not load any font?") ); | |
893 | ||
894 | return font; | |
895 | } | |
896 |