]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/font.cpp
workarounded false memory leak report
[wxWidgets.git] / src / gtk1 / 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 friend class wxFont;
75 };
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // wxFontRefData
83 // ----------------------------------------------------------------------------
84
85 void wxFontRefData::Init(int pointSize,
86 int family,
87 int style,
88 int weight,
89 bool underlined,
90 const wxString& faceName,
91 wxFontEncoding encoding)
92 {
93 if (family == wxDEFAULT)
94 m_family = wxSWISS;
95 else
96 m_family = family;
97
98 m_faceName = faceName;
99
100 if (style == wxDEFAULT)
101 m_style = wxNORMAL;
102 else
103 m_style = style;
104
105 if (weight == wxDEFAULT)
106 m_weight = wxNORMAL;
107 else
108 m_weight = weight;
109
110 if (pointSize == wxDEFAULT)
111 m_pointSize = 12;
112 else
113 m_pointSize = pointSize;
114
115 m_underlined = underlined;
116 m_encoding = encoding;
117 }
118
119 wxFontRefData::wxFontRefData( const wxFontRefData& data )
120 : m_scaled_xfonts(wxKEY_INTEGER)
121 {
122 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
123 data.m_underlined, data.m_faceName, data.m_encoding);
124 }
125
126 wxFontRefData::wxFontRefData(int size, int family, int style,
127 int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding )
128 : m_scaled_xfonts(wxKEY_INTEGER)
129 {
130 Init(size, family, style, weight,
131 underlined, faceName, encoding);
132 }
133
134 wxFontRefData::~wxFontRefData()
135 {
136 wxNode *node = m_scaled_xfonts.First();
137 while (node)
138 {
139 GdkFont *font = (GdkFont*)node->Data();
140 wxNode *next = node->Next();
141 gdk_font_unref( font );
142 node = next;
143 }
144 }
145
146 // ----------------------------------------------------------------------------
147 // wxFont
148 // ----------------------------------------------------------------------------
149
150 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
151
152 void wxFont::Init()
153 {
154 if (wxTheFontList)
155 wxTheFontList->Append( this );
156 }
157
158 wxFont::wxFont( const wxString& fontname, const wxFontData& fontdata )
159 {
160 Init();
161
162 wxCHECK_RET( !!fontname, _T("invalid font spec") );
163
164 m_refData = new wxFontRefData();
165
166 wxString tmp;
167
168 wxStringTokenizer tn( fontname, wxT("-") );
169
170 tn.GetNextToken(); // skip initial empty token
171 tn.GetNextToken(); // foundry
172
173 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
174
175 tmp = tn.GetNextToken().MakeUpper(); // weight
176 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
177 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD;
178 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
179 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD;
180 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
181
182 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
183 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
184
185 tmp = tn.GetNextToken().MakeUpper(); // slant
186 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
187 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
188
189 tn.GetNextToken(); // set width
190 tn.GetNextToken(); // add. style
191 tn.GetNextToken(); // pixel size
192
193 tmp = tn.GetNextToken(); // pointsize
194 if (tmp != wxT("*"))
195 {
196 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
197 M_FONTDATA->m_pointSize = (int)(num / 10);
198 }
199
200 tn.GetNextToken(); // x-res
201 tn.GetNextToken(); // y-res
202
203 tmp = tn.GetNextToken().MakeUpper(); // spacing
204
205 if (tmp == wxT("M"))
206 M_FONTDATA->m_family = wxMODERN;
207 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
208 M_FONTDATA->m_family = wxROMAN;
209 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
210 M_FONTDATA->m_family = wxSWISS;
211 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
212 M_FONTDATA->m_family = wxTELETYPE;
213 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
214 M_FONTDATA->m_family = wxDECORATIVE;
215 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
216 M_FONTDATA->m_family = wxSCRIPT;
217
218 tn.GetNextToken(); // avg width
219
220 // deal with font encoding
221 M_FONTDATA->m_encoding = fontdata.GetEncoding();
222 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
223 {
224 wxString registry = tn.GetNextToken().MakeUpper(),
225 encoding = tn.GetNextToken().MakeUpper();
226
227 if ( registry == _T("ISO8859") )
228 {
229 int cp;
230 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
231 {
232 M_FONTDATA->m_encoding =
233 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
234 }
235 }
236 else if ( registry == _T("MICROSOFT") )
237 {
238 int cp;
239 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
240 {
241 M_FONTDATA->m_encoding =
242 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
243 }
244 }
245 else if ( registry == _T("KOI8") )
246 {
247 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
248 }
249 //else: unknown encoding - may be give a warning here?
250 }
251 }
252
253 bool wxFont::Create( int pointSize,
254 int family,
255 int style,
256 int weight,
257 bool underlined,
258 const wxString& face,
259 wxFontEncoding encoding )
260 {
261 m_refData = new wxFontRefData(pointSize, family, style, weight,
262 underlined, face, encoding);
263
264 return TRUE;
265 }
266
267 void wxFont::Unshare()
268 {
269 if (!m_refData)
270 {
271 m_refData = new wxFontRefData();
272 }
273 else
274 {
275 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
276 UnRef();
277 m_refData = ref;
278 }
279 }
280
281 wxFont::~wxFont()
282 {
283 if (wxTheFontList)
284 wxTheFontList->DeleteObject( this );
285 }
286
287 // ----------------------------------------------------------------------------
288 // accessors
289 // ----------------------------------------------------------------------------
290
291 int wxFont::GetPointSize() const
292 {
293 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
294
295 return M_FONTDATA->m_pointSize;
296 }
297
298 wxString wxFont::GetFaceName() const
299 {
300 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
301
302 return M_FONTDATA->m_faceName;
303 }
304
305 int wxFont::GetFamily() const
306 {
307 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
308
309 return M_FONTDATA->m_family;
310 }
311
312 int wxFont::GetStyle() const
313 {
314 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
315
316 return M_FONTDATA->m_style;
317 }
318
319 int wxFont::GetWeight() const
320 {
321 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
322
323 return M_FONTDATA->m_weight;
324 }
325
326 bool wxFont::GetUnderlined() const
327 {
328 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
329
330 return M_FONTDATA->m_underlined;
331 }
332
333
334 wxFontEncoding wxFont::GetEncoding() const
335 {
336 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
337
338 return M_FONTDATA->m_encoding;
339 }
340
341 // ----------------------------------------------------------------------------
342 // change font attributes
343 // ----------------------------------------------------------------------------
344
345 void wxFont::SetPointSize(int pointSize)
346 {
347 Unshare();
348
349 M_FONTDATA->m_pointSize = pointSize;
350 }
351
352 void wxFont::SetFamily(int family)
353 {
354 Unshare();
355
356 M_FONTDATA->m_family = family;
357 }
358
359 void wxFont::SetStyle(int style)
360 {
361 Unshare();
362
363 M_FONTDATA->m_style = style;
364 }
365
366 void wxFont::SetWeight(int weight)
367 {
368 Unshare();
369
370 M_FONTDATA->m_weight = weight;
371 }
372
373 void wxFont::SetFaceName(const wxString& faceName)
374 {
375 Unshare();
376
377 M_FONTDATA->m_faceName = faceName;
378 }
379
380 void wxFont::SetUnderlined(bool underlined)
381 {
382 Unshare();
383
384 M_FONTDATA->m_underlined = underlined;
385 }
386
387 void wxFont::SetEncoding(wxFontEncoding encoding)
388 {
389 Unshare();
390
391 M_FONTDATA->m_encoding = encoding;
392 }
393
394 // ----------------------------------------------------------------------------
395 // get internal representation of font
396 // ----------------------------------------------------------------------------
397
398 static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
399
400 GdkFont *GtkGetDefaultGuiFont()
401 {
402 if (!g_systemDefaultGuiFont)
403 {
404 GtkWidget *widget = gtk_button_new();
405 GtkStyle *def = gtk_rc_get_style( widget );
406 if (def)
407 {
408 g_systemDefaultGuiFont = gdk_font_ref( def->font );
409 }
410 else
411 {
412 def = gtk_widget_get_default_style();
413 if (def)
414 g_systemDefaultGuiFont = gdk_font_ref( def->font );
415 }
416 gtk_widget_destroy( widget );
417 }
418 return g_systemDefaultGuiFont;
419 }
420
421 GdkFont *wxFont::GetInternalFont( float scale ) const
422 {
423 if (!Ok())
424 {
425 wxFAIL_MSG( wxT("invalid font") );
426
427 return (GdkFont*) NULL;
428 }
429
430 long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
431 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
432 GdkFont *font = (GdkFont *) NULL;
433
434 wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
435 if (node)
436 {
437 font = (GdkFont*)node->Data();
438 }
439 else
440 {
441 if (*this == wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT))
442 {
443 font = GtkGetDefaultGuiFont();
444 }
445 if (!font)
446 {
447 font = wxLoadQueryNearestFont( point_scale,
448 M_FONTDATA->m_family,
449 M_FONTDATA->m_style,
450 M_FONTDATA->m_weight,
451 M_FONTDATA->m_underlined,
452 M_FONTDATA->m_faceName,
453 M_FONTDATA->m_encoding );
454 }
455
456 M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
457 }
458
459 // it's quite useless to make it a wxCHECK because we're going to crash
460 // anyhow...
461 wxASSERT_MSG( font, wxT("could not load any font?") );
462
463 return font;
464 }
465