]> git.saurik.com Git - wxWidgets.git/blame - src/motif/font.cpp
Fix crash in wxDC::GetMultiLineTextExtent() after last commit.
[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
1248b41f
MB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
338dd992
JJ
23#ifdef __VMS
24#pragma message disable nosimpint
4dff3400 25#include "wx/vms_x_fix.h"
338dd992 26#endif
79e4b627 27#include <Xm/Xm.h>
338dd992
JJ
28#ifdef __VMS
29#pragma message enable nosimpint
30#endif
79e4b627 31
4bb6408c 32#include "wx/font.h"
df91131c
WS
33
34#ifndef WX_PRECOMP
35 #include "wx/string.h"
de6185e2 36 #include "wx/utils.h" // for wxGetDisplay()
9eddec69 37 #include "wx/settings.h"
dd05139a 38 #include "wx/gdicmn.h"
df91131c
WS
39#endif
40
98c9fc2d 41#include "wx/fontutil.h" // for wxNativeFontInfo
563f868d 42#include "wx/tokenzr.h"
da494b40 43#include "wx/motif/private.h"
4bb6408c 44
93ccaed8
VZ
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
79e4b627 51class wxXFont : public wxObject
93ccaed8
VZ
52{
53public:
54 wxXFont();
d3c7fc99 55 virtual ~wxXFont();
93ccaed8 56
996994c7 57#if !wxMOTIF_NEW_FONT_HANDLING
93ccaed8 58 WXFontStructPtr m_fontStruct; // XFontStruct
996994c7 59#endif
105fbe1f 60#if !wxMOTIF_USE_RENDER_TABLE
93ccaed8 61 WXFontList m_fontList; // Motif XmFontList
b0f76951 62#else // if wxMOTIF_USE_RENDER_TABLE
da494b40 63 WXRenderTable m_renderTable; // Motif XmRenderTable
996994c7 64 WXRendition m_rendition; // Motif XmRendition
da494b40 65#endif
93ccaed8
VZ
66 WXDisplay* m_display; // XDisplay
67 int m_scale; // Scale * 100
68};
69
70class wxFontRefData: public wxGDIRefData
71{
73608949 72 friend class wxFont;
93ccaed8
VZ
73
74public:
75 wxFontRefData(int size = wxDEFAULT,
0c14b6c3
FM
76 wxFontFamily family = wxFONTFAMILY_DEFAULT,
77 wxFontStyle style = wxFONTSTYLE_NORMAL,
78 wxFontWeight weight = wxFONTWEIGHT_NORMAL,
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
d3c7fc99 92 virtual ~wxFontRefData();
93ccaed8
VZ
93
94protected:
95 // common part of all ctors
96 void Init(int size,
0c14b6c3
FM
97 wxFontFamily family,
98 wxFontStyle style,
99 wxFontWeight weight,
93ccaed8
VZ
100 bool underlined,
101 const wxString& faceName,
102 wxFontEncoding encoding);
103
104 // font attributes
105 int m_pointSize;
0c14b6c3
FM
106 wxFontFamily m_family;
107 wxFontStyle m_style;
108 wxFontWeight m_weight;
93ccaed8
VZ
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{
996994c7 129#if !wxMOTIF_NEW_FONT_HANDLING
f97c9854 130 m_fontStruct = (WXFontStructPtr) 0;
996994c7 131#endif
105fbe1f 132#if !wxMOTIF_USE_RENDER_TABLE
f97c9854 133 m_fontList = (WXFontList) 0;
eb9d223a 134#else // if wxMOTIF_USE_RENDER_TABLE
da494b40 135 m_renderTable = (WXRenderTable) 0;
996994c7 136 m_rendition = (WXRendition) 0;
da494b40 137#endif
f97c9854
JS
138 m_display = (WXDisplay*) 0;
139 m_scale = 100;
140}
141
142wxXFont::~wxXFont()
143{
eb9d223a 144#if !wxMOTIF_USE_RENDER_TABLE
73608949
MB
145 if (m_fontList)
146 XmFontListFree ((XmFontList) m_fontList);
147 m_fontList = NULL;
b0f76951 148#else // if wxMOTIF_USE_RENDER_TABLE
73608949
MB
149 if (m_renderTable)
150 XmRenderTableFree ((XmRenderTable) m_renderTable);
151 m_renderTable = NULL;
da494b40
MB
152#endif
153
2d120f83 154 // TODO: why does freeing the font produce a segv???
f97c9854 155 // Note that XFreeFont wasn't called in wxWin 1.68 either.
73608949
MB
156 // MBN: probably some interaction with fonts being still
157 // in use in some widgets...
dfe1eee3 158 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
2d120f83 159 // XFreeFont((Display*) m_display, fontStruct);
f97c9854
JS
160}
161
93ccaed8
VZ
162// ----------------------------------------------------------------------------
163// wxFontRefData
164// ----------------------------------------------------------------------------
165
166void wxFontRefData::Init(int pointSize,
0c14b6c3
FM
167 wxFontFamily family,
168 wxFontStyle style,
169 wxFontWeight weight,
93ccaed8
VZ
170 bool underlined,
171 const wxString& faceName,
172 wxFontEncoding encoding)
4bb6408c 173{
93ccaed8 174 if (family == wxDEFAULT)
0c14b6c3 175 m_family = wxFONTFAMILY_SWISS;
93ccaed8
VZ
176 else
177 m_family = family;
4bb6408c 178
93ccaed8
VZ
179 m_faceName = faceName;
180
181 if (style == wxDEFAULT)
0c14b6c3 182 m_style = wxFONTSTYLE_NORMAL;
93ccaed8
VZ
183 else
184 m_style = style;
185
186 if (weight == wxDEFAULT)
0c14b6c3 187 m_weight = wxFONTWEIGHT_NORMAL;
93ccaed8
VZ
188 else
189 m_weight = weight;
190
191 if (pointSize == wxDEFAULT)
192 m_pointSize = 12;
193 else
194 m_pointSize = pointSize;
195
196 m_underlined = underlined;
197 m_encoding = encoding;
4bb6408c
JS
198}
199
200wxFontRefData::~wxFontRefData()
201{
ac32ba44 202 wxList::compatibility_iterator node = m_fonts.GetFirst();
dfc54541
JS
203 while (node)
204 {
fd304d98 205 wxXFont* f = (wxXFont*) node->GetData();
f97c9854 206 delete f;
fd304d98 207 node = node->GetNext();
dfc54541 208 }
f97c9854 209 m_fonts.Clear();
4bb6408c
JS
210}
211
68c95704 212#define M_FONTDATA ((wxFontRefData*)m_refData)
873fd4af 213
93ccaed8
VZ
214// ----------------------------------------------------------------------------
215// wxFont
216// ----------------------------------------------------------------------------
4bb6408c 217
ef4e8ebd 218wxFont::wxFont(const wxNativeFontInfo& info)
98c9fc2d 219{
42f30fa2 220 (void)Create(info.GetXFontName());
98c9fc2d
VZ
221}
222
93ccaed8 223bool wxFont::Create(int pointSize,
0c14b6c3
FM
224 wxFontFamily family,
225 wxFontStyle style,
226 wxFontWeight weight,
93ccaed8
VZ
227 bool underlined,
228 const wxString& faceName,
229 wxFontEncoding encoding)
4bb6408c
JS
230{
231 UnRef();
93ccaed8
VZ
232 m_refData = new wxFontRefData(pointSize, family, style, weight,
233 underlined, faceName, encoding);
dfe1eee3 234
96be256b 235 return true;
4bb6408c
JS
236}
237
563f868d
GD
238bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
239{
240 if( !fontname )
241 {
a756f210 242 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT);
96be256b 243 return true;
563f868d
GD
244 }
245
246 m_refData = new wxFontRefData();
247
42f30fa2 248 M_FONTDATA->m_nativeFontInfo.SetXFontName(fontname); // X font name
563f868d
GD
249
250 wxString tmp;
251
252 wxStringTokenizer tn( fontname, wxT("-") );
253
254 tn.GetNextToken(); // skip initial empty token
255 tn.GetNextToken(); // foundry
256
257
258 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
259
260 tmp = tn.GetNextToken().MakeUpper(); // weight
0c14b6c3
FM
261 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
262 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
263 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
264 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
265 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxFONTWEIGHT_BOLD;
563f868d 266
0c14b6c3
FM
267 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxFONTWEIGHT_LIGHT;
268 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxFONTWEIGHT_LIGHT;
563f868d
GD
269
270 tmp = tn.GetNextToken().MakeUpper(); // slant
0c14b6c3
FM
271 if (tmp == wxT("I")) M_FONTDATA->m_style = wxFONTSTYLE_ITALIC;
272 if (tmp == wxT("O")) M_FONTDATA->m_style = wxFONTSTYLE_ITALIC;
563f868d
GD
273
274 tn.GetNextToken(); // set width
275 tn.GetNextToken(); // add. style
276 tn.GetNextToken(); // pixel size
277
278 tmp = tn.GetNextToken(); // pointsize
279 if (tmp != wxT("*"))
280 {
9b135950 281 long num = wxStrtol (tmp.mb_str(), (wxChar **) NULL, 10);
563f868d
GD
282 M_FONTDATA->m_pointSize = (int)(num / 10);
283 }
284
285 tn.GetNextToken(); // x-res
286 tn.GetNextToken(); // y-res
287
288 tmp = tn.GetNextToken().MakeUpper(); // spacing
289
290 if (tmp == wxT("M"))
0c14b6c3 291 M_FONTDATA->m_family = wxFONTFAMILY_MODERN;
563f868d 292 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
0c14b6c3 293 M_FONTDATA->m_family = wxFONTFAMILY_ROMAN;
563f868d 294 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
0c14b6c3 295 M_FONTDATA->m_family = wxFONTFAMILY_SWISS;
563f868d 296 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
0c14b6c3 297 M_FONTDATA->m_family = wxFONTFAMILY_TELETYPE;
563f868d 298 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
0c14b6c3 299 M_FONTDATA->m_family = wxFONTFAMILY_DECORATIVE;
563f868d 300 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
0c14b6c3 301 M_FONTDATA->m_family = wxFONTFAMILY_SCRIPT;
563f868d
GD
302
303 tn.GetNextToken(); // avg width
304
305 // deal with font encoding
306 M_FONTDATA->m_encoding = enc;
307 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
308 {
309 wxString registry = tn.GetNextToken().MakeUpper(),
310 encoding = tn.GetNextToken().MakeUpper();
311
9a83f860 312 if ( registry == wxT("ISO8859") )
563f868d
GD
313 {
314 int cp;
315 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
316 {
317 M_FONTDATA->m_encoding =
318 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
319 }
320 }
9a83f860 321 else if ( registry == wxT("MICROSOFT") )
563f868d
GD
322 {
323 int cp;
324 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
325 {
326 M_FONTDATA->m_encoding =
327 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
328 }
329 }
9a83f860 330 else if ( registry == wxT("KOI8") )
563f868d
GD
331 {
332 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
333 }
334 //else: unknown encoding - may be give a warning here?
335 else
96be256b 336 return false;
563f868d 337 }
96be256b 338 return true;
563f868d
GD
339}
340
4bb6408c
JS
341wxFont::~wxFont()
342{
4bb6408c
JS
343}
344
8f884a0d
VZ
345wxGDIRefData *wxFont::CreateGDIRefData() const
346{
347 return new wxFontRefData;
348}
349
350wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
351{
5c33522f 352 return new wxFontRefData(*static_cast<const wxFontRefData *>(data));
8f884a0d
VZ
353}
354
93ccaed8
VZ
355// ----------------------------------------------------------------------------
356// change the font attributes
357// ----------------------------------------------------------------------------
4bb6408c
JS
358
359void wxFont::Unshare()
360{
2d120f83
JS
361 // Don't change shared data
362 if (!m_refData)
4bb6408c 363 {
2d120f83
JS
364 m_refData = new wxFontRefData();
365 }
4bb6408c
JS
366 else
367 {
2d120f83
JS
368 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
369 UnRef();
370 m_refData = ref;
371 }
4bb6408c
JS
372}
373
374void wxFont::SetPointSize(int pointSize)
375{
376 Unshare();
dfe1eee3 377
4bb6408c 378 M_FONTDATA->m_pointSize = pointSize;
42f30fa2 379 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
4bb6408c
JS
380}
381
0c14b6c3 382void wxFont::SetFamily(wxFontFamily family)
4bb6408c
JS
383{
384 Unshare();
dfe1eee3 385
4bb6408c 386 M_FONTDATA->m_family = family;
42f30fa2 387 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
4bb6408c
JS
388}
389
0c14b6c3 390void wxFont::SetStyle(wxFontStyle style)
4bb6408c
JS
391{
392 Unshare();
dfe1eee3 393
4bb6408c 394 M_FONTDATA->m_style = style;
42f30fa2 395 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
4bb6408c
JS
396}
397
0c14b6c3 398void wxFont::SetWeight(wxFontWeight weight)
4bb6408c
JS
399{
400 Unshare();
dfe1eee3 401
4bb6408c 402 M_FONTDATA->m_weight = weight;
42f30fa2 403 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
4bb6408c
JS
404}
405
85ab460e 406bool wxFont::SetFaceName(const wxString& faceName)
4bb6408c
JS
407{
408 Unshare();
dfe1eee3 409
4bb6408c 410 M_FONTDATA->m_faceName = faceName;
42f30fa2 411 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
85ab460e
VZ
412
413 return wxFontBase::SetFaceName(faceName);
4bb6408c
JS
414}
415
416void wxFont::SetUnderlined(bool underlined)
417{
418 Unshare();
dfe1eee3 419
4bb6408c 420 M_FONTDATA->m_underlined = underlined;
4bb6408c
JS
421}
422
93ccaed8 423void wxFont::SetEncoding(wxFontEncoding encoding)
4bb6408c 424{
93ccaed8
VZ
425 Unshare();
426
427 M_FONTDATA->m_encoding = encoding;
42f30fa2 428 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
93ccaed8
VZ
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{
a1b806b9 444 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
9045ad9d 445
93ccaed8
VZ
446 return M_FONTDATA->m_pointSize;
447}
448
563f868d
GD
449wxString wxFont::GetFaceName() const
450{
a1b806b9 451 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
563f868d
GD
452
453 return M_FONTDATA->m_faceName ;
454}
455
59b7da02 456wxFontFamily wxFont::DoGetFamily() const
93ccaed8
VZ
457{
458 return M_FONTDATA->m_family;
459}
460
0c14b6c3 461wxFontStyle wxFont::GetStyle() const
93ccaed8 462{
a1b806b9 463 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX, wxT("invalid font") );
563f868d 464
93ccaed8
VZ
465 return M_FONTDATA->m_style;
466}
467
0c14b6c3 468wxFontWeight wxFont::GetWeight() const
93ccaed8 469{
a1b806b9 470 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") );
563f868d 471
93ccaed8
VZ
472 return M_FONTDATA->m_weight;
473}
474
475bool wxFont::GetUnderlined() const
476{
a1b806b9 477 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
563f868d 478
93ccaed8 479 return M_FONTDATA->m_underlined;
4bb6408c
JS
480}
481
563f868d 482wxFontEncoding wxFont::GetEncoding() const
4bb6408c 483{
a1b806b9 484 wxCHECK_MSG( IsOk(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
563f868d
GD
485
486 return M_FONTDATA->m_encoding;
4bb6408c
JS
487}
488
3bf5a59b 489const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
4bb6408c 490{
a1b806b9 491 wxCHECK_MSG( IsOk(), NULL, wxT("invalid font") );
563f868d 492
7520f3da 493 if(M_FONTDATA->m_nativeFontInfo.GetXFontName().empty())
563f868d
GD
494 GetInternalFont();
495
3bf5a59b 496 return &(M_FONTDATA->m_nativeFontInfo);
4bb6408c
JS
497}
498
93ccaed8
VZ
499// ----------------------------------------------------------------------------
500// real implementation
501// ----------------------------------------------------------------------------
4bb6408c 502
dfc54541
JS
503// Find an existing, or create a new, XFontStruct
504// based on this wxFont and the given scale. Append the
505// font to list in the private data for future reference.
f97c9854 506wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const
dfc54541 507{
a1b806b9 508 if ( !IsOk() )
d3b9f782 509 return NULL;
dfe1eee3 510
2d120f83
JS
511 long intScale = long(scale * 100.0 + 0.5); // key for wxXFont
512 int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100;
dfe1eee3 513
93ccaed8 514 // search existing fonts first
ac32ba44 515 wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst();
2d120f83
JS
516 while (node)
517 {
fd304d98 518 wxXFont* f = (wxXFont*) node->GetData();
2d120f83
JS
519 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
520 return f;
fd304d98 521 node = node->GetNext();
2d120f83 522 }
dfe1eee3 523
93ccaed8 524 // not found, create a new one
996994c7 525 wxString xFontSpec;
0d083705
VZ
526 XFontStruct *font = (XFontStruct *)
527 wxLoadQueryNearestFont(pointSize,
93ccaed8
VZ
528 M_FONTDATA->m_family,
529 M_FONTDATA->m_style,
530 M_FONTDATA->m_weight,
531 M_FONTDATA->m_underlined,
223d09f6 532 wxT(""),
996994c7
MB
533 M_FONTDATA->m_encoding,
534 &xFontSpec);
dfe1eee3 535
93ccaed8 536 if ( !font )
2d120f83 537 {
223d09f6 538 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
93ccaed8 539
d3b9f782 540 return NULL;
2d120f83 541 }
93ccaed8
VZ
542
543 wxXFont* f = new wxXFont;
996994c7
MB
544#if wxMOTIF_NEW_FONT_HANDLING
545 XFreeFont( (Display*) display, font );
546#else
93ccaed8 547 f->m_fontStruct = (WXFontStructPtr)font;
996994c7 548#endif
93ccaed8
VZ
549 f->m_display = ( display ? display : wxGetDisplay() );
550 f->m_scale = intScale;
7520f3da 551
eb9d223a 552#if wxMOTIF_USE_RENDER_TABLE
da494b40
MB
553 XmRendition rendition;
554 XmRenderTable renderTable;
555 Arg args[5];
556 int count = 0;
557
996994c7 558#if wxMOTIF_NEW_FONT_HANDLING
6991087b 559 char* fontSpec = wxStrdup(xFontSpec.mb_str());
996994c7
MB
560 XtSetArg( args[count], XmNfontName, fontSpec ); ++count;
561 XtSetArg( args[count], XmNfontType, XmFONT_IS_FONTSET ); ++count;
562#else
da494b40 563 XtSetArg( args[count], XmNfont, font ); ++count;
996994c7 564#endif
da494b40
MB
565 XtSetArg( args[count], XmNunderlineType,
566 GetUnderlined() ? XmSINGLE_LINE : XmNO_LINE ); ++count;
567 rendition = XmRenditionCreate( XmGetXmDisplay( (Display*)f->m_display ),
568 (XmStringTag)"",
569 args, count );
570 renderTable = XmRenderTableAddRenditions( NULL, &rendition, 1,
571 XmMERGE_REPLACE );
572
573 f->m_renderTable = (WXRenderTable)renderTable;
996994c7
MB
574 f->m_rendition = (WXRendition)rendition;
575 wxASSERT( f->m_renderTable != NULL );
eb9d223a 576#else // if !wxMOTIF_USE_RENDER_TABLE
996994c7
MB
577 f->m_fontList = XmFontListCreate ((XFontStruct*) font, XmSTRING_DEFAULT_CHARSET);
578 wxASSERT( f->m_fontList != NULL );
da494b40
MB
579#endif
580
996994c7
MB
581 M_FONTDATA->m_fonts.Append(f);
582
93ccaed8 583 return f;
dfc54541
JS
584}
585
996994c7
MB
586#if !wxMOTIF_NEW_FONT_HANDLING
587
93ccaed8 588WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const
dfc54541 589{
93ccaed8 590 wxXFont* f = GetInternalFont(scale, display);
dfe1eee3 591
93ccaed8
VZ
592 return (f ? f->m_fontStruct : (WXFontStructPtr) 0);
593}
dfe1eee3 594
eb9d223a
MB
595#endif
596
597#if !wxMOTIF_USE_RENDER_TABLE
598
93ccaed8
VZ
599WXFontList wxFont::GetFontList(double scale, WXDisplay* display) const
600{
601 wxXFont* f = GetInternalFont(scale, display);
dfe1eee3 602
93ccaed8 603 return (f ? f->m_fontList : (WXFontList) 0);
dfc54541 604}
7ecb8b06 605
eb9d223a 606#else // if wxMOTIF_USE_RENDER_TABLE
da494b40
MB
607
608WXRenderTable wxFont::GetRenderTable(WXDisplay* display) const
609{
610 wxXFont* f = GetInternalFont(1.0, display);
611
eb9d223a 612 return (f ? f->m_renderTable : (WXRenderTable) 0);
da494b40
MB
613}
614
eb9d223a 615#endif // wxMOTIF_USE_RENDER_TABLE
da494b40
MB
616
617WXFontType wxFont::GetFontType(WXDisplay* display) const
618{
eb9d223a 619#if wxMOTIF_USE_RENDER_TABLE
a1b806b9 620 return IsOk() ? GetRenderTable(display) : NULL;
da494b40 621#else
a1b806b9 622 return IsOk() ? GetFontList(1.0, display) : NULL;
da494b40
MB
623#endif
624}
625
73608949
MB
626WXFontType wxFont::GetFontTypeC(WXDisplay* display) const
627{
eb9d223a 628#if wxMOTIF_USE_RENDER_TABLE
a1b806b9 629 return IsOk() ? GetRenderTable(display) : NULL;
73608949 630#else
a1b806b9 631 return IsOk() ? XmFontListCopy( (XmFontList)GetFontList(1.0, display) ) : NULL;
73608949
MB
632#endif
633}
634
da494b40
MB
635/*static*/ WXString wxFont::GetFontTag()
636{
eb9d223a 637#if wxMOTIF_USE_RENDER_TABLE
da494b40
MB
638 return (WXString)XmNrenderTable;
639#else
640 return (WXString)XmNfontList;
641#endif
642}
996994c7 643
105fbe1f 644#if wxMOTIF_USE_RENDER_TABLE
996994c7
MB
645
646WXFontSet wxFont::GetFontSet(double scale, WXDisplay* display) const
647{
648 wxXFont* f = GetInternalFont(scale, display);
649
650 if( !f ) return (WXFontSet) 0;
651
652 Arg args[2];
653 int count = 0;
654
655 XtSetArg( args[count], XmNfont, 0 ); ++count;
656 XmRenditionRetrieve( (XmRendition) f->m_rendition, args, count );
657
658 return (WXFontSet) args[0].value;
659}
660
661void wxGetTextExtent(WXDisplay* display, const wxFont& font, double scale,
662 const wxString& str,
663 int* width, int* height, int* ascent, int* descent)
664{
665 XRectangle ink, logical;
666 WXFontSet fset = font.GetFontSet(scale, display);
7520f3da 667
9b135950 668 XmbTextExtents( (XFontSet)fset, str.mb_str(), str.length(), &ink, &logical);
996994c7
MB
669
670 if( width ) *width = logical.width;
671 if( height ) *height = logical.height;
7520f3da
WS
672 if( ascent ) *ascent = -logical.y;
673 if( descent ) *descent = logical.height + logical.y;
996994c7
MB
674}
675
105fbe1f 676#else // if !wxMOTIF_USE_RENDER_TABLE
996994c7
MB
677
678void wxGetTextExtent(WXDisplay* display, const wxFont& font,
679 double scale, const wxString& str,
680 int* width, int* height, int* ascent, int* descent)
681{
682 WXFontStructPtr pFontStruct = font.GetFontStruct(scale, display);
683
684 int direction, ascent2, descent2;
685 XCharStruct overall;
dd05139a 686 int slen = str.length();
996994c7 687
efb35beb 688 XTextExtents((XFontStruct*) pFontStruct,
5c33522f 689 const_cast<char*>((const char *)str.mb_str()), slen,
996994c7
MB
690 &direction, &ascent2, &descent2, &overall);
691
692 if ( width )
693 *width = (overall.width);
694 if ( height )
695 *height = (ascent2 + descent2);
696 if ( descent )
697 *descent = descent2;
698 if ( ascent )
699 *ascent = ascent2;
700}
701
105fbe1f 702#endif // !wxMOTIF_USE_RENDER_TABLE