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