]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/font.cpp
fix misc typos
[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 // do we have the native font info?
68 bool HasNativeFont() const
69 {
011ba5ed 70 // we always have a Pango font description
de6185e2 71 return true;
409d5a58
VZ
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);
0c14b6c3
FM
77 void SetFamily(wxFontFamily family);
78 void SetStyle(wxFontStyle style);
79 void SetWeight(wxFontWeight weight);
409d5a58 80 void SetUnderlined(bool underlined);
85ab460e 81 bool SetFaceName(const wxString& facename);
409d5a58
VZ
82 void SetEncoding(wxFontEncoding encoding);
83
de6185e2 84 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
5ac2e80c 85 bool GetNoAntiAliasing() const { return m_noAA; }
cd9a673c 86
011ba5ed
VZ
87 // and this one also modifies all the other font data fields
88 void SetNativeFontInfo(const wxNativeFontInfo& info);
89
0c5d3e1c
VZ
90protected:
91 // common part of all ctors
92 void Init(int pointSize,
0c14b6c3
FM
93 wxFontFamily family,
94 wxFontStyle style,
95 wxFontWeight weight,
0c5d3e1c
VZ
96 bool underlined,
97 const wxString& faceName,
7826e2dd 98 wxFontEncoding encoding);
0c5d3e1c 99
011ba5ed
VZ
100 // set all fields from (already initialized and valid) m_nativeFontInfo
101 void InitFromNative();
102
0c5d3e1c 103private:
2b5f62a0 104 // clear m_scaled_xfonts if any
011ba5ed
VZ
105 void ClearGdkFonts();
106
f35c2659 107 int m_pointSize;
0c14b6c3
FM
108 wxFontFamily m_family;
109 wxFontStyle m_style;
110 wxFontWeight m_weight;
f35c2659
RR
111 bool m_underlined;
112 wxString m_faceName;
5f11fef5 113 wxFontEncoding m_encoding;
2b5f62a0 114 bool m_noAA; // No anti-aliasing
7826e2dd 115
b5791cc7 116 // The native font info: basically a PangoFontDescription
30764ab5 117 wxNativeFontInfo m_nativeFontInfo;
8bbe427f 118
f6bcfd97 119 friend class wxFont;
c801d85f
KB
120};
121
68c95704 122#define M_FONTDATA ((wxFontRefData*)m_refData)
873fd4af 123
0c5d3e1c 124// ----------------------------------------------------------------------------
cd9a673c 125// wxFontRefData
0c5d3e1c
VZ
126// ----------------------------------------------------------------------------
127
128void wxFontRefData::Init(int pointSize,
0c14b6c3
FM
129 wxFontFamily family,
130 wxFontStyle style,
131 wxFontWeight weight,
0c5d3e1c
VZ
132 bool underlined,
133 const wxString& faceName,
7826e2dd 134 wxFontEncoding encoding)
8bbe427f 135{
409d5a58 136 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
0c5d3e1c
VZ
137
138 m_faceName = faceName;
139
409d5a58
VZ
140 // we accept both wxDEFAULT and wxNORMAL here - should we?
141 m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
142 m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
0c5d3e1c 143
409d5a58
VZ
144 // and here, do we really want to forbid creation of the font of the size
145 // 90 (the value of wxDEFAULT)??
011ba5ed
VZ
146 m_pointSize = pointSize == wxDEFAULT || pointSize == -1
147 ? wxDEFAULT_FONT_SIZE
148 : pointSize;
0c5d3e1c
VZ
149
150 m_underlined = underlined;
151 m_encoding = encoding;
295ebfd0
VZ
152 if ( m_encoding == wxFONTENCODING_DEFAULT )
153 m_encoding = wxFont::GetDefaultEncoding();
cd9a673c 154
de6185e2 155 m_noAA = false;
011ba5ed 156
46eed000
RR
157 // Create native font info
158 m_nativeFontInfo.description = pango_font_description_new();
159
011ba5ed 160 // And set its values
2b5f62a0
VZ
161 if (!m_faceName.empty())
162 {
5f11fef5
VZ
163 pango_font_description_set_family( m_nativeFontInfo.description,
164 wxGTK_CONV_SYS(m_faceName) );
2b5f62a0
VZ
165 }
166 else
167 {
168 switch (m_family)
169 {
170 case wxFONTFAMILY_MODERN:
171 case wxFONTFAMILY_TELETYPE:
172 pango_font_description_set_family( m_nativeFontInfo.description, "monospace" );
173 break;
174 case wxFONTFAMILY_ROMAN:
175 pango_font_description_set_family( m_nativeFontInfo.description, "serif" );
176 break;
177 case wxFONTFAMILY_SWISS:
178 // SWISS = sans serif
179 default:
180 pango_font_description_set_family( m_nativeFontInfo.description, "sans" );
181 break;
182 }
46eed000 183 }
cd9a673c 184
46eed000
RR
185 SetStyle( m_style );
186 SetPointSize( m_pointSize );
187 SetWeight( m_weight );
358fc25c
RR
188}
189
011ba5ed 190void wxFontRefData::InitFromNative()
409d5a58 191{
de6185e2 192 m_noAA = false;
2b5f62a0 193
db16cab4
RR
194 // Get native info
195 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 196
db16cab4 197 // init fields
30083ad8 198 m_faceName = wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc));
011ba5ed 199
b6b579bd
RR
200 // Pango sometimes needs to have a size
201 int pango_size = pango_font_description_get_size( desc );
202 if (pango_size == 0)
d332c514 203 m_nativeFontInfo.SetPointSize(12);
0f6858b6 204
d332c514
MR
205 m_pointSize = m_nativeFontInfo.GetPointSize();
206 m_style = m_nativeFontInfo.GetStyle();
207 m_weight = m_nativeFontInfo.GetWeight();
011ba5ed 208
a732ef91 209 if (m_faceName == wxT("monospace"))
db16cab4
RR
210 {
211 m_family = wxFONTFAMILY_TELETYPE;
212 }
213 else if (m_faceName == wxT("sans"))
214 {
215 m_family = wxFONTFAMILY_SWISS;
216 }
2b5f62a0
VZ
217 else if (m_faceName == wxT("serif"))
218 {
219 m_family = wxFONTFAMILY_ROMAN;
220 }
db16cab4
RR
221 else
222 {
223 m_family = wxFONTFAMILY_UNKNOWN;
224 }
225
226 // Pango description are never underlined (?)
de6185e2 227 m_underlined = false;
db16cab4 228
5f11fef5
VZ
229 // always with GTK+ 2
230 m_encoding = wxFONTENCODING_UTF8;
409d5a58
VZ
231}
232
011ba5ed 233wxFontRefData::wxFontRefData( const wxFontRefData& data )
8f884a0d 234 : wxGDIRefData()
011ba5ed
VZ
235{
236 m_pointSize = data.m_pointSize;
237 m_family = data.m_family;
238 m_style = data.m_style;
239 m_weight = data.m_weight;
240
241 m_underlined = data.m_underlined;
242
243 m_faceName = data.m_faceName;
244 m_encoding = data.m_encoding;
245
2b5f62a0 246 m_noAA = data.m_noAA;
cd9a673c
RD
247
248 // Forces a copy of the internal data. wxNativeFontInfo should probably
249 // have a copy ctor and assignment operator to fix this properly but that
250 // would break binary compatibility...
251 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
011ba5ed
VZ
252}
253
0c14b6c3
FM
254wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style,
255 wxFontWeight weight, bool underlined,
011ba5ed
VZ
256 const wxString& faceName,
257 wxFontEncoding encoding)
258{
259 Init(size, family, style, weight, underlined, faceName, encoding);
260}
261
262wxFontRefData::wxFontRefData(const wxString& fontname)
8bbe427f 263{
011ba5ed 264 m_nativeFontInfo.FromString( fontname );
011ba5ed
VZ
265
266 InitFromNative();
267}
268
011ba5ed
VZ
269void wxFontRefData::ClearGdkFonts()
270{
2b5f62a0 271}
011ba5ed
VZ
272
273wxFontRefData::~wxFontRefData()
274{
275 ClearGdkFonts();
0c5d3e1c 276}
c801d85f 277
0c5d3e1c 278// ----------------------------------------------------------------------------
409d5a58 279// wxFontRefData SetXXX()
0c5d3e1c 280// ----------------------------------------------------------------------------
c801d85f 281
409d5a58 282void wxFontRefData::SetPointSize(int pointSize)
c801d85f 283{
409d5a58 284 m_pointSize = pointSize;
c801d85f 285
8a15e8ba 286 m_nativeFontInfo.SetPointSize(pointSize);
7826e2dd
VZ
287}
288
b5791cc7
FM
289/*
290 NOTE: disabled because pango_font_description_set_absolute_size() and
291 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
292 size of "30" makes the latter return 36...
293 Besides, we need to return GetPointSize() a point size value even if
294 SetPixelSize() was used and this would require further changes
295 (and use of pango_font_description_get_size_is_absolute in some places).
296
297bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
298{
299 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
300 "Negative values for the pixel size or zero pixel height are not allowed" );
301
302 if (wx_pango_version_check(1,8,0) != NULL ||
303 pixelSize.GetWidth() != 0)
304 {
305 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
306 // if the user set the pixel width of the font explicitly or the pango
307 // library is too old, we cannot proceed
308 return false;
309 }
310
311 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
312 pixelSize.GetHeight() * PANGO_SCALE );
313
314 return true;
315}
316
317*/
318
0c14b6c3 319void wxFontRefData::SetFamily(wxFontFamily family)
7826e2dd 320{
409d5a58 321 m_family = family;
30764ab5 322
409d5a58 323 // TODO: what are we supposed to do with m_nativeFontInfo here?
30764ab5
VZ
324}
325
0c14b6c3 326void wxFontRefData::SetStyle(wxFontStyle style)
c801d85f 327{
409d5a58
VZ
328 m_style = style;
329
7533ba25 330 m_nativeFontInfo.SetStyle((wxFontStyle)style);
409d5a58 331}
7beba2fc 332
0c14b6c3 333void wxFontRefData::SetWeight(wxFontWeight weight)
409d5a58
VZ
334{
335 m_weight = weight;
8bbe427f 336
7533ba25 337 m_nativeFontInfo.SetWeight((wxFontWeight)weight);
409d5a58 338}
30764ab5 339
409d5a58
VZ
340void wxFontRefData::SetUnderlined(bool underlined)
341{
342 m_underlined = underlined;
8636aed8 343
409d5a58
VZ
344 // the XLFD doesn't have "underlined" field anyhow
345}
30760ce7 346
85ab460e 347bool wxFontRefData::SetFaceName(const wxString& facename)
409d5a58
VZ
348{
349 m_faceName = facename;
7beba2fc 350
85ab460e 351 return m_nativeFontInfo.SetFaceName(facename);
409d5a58 352}
284b4c88 353
409d5a58
VZ
354void wxFontRefData::SetEncoding(wxFontEncoding encoding)
355{
356 m_encoding = encoding;
409d5a58 357}
284b4c88 358
011ba5ed
VZ
359void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
360{
361 // previously cached fonts shouldn't be used
362 ClearGdkFonts();
363
364 m_nativeFontInfo = info;
365
366 // set all the other font parameters from the native font info
367 InitFromNative();
368}
369
409d5a58
VZ
370// ----------------------------------------------------------------------------
371// wxFont creation
372// ----------------------------------------------------------------------------
36f210c8 373
409d5a58 374IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
36f210c8 375
409d5a58
VZ
376wxFont::wxFont(const wxNativeFontInfo& info)
377{
011ba5ed 378 Create( info.GetPointSize(),
db16cab4
RR
379 info.GetFamily(),
380 info.GetStyle(),
381 info.GetWeight(),
382 info.GetUnderlined(),
383 info.GetFaceName(),
384 info.GetEncoding() );
409d5a58
VZ
385}
386
387bool wxFont::Create( int pointSize,
b5791cc7
FM
388 wxFontFamily family,
389 wxFontStyle style,
390 wxFontWeight weight,
409d5a58
VZ
391 bool underlined,
392 const wxString& face,
b5791cc7 393 wxFontEncoding encoding )
409d5a58 394{
2b5f62a0
VZ
395 UnRef();
396
409d5a58
VZ
397 m_refData = new wxFontRefData(pointSize, family, style, weight,
398 underlined, face, encoding);
399
de6185e2 400 return true;
409d5a58
VZ
401}
402
403bool wxFont::Create(const wxString& fontname)
404{
405 // VZ: does this really happen?
406 if ( fontname.empty() )
36f210c8 407 {
409d5a58 408 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7beba2fc 409
de6185e2 410 return true;
36f210c8 411 }
409d5a58
VZ
412
413 m_refData = new wxFontRefData(fontname);
414
de6185e2 415 return true;
ff7b1510 416}
c801d85f 417
8bbe427f 418wxFont::~wxFont()
c801d85f 419{
ff7b1510 420}
c801d85f 421
0c5d3e1c
VZ
422// ----------------------------------------------------------------------------
423// accessors
424// ----------------------------------------------------------------------------
c801d85f 425
8bbe427f 426int wxFont::GetPointSize() const
c801d85f 427{
223d09f6 428 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f 429
02d9204c
MR
430 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetPointSize()
431 : M_FONTDATA->m_pointSize;
ff7b1510 432}
c801d85f 433
8bbe427f 434wxString wxFont::GetFaceName() const
c801d85f 435{
e4db172a 436 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
8bbe427f 437
02d9204c
MR
438 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetFaceName()
439 : M_FONTDATA->m_faceName;
ff7b1510 440}
c801d85f 441
0c14b6c3 442wxFontFamily wxFont::GetFamily() const
c801d85f 443{
0c14b6c3 444 wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
8bbe427f 445
0c14b6c3 446 wxFontFamily ret = M_FONTDATA->m_family;
b67d14be
MR
447 if (M_FONTDATA->HasNativeFont())
448 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
449 ret = M_FONTDATA->m_nativeFontInfo.GetFamily();
450
451 if (ret == wxFONTFAMILY_DEFAULT)
452 ret = M_FONTDATA->m_family;
453
454 return ret;
ff7b1510 455}
c801d85f 456
0c14b6c3 457wxFontStyle wxFont::GetStyle() const
c801d85f 458{
0c14b6c3 459 wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX, wxT("invalid font") );
d84eb083 460
02d9204c
MR
461 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetStyle()
462 : M_FONTDATA->m_style;
ff7b1510 463}
c801d85f 464
0c14b6c3 465wxFontWeight wxFont::GetWeight() const
c801d85f 466{
0c14b6c3 467 wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX, wxT("invalid font") );
8bbe427f 468
02d9204c
MR
469 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetWeight()
470 : M_FONTDATA->m_weight;
8bbe427f
VZ
471}
472
8bbe427f
VZ
473bool wxFont::GetUnderlined() const
474{
de6185e2 475 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
8bbe427f
VZ
476
477 return M_FONTDATA->m_underlined;
ff7b1510 478}
c801d85f 479
0c5d3e1c 480wxFontEncoding wxFont::GetEncoding() const
358fc25c 481{
5f11fef5 482 wxCHECK_MSG( Ok(), wxFONTENCODING_SYSTEM, wxT("invalid font") );
0c5d3e1c
VZ
483
484 return M_FONTDATA->m_encoding;
358fc25c
RR
485}
486
5ac2e80c 487bool wxFont::GetNoAntiAliasing() const
2b5f62a0 488{
5f11fef5 489 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
2b5f62a0
VZ
490
491 return M_FONTDATA->m_noAA;
492}
493
3bf5a59b 494const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
30764ab5 495{
d3b9f782 496 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
30764ab5 497
3bf5a59b 498 return &(M_FONTDATA->m_nativeFontInfo);
30764ab5
VZ
499}
500
53f6aab7
VZ
501bool wxFont::IsFixedWidth() const
502{
de6185e2 503 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
53f6aab7 504
53f6aab7
VZ
505 return wxFontBase::IsFixedWidth();
506}
30764ab5 507
0c5d3e1c
VZ
508// ----------------------------------------------------------------------------
509// change font attributes
510// ----------------------------------------------------------------------------
511
358fc25c
RR
512void wxFont::SetPointSize(int pointSize)
513{
fd7a7443 514 AllocExclusive();
011ba5ed 515
409d5a58 516 M_FONTDATA->SetPointSize(pointSize);
358fc25c
RR
517}
518
0c14b6c3 519void wxFont::SetFamily(wxFontFamily family)
358fc25c 520{
fd7a7443 521 AllocExclusive();
358fc25c 522
409d5a58 523 M_FONTDATA->SetFamily(family);
358fc25c
RR
524}
525
0c14b6c3 526void wxFont::SetStyle(wxFontStyle style)
358fc25c 527{
fd7a7443 528 AllocExclusive();
358fc25c 529
409d5a58 530 M_FONTDATA->SetStyle(style);
358fc25c
RR
531}
532
0c14b6c3 533void wxFont::SetWeight(wxFontWeight weight)
358fc25c 534{
fd7a7443 535 AllocExclusive();
358fc25c 536
409d5a58 537 M_FONTDATA->SetWeight(weight);
358fc25c
RR
538}
539
85ab460e 540bool wxFont::SetFaceName(const wxString& faceName)
358fc25c 541{
fd7a7443 542 AllocExclusive();
358fc25c 543
85ab460e
VZ
544 return M_FONTDATA->SetFaceName(faceName) &&
545 wxFontBase::SetFaceName(faceName);
358fc25c
RR
546}
547
548void wxFont::SetUnderlined(bool underlined)
549{
fd7a7443 550 AllocExclusive();
358fc25c 551
409d5a58 552 M_FONTDATA->SetUnderlined(underlined);
358fc25c
RR
553}
554
0c5d3e1c
VZ
555void wxFont::SetEncoding(wxFontEncoding encoding)
556{
fd7a7443 557 AllocExclusive();
c801d85f 558
409d5a58 559 M_FONTDATA->SetEncoding(encoding);
30764ab5
VZ
560}
561
9045ad9d 562void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
2b5f62a0 563{
fd7a7443 564 AllocExclusive();
2b5f62a0
VZ
565
566 M_FONTDATA->SetNativeFontInfo( info );
567}
568
569void wxFont::SetNoAntiAliasing( bool no )
30764ab5 570{
fd7a7443 571 AllocExclusive();
30764ab5 572
2b5f62a0 573 M_FONTDATA->SetNoAntiAliasing( no );
0c5d3e1c 574}
fd7a7443 575
8f884a0d 576wxGDIRefData* wxFont::CreateGDIRefData() const
fd7a7443
PC
577{
578 return new wxFontRefData;
579}
580
8f884a0d 581wxGDIRefData* wxFont::CloneGDIRefData(const wxGDIRefData* data) const
fd7a7443 582{
5c33522f 583 return new wxFontRefData(*static_cast<const wxFontRefData*>(data));
fd7a7443 584}