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