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