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