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