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