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