]>
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 | { | |
81 | return !m_nativeFontInfo.IsDefault(); | |
82 | } | |
83 | ||
84 | // setters: all of them also take care to modify m_nativeFontInfo if we | |
85 | // have it so as to not lose the information not carried by our fields | |
86 | void SetPointSize(int pointSize); | |
87 | void SetFamily(int family); | |
88 | void SetStyle(int style); | |
89 | void SetWeight(int weight); | |
90 | void SetUnderlined(bool underlined); | |
91 | void SetFaceName(const wxString& facename); | |
92 | void SetEncoding(wxFontEncoding encoding); | |
93 | ||
94 | // debugger helper: shows what the font really is | |
95 | // | |
96 | // VZ: I need this as my gdb either shows wildly wrong values or crashes | |
97 | // when I ask it to "p fontRefData" :-( | |
98 | #ifdef __WXDEBUG__ | |
99 | void Dump() const | |
100 | { | |
101 | wxPrintf(_T("%s-%s-%s-%d-%d\n"), | |
102 | m_faceName.c_str(), | |
103 | m_weight == wxFONTWEIGHT_NORMAL | |
104 | ? _T("normal") | |
105 | : m_weight == wxFONTWEIGHT_BOLD | |
106 | ? _T("bold") | |
107 | : _T("light"), | |
108 | m_style == wxFONTSTYLE_NORMAL ? _T("regular") : _T("italic"), | |
109 | m_pointSize, | |
110 | m_encoding); | |
111 | } | |
112 | #endif // Debug | |
113 | ||
0c5d3e1c VZ |
114 | protected: |
115 | // common part of all ctors | |
116 | void Init(int pointSize, | |
117 | int family, | |
118 | int style, | |
119 | int weight, | |
120 | bool underlined, | |
121 | const wxString& faceName, | |
7826e2dd | 122 | wxFontEncoding encoding); |
0c5d3e1c VZ |
123 | |
124 | private: | |
409d5a58 VZ |
125 | // the map of font sizes to "GdkFont *" |
126 | wxScaledFontList m_scaled_xfonts; | |
127 | ||
128 | // the broken down font parameters | |
f35c2659 RR |
129 | int m_pointSize; |
130 | int m_family, | |
131 | m_style, | |
132 | m_weight; | |
133 | bool m_underlined; | |
134 | wxString m_faceName; | |
135 | wxFontEncoding m_encoding; | |
7826e2dd | 136 | |
409d5a58 | 137 | // the native font info, basicly an XFLD |
30764ab5 | 138 | wxNativeFontInfo m_nativeFontInfo; |
8bbe427f | 139 | |
f6bcfd97 | 140 | friend class wxFont; |
c801d85f KB |
141 | }; |
142 | ||
0c5d3e1c | 143 | // ============================================================================ |
409d5a58 | 144 | // wxFontRefData implementation |
0c5d3e1c VZ |
145 | // ============================================================================ |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
409d5a58 | 148 | // wxFontRefData creation |
0c5d3e1c VZ |
149 | // ---------------------------------------------------------------------------- |
150 | ||
151 | void wxFontRefData::Init(int pointSize, | |
152 | int family, | |
153 | int style, | |
154 | int weight, | |
155 | bool underlined, | |
156 | const wxString& faceName, | |
7826e2dd | 157 | wxFontEncoding encoding) |
8bbe427f | 158 | { |
409d5a58 | 159 | m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family; |
0c5d3e1c VZ |
160 | |
161 | m_faceName = faceName; | |
162 | ||
409d5a58 VZ |
163 | // we accept both wxDEFAULT and wxNORMAL here - should we? |
164 | m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style; | |
165 | m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight; | |
0c5d3e1c | 166 | |
409d5a58 VZ |
167 | // and here, do we really want to forbid creation of the font of the size |
168 | // 90 (the value of wxDEFAULT)?? | |
169 | m_pointSize = pointSize == wxDEFAULT || | |
170 | pointSize == -1 ? wxDEFAULT_FONT_SIZE : pointSize; | |
0c5d3e1c VZ |
171 | |
172 | m_underlined = underlined; | |
173 | m_encoding = encoding; | |
8bbe427f VZ |
174 | } |
175 | ||
0c5d3e1c | 176 | wxFontRefData::wxFontRefData( const wxFontRefData& data ) |
d84afea9 | 177 | : wxObjectRefData() |
358fc25c | 178 | { |
409d5a58 VZ |
179 | m_pointSize = data.m_pointSize; |
180 | m_family = data.m_family; | |
181 | m_style = data.m_style; | |
182 | m_weight = data.m_weight; | |
183 | ||
184 | m_underlined = data.m_underlined; | |
185 | ||
186 | m_faceName = data.m_faceName; | |
187 | m_encoding = data.m_encoding; | |
188 | ||
189 | m_nativeFontInfo = data.m_nativeFontInfo; | |
f35c2659 | 190 | } |
0c5d3e1c | 191 | |
f35c2659 | 192 | wxFontRefData::wxFontRefData(int size, int family, int style, |
7826e2dd VZ |
193 | int weight, bool underlined, |
194 | const wxString& faceName, | |
195 | wxFontEncoding encoding) | |
f35c2659 | 196 | { |
7826e2dd | 197 | Init(size, family, style, weight, underlined, faceName, encoding); |
358fc25c RR |
198 | } |
199 | ||
409d5a58 VZ |
200 | wxFontRefData::wxFontRefData(const wxString& fontname) |
201 | { | |
202 | // remember the X font name | |
203 | m_nativeFontInfo.SetXFontName(fontname); | |
204 | ||
205 | // get the font parameters from the XLFD | |
206 | // ------------------------------------- | |
207 | ||
208 | m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY); | |
209 | ||
210 | m_weight = wxFONTWEIGHT_NORMAL; | |
211 | ||
212 | wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper(); | |
213 | if ( !w.empty() && w != _T('*') ) | |
214 | { | |
215 | // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD | |
216 | // and BLACK | |
217 | if ( ((w[0u] == _T('B') && (!strcmp(w.c_str() + 1, _T("OLD")) || | |
218 | !strcmp(w.c_str() + 1, _T("LACK"))))) || | |
219 | strstr(w.c_str() + 1, _T("BOLD")) ) | |
220 | { | |
221 | m_weight = wxFONTWEIGHT_BOLD; | |
222 | } | |
223 | else if ( w == _T("LIGHT") || w == _T("THIN") ) | |
224 | { | |
225 | m_weight = wxFONTWEIGHT_LIGHT; | |
226 | } | |
227 | } | |
228 | ||
229 | switch ( wxToupper(*m_nativeFontInfo. | |
230 | GetXFontComponent(wxXLFD_SLANT).c_str()) ) | |
231 | { | |
232 | case _T('I'): // italique | |
233 | m_style = wxFONTSTYLE_ITALIC; | |
234 | break; | |
235 | ||
236 | case _T('O'): // oblique | |
237 | m_style = wxFONTSTYLE_SLANT; | |
238 | break; | |
239 | ||
240 | default: | |
241 | m_style = wxFONTSTYLE_NORMAL; | |
242 | } | |
243 | ||
244 | long ptSize; | |
245 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) ) | |
246 | { | |
247 | // size in XLFD is in 10 point units | |
248 | m_pointSize = (int)(ptSize / 10); | |
249 | } | |
250 | else | |
251 | { | |
252 | m_pointSize = wxDEFAULT_FONT_SIZE; | |
253 | } | |
254 | ||
255 | // examine the spacing: if the font is monospaced, assume wxTELETYPE | |
256 | // family for compatibility with the old code which used it instead of | |
257 | // IsFixedWidth() | |
258 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') ) | |
259 | { | |
260 | m_family = wxFONTFAMILY_TELETYPE; | |
261 | } | |
262 | else // not monospaceed | |
263 | { | |
264 | // don't even try guessing it, it doesn't work for too many fonts | |
265 | // anyhow | |
266 | m_family = wxFONTFAMILY_UNKNOWN; | |
267 | } | |
268 | ||
269 | // X fonts are never underlined... | |
270 | m_underlined = FALSE; | |
271 | ||
272 | // deal with font encoding | |
273 | wxString | |
274 | registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(), | |
275 | encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper(); | |
276 | ||
277 | if ( registry == _T("ISO8859") ) | |
278 | { | |
279 | int cp; | |
280 | if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 ) | |
281 | { | |
282 | m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1); | |
283 | } | |
284 | } | |
285 | else if ( registry == _T("MICROSOFT") ) | |
286 | { | |
287 | int cp; | |
288 | if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 ) | |
289 | { | |
290 | m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp); | |
291 | } | |
292 | } | |
293 | else if ( registry == _T("KOI8") ) | |
294 | { | |
295 | m_encoding = wxFONTENCODING_KOI8; | |
296 | } | |
297 | else // unknown encoding | |
298 | { | |
299 | // may be give a warning here? | |
300 | m_encoding = wxFONTENCODING_SYSTEM; | |
301 | } | |
302 | } | |
303 | ||
8bbe427f VZ |
304 | wxFontRefData::~wxFontRefData() |
305 | { | |
306 | wxNode *node = m_scaled_xfonts.First(); | |
307 | while (node) | |
308 | { | |
309 | GdkFont *font = (GdkFont*)node->Data(); | |
310 | wxNode *next = node->Next(); | |
311 | gdk_font_unref( font ); | |
312 | node = next; | |
313 | } | |
0c5d3e1c | 314 | } |
c801d85f | 315 | |
0c5d3e1c | 316 | // ---------------------------------------------------------------------------- |
409d5a58 | 317 | // wxFontRefData SetXXX() |
0c5d3e1c | 318 | // ---------------------------------------------------------------------------- |
c801d85f | 319 | |
409d5a58 | 320 | void wxFontRefData::SetPointSize(int pointSize) |
c801d85f | 321 | { |
409d5a58 | 322 | m_pointSize = pointSize; |
c801d85f | 323 | |
409d5a58 VZ |
324 | if ( HasNativeFont() ) |
325 | { | |
326 | wxString size; | |
327 | if ( pointSize == -1 ) | |
328 | size = _T('*'); | |
329 | else | |
330 | size.Printf(_T("%d"), 10*pointSize); | |
7826e2dd | 331 | |
409d5a58 VZ |
332 | m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size); |
333 | } | |
7826e2dd VZ |
334 | } |
335 | ||
409d5a58 | 336 | void wxFontRefData::SetFamily(int family) |
7826e2dd | 337 | { |
409d5a58 | 338 | m_family = family; |
30764ab5 | 339 | |
409d5a58 | 340 | // TODO: what are we supposed to do with m_nativeFontInfo here? |
30764ab5 VZ |
341 | } |
342 | ||
409d5a58 | 343 | void wxFontRefData::SetStyle(int style) |
c801d85f | 344 | { |
409d5a58 VZ |
345 | m_style = style; |
346 | ||
347 | if ( HasNativeFont() ) | |
30764ab5 | 348 | { |
409d5a58 VZ |
349 | wxString slant; |
350 | switch ( style ) | |
351 | { | |
352 | case wxFONTSTYLE_ITALIC: | |
353 | slant = _T('i'); | |
354 | break; | |
355 | ||
356 | case wxFONTSTYLE_SLANT: | |
357 | slant = _T('o'); | |
358 | break; | |
359 | ||
360 | default: | |
361 | wxFAIL_MSG( _T("unknown font style") ); | |
362 | // fall through | |
363 | ||
364 | case wxFONTSTYLE_NORMAL: | |
365 | slant = _T('r'); | |
366 | } | |
367 | ||
368 | m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant); | |
30764ab5 | 369 | } |
409d5a58 | 370 | } |
7beba2fc | 371 | |
409d5a58 VZ |
372 | void wxFontRefData::SetWeight(int weight) |
373 | { | |
374 | m_weight = weight; | |
8bbe427f | 375 | |
409d5a58 VZ |
376 | if ( HasNativeFont() ) |
377 | { | |
378 | wxString boldness; | |
379 | switch ( weight ) | |
380 | { | |
381 | case wxFONTWEIGHT_BOLD: | |
382 | boldness = _T("bold"); | |
383 | break; | |
30764ab5 | 384 | |
409d5a58 VZ |
385 | case wxFONTWEIGHT_LIGHT: |
386 | boldness = _T("light"); | |
387 | break; | |
284b4c88 | 388 | |
409d5a58 VZ |
389 | default: |
390 | wxFAIL_MSG( _T("unknown font weight") ); | |
391 | // fall through | |
284b4c88 | 392 | |
409d5a58 VZ |
393 | case wxFONTWEIGHT_NORMAL: |
394 | // unspecified | |
395 | boldness = _T("medium"); | |
396 | } | |
284b4c88 | 397 | |
409d5a58 VZ |
398 | m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness); |
399 | } | |
400 | } | |
30764ab5 | 401 | |
409d5a58 VZ |
402 | void wxFontRefData::SetUnderlined(bool underlined) |
403 | { | |
404 | m_underlined = underlined; | |
8636aed8 | 405 | |
409d5a58 VZ |
406 | // the XLFD doesn't have "underlined" field anyhow |
407 | } | |
30760ce7 | 408 | |
409d5a58 VZ |
409 | void wxFontRefData::SetFaceName(const wxString& facename) |
410 | { | |
411 | m_faceName = facename; | |
7beba2fc | 412 | |
409d5a58 VZ |
413 | if ( HasNativeFont() ) |
414 | { | |
415 | m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename); | |
416 | } | |
417 | } | |
284b4c88 | 418 | |
409d5a58 VZ |
419 | void wxFontRefData::SetEncoding(wxFontEncoding encoding) |
420 | { | |
421 | m_encoding = encoding; | |
284b4c88 | 422 | |
409d5a58 | 423 | if ( HasNativeFont() ) |
d06b34a7 | 424 | { |
409d5a58 VZ |
425 | wxNativeEncodingInfo info; |
426 | if ( wxGetNativeFontEncoding(encoding, &info) ) | |
427 | { | |
428 | m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry); | |
429 | m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding); | |
430 | } | |
d06b34a7 | 431 | } |
409d5a58 | 432 | } |
284b4c88 | 433 | |
409d5a58 VZ |
434 | // ============================================================================ |
435 | // wxFont implementation | |
436 | // ============================================================================ | |
284b4c88 | 437 | |
409d5a58 VZ |
438 | // ---------------------------------------------------------------------------- |
439 | // wxFont creation | |
440 | // ---------------------------------------------------------------------------- | |
36f210c8 | 441 | |
409d5a58 | 442 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
36f210c8 | 443 | |
409d5a58 VZ |
444 | void wxFont::Init() |
445 | { | |
446 | } | |
36f210c8 | 447 | |
409d5a58 VZ |
448 | wxFont::wxFont(const wxNativeFontInfo& info) |
449 | { | |
450 | Init(); | |
451 | ||
452 | Create(info.GetXFontName()); | |
453 | } | |
454 | ||
455 | bool wxFont::Create( int pointSize, | |
456 | int family, | |
457 | int style, | |
458 | int weight, | |
459 | bool underlined, | |
460 | const wxString& face, | |
461 | wxFontEncoding encoding) | |
462 | { | |
463 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
464 | underlined, face, encoding); | |
465 | ||
466 | return TRUE; | |
467 | } | |
468 | ||
469 | bool wxFont::Create(const wxString& fontname) | |
470 | { | |
471 | // VZ: does this really happen? | |
472 | if ( fontname.empty() ) | |
36f210c8 | 473 | { |
409d5a58 | 474 | *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); |
7beba2fc | 475 | |
409d5a58 | 476 | return TRUE; |
36f210c8 | 477 | } |
409d5a58 VZ |
478 | |
479 | m_refData = new wxFontRefData(fontname); | |
480 | ||
0c5d3e1c | 481 | return TRUE; |
ff7b1510 | 482 | } |
c801d85f | 483 | |
0c5d3e1c | 484 | void wxFont::Unshare() |
8bbe427f | 485 | { |
0c5d3e1c VZ |
486 | if (!m_refData) |
487 | { | |
488 | m_refData = new wxFontRefData(); | |
489 | } | |
490 | else | |
491 | { | |
492 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); | |
493 | UnRef(); | |
494 | m_refData = ref; | |
495 | } | |
ff7b1510 | 496 | } |
c801d85f | 497 | |
8bbe427f | 498 | wxFont::~wxFont() |
c801d85f | 499 | { |
ff7b1510 | 500 | } |
c801d85f | 501 | |
0c5d3e1c VZ |
502 | // ---------------------------------------------------------------------------- |
503 | // accessors | |
504 | // ---------------------------------------------------------------------------- | |
c801d85f | 505 | |
409d5a58 VZ |
506 | // all accessors are just forwarded to wxFontRefData which has everything we |
507 | // need | |
53f6aab7 | 508 | |
8bbe427f | 509 | int wxFont::GetPointSize() const |
c801d85f | 510 | { |
223d09f6 | 511 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
8bbe427f VZ |
512 | |
513 | return M_FONTDATA->m_pointSize; | |
ff7b1510 | 514 | } |
c801d85f | 515 | |
8bbe427f | 516 | wxString wxFont::GetFaceName() const |
c801d85f | 517 | { |
223d09f6 | 518 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); |
8bbe427f | 519 | |
36b3b54a | 520 | return M_FONTDATA->m_faceName; |
ff7b1510 | 521 | } |
c801d85f | 522 | |
8bbe427f | 523 | int wxFont::GetFamily() const |
c801d85f | 524 | { |
223d09f6 | 525 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
8bbe427f VZ |
526 | |
527 | return M_FONTDATA->m_family; | |
ff7b1510 | 528 | } |
c801d85f | 529 | |
8bbe427f | 530 | int wxFont::GetStyle() const |
c801d85f | 531 | { |
223d09f6 | 532 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
d84eb083 | 533 | |
8bbe427f | 534 | return M_FONTDATA->m_style; |
ff7b1510 | 535 | } |
c801d85f | 536 | |
8bbe427f | 537 | int wxFont::GetWeight() const |
c801d85f | 538 | { |
223d09f6 | 539 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
8bbe427f VZ |
540 | |
541 | return M_FONTDATA->m_weight; | |
542 | } | |
543 | ||
8bbe427f VZ |
544 | bool wxFont::GetUnderlined() const |
545 | { | |
223d09f6 | 546 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
8bbe427f VZ |
547 | |
548 | return M_FONTDATA->m_underlined; | |
ff7b1510 | 549 | } |
c801d85f | 550 | |
0c5d3e1c | 551 | wxFontEncoding wxFont::GetEncoding() const |
358fc25c | 552 | { |
223d09f6 | 553 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
0c5d3e1c VZ |
554 | |
555 | return M_FONTDATA->m_encoding; | |
358fc25c RR |
556 | } |
557 | ||
7826e2dd | 558 | wxNativeFontInfo *wxFont::GetNativeFontInfo() const |
30764ab5 | 559 | { |
7826e2dd | 560 | wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") ); |
30764ab5 | 561 | |
409d5a58 | 562 | if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() ) |
30764ab5 | 563 | GetInternalFont(); |
7826e2dd VZ |
564 | |
565 | return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo); | |
30764ab5 VZ |
566 | } |
567 | ||
53f6aab7 VZ |
568 | bool wxFont::IsFixedWidth() const |
569 | { | |
570 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
571 | ||
409d5a58 | 572 | if ( M_FONTDATA->HasNativeFont() ) |
53f6aab7 VZ |
573 | { |
574 | // the monospace fonts are supposed to have "M" in the spacing field | |
575 | wxString spacing = M_FONTDATA-> | |
576 | m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING); | |
577 | ||
578 | return spacing.Upper() == _T('M'); | |
579 | } | |
580 | ||
581 | return wxFontBase::IsFixedWidth(); | |
582 | } | |
30764ab5 | 583 | |
0c5d3e1c VZ |
584 | // ---------------------------------------------------------------------------- |
585 | // change font attributes | |
586 | // ---------------------------------------------------------------------------- | |
587 | ||
358fc25c RR |
588 | void wxFont::SetPointSize(int pointSize) |
589 | { | |
590 | Unshare(); | |
591 | ||
409d5a58 | 592 | M_FONTDATA->SetPointSize(pointSize); |
358fc25c RR |
593 | } |
594 | ||
595 | void wxFont::SetFamily(int family) | |
596 | { | |
597 | Unshare(); | |
598 | ||
409d5a58 | 599 | M_FONTDATA->SetFamily(family); |
358fc25c RR |
600 | } |
601 | ||
602 | void wxFont::SetStyle(int style) | |
603 | { | |
604 | Unshare(); | |
605 | ||
409d5a58 | 606 | M_FONTDATA->SetStyle(style); |
358fc25c RR |
607 | } |
608 | ||
609 | void wxFont::SetWeight(int weight) | |
610 | { | |
611 | Unshare(); | |
612 | ||
409d5a58 | 613 | M_FONTDATA->SetWeight(weight); |
358fc25c RR |
614 | } |
615 | ||
616 | void wxFont::SetFaceName(const wxString& faceName) | |
617 | { | |
618 | Unshare(); | |
619 | ||
409d5a58 | 620 | M_FONTDATA->SetFaceName(faceName); |
358fc25c RR |
621 | } |
622 | ||
623 | void wxFont::SetUnderlined(bool underlined) | |
624 | { | |
625 | Unshare(); | |
626 | ||
409d5a58 | 627 | M_FONTDATA->SetUnderlined(underlined); |
358fc25c RR |
628 | } |
629 | ||
0c5d3e1c VZ |
630 | void wxFont::SetEncoding(wxFontEncoding encoding) |
631 | { | |
632 | Unshare(); | |
c801d85f | 633 | |
409d5a58 | 634 | M_FONTDATA->SetEncoding(encoding); |
30764ab5 VZ |
635 | } |
636 | ||
637 | void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info) | |
638 | { | |
639 | Unshare(); | |
640 | ||
641 | M_FONTDATA->m_nativeFontInfo = info; | |
0c5d3e1c VZ |
642 | } |
643 | ||
644 | // ---------------------------------------------------------------------------- | |
645 | // get internal representation of font | |
646 | // ---------------------------------------------------------------------------- | |
c801d85f | 647 | |
c7985368 RR |
648 | static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL; |
649 | ||
409d5a58 VZ |
650 | // this is also used from tbargtk.cpp and tooltip.cpp, hence extern |
651 | extern GdkFont *GtkGetDefaultGuiFont() | |
c7985368 RR |
652 | { |
653 | if (!g_systemDefaultGuiFont) | |
654 | { | |
655 | GtkWidget *widget = gtk_button_new(); | |
656 | GtkStyle *def = gtk_rc_get_style( widget ); | |
e6527f9d RR |
657 | if (def) |
658 | { | |
9e691f46 | 659 | g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) ); |
e6527f9d RR |
660 | } |
661 | else | |
662 | { | |
663 | def = gtk_widget_get_default_style(); | |
664 | if (def) | |
9e691f46 | 665 | g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) ); |
e6527f9d | 666 | } |
c7985368 RR |
667 | gtk_widget_destroy( widget ); |
668 | } | |
b1d1dc51 VZ |
669 | else |
670 | { | |
671 | // already have it, but ref it once more before returning | |
672 | gdk_font_ref(g_systemDefaultGuiFont); | |
673 | } | |
674 | ||
c7985368 RR |
675 | return g_systemDefaultGuiFont; |
676 | } | |
677 | ||
36b3b54a | 678 | GdkFont *wxFont::GetInternalFont( float scale ) const |
c801d85f | 679 | { |
409d5a58 | 680 | GdkFont *font = (GdkFont *) NULL; |
0c5d3e1c | 681 | |
409d5a58 | 682 | wxCHECK_MSG( Ok(), font, wxT("invalid font") ) |
8bbe427f | 683 | |
36b3b54a | 684 | long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */ |
b02da6b1 | 685 | int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100); |
8bbe427f VZ |
686 | |
687 | wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale); | |
688 | if (node) | |
689 | { | |
690 | font = (GdkFont*)node->Data(); | |
691 | } | |
409d5a58 | 692 | else // we don't have this font in this size yet |
8bbe427f | 693 | { |
a756f210 | 694 | if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT)) |
8bbe427f | 695 | { |
c7985368 | 696 | font = GtkGetDefaultGuiFont(); |
8bbe427f | 697 | } |
409d5a58 VZ |
698 | |
699 | if ( !font ) | |
8bbe427f | 700 | { |
409d5a58 VZ |
701 | // do we have the XLFD? |
702 | if ( M_FONTDATA->HasNativeFont() ) | |
703 | { | |
704 | font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName()); | |
705 | } | |
706 | ||
707 | // no XLFD of no exact match - try the approximate one now | |
708 | if ( !font ) | |
709 | { | |
710 | wxString xfontname; | |
711 | font = wxLoadQueryNearestFont( point_scale, | |
712 | M_FONTDATA->m_family, | |
713 | M_FONTDATA->m_style, | |
714 | M_FONTDATA->m_weight, | |
715 | M_FONTDATA->m_underlined, | |
716 | M_FONTDATA->m_faceName, | |
717 | M_FONTDATA->m_encoding, | |
718 | &xfontname); | |
719 | if ( font ) | |
720 | { | |
721 | M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname); | |
722 | } | |
723 | } | |
8bbe427f | 724 | } |
0c5d3e1c | 725 | |
409d5a58 VZ |
726 | if ( font ) |
727 | { | |
728 | M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font ); | |
729 | } | |
8bbe427f | 730 | } |
284b4c88 | 731 | |
7beba2fc VZ |
732 | // it's quite useless to make it a wxCHECK because we're going to crash |
733 | // anyhow... | |
734 | wxASSERT_MSG( font, wxT("could not load any font?") ); | |
284b4c88 | 735 | |
8bbe427f | 736 | return font; |
ff7b1510 | 737 | } |
c801d85f | 738 |