]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/font.cpp
Remove obsolete GTK1 compatibility macros. Patch #1413458 - Paul Cornett
[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;
db16cab4 119 wxFontEncoding m_encoding; // Unused under GTK 2.0
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 {
166 pango_font_description_set_family( m_nativeFontInfo.description, wxGTK_CONV(m_faceName) );
167 }
168 else
169 {
170 switch (m_family)
171 {
172 case wxFONTFAMILY_MODERN:
173 case wxFONTFAMILY_TELETYPE:
174 pango_font_description_set_family( m_nativeFontInfo.description, "monospace" );
175 break;
176 case wxFONTFAMILY_ROMAN:
177 pango_font_description_set_family( m_nativeFontInfo.description, "serif" );
178 break;
179 case wxFONTFAMILY_SWISS:
180 // SWISS = sans serif
181 default:
182 pango_font_description_set_family( m_nativeFontInfo.description, "sans" );
183 break;
184 }
46eed000 185 }
cd9a673c 186
46eed000
RR
187 SetStyle( m_style );
188 SetPointSize( m_pointSize );
189 SetWeight( m_weight );
358fc25c
RR
190}
191
011ba5ed 192void wxFontRefData::InitFromNative()
409d5a58 193{
2b5f62a0
VZ
194 m_noAA = FALSE;
195
db16cab4
RR
196 // Get native info
197 PangoFontDescription *desc = m_nativeFontInfo.description;
011ba5ed 198
db16cab4
RR
199 // init fields
200 m_faceName = wxGTK_CONV_BACK( pango_font_description_get_family( desc ) );
011ba5ed 201
b6b579bd
RR
202 // Pango sometimes needs to have a size
203 int pango_size = pango_font_description_get_size( desc );
204 if (pango_size == 0)
d332c514 205 m_nativeFontInfo.SetPointSize(12);
0f6858b6 206
d332c514
MR
207 m_pointSize = m_nativeFontInfo.GetPointSize();
208 m_style = m_nativeFontInfo.GetStyle();
209 m_weight = m_nativeFontInfo.GetWeight();
011ba5ed 210
a732ef91 211 if (m_faceName == wxT("monospace"))
db16cab4
RR
212 {
213 m_family = wxFONTFAMILY_TELETYPE;
214 }
215 else if (m_faceName == wxT("sans"))
216 {
217 m_family = wxFONTFAMILY_SWISS;
218 }
2b5f62a0
VZ
219 else if (m_faceName == wxT("serif"))
220 {
221 m_family = wxFONTFAMILY_ROMAN;
222 }
db16cab4
RR
223 else
224 {
225 m_family = wxFONTFAMILY_UNKNOWN;
226 }
227
228 // Pango description are never underlined (?)
229 m_underlined = FALSE;
230
231 // Cannot we choose that
232 m_encoding = wxFONTENCODING_SYSTEM;
409d5a58
VZ
233}
234
011ba5ed
VZ
235wxFontRefData::wxFontRefData( const wxFontRefData& data )
236 : wxObjectRefData()
237{
238 m_pointSize = data.m_pointSize;
239 m_family = data.m_family;
240 m_style = data.m_style;
241 m_weight = data.m_weight;
242
243 m_underlined = data.m_underlined;
244
245 m_faceName = data.m_faceName;
246 m_encoding = data.m_encoding;
247
2b5f62a0 248 m_noAA = data.m_noAA;
cd9a673c
RD
249
250 // Forces a copy of the internal data. wxNativeFontInfo should probably
251 // have a copy ctor and assignment operator to fix this properly but that
252 // would break binary compatibility...
253 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
011ba5ed
VZ
254}
255
256wxFontRefData::wxFontRefData(int size, int family, int style,
257 int weight, bool underlined,
258 const wxString& faceName,
259 wxFontEncoding encoding)
260{
261 Init(size, family, style, weight, underlined, faceName, encoding);
262}
263
264wxFontRefData::wxFontRefData(const wxString& fontname)
8bbe427f 265{
011ba5ed 266 m_nativeFontInfo.FromString( fontname );
011ba5ed
VZ
267
268 InitFromNative();
269}
270
011ba5ed
VZ
271void wxFontRefData::ClearGdkFonts()
272{
2b5f62a0 273}
011ba5ed
VZ
274
275wxFontRefData::~wxFontRefData()
276{
277 ClearGdkFonts();
0c5d3e1c 278}
c801d85f 279
0c5d3e1c 280// ----------------------------------------------------------------------------
409d5a58 281// wxFontRefData SetXXX()
0c5d3e1c 282// ----------------------------------------------------------------------------
c801d85f 283
409d5a58 284void wxFontRefData::SetPointSize(int pointSize)
c801d85f 285{
409d5a58 286 m_pointSize = pointSize;
c801d85f 287
8a15e8ba 288 m_nativeFontInfo.SetPointSize(pointSize);
7826e2dd
VZ
289}
290
409d5a58 291void wxFontRefData::SetFamily(int family)
7826e2dd 292{
409d5a58 293 m_family = family;
30764ab5 294
409d5a58 295 // TODO: what are we supposed to do with m_nativeFontInfo here?
30764ab5
VZ
296}
297
409d5a58 298void wxFontRefData::SetStyle(int style)
c801d85f 299{
409d5a58
VZ
300 m_style = style;
301
7533ba25 302 m_nativeFontInfo.SetStyle((wxFontStyle)style);
409d5a58 303}
7beba2fc 304
409d5a58
VZ
305void wxFontRefData::SetWeight(int weight)
306{
307 m_weight = weight;
8bbe427f 308
7533ba25 309 m_nativeFontInfo.SetWeight((wxFontWeight)weight);
409d5a58 310}
30764ab5 311
409d5a58
VZ
312void wxFontRefData::SetUnderlined(bool underlined)
313{
314 m_underlined = underlined;
8636aed8 315
409d5a58
VZ
316 // the XLFD doesn't have "underlined" field anyhow
317}
30760ce7 318
409d5a58
VZ
319void wxFontRefData::SetFaceName(const wxString& facename)
320{
321 m_faceName = facename;
7beba2fc 322
8a15e8ba 323 m_nativeFontInfo.SetFaceName(facename);
409d5a58 324}
284b4c88 325
409d5a58
VZ
326void wxFontRefData::SetEncoding(wxFontEncoding encoding)
327{
328 m_encoding = encoding;
409d5a58 329}
284b4c88 330
011ba5ed
VZ
331void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
332{
333 // previously cached fonts shouldn't be used
334 ClearGdkFonts();
335
336 m_nativeFontInfo = info;
337
338 // set all the other font parameters from the native font info
339 InitFromNative();
340}
341
409d5a58
VZ
342// ----------------------------------------------------------------------------
343// wxFont creation
344// ----------------------------------------------------------------------------
36f210c8 345
409d5a58 346IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
36f210c8 347
409d5a58
VZ
348void wxFont::Init()
349{
350}
36f210c8 351
409d5a58
VZ
352wxFont::wxFont(const wxNativeFontInfo& info)
353{
354 Init();
355
011ba5ed 356 Create( info.GetPointSize(),
db16cab4
RR
357 info.GetFamily(),
358 info.GetStyle(),
359 info.GetWeight(),
360 info.GetUnderlined(),
361 info.GetFaceName(),
362 info.GetEncoding() );
409d5a58
VZ
363}
364
365bool wxFont::Create( int pointSize,
366 int family,
367 int style,
368 int weight,
369 bool underlined,
370 const wxString& face,
371 wxFontEncoding encoding)
372{
2b5f62a0
VZ
373 UnRef();
374
409d5a58
VZ
375 m_refData = new wxFontRefData(pointSize, family, style, weight,
376 underlined, face, encoding);
377
378 return TRUE;
379}
380
381bool wxFont::Create(const wxString& fontname)
382{
383 // VZ: does this really happen?
384 if ( fontname.empty() )
36f210c8 385 {
409d5a58 386 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7beba2fc 387
409d5a58 388 return TRUE;
36f210c8 389 }
409d5a58
VZ
390
391 m_refData = new wxFontRefData(fontname);
392
0c5d3e1c 393 return TRUE;
ff7b1510 394}
c801d85f 395
0c5d3e1c 396void wxFont::Unshare()
8bbe427f 397{
0c5d3e1c
VZ
398 if (!m_refData)
399 {
400 m_refData = new wxFontRefData();
401 }
402 else
403 {
404 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
405 UnRef();
406 m_refData = ref;
407 }
ff7b1510 408}
c801d85f 409
8bbe427f 410wxFont::~wxFont()
c801d85f 411{
ff7b1510 412}
c801d85f 413
0c5d3e1c
VZ
414// ----------------------------------------------------------------------------
415// accessors
416// ----------------------------------------------------------------------------
c801d85f 417
8bbe427f 418int wxFont::GetPointSize() const
c801d85f 419{
223d09f6 420 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f 421
02d9204c
MR
422#if wxUSE_PANGO
423 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetPointSize()
424 : M_FONTDATA->m_pointSize;
425#else
8bbe427f 426 return M_FONTDATA->m_pointSize;
02d9204c 427#endif
ff7b1510 428}
c801d85f 429
8bbe427f 430wxString wxFont::GetFaceName() const
c801d85f 431{
223d09f6 432 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
8bbe427f 433
02d9204c
MR
434#if wxUSE_PANGO
435 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetFaceName()
436 : M_FONTDATA->m_faceName;
437#else
36b3b54a 438 return M_FONTDATA->m_faceName;
02d9204c 439#endif
ff7b1510 440}
c801d85f 441
8bbe427f 442int wxFont::GetFamily() const
c801d85f 443{
223d09f6 444 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f 445
02d9204c 446#if wxUSE_PANGO
b67d14be
MR
447 int ret = M_FONTDATA->m_family;
448 if (M_FONTDATA->HasNativeFont())
449 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
450 ret = M_FONTDATA->m_nativeFontInfo.GetFamily();
451
452 if (ret == wxFONTFAMILY_DEFAULT)
453 ret = M_FONTDATA->m_family;
454
455 return ret;
02d9204c 456#else
8bbe427f 457 return M_FONTDATA->m_family;
02d9204c 458#endif
ff7b1510 459}
c801d85f 460
8bbe427f 461int wxFont::GetStyle() const
c801d85f 462{
223d09f6 463 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
d84eb083 464
02d9204c
MR
465#if wxUSE_PANGO
466 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetStyle()
467 : M_FONTDATA->m_style;
468#else
8bbe427f 469 return M_FONTDATA->m_style;
02d9204c 470#endif
ff7b1510 471}
c801d85f 472
8bbe427f 473int wxFont::GetWeight() const
c801d85f 474{
223d09f6 475 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f 476
02d9204c
MR
477#if wxUSE_PANGO
478 return M_FONTDATA->HasNativeFont() ? M_FONTDATA->m_nativeFontInfo.GetWeight()
479 : M_FONTDATA->m_weight;
480#else
8bbe427f 481 return M_FONTDATA->m_weight;
02d9204c 482#endif
8bbe427f
VZ
483}
484
8bbe427f
VZ
485bool wxFont::GetUnderlined() const
486{
223d09f6 487 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
8bbe427f
VZ
488
489 return M_FONTDATA->m_underlined;
ff7b1510 490}
c801d85f 491
0c5d3e1c 492wxFontEncoding wxFont::GetEncoding() const
358fc25c 493{
223d09f6 494 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
0c5d3e1c 495
02d9204c 496 // m_encoding is unused in wxGTK2, return encoding that the user set.
0c5d3e1c 497 return M_FONTDATA->m_encoding;
358fc25c
RR
498}
499
5ac2e80c 500bool wxFont::GetNoAntiAliasing() const
2b5f62a0
VZ
501{
502 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
503
504 return M_FONTDATA->m_noAA;
505}
506
3bf5a59b 507const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
30764ab5 508{
7826e2dd 509 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
30764ab5 510
3bf5a59b 511 return &(M_FONTDATA->m_nativeFontInfo);
30764ab5
VZ
512}
513
53f6aab7
VZ
514bool wxFont::IsFixedWidth() const
515{
516 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
517
53f6aab7
VZ
518 return wxFontBase::IsFixedWidth();
519}
30764ab5 520
0c5d3e1c
VZ
521// ----------------------------------------------------------------------------
522// change font attributes
523// ----------------------------------------------------------------------------
524
358fc25c
RR
525void wxFont::SetPointSize(int pointSize)
526{
527 Unshare();
011ba5ed 528
409d5a58 529 M_FONTDATA->SetPointSize(pointSize);
358fc25c
RR
530}
531
532void wxFont::SetFamily(int family)
533{
534 Unshare();
535
409d5a58 536 M_FONTDATA->SetFamily(family);
358fc25c
RR
537}
538
539void wxFont::SetStyle(int style)
540{
541 Unshare();
542
409d5a58 543 M_FONTDATA->SetStyle(style);
358fc25c
RR
544}
545
546void wxFont::SetWeight(int weight)
547{
548 Unshare();
549
409d5a58 550 M_FONTDATA->SetWeight(weight);
358fc25c
RR
551}
552
553void wxFont::SetFaceName(const wxString& faceName)
554{
555 Unshare();
556
409d5a58 557 M_FONTDATA->SetFaceName(faceName);
358fc25c
RR
558}
559
560void wxFont::SetUnderlined(bool underlined)
561{
562 Unshare();
563
409d5a58 564 M_FONTDATA->SetUnderlined(underlined);
358fc25c
RR
565}
566
0c5d3e1c
VZ
567void wxFont::SetEncoding(wxFontEncoding encoding)
568{
569 Unshare();
c801d85f 570
409d5a58 571 M_FONTDATA->SetEncoding(encoding);
30764ab5
VZ
572}
573
9045ad9d 574void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
2b5f62a0
VZ
575{
576 Unshare();
577
578 M_FONTDATA->SetNativeFontInfo( info );
579}
580
581void wxFont::SetNoAntiAliasing( bool no )
30764ab5
VZ
582{
583 Unshare();
584
2b5f62a0 585 M_FONTDATA->SetNoAntiAliasing( no );
0c5d3e1c 586}