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