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