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