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