]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/font.cpp
Added a couple of wxRTC string translations in es.po
[wxWidgets.git] / src / gtk / font.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/gtk/font.cpp
b5791cc7 3// Purpose: wxFont for wxGTK
c801d85f 4// Author: Robert Roebling
a81258be 5// Id: $Id$
6c9a19aa 6// Copyright: (c) 1998 Robert Roebling and Julian Smart
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
0c5d3e1c
VZ
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
14f355c2
VS
18// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
c801d85f 21#include "wx/font.h"
e4db172a
WS
22
23#ifndef WX_PRECOMP
24 #include "wx/log.h"
de6185e2 25 #include "wx/utils.h"
9eddec69 26 #include "wx/settings.h"
ce5d92e1 27 #include "wx/cmndata.h"
dd05139a 28 #include "wx/gdicmn.h"
e4db172a
WS
29#endif
30
7beba2fc 31#include "wx/fontutil.h"
8636aed8 32#include "wx/tokenzr.h"
0c5d3e1c 33
9e691f46 34#include "wx/gtk/private.h"
83624f79 35
409d5a58
VZ
36// ----------------------------------------------------------------------------
37// constants
38// ----------------------------------------------------------------------------
39
40// the default size (in points) for the fonts
41static const int wxDEFAULT_FONT_SIZE = 12;
42
0c5d3e1c
VZ
43// ----------------------------------------------------------------------------
44// wxFontRefData
45// ----------------------------------------------------------------------------
46
8f884a0d 47class wxFontRefData : public wxGDIRefData
c801d85f 48{
8bbe427f 49public:
409d5a58
VZ
50 // from broken down font parameters, also default ctor
51 wxFontRefData(int size = -1,
0c14b6c3
FM
52 wxFontFamily family = wxFONTFAMILY_DEFAULT,
53 wxFontStyle style = wxFONTSTYLE_NORMAL,
54 wxFontWeight weight = wxFONTWEIGHT_NORMAL,
de6185e2 55 bool underlined = false,
0c5d3e1c 56 const wxString& faceName = wxEmptyString,
7826e2dd 57 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
409d5a58
VZ
58
59 // from XFLD
60 wxFontRefData(const wxString& fontname);
61
62 // copy ctor
358fc25c 63 wxFontRefData( const wxFontRefData& data );
409d5a58 64
0c5d3e1c
VZ
65 virtual ~wxFontRefData();
66
409d5a58
VZ
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);
0c14b6c3
FM
70 void SetFamily(wxFontFamily family);
71 void SetStyle(wxFontStyle style);
72 void SetWeight(wxFontWeight weight);
409d5a58 73 void SetUnderlined(bool underlined);
85ab460e 74 bool SetFaceName(const wxString& facename);
409d5a58
VZ
75 void SetEncoding(wxFontEncoding encoding);
76
de6185e2 77 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
5ac2e80c 78 bool GetNoAntiAliasing() const { return m_noAA; }
cd9a673c 79
011ba5ed
VZ
80 // and this one also modifies all the other font data fields
81 void SetNativeFontInfo(const wxNativeFontInfo& info);
82
0c5d3e1c
VZ
83protected:
84 // common part of all ctors
85 void Init(int pointSize,
0c14b6c3
FM
86 wxFontFamily family,
87 wxFontStyle style,
88 wxFontWeight weight,
0c5d3e1c
VZ
89 bool underlined,
90 const wxString& faceName,
7826e2dd 91 wxFontEncoding encoding);
0c5d3e1c 92
011ba5ed
VZ
93 // set all fields from (already initialized and valid) m_nativeFontInfo
94 void InitFromNative();
95
0c5d3e1c 96private:
0c14b6c3 97 wxFontFamily m_family;
5f11fef5 98 wxFontEncoding m_encoding;
ecde8361 99 bool m_underlined;
2b5f62a0 100 bool m_noAA; // No anti-aliasing
7826e2dd 101
b5791cc7 102 // The native font info: basically a PangoFontDescription
30764ab5 103 wxNativeFontInfo m_nativeFontInfo;
8bbe427f 104
f6bcfd97 105 friend class wxFont;
c801d85f
KB
106};
107
68c95704 108#define M_FONTDATA ((wxFontRefData*)m_refData)
873fd4af 109
0c5d3e1c 110// ----------------------------------------------------------------------------
cd9a673c 111// wxFontRefData
0c5d3e1c
VZ
112// ----------------------------------------------------------------------------
113
114void wxFontRefData::Init(int pointSize,
0c14b6c3
FM
115 wxFontFamily family,
116 wxFontStyle style,
117 wxFontWeight weight,
0c5d3e1c
VZ
118 bool underlined,
119 const wxString& faceName,
7826e2dd 120 wxFontEncoding encoding)
8bbe427f 121{
409d5a58 122 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
0c5d3e1c 123
0c5d3e1c
VZ
124 m_underlined = underlined;
125 m_encoding = encoding;
295ebfd0
VZ
126 if ( m_encoding == wxFONTENCODING_DEFAULT )
127 m_encoding = wxFont::GetDefaultEncoding();
cd9a673c 128
de6185e2 129 m_noAA = false;
011ba5ed 130
46eed000
RR
131 // Create native font info
132 m_nativeFontInfo.description = pango_font_description_new();
133
011ba5ed 134 // And set its values
ecde8361 135 if (!faceName.empty())
2b5f62a0 136 {
5f11fef5 137 pango_font_description_set_family( m_nativeFontInfo.description,
ecde8361 138 wxGTK_CONV_SYS(faceName) );
2b5f62a0
VZ
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 }
46eed000 157 }
cd9a673c 158
ecde8361
FM
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 );
358fc25c
RR
164}
165
011ba5ed 166void wxFontRefData::InitFromNative()
409d5a58 167{
de6185e2 168 m_noAA = false;
2b5f62a0 169
db16cab4
RR
170 // Get native info
171 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 172
b6b579bd
RR
173 // Pango sometimes needs to have a size
174 int pango_size = pango_font_description_get_size( desc );
175 if (pango_size == 0)
ecde8361 176 m_nativeFontInfo.SetPointSize(wxDEFAULT_FONT_SIZE);
0f6858b6 177
ecde8361
FM
178 wxString faceName = wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc));
179 if (faceName == wxT("monospace"))
db16cab4
RR
180 {
181 m_family = wxFONTFAMILY_TELETYPE;
182 }
ecde8361 183 else if (faceName == wxT("sans"))
db16cab4
RR
184 {
185 m_family = wxFONTFAMILY_SWISS;
186 }
ecde8361 187 else if (faceName == wxT("serif"))
2b5f62a0
VZ
188 {
189 m_family = wxFONTFAMILY_ROMAN;
190 }
db16cab4
RR
191 else
192 {
193 m_family = wxFONTFAMILY_UNKNOWN;
194 }
195
ecde8361 196 // Pango description are never underlined
de6185e2 197 m_underlined = false;
db16cab4 198
5f11fef5
VZ
199 // always with GTK+ 2
200 m_encoding = wxFONTENCODING_UTF8;
409d5a58
VZ
201}
202
011ba5ed 203wxFontRefData::wxFontRefData( const wxFontRefData& data )
8f884a0d 204 : wxGDIRefData()
011ba5ed 205{
011ba5ed 206 m_family = data.m_family;
011ba5ed 207 m_underlined = data.m_underlined;
011ba5ed 208 m_encoding = data.m_encoding;
2b5f62a0 209 m_noAA = data.m_noAA;
cd9a673c
RD
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());
011ba5ed
VZ
215}
216
0c14b6c3
FM
217wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style,
218 wxFontWeight weight, bool underlined,
011ba5ed
VZ
219 const wxString& faceName,
220 wxFontEncoding encoding)
221{
222 Init(size, family, style, weight, underlined, faceName, encoding);
223}
224
225wxFontRefData::wxFontRefData(const wxString& fontname)
8bbe427f 226{
011ba5ed 227 m_nativeFontInfo.FromString( fontname );
011ba5ed
VZ
228
229 InitFromNative();
230}
231
011ba5ed
VZ
232wxFontRefData::~wxFontRefData()
233{
0c5d3e1c 234}
c801d85f 235
0c5d3e1c 236// ----------------------------------------------------------------------------
409d5a58 237// wxFontRefData SetXXX()
0c5d3e1c 238// ----------------------------------------------------------------------------
c801d85f 239
409d5a58 240void wxFontRefData::SetPointSize(int pointSize)
c801d85f 241{
8a15e8ba 242 m_nativeFontInfo.SetPointSize(pointSize);
7826e2dd
VZ
243}
244
b5791cc7
FM
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
253bool 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
0c14b6c3 275void wxFontRefData::SetFamily(wxFontFamily family)
7826e2dd 276{
409d5a58 277 m_family = family;
30764ab5 278
ecde8361
FM
279 // wxNativeInfo::SetFamily asserts because is currently not implemented---
280 // we just save the family here FIXME
30764ab5
VZ
281}
282
0c14b6c3 283void wxFontRefData::SetStyle(wxFontStyle style)
c801d85f 284{
7533ba25 285 m_nativeFontInfo.SetStyle((wxFontStyle)style);
409d5a58 286}
7beba2fc 287
0c14b6c3 288void wxFontRefData::SetWeight(wxFontWeight weight)
409d5a58 289{
7533ba25 290 m_nativeFontInfo.SetWeight((wxFontWeight)weight);
409d5a58 291}
30764ab5 292
409d5a58
VZ
293void wxFontRefData::SetUnderlined(bool underlined)
294{
295 m_underlined = underlined;
8636aed8 296
ecde8361
FM
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
409d5a58 301}
30760ce7 302
85ab460e 303bool wxFontRefData::SetFaceName(const wxString& facename)
409d5a58 304{
85ab460e 305 return m_nativeFontInfo.SetFaceName(facename);
409d5a58 306}
284b4c88 307
409d5a58
VZ
308void wxFontRefData::SetEncoding(wxFontEncoding encoding)
309{
310 m_encoding = encoding;
ecde8361
FM
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?
409d5a58 315}
284b4c88 316
011ba5ed
VZ
317void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
318{
011ba5ed
VZ
319 m_nativeFontInfo = info;
320
321 // set all the other font parameters from the native font info
322 InitFromNative();
323}
324
409d5a58
VZ
325// ----------------------------------------------------------------------------
326// wxFont creation
327// ----------------------------------------------------------------------------
36f210c8 328
409d5a58 329IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
36f210c8 330
409d5a58
VZ
331wxFont::wxFont(const wxNativeFontInfo& info)
332{
011ba5ed 333 Create( info.GetPointSize(),
db16cab4
RR
334 info.GetFamily(),
335 info.GetStyle(),
336 info.GetWeight(),
337 info.GetUnderlined(),
338 info.GetFaceName(),
339 info.GetEncoding() );
409d5a58
VZ
340}
341
342bool wxFont::Create( int pointSize,
b5791cc7
FM
343 wxFontFamily family,
344 wxFontStyle style,
345 wxFontWeight weight,
409d5a58
VZ
346 bool underlined,
347 const wxString& face,
b5791cc7 348 wxFontEncoding encoding )
409d5a58 349{
2b5f62a0
VZ
350 UnRef();
351
409d5a58
VZ
352 m_refData = new wxFontRefData(pointSize, family, style, weight,
353 underlined, face, encoding);
354
de6185e2 355 return true;
409d5a58
VZ
356}
357
358bool wxFont::Create(const wxString& fontname)
359{
360 // VZ: does this really happen?
361 if ( fontname.empty() )
36f210c8 362 {
409d5a58 363 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7beba2fc 364
de6185e2 365 return true;
36f210c8 366 }
409d5a58
VZ
367
368 m_refData = new wxFontRefData(fontname);
369
de6185e2 370 return true;
ff7b1510 371}
c801d85f 372
8bbe427f 373wxFont::~wxFont()
c801d85f 374{
ff7b1510 375}
c801d85f 376
0c5d3e1c
VZ
377// ----------------------------------------------------------------------------
378// accessors
379// ----------------------------------------------------------------------------
c801d85f 380
8bbe427f 381int wxFont::GetPointSize() const
c801d85f 382{
ecde8361 383 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
8bbe427f 384
ecde8361 385 return M_FONTDATA->m_nativeFontInfo.GetPointSize();
ff7b1510 386}
c801d85f 387
8bbe427f 388wxString wxFont::GetFaceName() const
c801d85f 389{
ecde8361 390 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
8bbe427f 391
ecde8361 392 return M_FONTDATA->m_nativeFontInfo.GetFaceName();
ff7b1510 393}
c801d85f 394
0c14b6c3 395wxFontFamily wxFont::GetFamily() const
c801d85f 396{
ecde8361 397 wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX, wxT("invalid font") );
8bbe427f 398
ecde8361 399 wxFontFamily ret = M_FONTDATA->m_nativeFontInfo.GetFamily();
b67d14be
MR
400
401 if (ret == wxFONTFAMILY_DEFAULT)
402 ret = M_FONTDATA->m_family;
403
404 return ret;
ff7b1510 405}
c801d85f 406
0c14b6c3 407wxFontStyle wxFont::GetStyle() const
c801d85f 408{
ecde8361 409 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX, wxT("invalid font") );
d84eb083 410
ecde8361 411 return M_FONTDATA->m_nativeFontInfo.GetStyle();
ff7b1510 412}
c801d85f 413
0c14b6c3 414wxFontWeight wxFont::GetWeight() const
c801d85f 415{
ecde8361 416 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") );
8bbe427f 417
ecde8361 418 return M_FONTDATA->m_nativeFontInfo.GetWeight();
8bbe427f
VZ
419}
420
8bbe427f
VZ
421bool wxFont::GetUnderlined() const
422{
ecde8361 423 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
8bbe427f
VZ
424
425 return M_FONTDATA->m_underlined;
ff7b1510 426}
c801d85f 427
0c5d3e1c 428wxFontEncoding wxFont::GetEncoding() const
358fc25c 429{
ecde8361 430 wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM, wxT("invalid font") );
0c5d3e1c
VZ
431
432 return M_FONTDATA->m_encoding;
358fc25c
RR
433}
434
5ac2e80c 435bool wxFont::GetNoAntiAliasing() const
2b5f62a0 436{
ecde8361 437 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
2b5f62a0
VZ
438
439 return M_FONTDATA->m_noAA;
440}
441
3bf5a59b 442const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
30764ab5 443{
ecde8361 444 wxCHECK_MSG( IsOk(), NULL, wxT("invalid font") );
30764ab5 445
3bf5a59b 446 return &(M_FONTDATA->m_nativeFontInfo);
30764ab5
VZ
447}
448
53f6aab7
VZ
449bool wxFont::IsFixedWidth() const
450{
ecde8361 451 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
53f6aab7 452
53f6aab7
VZ
453 return wxFontBase::IsFixedWidth();
454}
30764ab5 455
0c5d3e1c
VZ
456// ----------------------------------------------------------------------------
457// change font attributes
458// ----------------------------------------------------------------------------
459
358fc25c
RR
460void wxFont::SetPointSize(int pointSize)
461{
fd7a7443 462 AllocExclusive();
011ba5ed 463
409d5a58 464 M_FONTDATA->SetPointSize(pointSize);
358fc25c
RR
465}
466
0c14b6c3 467void wxFont::SetFamily(wxFontFamily family)
358fc25c 468{
fd7a7443 469 AllocExclusive();
358fc25c 470
409d5a58 471 M_FONTDATA->SetFamily(family);
358fc25c
RR
472}
473
0c14b6c3 474void wxFont::SetStyle(wxFontStyle style)
358fc25c 475{
fd7a7443 476 AllocExclusive();
358fc25c 477
409d5a58 478 M_FONTDATA->SetStyle(style);
358fc25c
RR
479}
480
0c14b6c3 481void wxFont::SetWeight(wxFontWeight weight)
358fc25c 482{
fd7a7443 483 AllocExclusive();
358fc25c 484
409d5a58 485 M_FONTDATA->SetWeight(weight);
358fc25c
RR
486}
487
85ab460e 488bool wxFont::SetFaceName(const wxString& faceName)
358fc25c 489{
fd7a7443 490 AllocExclusive();
358fc25c 491
85ab460e
VZ
492 return M_FONTDATA->SetFaceName(faceName) &&
493 wxFontBase::SetFaceName(faceName);
358fc25c
RR
494}
495
496void wxFont::SetUnderlined(bool underlined)
497{
fd7a7443 498 AllocExclusive();
358fc25c 499
409d5a58 500 M_FONTDATA->SetUnderlined(underlined);
358fc25c
RR
501}
502
0c5d3e1c
VZ
503void wxFont::SetEncoding(wxFontEncoding encoding)
504{
fd7a7443 505 AllocExclusive();
c801d85f 506
409d5a58 507 M_FONTDATA->SetEncoding(encoding);
30764ab5
VZ
508}
509
9045ad9d 510void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
2b5f62a0 511{
fd7a7443 512 AllocExclusive();
2b5f62a0
VZ
513
514 M_FONTDATA->SetNativeFontInfo( info );
515}
516
517void wxFont::SetNoAntiAliasing( bool no )
30764ab5 518{
fd7a7443 519 AllocExclusive();
30764ab5 520
2b5f62a0 521 M_FONTDATA->SetNoAntiAliasing( no );
0c5d3e1c 522}
fd7a7443 523
8f884a0d 524wxGDIRefData* wxFont::CreateGDIRefData() const
fd7a7443
PC
525{
526 return new wxFontRefData;
527}
528
8f884a0d 529wxGDIRefData* wxFont::CloneGDIRefData(const wxGDIRefData* data) const
fd7a7443 530{
5c33522f 531 return new wxFontRefData(*static_cast<const wxFontRefData*>(data));
fd7a7443 532}