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