]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/font.cpp
Removed two more files from wxMicroWindows compilation.
[wxWidgets.git] / src / gtk / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: font.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 #ifdef __GNUG__
19 #pragma implementation "font.h"
20 #endif
21
22 #include "wx/font.h"
23 #include "wx/fontutil.h"
24 #include "wx/cmndata.h"
25 #include "wx/utils.h"
26 #include "wx/log.h"
27 #include "wx/gdicmn.h"
28 #include "wx/tokenzr.h"
29 #include "wx/settings.h"
30
31 #include <strings.h>
32
33 #include <gdk/gdk.h>
34 #include <gdk/gdkprivate.h>
35 #include <gtk/gtk.h>
36
37 // ----------------------------------------------------------------------------
38 // wxFontRefData
39 // ----------------------------------------------------------------------------
40
41 class wxFontRefData : public wxObjectRefData
42 {
43 public:
44 wxFontRefData(int size = wxDEFAULT,
45 int family = wxDEFAULT,
46 int style = wxDEFAULT,
47 int weight = wxDEFAULT,
48 bool underlined = FALSE,
49 const wxString& faceName = wxEmptyString,
50 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
51 wxFontRefData( const wxFontRefData& data );
52 virtual ~wxFontRefData();
53
54 protected:
55 // common part of all ctors
56 void Init(int pointSize,
57 int family,
58 int style,
59 int weight,
60 bool underlined,
61 const wxString& faceName,
62 wxFontEncoding encoding);
63
64 private:
65 wxList m_scaled_xfonts;
66 int m_pointSize;
67 int m_family,
68 m_style,
69 m_weight;
70 bool m_underlined;
71 wxString m_faceName;
72 wxFontEncoding m_encoding;
73
74 wxNativeFontInfo m_nativeFontInfo;
75
76 friend class wxFont;
77 };
78
79 // ============================================================================
80 // implementation
81 // ============================================================================
82
83 // ----------------------------------------------------------------------------
84 // wxFontRefData
85 // ----------------------------------------------------------------------------
86
87 void wxFontRefData::Init(int pointSize,
88 int family,
89 int style,
90 int weight,
91 bool underlined,
92 const wxString& faceName,
93 wxFontEncoding encoding)
94 {
95 if (family == wxDEFAULT)
96 m_family = wxSWISS;
97 else
98 m_family = family;
99
100 m_faceName = faceName;
101
102 if (style == wxDEFAULT)
103 m_style = wxNORMAL;
104 else
105 m_style = style;
106
107 if (weight == wxDEFAULT)
108 m_weight = wxNORMAL;
109 else
110 m_weight = weight;
111
112 if (pointSize == wxDEFAULT)
113 m_pointSize = 12;
114 else
115 m_pointSize = pointSize;
116
117 m_underlined = underlined;
118 m_encoding = encoding;
119 }
120
121 wxFontRefData::wxFontRefData( const wxFontRefData& data )
122 : m_scaled_xfonts(wxKEY_INTEGER)
123 {
124 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
125 data.m_underlined, data.m_faceName, data.m_encoding);
126 }
127
128 wxFontRefData::wxFontRefData(int size, int family, int style,
129 int weight, bool underlined,
130 const wxString& faceName,
131 wxFontEncoding encoding)
132 : m_scaled_xfonts(wxKEY_INTEGER)
133 {
134 Init(size, family, style, weight, underlined, faceName, encoding);
135 }
136
137 wxFontRefData::~wxFontRefData()
138 {
139 wxNode *node = m_scaled_xfonts.First();
140 while (node)
141 {
142 GdkFont *font = (GdkFont*)node->Data();
143 wxNode *next = node->Next();
144 gdk_font_unref( font );
145 node = next;
146 }
147 }
148
149 // ----------------------------------------------------------------------------
150 // wxFont
151 // ----------------------------------------------------------------------------
152
153 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
154
155 void wxFont::Init()
156 {
157 }
158
159 wxFont::wxFont(const wxNativeFontInfo& info)
160 {
161 Init();
162
163 Create(info.xFontName);
164 }
165
166 bool wxFont::Create(const wxNativeFontInfo& info)
167 {
168 return Create(info.xFontName);
169 }
170
171 bool wxFont::Create( int pointSize,
172 int family,
173 int style,
174 int weight,
175 bool underlined,
176 const wxString& face,
177 wxFontEncoding encoding)
178 {
179 m_refData = new wxFontRefData(pointSize, family, style, weight,
180 underlined, face, encoding);
181
182 return TRUE;
183 }
184
185 bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
186 {
187 if( !fontname )
188 {
189 *this = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT);
190 return TRUE;
191 }
192
193 m_refData = new wxFontRefData();
194
195 M_FONTDATA->m_nativeFontInfo.xFontName = fontname; // X font name
196
197 wxString tmp;
198
199 wxStringTokenizer tn( fontname, wxT("-") );
200
201 tn.GetNextToken(); // skip initial empty token
202 tn.GetNextToken(); // foundry
203
204
205 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
206
207 tmp = tn.GetNextToken().MakeUpper(); // weight
208 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
209 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD;
210 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
211 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD;
212 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
213
214 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
215 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
216
217 tmp = tn.GetNextToken().MakeUpper(); // slant
218 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
219 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
220
221 tn.GetNextToken(); // set width
222 tn.GetNextToken(); // add. style
223 tn.GetNextToken(); // pixel size
224
225 tmp = tn.GetNextToken(); // pointsize
226 if (tmp != wxT("*"))
227 {
228 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
229 M_FONTDATA->m_pointSize = (int)(num / 10);
230 }
231
232 tn.GetNextToken(); // x-res
233 tn.GetNextToken(); // y-res
234
235 tmp = tn.GetNextToken().MakeUpper(); // spacing
236
237 if (tmp == wxT("M"))
238 M_FONTDATA->m_family = wxMODERN;
239 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
240 M_FONTDATA->m_family = wxROMAN;
241 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
242 M_FONTDATA->m_family = wxSWISS;
243 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
244 M_FONTDATA->m_family = wxTELETYPE;
245 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
246 M_FONTDATA->m_family = wxDECORATIVE;
247 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
248 M_FONTDATA->m_family = wxSCRIPT;
249
250 tn.GetNextToken(); // avg width
251
252 // deal with font encoding
253 M_FONTDATA->m_encoding = enc;
254 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
255 {
256 wxString registry = tn.GetNextToken().MakeUpper(),
257 encoding = tn.GetNextToken().MakeUpper();
258
259 if ( registry == _T("ISO8859") )
260 {
261 int cp;
262 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
263 {
264 M_FONTDATA->m_encoding =
265 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
266 }
267 }
268 else if ( registry == _T("MICROSOFT") )
269 {
270 int cp;
271 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
272 {
273 M_FONTDATA->m_encoding =
274 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
275 }
276 }
277 else if ( registry == _T("KOI8") )
278 {
279 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
280 }
281 //else: unknown encoding - may be give a warning here?
282 else
283 return FALSE;
284 }
285 return TRUE;
286 }
287
288 void wxFont::Unshare()
289 {
290 if (!m_refData)
291 {
292 m_refData = new wxFontRefData();
293 }
294 else
295 {
296 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
297 UnRef();
298 m_refData = ref;
299 }
300 }
301
302 wxFont::~wxFont()
303 {
304 }
305
306 // ----------------------------------------------------------------------------
307 // accessors
308 // ----------------------------------------------------------------------------
309
310 int wxFont::GetPointSize() const
311 {
312 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
313
314 return M_FONTDATA->m_pointSize;
315 }
316
317 wxString wxFont::GetFaceName() const
318 {
319 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
320
321 return M_FONTDATA->m_faceName;
322 }
323
324 int wxFont::GetFamily() const
325 {
326 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
327
328 return M_FONTDATA->m_family;
329 }
330
331 int wxFont::GetStyle() const
332 {
333 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
334
335 return M_FONTDATA->m_style;
336 }
337
338 int wxFont::GetWeight() const
339 {
340 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
341
342 return M_FONTDATA->m_weight;
343 }
344
345 bool wxFont::GetUnderlined() const
346 {
347 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
348
349 return M_FONTDATA->m_underlined;
350 }
351
352
353 wxFontEncoding wxFont::GetEncoding() const
354 {
355 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
356
357 return M_FONTDATA->m_encoding;
358 }
359
360 wxNativeFontInfo *wxFont::GetNativeFontInfo() const
361 {
362 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
363
364 if(M_FONTDATA->m_nativeFontInfo.xFontName.IsEmpty())
365 GetInternalFont();
366
367 return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
368 }
369
370
371 // ----------------------------------------------------------------------------
372 // change font attributes
373 // ----------------------------------------------------------------------------
374
375 void wxFont::SetPointSize(int pointSize)
376 {
377 Unshare();
378
379 M_FONTDATA->m_pointSize = pointSize;
380 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
381 }
382
383 void wxFont::SetFamily(int family)
384 {
385 Unshare();
386
387 M_FONTDATA->m_family = family;
388 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
389 }
390
391 void wxFont::SetStyle(int style)
392 {
393 Unshare();
394
395 M_FONTDATA->m_style = style;
396 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
397 }
398
399 void wxFont::SetWeight(int weight)
400 {
401 Unshare();
402
403 M_FONTDATA->m_weight = weight;
404 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
405 }
406
407 void wxFont::SetFaceName(const wxString& faceName)
408 {
409 Unshare();
410
411 M_FONTDATA->m_faceName = faceName;
412 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
413 }
414
415 void wxFont::SetUnderlined(bool underlined)
416 {
417 Unshare();
418
419 M_FONTDATA->m_underlined = underlined;
420 }
421
422 void wxFont::SetEncoding(wxFontEncoding encoding)
423 {
424 Unshare();
425
426 M_FONTDATA->m_encoding = encoding;
427 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
428 }
429
430 void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
431 {
432 Unshare();
433
434 M_FONTDATA->m_nativeFontInfo = info;
435 }
436
437 // ----------------------------------------------------------------------------
438 // get internal representation of font
439 // ----------------------------------------------------------------------------
440
441 static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
442
443 GdkFont *GtkGetDefaultGuiFont()
444 {
445 if (!g_systemDefaultGuiFont)
446 {
447 GtkWidget *widget = gtk_button_new();
448 GtkStyle *def = gtk_rc_get_style( widget );
449 if (def)
450 {
451 g_systemDefaultGuiFont = gdk_font_ref( def->font );
452 }
453 else
454 {
455 def = gtk_widget_get_default_style();
456 if (def)
457 g_systemDefaultGuiFont = gdk_font_ref( def->font );
458 }
459 gtk_widget_destroy( widget );
460 }
461 else
462 {
463 // already have it, but ref it once more before returning
464 gdk_font_ref(g_systemDefaultGuiFont);
465 }
466
467 return g_systemDefaultGuiFont;
468 }
469
470 GdkFont *wxFont::GetInternalFont( float scale ) const
471 {
472 if (!Ok())
473 {
474 wxFAIL_MSG( wxT("invalid font") );
475
476 return (GdkFont*) NULL;
477 }
478
479 long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
480 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
481 GdkFont *font = (GdkFont *) NULL;
482
483 wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
484 if (node)
485 {
486 font = (GdkFont*)node->Data();
487 }
488 else
489 {
490 if (*this == wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT))
491 {
492 font = GtkGetDefaultGuiFont();
493 }
494 if (!font)
495 {
496 font = wxLoadQueryNearestFont( point_scale,
497 M_FONTDATA->m_family,
498 M_FONTDATA->m_style,
499 M_FONTDATA->m_weight,
500 M_FONTDATA->m_underlined,
501 M_FONTDATA->m_faceName,
502 M_FONTDATA->m_encoding,
503 &M_FONTDATA->m_nativeFontInfo.xFontName );
504 }
505
506 M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
507 }
508
509 // it's quite useless to make it a wxCHECK because we're going to crash
510 // anyhow...
511 wxASSERT_MSG( font, wxT("could not load any font?") );
512
513 return font;
514 }
515