]>
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 | void wxFontRefData::ReInit(const wxString& fontname) | |
452 | { | |
453 | m_nativeFontInfo.SetXFontName(fontname); | |
454 | ||
455 | InitFromNative(); | |
456 | } | |
457 | #endif | |
458 | ||
459 | void wxFontRefData::ClearGdkFonts() | |
460 | { | |
461 | #ifndef __WXGTK20__ | |
462 | for ( wxScaledFontList::iterator i = m_scaled_xfonts.begin(); | |
463 | i != m_scaled_xfonts.end(); | |
464 | ++i ) | |
465 | { | |
466 | GdkFont *font = i->second; | |
467 | gdk_font_unref( font ); | |
468 | } | |
469 | ||
470 | m_scaled_xfonts.clear(); | |
471 | #endif // GTK 1.x | |
472 | } | |
473 | ||
474 | wxFontRefData::~wxFontRefData() | |
475 | { | |
476 | ClearGdkFonts(); | |
477 | } | |
478 | ||
479 | // ---------------------------------------------------------------------------- | |
480 | // wxFontRefData SetXXX() | |
481 | // ---------------------------------------------------------------------------- | |
482 | ||
483 | void wxFontRefData::SetPointSize(int pointSize) | |
484 | { | |
485 | m_pointSize = pointSize; | |
486 | ||
487 | #ifdef __WXGTK20__ | |
488 | // Get native info | |
489 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
490 | ||
491 | pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE ); | |
492 | #else | |
493 | if ( HasNativeFont() ) | |
494 | { | |
495 | wxString size; | |
496 | if ( pointSize == -1 ) | |
497 | size = _T('*'); | |
498 | else | |
499 | size.Printf(_T("%d"), 10*pointSize); | |
500 | ||
501 | m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size); | |
502 | } | |
503 | #endif | |
504 | } | |
505 | ||
506 | void wxFontRefData::SetFamily(int family) | |
507 | { | |
508 | m_family = family; | |
509 | ||
510 | // TODO: what are we supposed to do with m_nativeFontInfo here? | |
511 | } | |
512 | ||
513 | void wxFontRefData::SetStyle(int style) | |
514 | { | |
515 | m_style = style; | |
516 | ||
517 | #ifdef __WXGTK20__ | |
518 | // Get native info | |
519 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
520 | ||
521 | switch ( style ) | |
522 | { | |
523 | case wxFONTSTYLE_ITALIC: | |
524 | pango_font_description_set_style( desc, PANGO_STYLE_ITALIC ); | |
525 | break; | |
526 | case wxFONTSTYLE_SLANT: | |
527 | pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE ); | |
528 | break; | |
529 | default: | |
530 | wxFAIL_MSG( _T("unknown font style") ); | |
531 | // fall through | |
532 | case wxFONTSTYLE_NORMAL: | |
533 | pango_font_description_set_style( desc, PANGO_STYLE_NORMAL ); | |
534 | break; | |
535 | } | |
536 | #else | |
537 | if ( HasNativeFont() ) | |
538 | { | |
539 | wxString slant; | |
540 | switch ( style ) | |
541 | { | |
542 | case wxFONTSTYLE_ITALIC: | |
543 | slant = _T('i'); | |
544 | break; | |
545 | ||
546 | case wxFONTSTYLE_SLANT: | |
547 | slant = _T('o'); | |
548 | break; | |
549 | ||
550 | default: | |
551 | wxFAIL_MSG( _T("unknown font style") ); | |
552 | // fall through | |
553 | ||
554 | case wxFONTSTYLE_NORMAL: | |
555 | slant = _T('r'); | |
556 | } | |
557 | ||
558 | m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant); | |
559 | } | |
560 | #endif | |
561 | } | |
562 | ||
563 | void wxFontRefData::SetWeight(int weight) | |
564 | { | |
565 | m_weight = weight; | |
566 | ||
567 | #ifdef __WXGTK20__ | |
568 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
569 | switch ( weight ) | |
570 | { | |
571 | case wxFONTWEIGHT_BOLD: | |
572 | pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD); | |
573 | break; | |
574 | ||
575 | case wxFONTWEIGHT_LIGHT: | |
576 | pango_font_description_set_weight(desc, PANGO_WEIGHT_LIGHT); | |
577 | break; | |
578 | ||
579 | default: | |
580 | wxFAIL_MSG( _T("unknown font weight") ); | |
581 | // fall through | |
582 | ||
583 | case wxFONTWEIGHT_NORMAL: | |
584 | // unspecified | |
585 | pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL); | |
586 | } | |
587 | #else //!__WXGTK20__ | |
588 | if ( HasNativeFont() ) | |
589 | { | |
590 | wxString boldness; | |
591 | switch ( weight ) | |
592 | { | |
593 | case wxFONTWEIGHT_BOLD: | |
594 | boldness = _T("bold"); | |
595 | break; | |
596 | ||
597 | case wxFONTWEIGHT_LIGHT: | |
598 | boldness = _T("light"); | |
599 | break; | |
600 | ||
601 | default: | |
602 | wxFAIL_MSG( _T("unknown font weight") ); | |
603 | // fall through | |
604 | ||
605 | case wxFONTWEIGHT_NORMAL: | |
606 | // unspecified | |
607 | boldness = _T("medium"); | |
608 | } | |
609 | ||
610 | m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness); | |
611 | } | |
612 | #endif | |
613 | } | |
614 | ||
615 | void wxFontRefData::SetUnderlined(bool underlined) | |
616 | { | |
617 | m_underlined = underlined; | |
618 | ||
619 | // the XLFD doesn't have "underlined" field anyhow | |
620 | } | |
621 | ||
622 | void wxFontRefData::SetFaceName(const wxString& facename) | |
623 | { | |
624 | m_faceName = facename; | |
625 | ||
626 | #ifndef __WXGTK20__ | |
627 | if ( HasNativeFont() ) | |
628 | { | |
629 | m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename); | |
630 | } | |
631 | #endif | |
632 | } | |
633 | ||
634 | void wxFontRefData::SetEncoding(wxFontEncoding encoding) | |
635 | { | |
636 | m_encoding = encoding; | |
637 | ||
638 | #ifndef __WXGTK20__ | |
639 | if ( HasNativeFont() ) | |
640 | { | |
641 | wxNativeEncodingInfo info; | |
642 | if ( wxGetNativeFontEncoding(encoding, &info) ) | |
643 | { | |
644 | m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry); | |
645 | m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding); | |
646 | } | |
647 | } | |
648 | #endif | |
649 | } | |
650 | ||
651 | void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info) | |
652 | { | |
653 | // previously cached fonts shouldn't be used | |
654 | ClearGdkFonts(); | |
655 | ||
656 | m_nativeFontInfo = info; | |
657 | ||
658 | // set all the other font parameters from the native font info | |
659 | InitFromNative(); | |
660 | } | |
661 | ||
662 | // ---------------------------------------------------------------------------- | |
663 | // wxFont creation | |
664 | // ---------------------------------------------------------------------------- | |
665 | ||
666 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
667 | ||
668 | void wxFont::Init() | |
669 | { | |
670 | } | |
671 | ||
672 | wxFont::wxFont(const wxNativeFontInfo& info) | |
673 | { | |
674 | Init(); | |
675 | ||
676 | #ifdef __WXGTK20__ | |
677 | Create( info.GetPointSize(), | |
678 | info.GetFamily(), | |
679 | info.GetStyle(), | |
680 | info.GetWeight(), | |
681 | info.GetUnderlined(), | |
682 | info.GetFaceName(), | |
683 | info.GetEncoding() ); | |
684 | #else | |
685 | (void) Create(info.GetXFontName()); | |
686 | #endif | |
687 | } | |
688 | ||
689 | bool wxFont::Create( int pointSize, | |
690 | int family, | |
691 | int style, | |
692 | int weight, | |
693 | bool underlined, | |
694 | const wxString& face, | |
695 | wxFontEncoding encoding) | |
696 | { | |
697 | UnRef(); | |
698 | ||
699 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
700 | underlined, face, encoding); | |
701 | ||
702 | return TRUE; | |
703 | } | |
704 | ||
705 | bool wxFont::Create(const wxString& fontname) | |
706 | { | |
707 | // VZ: does this really happen? | |
708 | if ( fontname.empty() ) | |
709 | { | |
710 | *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
711 | ||
712 | return TRUE; | |
713 | } | |
714 | ||
715 | m_refData = new wxFontRefData(fontname); | |
716 | ||
717 | return TRUE; | |
718 | } | |
719 | ||
720 | void wxFont::Unshare() | |
721 | { | |
722 | if (!m_refData) | |
723 | { | |
724 | m_refData = new wxFontRefData(); | |
725 | } | |
726 | else | |
727 | { | |
728 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); | |
729 | UnRef(); | |
730 | m_refData = ref; | |
731 | } | |
732 | } | |
733 | ||
734 | wxFont::~wxFont() | |
735 | { | |
736 | } | |
737 | ||
738 | // ---------------------------------------------------------------------------- | |
739 | // accessors | |
740 | // ---------------------------------------------------------------------------- | |
741 | ||
742 | int wxFont::GetPointSize() const | |
743 | { | |
744 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
745 | ||
746 | return M_FONTDATA->m_pointSize; | |
747 | } | |
748 | ||
749 | wxString wxFont::GetFaceName() const | |
750 | { | |
751 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); | |
752 | ||
753 | return M_FONTDATA->m_faceName; | |
754 | } | |
755 | ||
756 | int wxFont::GetFamily() const | |
757 | { | |
758 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
759 | ||
760 | return M_FONTDATA->m_family; | |
761 | } | |
762 | ||
763 | int wxFont::GetStyle() const | |
764 | { | |
765 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
766 | ||
767 | return M_FONTDATA->m_style; | |
768 | } | |
769 | ||
770 | int wxFont::GetWeight() const | |
771 | { | |
772 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
773 | ||
774 | return M_FONTDATA->m_weight; | |
775 | } | |
776 | ||
777 | bool wxFont::GetUnderlined() const | |
778 | { | |
779 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
780 | ||
781 | return M_FONTDATA->m_underlined; | |
782 | } | |
783 | ||
784 | wxFontEncoding wxFont::GetEncoding() const | |
785 | { | |
786 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
787 | ||
788 | return M_FONTDATA->m_encoding; | |
789 | } | |
790 | ||
791 | bool wxFont::GetNoAntiAliasing() | |
792 | { | |
793 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
794 | ||
795 | return M_FONTDATA->m_noAA; | |
796 | } | |
797 | ||
798 | const wxNativeFontInfo *wxFont::GetNativeFontInfo() const | |
799 | { | |
800 | wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") ); | |
801 | ||
802 | #ifndef __WXGTK20__ | |
803 | if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() ) | |
804 | GetInternalFont(); | |
805 | #endif | |
806 | ||
807 | return &(M_FONTDATA->m_nativeFontInfo); | |
808 | } | |
809 | ||
810 | bool wxFont::IsFixedWidth() const | |
811 | { | |
812 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
813 | ||
814 | #ifndef __WXGTK20__ | |
815 | if ( M_FONTDATA->HasNativeFont() ) | |
816 | { | |
817 | // the monospace fonts are supposed to have "M" in the spacing field | |
818 | wxString spacing = M_FONTDATA-> | |
819 | m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING); | |
820 | ||
821 | return spacing.Upper() == _T('M'); | |
822 | } | |
823 | #endif | |
824 | ||
825 | return wxFontBase::IsFixedWidth(); | |
826 | } | |
827 | ||
828 | // ---------------------------------------------------------------------------- | |
829 | // change font attributes | |
830 | // ---------------------------------------------------------------------------- | |
831 | ||
832 | void wxFont::SetPointSize(int pointSize) | |
833 | { | |
834 | Unshare(); | |
835 | ||
836 | M_FONTDATA->SetPointSize(pointSize); | |
837 | } | |
838 | ||
839 | void wxFont::SetFamily(int family) | |
840 | { | |
841 | Unshare(); | |
842 | ||
843 | M_FONTDATA->SetFamily(family); | |
844 | } | |
845 | ||
846 | void wxFont::SetStyle(int style) | |
847 | { | |
848 | Unshare(); | |
849 | ||
850 | M_FONTDATA->SetStyle(style); | |
851 | } | |
852 | ||
853 | void wxFont::SetWeight(int weight) | |
854 | { | |
855 | Unshare(); | |
856 | ||
857 | M_FONTDATA->SetWeight(weight); | |
858 | } | |
859 | ||
860 | void wxFont::SetFaceName(const wxString& faceName) | |
861 | { | |
862 | Unshare(); | |
863 | ||
864 | M_FONTDATA->SetFaceName(faceName); | |
865 | } | |
866 | ||
867 | void wxFont::SetUnderlined(bool underlined) | |
868 | { | |
869 | Unshare(); | |
870 | ||
871 | M_FONTDATA->SetUnderlined(underlined); | |
872 | } | |
873 | ||
874 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
875 | { | |
876 | Unshare(); | |
877 | ||
878 | M_FONTDATA->SetEncoding(encoding); | |
879 | } | |
880 | ||
881 | void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info ) | |
882 | { | |
883 | Unshare(); | |
884 | ||
885 | M_FONTDATA->SetNativeFontInfo( info ); | |
886 | } | |
887 | ||
888 | void wxFont::SetNoAntiAliasing( bool no ) | |
889 | { | |
890 | Unshare(); | |
891 | ||
892 | M_FONTDATA->SetNoAntiAliasing( no ); | |
893 | } | |
894 | ||
895 | // ---------------------------------------------------------------------------- | |
896 | // get internal representation of font | |
897 | // ---------------------------------------------------------------------------- | |
898 | ||
899 | #ifndef __WXGTK20__ | |
900 | static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL; | |
901 | ||
902 | // this is also used from tbargtk.cpp and tooltip.cpp, hence extern | |
903 | extern GdkFont *GtkGetDefaultGuiFont() | |
904 | { | |
905 | if (!g_systemDefaultGuiFont) | |
906 | { | |
907 | GtkWidget *widget = gtk_button_new(); | |
908 | GtkStyle *def = gtk_rc_get_style( widget ); | |
909 | if (def) | |
910 | { | |
911 | g_systemDefaultGuiFont = gdk_font_ref( def->font ); | |
912 | } | |
913 | else | |
914 | { | |
915 | def = gtk_widget_get_default_style(); | |
916 | if (def) | |
917 | g_systemDefaultGuiFont = gdk_font_ref( def->font ); | |
918 | } | |
919 | gtk_widget_destroy( widget ); | |
920 | } | |
921 | else | |
922 | { | |
923 | // already have it, but ref it once more before returning | |
924 | gdk_font_ref(g_systemDefaultGuiFont); | |
925 | } | |
926 | ||
927 | return g_systemDefaultGuiFont; | |
928 | } | |
929 | ||
930 | GdkFont *wxFont::GetInternalFont( float scale ) const | |
931 | { | |
932 | GdkFont *font = (GdkFont *) NULL; | |
933 | ||
934 | wxCHECK_MSG( Ok(), font, wxT("invalid font") ) | |
935 | ||
936 | long int_scale = long(scale * 100.0 + 0.5); // key for fontlist | |
937 | int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100); | |
938 | ||
939 | wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts; | |
940 | wxScaledFontList::iterator i = list.find(int_scale); | |
941 | if ( i != list.end() ) | |
942 | { | |
943 | font = i->second; | |
944 | } | |
945 | else // we don't have this font in this size yet | |
946 | { | |
947 | if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT)) | |
948 | { | |
949 | font = GtkGetDefaultGuiFont(); | |
950 | } | |
951 | ||
952 | if ( !font ) | |
953 | { | |
954 | // do we have the XLFD? | |
955 | if ( M_FONTDATA->HasNativeFont() ) | |
956 | { | |
957 | font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName()); | |
958 | } | |
959 | ||
960 | // no XLFD of no exact match - try the approximate one now | |
961 | if ( !font ) | |
962 | { | |
963 | wxString xfontname; | |
964 | font = wxLoadQueryNearestFont( point_scale, | |
965 | M_FONTDATA->m_family, | |
966 | M_FONTDATA->m_style, | |
967 | M_FONTDATA->m_weight, | |
968 | M_FONTDATA->m_underlined, | |
969 | M_FONTDATA->m_faceName, | |
970 | M_FONTDATA->m_encoding, | |
971 | &xfontname); | |
972 | if ( font ) | |
973 | { | |
974 | M_FONTDATA->ReInit(xfontname); | |
975 | } | |
976 | } | |
977 | } | |
978 | ||
979 | if ( font ) | |
980 | { | |
981 | list[int_scale] = font; | |
982 | } | |
983 | } | |
984 | ||
985 | // it's quite useless to make it a wxCHECK because we're going to crash | |
986 | // anyhow... | |
987 | wxASSERT_MSG( font, wxT("could not load any font?") ); | |
988 | ||
989 | return font; | |
990 | } | |
991 | #endif // not GTK 2.0 | |
992 |