]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/font.cpp
set icons bundle, not single icon, for frames loaded from XRC
[wxWidgets.git] / src / gtk / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/font.cpp
3 // Purpose: wxFont for wxGTK
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/utils.h"
26 #include "wx/settings.h"
27 #include "wx/cmndata.h"
28 #include "wx/gdicmn.h"
29 #endif
30
31 #include "wx/fontutil.h"
32 #include "wx/tokenzr.h"
33
34 #include "wx/gtk/private.h"
35
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 // wxFontRefData
45 // ----------------------------------------------------------------------------
46
47 class wxFontRefData : public wxGDIRefData
48 {
49 public:
50 // from broken down font parameters, also default ctor
51 wxFontRefData(int size = -1,
52 wxFontFamily family = wxFONTFAMILY_DEFAULT,
53 wxFontStyle style = wxFONTSTYLE_NORMAL,
54 wxFontWeight weight = wxFONTWEIGHT_NORMAL,
55 bool underlined = false,
56 const wxString& faceName = wxEmptyString,
57 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
58
59 // from XFLD
60 wxFontRefData(const wxString& fontname);
61
62 // copy ctor
63 wxFontRefData( const wxFontRefData& data );
64
65 virtual ~wxFontRefData();
66
67 // setters: all of them also take care to modify m_nativeFontInfo if we
68 // have it so as to not lose the information not carried by our fields
69 void SetPointSize(int pointSize);
70 void SetFamily(wxFontFamily family);
71 void SetStyle(wxFontStyle style);
72 void SetWeight(wxFontWeight weight);
73 void SetUnderlined(bool underlined);
74 bool SetFaceName(const wxString& facename);
75 void SetEncoding(wxFontEncoding encoding);
76
77 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
78 bool GetNoAntiAliasing() const { return m_noAA; }
79
80 // and this one also modifies all the other font data fields
81 void SetNativeFontInfo(const wxNativeFontInfo& info);
82
83 protected:
84 // common part of all ctors
85 void Init(int pointSize,
86 wxFontFamily family,
87 wxFontStyle style,
88 wxFontWeight weight,
89 bool underlined,
90 const wxString& faceName,
91 wxFontEncoding encoding);
92
93 // set all fields from (already initialized and valid) m_nativeFontInfo
94 void InitFromNative();
95
96 private:
97 wxFontFamily m_family;
98 wxFontEncoding m_encoding;
99 bool m_underlined;
100 bool m_noAA; // No anti-aliasing
101
102 // The native font info: basically a PangoFontDescription
103 wxNativeFontInfo m_nativeFontInfo;
104
105 friend class wxFont;
106 };
107
108 #define M_FONTDATA ((wxFontRefData*)m_refData)
109
110 // ----------------------------------------------------------------------------
111 // wxFontRefData
112 // ----------------------------------------------------------------------------
113
114 void wxFontRefData::Init(int pointSize,
115 wxFontFamily family,
116 wxFontStyle style,
117 wxFontWeight weight,
118 bool underlined,
119 const wxString& faceName,
120 wxFontEncoding encoding)
121 {
122 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
123
124 m_underlined = underlined;
125 m_encoding = encoding;
126 if ( m_encoding == wxFONTENCODING_DEFAULT )
127 m_encoding = wxFont::GetDefaultEncoding();
128
129 m_noAA = false;
130
131 // Create native font info
132 m_nativeFontInfo.description = pango_font_description_new();
133
134 // And set its values
135 if (!faceName.empty())
136 {
137 pango_font_description_set_family( m_nativeFontInfo.description,
138 wxGTK_CONV_SYS(faceName) );
139 }
140 else
141 {
142 switch (m_family)
143 {
144 case wxFONTFAMILY_MODERN:
145 case wxFONTFAMILY_TELETYPE:
146 pango_font_description_set_family( m_nativeFontInfo.description, "monospace" );
147 break;
148 case wxFONTFAMILY_ROMAN:
149 pango_font_description_set_family( m_nativeFontInfo.description, "serif" );
150 break;
151 case wxFONTFAMILY_SWISS:
152 // SWISS = sans serif
153 default:
154 pango_font_description_set_family( m_nativeFontInfo.description, "sans" );
155 break;
156 }
157 }
158
159 SetStyle( style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style );
160 SetPointSize( (pointSize == wxDEFAULT || pointSize == -1)
161 ? wxDEFAULT_FONT_SIZE
162 : pointSize );
163 SetWeight( weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight );
164 }
165
166 void wxFontRefData::InitFromNative()
167 {
168 m_noAA = false;
169
170 // Get native info
171 PangoFontDescription *desc = m_nativeFontInfo.description;
172
173 // Pango sometimes needs to have a size
174 int pango_size = pango_font_description_get_size( desc );
175 if (pango_size == 0)
176 m_nativeFontInfo.SetPointSize(wxDEFAULT_FONT_SIZE);
177
178 wxString faceName = wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc));
179 if (faceName == wxT("monospace"))
180 {
181 m_family = wxFONTFAMILY_TELETYPE;
182 }
183 else if (faceName == wxT("sans"))
184 {
185 m_family = wxFONTFAMILY_SWISS;
186 }
187 else if (faceName == wxT("serif"))
188 {
189 m_family = wxFONTFAMILY_ROMAN;
190 }
191 else
192 {
193 m_family = wxFONTFAMILY_UNKNOWN;
194 }
195
196 // Pango description are never underlined
197 m_underlined = false;
198
199 // always with GTK+ 2
200 m_encoding = wxFONTENCODING_UTF8;
201 }
202
203 wxFontRefData::wxFontRefData( const wxFontRefData& data )
204 : wxGDIRefData()
205 {
206 m_family = data.m_family;
207 m_underlined = data.m_underlined;
208 m_encoding = data.m_encoding;
209 m_noAA = data.m_noAA;
210
211 // Forces a copy of the internal data. wxNativeFontInfo should probably
212 // have a copy ctor and assignment operator to fix this properly but that
213 // would break binary compatibility...
214 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
215 }
216
217 wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style,
218 wxFontWeight weight, bool underlined,
219 const wxString& faceName,
220 wxFontEncoding encoding)
221 {
222 Init(size, family, style, weight, underlined, faceName, encoding);
223 }
224
225 wxFontRefData::wxFontRefData(const wxString& fontname)
226 {
227 m_nativeFontInfo.FromString( fontname );
228
229 InitFromNative();
230 }
231
232 wxFontRefData::~wxFontRefData()
233 {
234 }
235
236 // ----------------------------------------------------------------------------
237 // wxFontRefData SetXXX()
238 // ----------------------------------------------------------------------------
239
240 void wxFontRefData::SetPointSize(int pointSize)
241 {
242 m_nativeFontInfo.SetPointSize(pointSize);
243 }
244
245 /*
246 NOTE: disabled because pango_font_description_set_absolute_size() and
247 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
248 size of "30" makes the latter return 36...
249 Besides, we need to return GetPointSize() a point size value even if
250 SetPixelSize() was used and this would require further changes
251 (and use of pango_font_description_get_size_is_absolute in some places).
252
253 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
254 {
255 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
256 "Negative values for the pixel size or zero pixel height are not allowed" );
257
258 if (wx_pango_version_check(1,8,0) != NULL ||
259 pixelSize.GetWidth() != 0)
260 {
261 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
262 // if the user set the pixel width of the font explicitly or the pango
263 // library is too old, we cannot proceed
264 return false;
265 }
266
267 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
268 pixelSize.GetHeight() * PANGO_SCALE );
269
270 return true;
271 }
272
273 */
274
275 void wxFontRefData::SetFamily(wxFontFamily family)
276 {
277 m_family = family;
278
279 // wxNativeInfo::SetFamily asserts because is currently not implemented---
280 // we just save the family here FIXME
281 }
282
283 void wxFontRefData::SetStyle(wxFontStyle style)
284 {
285 m_nativeFontInfo.SetStyle((wxFontStyle)style);
286 }
287
288 void wxFontRefData::SetWeight(wxFontWeight weight)
289 {
290 m_nativeFontInfo.SetWeight((wxFontWeight)weight);
291 }
292
293 void wxFontRefData::SetUnderlined(bool underlined)
294 {
295 m_underlined = underlined;
296
297 // the Pango font descriptor does not have an underlined attribute
298 // (and wxNativeFontInfo::SetUnderlined asserts); rather it's
299 // wxWindowDCImpl::DoDrawText that handles underlined fonts, so we
300 // here we just need to save the underlined attribute
301 }
302
303 bool wxFontRefData::SetFaceName(const wxString& facename)
304 {
305 return m_nativeFontInfo.SetFaceName(facename);
306 }
307
308 void wxFontRefData::SetEncoding(wxFontEncoding encoding)
309 {
310 m_encoding = encoding;
311
312 // the internal Pango encoding is always UTF8; here we save the
313 // encoding just to make it possible to return it from GetEncoding()
314 // FIXME: this seems wrong; shouldn't GetEncoding() always return wxFONTENCODING_UTF8?
315 }
316
317 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
318 {
319 m_nativeFontInfo = info;
320
321 // set all the other font parameters from the native font info
322 InitFromNative();
323 }
324
325 // ----------------------------------------------------------------------------
326 // wxFont creation
327 // ----------------------------------------------------------------------------
328
329 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
330
331 wxFont::wxFont(const wxNativeFontInfo& info)
332 {
333 Create( info.GetPointSize(),
334 info.GetFamily(),
335 info.GetStyle(),
336 info.GetWeight(),
337 info.GetUnderlined(),
338 info.GetFaceName(),
339 info.GetEncoding() );
340 }
341
342 bool wxFont::Create( int pointSize,
343 wxFontFamily family,
344 wxFontStyle style,
345 wxFontWeight weight,
346 bool underlined,
347 const wxString& face,
348 wxFontEncoding encoding )
349 {
350 UnRef();
351
352 m_refData = new wxFontRefData(pointSize, family, style, weight,
353 underlined, face, encoding);
354
355 return true;
356 }
357
358 bool wxFont::Create(const wxString& fontname)
359 {
360 // VZ: does this really happen?
361 if ( fontname.empty() )
362 {
363 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
364
365 return true;
366 }
367
368 m_refData = new wxFontRefData(fontname);
369
370 return true;
371 }
372
373 wxFont::~wxFont()
374 {
375 }
376
377 // ----------------------------------------------------------------------------
378 // accessors
379 // ----------------------------------------------------------------------------
380
381 int wxFont::GetPointSize() const
382 {
383 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
384
385 return M_FONTDATA->m_nativeFontInfo.GetPointSize();
386 }
387
388 wxString wxFont::GetFaceName() const
389 {
390 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
391
392 return M_FONTDATA->m_nativeFontInfo.GetFaceName();
393 }
394
395 wxFontFamily wxFont::GetFamily() const
396 {
397 wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX, wxT("invalid font") );
398
399 wxFontFamily ret = M_FONTDATA->m_nativeFontInfo.GetFamily();
400
401 if (ret == wxFONTFAMILY_DEFAULT)
402 ret = M_FONTDATA->m_family;
403
404 return ret;
405 }
406
407 wxFontStyle wxFont::GetStyle() const
408 {
409 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX, wxT("invalid font") );
410
411 return M_FONTDATA->m_nativeFontInfo.GetStyle();
412 }
413
414 wxFontWeight wxFont::GetWeight() const
415 {
416 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") );
417
418 return M_FONTDATA->m_nativeFontInfo.GetWeight();
419 }
420
421 bool wxFont::GetUnderlined() const
422 {
423 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
424
425 return M_FONTDATA->m_underlined;
426 }
427
428 wxFontEncoding wxFont::GetEncoding() const
429 {
430 wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM, wxT("invalid font") );
431
432 return M_FONTDATA->m_encoding;
433 }
434
435 bool wxFont::GetNoAntiAliasing() const
436 {
437 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
438
439 return M_FONTDATA->m_noAA;
440 }
441
442 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
443 {
444 wxCHECK_MSG( IsOk(), NULL, wxT("invalid font") );
445
446 return &(M_FONTDATA->m_nativeFontInfo);
447 }
448
449 bool wxFont::IsFixedWidth() const
450 {
451 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
452
453 return wxFontBase::IsFixedWidth();
454 }
455
456 // ----------------------------------------------------------------------------
457 // change font attributes
458 // ----------------------------------------------------------------------------
459
460 void wxFont::SetPointSize(int pointSize)
461 {
462 AllocExclusive();
463
464 M_FONTDATA->SetPointSize(pointSize);
465 }
466
467 void wxFont::SetFamily(wxFontFamily family)
468 {
469 AllocExclusive();
470
471 M_FONTDATA->SetFamily(family);
472 }
473
474 void wxFont::SetStyle(wxFontStyle style)
475 {
476 AllocExclusive();
477
478 M_FONTDATA->SetStyle(style);
479 }
480
481 void wxFont::SetWeight(wxFontWeight weight)
482 {
483 AllocExclusive();
484
485 M_FONTDATA->SetWeight(weight);
486 }
487
488 bool wxFont::SetFaceName(const wxString& faceName)
489 {
490 AllocExclusive();
491
492 return M_FONTDATA->SetFaceName(faceName) &&
493 wxFontBase::SetFaceName(faceName);
494 }
495
496 void wxFont::SetUnderlined(bool underlined)
497 {
498 AllocExclusive();
499
500 M_FONTDATA->SetUnderlined(underlined);
501 }
502
503 void wxFont::SetEncoding(wxFontEncoding encoding)
504 {
505 AllocExclusive();
506
507 M_FONTDATA->SetEncoding(encoding);
508 }
509
510 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
511 {
512 AllocExclusive();
513
514 M_FONTDATA->SetNativeFontInfo( info );
515 }
516
517 void wxFont::SetNoAntiAliasing( bool no )
518 {
519 AllocExclusive();
520
521 M_FONTDATA->SetNoAntiAliasing( no );
522 }
523
524 wxGDIRefData* wxFont::CreateGDIRefData() const
525 {
526 return new wxFontRefData;
527 }
528
529 wxGDIRefData* wxFont::CloneGDIRefData(const wxGDIRefData* data) const
530 {
531 return new wxFontRefData(*static_cast<const wxFontRefData*>(data));
532 }