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