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