]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/font.cpp
Tries to fix wxListBox focus problems.
[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 // wxNativeFontInfo
151 // ----------------------------------------------------------------------------
152
153 bool wxNativeFontInfo::FromString(const wxString& s)
154 {
155 wxStringTokenizer tokenizer(s, _T(";"));
156
157 wxString token = tokenizer.GetNextToken();
158 //
159 // Ignore the version for now
160 //
161
162 xFontName = tokenizer.GetNextToken();
163 if(!xFontName)
164 return FALSE;
165
166 return TRUE;
167 }
168
169 wxString wxNativeFontInfo::ToString() const
170 {
171 wxString s;
172
173 s.Printf(_T("%d;%s"),
174 0, // version
175 xFontName.c_str());
176
177 return s;
178 }
179
180 // ----------------------------------------------------------------------------
181 // wxFont
182 // ----------------------------------------------------------------------------
183
184 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
185
186 void wxFont::Init()
187 {
188 }
189
190 wxFont::wxFont(const wxNativeFontInfo& info)
191 {
192 Init();
193
194 Create(info.xFontName);
195 }
196
197 bool wxFont::Create(const wxNativeFontInfo& info)
198 {
199 return Create(info.xFontName);
200 }
201
202 bool wxFont::Create( int pointSize,
203 int family,
204 int style,
205 int weight,
206 bool underlined,
207 const wxString& face,
208 wxFontEncoding encoding)
209 {
210 m_refData = new wxFontRefData(pointSize, family, style, weight,
211 underlined, face, encoding);
212
213 return TRUE;
214 }
215
216 bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
217 {
218 if( !fontname )
219 {
220 *this = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT);
221 return TRUE;
222 }
223
224 m_refData = new wxFontRefData();
225
226 M_FONTDATA->m_nativeFontInfo.xFontName = fontname; // X font name
227
228 wxString tmp;
229
230 wxStringTokenizer tn( fontname, wxT("-") );
231
232 tn.GetNextToken(); // skip initial empty token
233 tn.GetNextToken(); // foundry
234
235
236 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
237
238 tmp = tn.GetNextToken().MakeUpper(); // weight
239 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
240 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD;
241 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
242 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD;
243 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
244
245 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
246 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
247
248 tmp = tn.GetNextToken().MakeUpper(); // slant
249 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
250 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
251
252 tn.GetNextToken(); // set width
253 tn.GetNextToken(); // add. style
254 tn.GetNextToken(); // pixel size
255
256 tmp = tn.GetNextToken(); // pointsize
257 if (tmp != wxT("*"))
258 {
259 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
260 M_FONTDATA->m_pointSize = (int)(num / 10);
261 }
262
263 tn.GetNextToken(); // x-res
264 tn.GetNextToken(); // y-res
265
266 tmp = tn.GetNextToken().MakeUpper(); // spacing
267
268 if (tmp == wxT("M"))
269 M_FONTDATA->m_family = wxMODERN;
270 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
271 M_FONTDATA->m_family = wxROMAN;
272 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
273 M_FONTDATA->m_family = wxSWISS;
274 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
275 M_FONTDATA->m_family = wxTELETYPE;
276 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
277 M_FONTDATA->m_family = wxDECORATIVE;
278 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
279 M_FONTDATA->m_family = wxSCRIPT;
280
281 tn.GetNextToken(); // avg width
282
283 // deal with font encoding
284 M_FONTDATA->m_encoding = enc;
285 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
286 {
287 wxString registry = tn.GetNextToken().MakeUpper(),
288 encoding = tn.GetNextToken().MakeUpper();
289
290 if ( registry == _T("ISO8859") )
291 {
292 int cp;
293 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
294 {
295 M_FONTDATA->m_encoding =
296 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
297 }
298 }
299 else if ( registry == _T("MICROSOFT") )
300 {
301 int cp;
302 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
303 {
304 M_FONTDATA->m_encoding =
305 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
306 }
307 }
308 else if ( registry == _T("KOI8") )
309 {
310 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
311 }
312 //else: unknown encoding - may be give a warning here?
313 else
314 return FALSE;
315 }
316 return TRUE;
317 }
318
319 void wxFont::Unshare()
320 {
321 if (!m_refData)
322 {
323 m_refData = new wxFontRefData();
324 }
325 else
326 {
327 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
328 UnRef();
329 m_refData = ref;
330 }
331 }
332
333 wxFont::~wxFont()
334 {
335 }
336
337 // ----------------------------------------------------------------------------
338 // accessors
339 // ----------------------------------------------------------------------------
340
341 int wxFont::GetPointSize() const
342 {
343 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
344
345 return M_FONTDATA->m_pointSize;
346 }
347
348 wxString wxFont::GetFaceName() const
349 {
350 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
351
352 return M_FONTDATA->m_faceName;
353 }
354
355 int wxFont::GetFamily() const
356 {
357 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
358
359 return M_FONTDATA->m_family;
360 }
361
362 int wxFont::GetStyle() const
363 {
364 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
365
366 return M_FONTDATA->m_style;
367 }
368
369 int wxFont::GetWeight() const
370 {
371 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
372
373 return M_FONTDATA->m_weight;
374 }
375
376 bool wxFont::GetUnderlined() const
377 {
378 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
379
380 return M_FONTDATA->m_underlined;
381 }
382
383
384 wxFontEncoding wxFont::GetEncoding() const
385 {
386 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
387
388 return M_FONTDATA->m_encoding;
389 }
390
391 wxNativeFontInfo *wxFont::GetNativeFontInfo() const
392 {
393 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
394
395 if(M_FONTDATA->m_nativeFontInfo.xFontName.IsEmpty())
396 GetInternalFont();
397
398 return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
399 }
400
401
402 // ----------------------------------------------------------------------------
403 // change font attributes
404 // ----------------------------------------------------------------------------
405
406 void wxFont::SetPointSize(int pointSize)
407 {
408 Unshare();
409
410 M_FONTDATA->m_pointSize = pointSize;
411 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
412 }
413
414 void wxFont::SetFamily(int family)
415 {
416 Unshare();
417
418 M_FONTDATA->m_family = family;
419 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
420 }
421
422 void wxFont::SetStyle(int style)
423 {
424 Unshare();
425
426 M_FONTDATA->m_style = style;
427 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
428 }
429
430 void wxFont::SetWeight(int weight)
431 {
432 Unshare();
433
434 M_FONTDATA->m_weight = weight;
435 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
436 }
437
438 void wxFont::SetFaceName(const wxString& faceName)
439 {
440 Unshare();
441
442 M_FONTDATA->m_faceName = faceName;
443 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
444 }
445
446 void wxFont::SetUnderlined(bool underlined)
447 {
448 Unshare();
449
450 M_FONTDATA->m_underlined = underlined;
451 }
452
453 void wxFont::SetEncoding(wxFontEncoding encoding)
454 {
455 Unshare();
456
457 M_FONTDATA->m_encoding = encoding;
458 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
459 }
460
461 void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
462 {
463 Unshare();
464
465 M_FONTDATA->m_nativeFontInfo = info;
466 }
467
468 // ----------------------------------------------------------------------------
469 // get internal representation of font
470 // ----------------------------------------------------------------------------
471
472 static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
473
474 GdkFont *GtkGetDefaultGuiFont()
475 {
476 if (!g_systemDefaultGuiFont)
477 {
478 GtkWidget *widget = gtk_button_new();
479 GtkStyle *def = gtk_rc_get_style( widget );
480 if (def)
481 {
482 g_systemDefaultGuiFont = gdk_font_ref( def->font );
483 }
484 else
485 {
486 def = gtk_widget_get_default_style();
487 if (def)
488 g_systemDefaultGuiFont = gdk_font_ref( def->font );
489 }
490 gtk_widget_destroy( widget );
491 }
492 else
493 {
494 // already have it, but ref it once more before returning
495 gdk_font_ref(g_systemDefaultGuiFont);
496 }
497
498 return g_systemDefaultGuiFont;
499 }
500
501 GdkFont *wxFont::GetInternalFont( float scale ) const
502 {
503 if (!Ok())
504 {
505 wxFAIL_MSG( wxT("invalid font") );
506
507 return (GdkFont*) NULL;
508 }
509
510 long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
511 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
512 GdkFont *font = (GdkFont *) NULL;
513
514 wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
515 if (node)
516 {
517 font = (GdkFont*)node->Data();
518 }
519 else
520 {
521 if (*this == wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT))
522 {
523 font = GtkGetDefaultGuiFont();
524 }
525 if (!font)
526 {
527 font = wxLoadQueryNearestFont( point_scale,
528 M_FONTDATA->m_family,
529 M_FONTDATA->m_style,
530 M_FONTDATA->m_weight,
531 M_FONTDATA->m_underlined,
532 M_FONTDATA->m_faceName,
533 M_FONTDATA->m_encoding,
534 &M_FONTDATA->m_nativeFontInfo.xFontName );
535 }
536
537 M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
538 }
539
540 // it's quite useless to make it a wxCHECK because we're going to crash
541 // anyhow...
542 wxASSERT_MSG( font, wxT("could not load any font?") );
543
544 return font;
545 }
546