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