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