]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/font.cpp
Warning fix about assigning unused value + little source cleaning.
[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#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "font.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#include "wx/defs.h"
28
29#ifdef __VMS
30#pragma message disable nosimpint
31#include "wx/vms_x_fix.h"
32#endif
33#include <Xm/Xm.h>
34#ifdef __VMS
35#pragma message enable nosimpint
36#endif
37
38#include "wx/string.h"
39#include "wx/font.h"
40#include "wx/gdicmn.h"
41#include "wx/utils.h" // for wxGetDisplay()
42#include "wx/fontutil.h" // for wxNativeFontInfo
43#include "wx/tokenzr.h"
44#include "wx/settings.h"
45#include "wx/motif/private.h"
46
47IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
48
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
55class wxXFont : public wxObject
56{
57public:
58 wxXFont();
59 ~wxXFont();
60
61 WXFontStructPtr m_fontStruct; // XFontStruct
62 WXFontList m_fontList; // Motif XmFontList
63#if wxCHECK_MOTIF_VERSION( 2, 0 )
64 WXRenderTable m_renderTable; // Motif XmRenderTable
65#endif
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,
79 bool underlined = false,
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
113 wxNativeFontInfo m_nativeFontInfo;
114
115 // A list of wxXFonts
116 wxList m_fonts;
117};
118
119// ============================================================================
120// implementation
121// ============================================================================
122
123// ----------------------------------------------------------------------------
124// wxXFont
125// ----------------------------------------------------------------------------
126
127wxXFont::wxXFont()
128{
129 m_fontStruct = (WXFontStructPtr) 0;
130 m_fontList = (WXFontList) 0;
131#if wxCHECK_MOTIF_VERSION( 2, 0 )
132 m_renderTable = (WXRenderTable) 0;
133#endif
134 m_display = (WXDisplay*) 0;
135 m_scale = 100;
136}
137
138wxXFont::~wxXFont()
139{
140 XmFontList fontList = (XmFontList) m_fontList;
141 XmFontListFree (fontList);
142
143#if wxCHECK_MOTIF_VERSION( 2, 0 ) && !wxCHECK_LESSTIF()
144 XmRenderTable renderTable = (XmRenderTable) m_renderTable;
145 XmRenderTableFree (renderTable);
146#endif
147
148 // TODO: why does freeing the font produce a segv???
149 // Note that XFreeFont wasn't called in wxWin 1.68 either.
150 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
151 // XFreeFont((Display*) m_display, fontStruct);
152}
153
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)
165{
166 if (family == wxDEFAULT)
167 m_family = wxSWISS;
168 else
169 m_family = family;
170
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;
190}
191
192wxFontRefData::~wxFontRefData()
193{
194 wxList::compatibility_iterator node = m_fonts.GetFirst();
195 while (node)
196 {
197 wxXFont* f = (wxXFont*) node->GetData();
198 delete f;
199 node = node->GetNext();
200 }
201 m_fonts.Clear();
202}
203
204// ----------------------------------------------------------------------------
205// wxFont
206// ----------------------------------------------------------------------------
207
208wxFont::wxFont(const wxNativeFontInfo& info)
209{
210 Init();
211
212 (void)Create(info.GetXFontName());
213}
214
215void wxFont::Init()
216{
217}
218
219bool wxFont::Create(int pointSize,
220 int family,
221 int style,
222 int weight,
223 bool underlined,
224 const wxString& faceName,
225 wxFontEncoding encoding)
226{
227 UnRef();
228 m_refData = new wxFontRefData(pointSize, family, style, weight,
229 underlined, faceName, encoding);
230
231 RealizeResource();
232
233 return true;
234}
235
236bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
237{
238 if( !fontname )
239 {
240 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT);
241 return true;
242 }
243
244 m_refData = new wxFontRefData();
245
246 M_FONTDATA->m_nativeFontInfo.SetXFontName(fontname); // X font name
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
334 return false;
335 }
336 return true;
337}
338
339wxFont::~wxFont()
340{
341}
342
343// ----------------------------------------------------------------------------
344// change the font attributes
345// ----------------------------------------------------------------------------
346
347void wxFont::Unshare()
348{
349 // Don't change shared data
350 if (!m_refData)
351 {
352 m_refData = new wxFontRefData();
353 }
354 else
355 {
356 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
357 UnRef();
358 m_refData = ref;
359 }
360}
361
362void wxFont::SetPointSize(int pointSize)
363{
364 Unshare();
365
366 M_FONTDATA->m_pointSize = pointSize;
367 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
368
369 RealizeResource();
370}
371
372void wxFont::SetFamily(int family)
373{
374 Unshare();
375
376 M_FONTDATA->m_family = family;
377 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
378
379 RealizeResource();
380}
381
382void wxFont::SetStyle(int style)
383{
384 Unshare();
385
386 M_FONTDATA->m_style = style;
387 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
388
389 RealizeResource();
390}
391
392void wxFont::SetWeight(int weight)
393{
394 Unshare();
395
396 M_FONTDATA->m_weight = weight;
397 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
398
399 RealizeResource();
400}
401
402void wxFont::SetFaceName(const wxString& faceName)
403{
404 Unshare();
405
406 M_FONTDATA->m_faceName = faceName;
407 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
408
409 RealizeResource();
410}
411
412void wxFont::SetUnderlined(bool underlined)
413{
414 Unshare();
415
416 M_FONTDATA->m_underlined = underlined;
417
418 RealizeResource();
419}
420
421void wxFont::SetEncoding(wxFontEncoding encoding)
422{
423 Unshare();
424
425 M_FONTDATA->m_encoding = encoding;
426 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
427
428 RealizeResource();
429}
430
431void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info)
432{
433 Unshare();
434
435 M_FONTDATA->m_nativeFontInfo = info;
436}
437
438// ----------------------------------------------------------------------------
439// query font attributes
440// ----------------------------------------------------------------------------
441
442int wxFont::GetPointSize() const
443{
444 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
445
446 return M_FONTDATA->m_pointSize;
447}
448
449wxString wxFont::GetFaceName() const
450{
451 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
452
453 return M_FONTDATA->m_faceName ;
454}
455
456int wxFont::GetFamily() const
457{
458 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
459
460 return M_FONTDATA->m_family;
461}
462
463int wxFont::GetStyle() const
464{
465 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
466
467 return M_FONTDATA->m_style;
468}
469
470int wxFont::GetWeight() const
471{
472 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
473
474 return M_FONTDATA->m_weight;
475}
476
477bool wxFont::GetUnderlined() const
478{
479 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
480
481 return M_FONTDATA->m_underlined;
482}
483
484wxFontEncoding wxFont::GetEncoding() const
485{
486 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
487
488 return M_FONTDATA->m_encoding;
489}
490
491const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
492{
493 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
494
495 if(M_FONTDATA->m_nativeFontInfo.GetXFontName().IsEmpty())
496 GetInternalFont();
497
498 return &(M_FONTDATA->m_nativeFontInfo);
499}
500
501// ----------------------------------------------------------------------------
502// real implementation
503// ----------------------------------------------------------------------------
504
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.
508wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const
509{
510 if ( !Ok() )
511 return (wxXFont *)NULL;
512
513 long intScale = long(scale * 100.0 + 0.5); // key for wxXFont
514 int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100;
515
516 // search existing fonts first
517 wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst();
518 while (node)
519 {
520 wxXFont* f = (wxXFont*) node->GetData();
521 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
522 return f;
523 node = node->GetNext();
524 }
525
526 // not found, create a new one
527 XFontStruct *font = (XFontStruct *)
528 wxLoadQueryNearestFont(pointSize,
529 M_FONTDATA->m_family,
530 M_FONTDATA->m_style,
531 M_FONTDATA->m_weight,
532 M_FONTDATA->m_underlined,
533 wxT(""),
534 M_FONTDATA->m_encoding);
535
536 if ( !font )
537 {
538 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
539
540 return (wxXFont*) NULL;
541 }
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
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
568 return f;
569}
570
571WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const
572{
573 wxXFont* f = GetInternalFont(scale, display);
574
575 return (f ? f->m_fontStruct : (WXFontStructPtr) 0);
576}
577
578WXFontList wxFont::GetFontList(double scale, WXDisplay* display) const
579{
580 wxXFont* f = GetInternalFont(scale, display);
581
582 return (f ? f->m_fontList : (WXFontList) 0);
583}
584
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}