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