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