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