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