]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/font.cpp
If -1 is used for width/height, use best size.
[wxWidgets.git] / src / motif / font.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/motif/font.cpp
3// Purpose: wxFont class
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "font.h"
22#endif
23
24#include "wx/defs.h"
25
26#ifdef __VMS
27#pragma message disable nosimpint
28#include "wx/vms_x_fix.h"
29#endif
30#include <Xm/Xm.h>
31#ifdef __VMS
32#pragma message enable nosimpint
33#endif
34
35#include "wx/string.h"
36#include "wx/font.h"
37#include "wx/gdicmn.h"
38#include "wx/utils.h" // for wxGetDisplay()
39#include "wx/fontutil.h" // for wxNativeFontInfo
40#include "wx/tokenzr.h"
41#include "wx/settings.h"
42
43IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
44
45// ----------------------------------------------------------------------------
46// private classes
47// ----------------------------------------------------------------------------
48
49// For every wxFont, there must be a font for each display and scale requested.
50// So these objects are stored in wxFontRefData::m_fonts
51class wxXFont : public wxObject
52{
53public:
54 wxXFont();
55 ~wxXFont();
56
57 WXFontStructPtr m_fontStruct; // XFontStruct
58 WXFontList m_fontList; // Motif XmFontList
59 WXDisplay* m_display; // XDisplay
60 int m_scale; // Scale * 100
61};
62
63class wxFontRefData: public wxGDIRefData
64{
65friend class wxFont;
66
67public:
68 wxFontRefData(int size = wxDEFAULT,
69 int family = wxDEFAULT,
70 int style = wxDEFAULT,
71 int weight = wxDEFAULT,
72 bool underlined = FALSE,
73 const wxString& faceName = wxEmptyString,
74 wxFontEncoding encoding = wxFONTENCODING_DEFAULT)
75 {
76 Init(size, family, style, weight, underlined, faceName, encoding);
77 }
78
79 wxFontRefData(const wxFontRefData& data)
80 {
81 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
82 data.m_underlined, data.m_faceName, data.m_encoding);
83 }
84
85 ~wxFontRefData();
86
87protected:
88 // common part of all ctors
89 void Init(int size,
90 int family,
91 int style,
92 int weight,
93 bool underlined,
94 const wxString& faceName,
95 wxFontEncoding encoding);
96
97 // font attributes
98 int m_pointSize;
99 int m_family;
100 int m_style;
101 int m_weight;
102 bool m_underlined;
103 wxString m_faceName;
104 wxFontEncoding m_encoding;
105
106 wxNativeFontInfo m_nativeFontInfo;
107
108 // A list of wxXFonts
109 wxList m_fonts;
110};
111
112// ============================================================================
113// implementation
114// ============================================================================
115
116// ----------------------------------------------------------------------------
117// wxXFont
118// ----------------------------------------------------------------------------
119
120wxXFont::wxXFont()
121{
122 m_fontStruct = (WXFontStructPtr) 0;
123 m_fontList = (WXFontList) 0;
124 m_display = (WXDisplay*) 0;
125 m_scale = 100;
126}
127
128wxXFont::~wxXFont()
129{
130 XmFontList fontList = (XmFontList) m_fontList;
131
132 XmFontListFree (fontList);
133
134 // TODO: why does freeing the font produce a segv???
135 // Note that XFreeFont wasn't called in wxWin 1.68 either.
136 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
137 // XFreeFont((Display*) m_display, fontStruct);
138}
139
140// ----------------------------------------------------------------------------
141// wxFontRefData
142// ----------------------------------------------------------------------------
143
144void wxFontRefData::Init(int pointSize,
145 int family,
146 int style,
147 int weight,
148 bool underlined,
149 const wxString& faceName,
150 wxFontEncoding encoding)
151{
152 if (family == wxDEFAULT)
153 m_family = wxSWISS;
154 else
155 m_family = family;
156
157 m_faceName = faceName;
158
159 if (style == wxDEFAULT)
160 m_style = wxNORMAL;
161 else
162 m_style = style;
163
164 if (weight == wxDEFAULT)
165 m_weight = wxNORMAL;
166 else
167 m_weight = weight;
168
169 if (pointSize == wxDEFAULT)
170 m_pointSize = 12;
171 else
172 m_pointSize = pointSize;
173
174 m_underlined = underlined;
175 m_encoding = encoding;
176}
177
178wxFontRefData::~wxFontRefData()
179{
180 wxNode* node = m_fonts.First();
181 while (node)
182 {
183 wxXFont* f = (wxXFont*) node->Data();
184 delete f;
185 node = node->Next();
186 }
187 m_fonts.Clear();
188}
189
190// ----------------------------------------------------------------------------
191// wxFont
192// ----------------------------------------------------------------------------
193
194wxFont::wxFont(const wxNativeFontInfo& info)
195{
196 Init();
197
198 (void)Create(info.GetXFontName());
199}
200
201void wxFont::Init()
202{
203}
204
205bool wxFont::Create(int pointSize,
206 int family,
207 int style,
208 int weight,
209 bool underlined,
210 const wxString& faceName,
211 wxFontEncoding encoding)
212{
213 UnRef();
214 m_refData = new wxFontRefData(pointSize, family, style, weight,
215 underlined, faceName, encoding);
216
217 RealizeResource();
218
219 return TRUE;
220}
221
222bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
223{
224 if( !fontname )
225 {
226 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT);
227 return TRUE;
228 }
229
230 m_refData = new wxFontRefData();
231
232 M_FONTDATA->m_nativeFontInfo.SetXFontName(fontname); // X font name
233
234 wxString tmp;
235
236 wxStringTokenizer tn( fontname, wxT("-") );
237
238 tn.GetNextToken(); // skip initial empty token
239 tn.GetNextToken(); // foundry
240
241
242 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
243
244 tmp = tn.GetNextToken().MakeUpper(); // weight
245 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
246 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD;
247 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
248 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD;
249 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
250
251 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
252 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
253
254 tmp = tn.GetNextToken().MakeUpper(); // slant
255 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
256 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
257
258 tn.GetNextToken(); // set width
259 tn.GetNextToken(); // add. style
260 tn.GetNextToken(); // pixel size
261
262 tmp = tn.GetNextToken(); // pointsize
263 if (tmp != wxT("*"))
264 {
265 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
266 M_FONTDATA->m_pointSize = (int)(num / 10);
267 }
268
269 tn.GetNextToken(); // x-res
270 tn.GetNextToken(); // y-res
271
272 tmp = tn.GetNextToken().MakeUpper(); // spacing
273
274 if (tmp == wxT("M"))
275 M_FONTDATA->m_family = wxMODERN;
276 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
277 M_FONTDATA->m_family = wxROMAN;
278 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
279 M_FONTDATA->m_family = wxSWISS;
280 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
281 M_FONTDATA->m_family = wxTELETYPE;
282 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
283 M_FONTDATA->m_family = wxDECORATIVE;
284 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
285 M_FONTDATA->m_family = wxSCRIPT;
286
287 tn.GetNextToken(); // avg width
288
289 // deal with font encoding
290 M_FONTDATA->m_encoding = enc;
291 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
292 {
293 wxString registry = tn.GetNextToken().MakeUpper(),
294 encoding = tn.GetNextToken().MakeUpper();
295
296 if ( registry == _T("ISO8859") )
297 {
298 int cp;
299 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
300 {
301 M_FONTDATA->m_encoding =
302 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
303 }
304 }
305 else if ( registry == _T("MICROSOFT") )
306 {
307 int cp;
308 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
309 {
310 M_FONTDATA->m_encoding =
311 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
312 }
313 }
314 else if ( registry == _T("KOI8") )
315 {
316 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
317 }
318 //else: unknown encoding - may be give a warning here?
319 else
320 return FALSE;
321 }
322 return TRUE;
323}
324
325wxFont::~wxFont()
326{
327}
328
329// ----------------------------------------------------------------------------
330// change the font attributes
331// ----------------------------------------------------------------------------
332
333void wxFont::Unshare()
334{
335 // Don't change shared data
336 if (!m_refData)
337 {
338 m_refData = new wxFontRefData();
339 }
340 else
341 {
342 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
343 UnRef();
344 m_refData = ref;
345 }
346}
347
348void wxFont::SetPointSize(int pointSize)
349{
350 Unshare();
351
352 M_FONTDATA->m_pointSize = pointSize;
353 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
354
355 RealizeResource();
356}
357
358void wxFont::SetFamily(int family)
359{
360 Unshare();
361
362 M_FONTDATA->m_family = family;
363 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
364
365 RealizeResource();
366}
367
368void wxFont::SetStyle(int style)
369{
370 Unshare();
371
372 M_FONTDATA->m_style = style;
373 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
374
375 RealizeResource();
376}
377
378void wxFont::SetWeight(int weight)
379{
380 Unshare();
381
382 M_FONTDATA->m_weight = weight;
383 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
384
385 RealizeResource();
386}
387
388void wxFont::SetFaceName(const wxString& faceName)
389{
390 Unshare();
391
392 M_FONTDATA->m_faceName = faceName;
393 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
394
395 RealizeResource();
396}
397
398void wxFont::SetUnderlined(bool underlined)
399{
400 Unshare();
401
402 M_FONTDATA->m_underlined = underlined;
403
404 RealizeResource();
405}
406
407void wxFont::SetEncoding(wxFontEncoding encoding)
408{
409 Unshare();
410
411 M_FONTDATA->m_encoding = encoding;
412 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
413
414 RealizeResource();
415}
416
417void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
418{
419 Unshare();
420
421 M_FONTDATA->m_nativeFontInfo = info;
422}
423
424// ----------------------------------------------------------------------------
425// query font attributes
426// ----------------------------------------------------------------------------
427
428int wxFont::GetPointSize() const
429{
430 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
431
432 return M_FONTDATA->m_pointSize;
433}
434
435wxString wxFont::GetFaceName() const
436{
437 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
438
439 return M_FONTDATA->m_faceName ;
440}
441
442int wxFont::GetFamily() const
443{
444 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
445
446 return M_FONTDATA->m_family;
447}
448
449int wxFont::GetStyle() const
450{
451 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
452
453 return M_FONTDATA->m_style;
454}
455
456int wxFont::GetWeight() const
457{
458 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
459
460 return M_FONTDATA->m_weight;
461}
462
463bool wxFont::GetUnderlined() const
464{
465 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
466
467 return M_FONTDATA->m_underlined;
468}
469
470wxFontEncoding wxFont::GetEncoding() const
471{
472 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
473
474 return M_FONTDATA->m_encoding;
475}
476
477wxNativeFontInfo *wxFont::GetNativeFontInfo() const
478{
479 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
480
481 if(M_FONTDATA->m_nativeFontInfo.GetXFontName().IsEmpty())
482 GetInternalFont();
483
484 return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
485}
486
487// ----------------------------------------------------------------------------
488// real implementation
489// ----------------------------------------------------------------------------
490
491// Find an existing, or create a new, XFontStruct
492// based on this wxFont and the given scale. Append the
493// font to list in the private data for future reference.
494wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const
495{
496 if ( !Ok() )
497 return (wxXFont *)NULL;
498
499 long intScale = long(scale * 100.0 + 0.5); // key for wxXFont
500 int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100;
501
502 // search existing fonts first
503 wxNode* node = M_FONTDATA->m_fonts.First();
504 while (node)
505 {
506 wxXFont* f = (wxXFont*) node->Data();
507 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
508 return f;
509 node = node->Next();
510 }
511
512 // not found, create a new one
513 XFontStruct *font = (XFontStruct *)
514 wxLoadQueryNearestFont(pointSize,
515 M_FONTDATA->m_family,
516 M_FONTDATA->m_style,
517 M_FONTDATA->m_weight,
518 M_FONTDATA->m_underlined,
519 wxT(""),
520 M_FONTDATA->m_encoding);
521
522 if ( !font )
523 {
524 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
525
526 return (wxXFont*) NULL;
527 }
528
529 wxXFont* f = new wxXFont;
530 f->m_fontStruct = (WXFontStructPtr)font;
531 f->m_display = ( display ? display : wxGetDisplay() );
532 f->m_scale = intScale;
533 f->m_fontList = XmFontListCreate ((XFontStruct*) font, XmSTRING_DEFAULT_CHARSET);
534 M_FONTDATA->m_fonts.Append(f);
535
536 return f;
537}
538
539WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const
540{
541 wxXFont* f = GetInternalFont(scale, display);
542
543 return (f ? f->m_fontStruct : (WXFontStructPtr) 0);
544}
545
546WXFontList wxFont::GetFontList(double scale, WXDisplay* display) const
547{
548 wxXFont* f = GetInternalFont(scale, display);
549
550 return (f ? f->m_fontList : (WXFontList) 0);
551}
552