]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk1/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 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/log.h" | |
25 | #include "wx/settings.h" | |
26 | #include "wx/gdicmn.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/fontutil.h" | |
30 | #include "wx/utils.h" | |
31 | #include "wx/tokenzr.h" | |
32 | ||
33 | #include <strings.h> | |
34 | ||
35 | #include "wx/gtk1/private.h" | |
36 | #include <gdk/gdkprivate.h> | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // constants | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // the default size (in points) for the fonts | |
43 | static const int wxDEFAULT_FONT_SIZE = 12; | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // wxScaledFontList: maps the font sizes to the GDK fonts for the given font | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | WX_DECLARE_HASH_MAP(int, GdkFont *, wxIntegerHash, wxIntegerEqual, | |
50 | wxScaledFontList); | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // wxFontRefData | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | class wxFontRefData : public wxGDIRefData | |
57 | { | |
58 | public: | |
59 | // from broken down font parameters, also default ctor | |
60 | wxFontRefData(int size = -1, | |
61 | wxFontFamily family = wxFONTFAMILY_DEFAULT, | |
62 | wxFontStyle style = wxFONTSTYLE_NORMAL, | |
63 | wxFontWeight weight = wxFONTWEIGHT_NORMAL, | |
64 | bool underlined = false, | |
65 | const wxString& faceName = wxEmptyString, | |
66 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); | |
67 | ||
68 | // from XFLD | |
69 | wxFontRefData(const wxString& fontname); | |
70 | ||
71 | // copy ctor | |
72 | wxFontRefData( const wxFontRefData& data ); | |
73 | ||
74 | virtual ~wxFontRefData(); | |
75 | ||
76 | // do we have the native font info? | |
77 | bool HasNativeFont() const | |
78 | { | |
79 | // only use m_nativeFontInfo if it had been initialized | |
80 | return !m_nativeFontInfo.IsDefault(); | |
81 | } | |
82 | ||
83 | // setters: all of them also take care to modify m_nativeFontInfo if we | |
84 | // have it so as to not lose the information not carried by our fields | |
85 | void SetPointSize(int pointSize); | |
86 | void SetFamily(wxFontFamily family); | |
87 | void SetStyle(wxFontStyle style); | |
88 | void SetWeight(wxFontWeight weight); | |
89 | void SetUnderlined(bool underlined); | |
90 | bool SetFaceName(const wxString& facename); | |
91 | void SetEncoding(wxFontEncoding encoding); | |
92 | ||
93 | // and this one also modifies all the other font data fields | |
94 | void SetNativeFontInfo(const wxNativeFontInfo& info); | |
95 | ||
96 | protected: | |
97 | // common part of all ctors | |
98 | void Init(int pointSize, | |
99 | wxFontFamily family, | |
100 | wxFontStyle style, | |
101 | wxFontWeight weight, | |
102 | bool underlined, | |
103 | const wxString& faceName, | |
104 | wxFontEncoding encoding); | |
105 | ||
106 | // set all fields from (already initialized and valid) m_nativeFontInfo | |
107 | void InitFromNative(); | |
108 | ||
109 | private: | |
110 | // clear m_scaled_xfonts if any | |
111 | void ClearGdkFonts(); | |
112 | ||
113 | // the map of font sizes to "GdkFont *" | |
114 | wxScaledFontList m_scaled_xfonts; | |
115 | ||
116 | int m_pointSize; | |
117 | wxFontFamily m_family; | |
118 | wxFontStyle m_style; | |
119 | wxFontWeight m_weight; | |
120 | bool m_underlined; | |
121 | wxString m_faceName; | |
122 | wxFontEncoding m_encoding; // Unused under GTK 2.0 | |
123 | ||
124 | // The native font info, basicly an XFLD under GTK 1.2 and | |
125 | // the pango font description under GTK 2.0. | |
126 | wxNativeFontInfo m_nativeFontInfo; | |
127 | ||
128 | friend class wxFont; | |
129 | }; | |
130 | ||
131 | #define M_FONTDATA ((wxFontRefData*)m_refData) | |
132 | ||
133 | // ---------------------------------------------------------------------------- | |
134 | // wxFontRefData | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
137 | void wxFontRefData::Init(int pointSize, | |
138 | wxFontFamily family, | |
139 | wxFontStyle style, | |
140 | wxFontWeight weight, | |
141 | bool underlined, | |
142 | const wxString& faceName, | |
143 | wxFontEncoding encoding) | |
144 | { | |
145 | m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family; | |
146 | ||
147 | m_faceName = faceName; | |
148 | ||
149 | // we accept both wxDEFAULT and wxNORMAL here - should we? | |
150 | m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style; | |
151 | m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight; | |
152 | ||
153 | // and here, do we really want to forbid creation of the font of the size | |
154 | // 90 (the value of wxDEFAULT)?? | |
155 | m_pointSize = pointSize == wxDEFAULT || pointSize == -1 | |
156 | ? wxDEFAULT_FONT_SIZE | |
157 | : pointSize; | |
158 | ||
159 | m_underlined = underlined; | |
160 | m_encoding = encoding; | |
161 | } | |
162 | ||
163 | void wxFontRefData::InitFromNative() | |
164 | { | |
165 | // get the font parameters from the XLFD | |
166 | // ------------------------------------- | |
167 | ||
168 | m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY); | |
169 | ||
170 | m_weight = wxFONTWEIGHT_NORMAL; | |
171 | ||
172 | wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper(); | |
173 | if ( !w.empty() && w != wxT('*') ) | |
174 | { | |
175 | // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD | |
176 | // and BLACK | |
177 | if ( ((w[0u] == wxT('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) || | |
178 | !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) || | |
179 | wxStrstr(w.c_str() + 1, wxT("BOLD")) ) | |
180 | { | |
181 | m_weight = wxFONTWEIGHT_BOLD; | |
182 | } | |
183 | else if ( w == wxT("LIGHT") || w == wxT("THIN") ) | |
184 | { | |
185 | m_weight = wxFONTWEIGHT_LIGHT; | |
186 | } | |
187 | } | |
188 | ||
189 | switch ( wxToupper(m_nativeFontInfo. | |
190 | GetXFontComponent(wxXLFD_SLANT)[0u]).GetValue() ) | |
191 | { | |
192 | case wxT('I'): // italique | |
193 | m_style = wxFONTSTYLE_ITALIC; | |
194 | break; | |
195 | ||
196 | case wxT('O'): // oblique | |
197 | m_style = wxFONTSTYLE_SLANT; | |
198 | break; | |
199 | ||
200 | default: | |
201 | m_style = wxFONTSTYLE_NORMAL; | |
202 | } | |
203 | ||
204 | long ptSize; | |
205 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) ) | |
206 | { | |
207 | // size in XLFD is in 10 point units | |
208 | m_pointSize = (int)(ptSize / 10); | |
209 | } | |
210 | else | |
211 | { | |
212 | m_pointSize = wxDEFAULT_FONT_SIZE; | |
213 | } | |
214 | ||
215 | // examine the spacing: if the font is monospaced, assume wxTELETYPE | |
216 | // family for compatibility with the old code which used it instead of | |
217 | // IsFixedWidth() | |
218 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == wxT('M') ) | |
219 | { | |
220 | m_family = wxFONTFAMILY_TELETYPE; | |
221 | } | |
222 | else // not monospaceed | |
223 | { | |
224 | // don't even try guessing it, it doesn't work for too many fonts | |
225 | // anyhow | |
226 | m_family = wxFONTFAMILY_UNKNOWN; | |
227 | } | |
228 | ||
229 | // X fonts are never underlined... | |
230 | m_underlined = false; | |
231 | ||
232 | // deal with font encoding | |
233 | wxString | |
234 | registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(), | |
235 | encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper(); | |
236 | ||
237 | if ( registry == wxT("ISO8859") ) | |
238 | { | |
239 | int cp; | |
240 | if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 ) | |
241 | { | |
242 | m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1); | |
243 | } | |
244 | } | |
245 | else if ( registry == wxT("MICROSOFT") ) | |
246 | { | |
247 | int cp; | |
248 | if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 ) | |
249 | { | |
250 | m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp); | |
251 | } | |
252 | } | |
253 | else if ( registry == wxT("KOI8") ) | |
254 | { | |
255 | m_encoding = wxFONTENCODING_KOI8; | |
256 | } | |
257 | else // unknown encoding | |
258 | { | |
259 | // may be give a warning here? or use wxFontMapper? | |
260 | m_encoding = wxFONTENCODING_SYSTEM; | |
261 | } | |
262 | } | |
263 | ||
264 | wxFontRefData::wxFontRefData( const wxFontRefData& data ) | |
265 | : wxGDIRefData() | |
266 | { | |
267 | m_pointSize = data.m_pointSize; | |
268 | m_family = data.m_family; | |
269 | m_style = data.m_style; | |
270 | m_weight = data.m_weight; | |
271 | ||
272 | m_underlined = data.m_underlined; | |
273 | ||
274 | m_faceName = data.m_faceName; | |
275 | m_encoding = data.m_encoding; | |
276 | ||
277 | // Forces a copy of the internal data. wxNativeFontInfo should probably | |
278 | // have a copy ctor and assignment operator to fix this properly but that | |
279 | // would break binary compatibility... | |
280 | m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString()); | |
281 | } | |
282 | ||
283 | wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style, | |
284 | wxFontWeight weight, bool underlined, | |
285 | const wxString& faceName, | |
286 | wxFontEncoding encoding) | |
287 | { | |
288 | Init(size, family, style, weight, underlined, faceName, encoding); | |
289 | } | |
290 | ||
291 | wxFontRefData::wxFontRefData(const wxString& fontname) | |
292 | { | |
293 | // FromString() should really work in GTK1 too, doesn't it? | |
294 | m_nativeFontInfo.SetXFontName(fontname); | |
295 | ||
296 | InitFromNative(); | |
297 | } | |
298 | ||
299 | void wxFontRefData::ClearGdkFonts() | |
300 | { | |
301 | for ( wxScaledFontList::iterator i = m_scaled_xfonts.begin(); | |
302 | i != m_scaled_xfonts.end(); | |
303 | ++i ) | |
304 | { | |
305 | GdkFont *font = i->second; | |
306 | gdk_font_unref( font ); | |
307 | } | |
308 | ||
309 | m_scaled_xfonts.clear(); | |
310 | } | |
311 | ||
312 | wxFontRefData::~wxFontRefData() | |
313 | { | |
314 | ClearGdkFonts(); | |
315 | } | |
316 | ||
317 | // ---------------------------------------------------------------------------- | |
318 | // wxFontRefData SetXXX() | |
319 | // ---------------------------------------------------------------------------- | |
320 | ||
321 | void wxFontRefData::SetPointSize(int pointSize) | |
322 | { | |
323 | m_pointSize = pointSize; | |
324 | ||
325 | if ( HasNativeFont() ) | |
326 | { | |
327 | wxString size; | |
328 | if ( pointSize == -1 ) | |
329 | size = wxT('*'); | |
330 | else | |
331 | size.Printf(wxT("%d"), 10*pointSize); | |
332 | ||
333 | m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size); | |
334 | } | |
335 | } | |
336 | ||
337 | void wxFontRefData::SetFamily(wxFontFamily family) | |
338 | { | |
339 | m_family = family; | |
340 | ||
341 | // TODO: what are we supposed to do with m_nativeFontInfo here? | |
342 | } | |
343 | ||
344 | void wxFontRefData::SetStyle(wxFontStyle style) | |
345 | { | |
346 | m_style = style; | |
347 | ||
348 | if ( HasNativeFont() ) | |
349 | { | |
350 | wxString slant; | |
351 | switch ( style ) | |
352 | { | |
353 | case wxFONTSTYLE_ITALIC: | |
354 | slant = wxT('i'); | |
355 | break; | |
356 | ||
357 | case wxFONTSTYLE_SLANT: | |
358 | slant = wxT('o'); | |
359 | break; | |
360 | ||
361 | default: | |
362 | wxFAIL_MSG( wxT("unknown font style") ); | |
363 | // fall through | |
364 | ||
365 | case wxFONTSTYLE_NORMAL: | |
366 | slant = wxT('r'); | |
367 | } | |
368 | ||
369 | m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant); | |
370 | } | |
371 | } | |
372 | ||
373 | void wxFontRefData::SetWeight(wxFontWeight weight) | |
374 | { | |
375 | m_weight = weight; | |
376 | ||
377 | if ( HasNativeFont() ) | |
378 | { | |
379 | wxString boldness; | |
380 | switch ( weight ) | |
381 | { | |
382 | case wxFONTWEIGHT_BOLD: | |
383 | boldness = wxT("bold"); | |
384 | break; | |
385 | ||
386 | case wxFONTWEIGHT_LIGHT: | |
387 | boldness = wxT("light"); | |
388 | break; | |
389 | ||
390 | default: | |
391 | wxFAIL_MSG( wxT("unknown font weight") ); | |
392 | // fall through | |
393 | ||
394 | case wxFONTWEIGHT_NORMAL: | |
395 | // unspecified | |
396 | boldness = wxT("medium"); | |
397 | } | |
398 | ||
399 | m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness); | |
400 | } | |
401 | } | |
402 | ||
403 | void wxFontRefData::SetUnderlined(bool underlined) | |
404 | { | |
405 | m_underlined = underlined; | |
406 | ||
407 | // the XLFD doesn't have "underlined" field anyhow | |
408 | } | |
409 | ||
410 | bool wxFontRefData::SetFaceName(const wxString& facename) | |
411 | { | |
412 | m_faceName = facename; | |
413 | ||
414 | if ( HasNativeFont() ) | |
415 | { | |
416 | m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename); | |
417 | } | |
418 | ||
419 | return true; | |
420 | } | |
421 | ||
422 | void wxFontRefData::SetEncoding(wxFontEncoding encoding) | |
423 | { | |
424 | m_encoding = encoding; | |
425 | ||
426 | if ( HasNativeFont() ) | |
427 | { | |
428 | wxNativeEncodingInfo info; | |
429 | if ( wxGetNativeFontEncoding(encoding, &info) ) | |
430 | { | |
431 | m_nativeFontInfo.SetXFontComponent(wxXLFD_REGISTRY, info.xregistry); | |
432 | m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding); | |
433 | } | |
434 | } | |
435 | } | |
436 | ||
437 | void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info) | |
438 | { | |
439 | // previously cached fonts shouldn't be used | |
440 | ClearGdkFonts(); | |
441 | ||
442 | m_nativeFontInfo = info; | |
443 | ||
444 | // set all the other font parameters from the native font info | |
445 | InitFromNative(); | |
446 | } | |
447 | ||
448 | // ---------------------------------------------------------------------------- | |
449 | // wxFont creation | |
450 | // ---------------------------------------------------------------------------- | |
451 | ||
452 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
453 | ||
454 | wxFont::wxFont(const wxNativeFontInfo& info) | |
455 | { | |
456 | (void) Create(info.GetXFontName()); | |
457 | } | |
458 | ||
459 | bool wxFont::Create( int pointSize, | |
460 | wxFontFamily family, | |
461 | wxFontStyle style, | |
462 | wxFontWeight weight, | |
463 | bool underlined, | |
464 | const wxString& face, | |
465 | wxFontEncoding encoding) | |
466 | { | |
467 | UnRef(); | |
468 | ||
469 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
470 | underlined, face, encoding); | |
471 | ||
472 | return true; | |
473 | } | |
474 | ||
475 | bool wxFont::Create(const wxString& fontname) | |
476 | { | |
477 | // VZ: does this really happen? | |
478 | if ( fontname.empty() ) | |
479 | { | |
480 | *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
481 | ||
482 | return true; | |
483 | } | |
484 | ||
485 | m_refData = new wxFontRefData(fontname); | |
486 | ||
487 | return true; | |
488 | } | |
489 | ||
490 | void wxFont::Unshare() | |
491 | { | |
492 | if (!m_refData) | |
493 | { | |
494 | m_refData = new wxFontRefData(); | |
495 | } | |
496 | else | |
497 | { | |
498 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); | |
499 | UnRef(); | |
500 | m_refData = ref; | |
501 | } | |
502 | } | |
503 | ||
504 | wxFont::~wxFont() | |
505 | { | |
506 | } | |
507 | ||
508 | wxGDIRefData *wxFont::CreateGDIRefData() const | |
509 | { | |
510 | return new wxFontRefData; | |
511 | } | |
512 | ||
513 | wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const | |
514 | { | |
515 | return new wxFontRefData(*static_cast<const wxFontRefData *>(data)); | |
516 | } | |
517 | ||
518 | // ---------------------------------------------------------------------------- | |
519 | // accessors | |
520 | // ---------------------------------------------------------------------------- | |
521 | ||
522 | int wxFont::GetPointSize() const | |
523 | { | |
524 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
525 | ||
526 | return M_FONTDATA->m_pointSize; | |
527 | } | |
528 | ||
529 | wxString wxFont::GetFaceName() const | |
530 | { | |
531 | wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") ); | |
532 | ||
533 | return M_FONTDATA->m_faceName; | |
534 | } | |
535 | ||
536 | wxFontFamily wxFont::DoGetFamily() const | |
537 | { | |
538 | return M_FONTDATA->m_family; | |
539 | } | |
540 | ||
541 | wxFontStyle wxFont::GetStyle() const | |
542 | { | |
543 | wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX, wxT("invalid font") ); | |
544 | ||
545 | return M_FONTDATA->m_style; | |
546 | } | |
547 | ||
548 | wxFontWeight wxFont::GetWeight() const | |
549 | { | |
550 | wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX, wxT("invalid font") ); | |
551 | ||
552 | return M_FONTDATA->m_weight; | |
553 | } | |
554 | ||
555 | bool wxFont::GetUnderlined() const | |
556 | { | |
557 | wxCHECK_MSG( Ok(), false, wxT("invalid font") ); | |
558 | ||
559 | return M_FONTDATA->m_underlined; | |
560 | } | |
561 | ||
562 | wxFontEncoding wxFont::GetEncoding() const | |
563 | { | |
564 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
565 | ||
566 | // m_encoding is unused in wxGTK2, return encoding that the user set. | |
567 | return M_FONTDATA->m_encoding; | |
568 | } | |
569 | ||
570 | const wxNativeFontInfo *wxFont::GetNativeFontInfo() const | |
571 | { | |
572 | wxCHECK_MSG( Ok(), NULL, wxT("invalid font") ); | |
573 | ||
574 | if ( !M_FONTDATA->HasNativeFont() ) | |
575 | { | |
576 | // NB: this call has important side-effect: it not only finds | |
577 | // GdkFont representation, it also initializes m_nativeFontInfo | |
578 | // by calling its SetXFontName method | |
579 | GetInternalFont(); | |
580 | } | |
581 | ||
582 | return &(M_FONTDATA->m_nativeFontInfo); | |
583 | } | |
584 | ||
585 | bool wxFont::IsFixedWidth() const | |
586 | { | |
587 | wxCHECK_MSG( Ok(), false, wxT("invalid font") ); | |
588 | ||
589 | if ( M_FONTDATA->HasNativeFont() ) | |
590 | { | |
591 | // the monospace fonts are supposed to have "M" in the spacing field | |
592 | wxString spacing = M_FONTDATA-> | |
593 | m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING); | |
594 | ||
595 | return spacing.Upper() == wxT('M'); | |
596 | } | |
597 | ||
598 | return wxFontBase::IsFixedWidth(); | |
599 | } | |
600 | ||
601 | // ---------------------------------------------------------------------------- | |
602 | // change font attributes | |
603 | // ---------------------------------------------------------------------------- | |
604 | ||
605 | void wxFont::SetPointSize(int pointSize) | |
606 | { | |
607 | Unshare(); | |
608 | ||
609 | M_FONTDATA->SetPointSize(pointSize); | |
610 | } | |
611 | ||
612 | void wxFont::SetFamily(wxFontFamily family) | |
613 | { | |
614 | Unshare(); | |
615 | ||
616 | M_FONTDATA->SetFamily(family); | |
617 | } | |
618 | ||
619 | void wxFont::SetStyle(wxFontStyle style) | |
620 | { | |
621 | Unshare(); | |
622 | ||
623 | M_FONTDATA->SetStyle(style); | |
624 | } | |
625 | ||
626 | void wxFont::SetWeight(wxFontWeight weight) | |
627 | { | |
628 | Unshare(); | |
629 | ||
630 | M_FONTDATA->SetWeight(weight); | |
631 | } | |
632 | ||
633 | bool wxFont::SetFaceName(const wxString& faceName) | |
634 | { | |
635 | Unshare(); | |
636 | ||
637 | return M_FONTDATA->SetFaceName(faceName) && | |
638 | wxFontBase::SetFaceName(faceName); | |
639 | } | |
640 | ||
641 | void wxFont::SetUnderlined(bool underlined) | |
642 | { | |
643 | Unshare(); | |
644 | ||
645 | M_FONTDATA->SetUnderlined(underlined); | |
646 | } | |
647 | ||
648 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
649 | { | |
650 | Unshare(); | |
651 | ||
652 | M_FONTDATA->SetEncoding(encoding); | |
653 | } | |
654 | ||
655 | void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info ) | |
656 | { | |
657 | Unshare(); | |
658 | ||
659 | M_FONTDATA->SetNativeFontInfo( info ); | |
660 | } | |
661 | ||
662 | // ---------------------------------------------------------------------------- | |
663 | // get internal representation of font | |
664 | // ---------------------------------------------------------------------------- | |
665 | ||
666 | static GdkFont *g_systemDefaultGuiFont = NULL; | |
667 | ||
668 | // this is also used from toolbar.cpp and tooltip.cpp, hence extern | |
669 | extern GdkFont *GtkGetDefaultGuiFont() | |
670 | { | |
671 | if (!g_systemDefaultGuiFont) | |
672 | { | |
673 | GtkWidget *widget = gtk_button_new(); | |
674 | GtkStyle *def = gtk_rc_get_style( widget ); | |
675 | if (def) | |
676 | { | |
677 | g_systemDefaultGuiFont = gdk_font_ref( def->font ); | |
678 | } | |
679 | else | |
680 | { | |
681 | def = gtk_widget_get_default_style(); | |
682 | if (def) | |
683 | g_systemDefaultGuiFont = gdk_font_ref( def->font ); | |
684 | } | |
685 | gtk_widget_destroy( widget ); | |
686 | } | |
687 | else | |
688 | { | |
689 | // already have it, but ref it once more before returning | |
690 | gdk_font_ref(g_systemDefaultGuiFont); | |
691 | } | |
692 | ||
693 | return g_systemDefaultGuiFont; | |
694 | } | |
695 | ||
696 | GdkFont *wxFont::GetInternalFont( float scale ) const | |
697 | { | |
698 | GdkFont *font = NULL; | |
699 | ||
700 | wxCHECK_MSG( Ok(), font, wxT("invalid font") ); | |
701 | ||
702 | long int_scale = long(scale * 100.0 + 0.5); // key for fontlist | |
703 | int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100); | |
704 | ||
705 | wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts; | |
706 | wxScaledFontList::iterator i = list.find(int_scale); | |
707 | if ( i != list.end() ) | |
708 | { | |
709 | font = i->second; | |
710 | } | |
711 | else // we don't have this font in this size yet | |
712 | { | |
713 | if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT)) | |
714 | { | |
715 | font = GtkGetDefaultGuiFont(); | |
716 | } | |
717 | ||
718 | if ( !font ) | |
719 | { | |
720 | // do we have the XLFD? | |
721 | if ( int_scale == 100 && M_FONTDATA->HasNativeFont() ) | |
722 | { | |
723 | font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName()); | |
724 | } | |
725 | ||
726 | // no XLFD of no exact match - try the approximate one now | |
727 | if ( !font ) | |
728 | { | |
729 | wxString xfontname; | |
730 | font = wxLoadQueryNearestFont( point_scale, | |
731 | M_FONTDATA->m_family, | |
732 | M_FONTDATA->m_style, | |
733 | M_FONTDATA->m_weight, | |
734 | M_FONTDATA->m_underlined, | |
735 | M_FONTDATA->m_faceName, | |
736 | M_FONTDATA->m_encoding, | |
737 | &xfontname); | |
738 | // NB: wxFont::GetNativeFontInfo relies on this | |
739 | // side-effect of GetInternalFont | |
740 | if ( int_scale == 100 ) | |
741 | M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname); | |
742 | } | |
743 | } | |
744 | ||
745 | if ( font ) | |
746 | { | |
747 | list[int_scale] = font; | |
748 | } | |
749 | } | |
750 | ||
751 | // it's quite useless to make it a wxCHECK because we're going to crash | |
752 | // anyhow... | |
753 | wxASSERT_MSG( font, wxT("could not load any font?") ); | |
754 | ||
755 | return font; | |
756 | } |