]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/font.cpp
Semicolon needed.
[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// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#include "wx/defs.h"
24
25#ifdef __VMS
26#pragma message disable nosimpint
27#include "wx/vms_x_fix.h"
28#endif
29#include <Xm/Xm.h>
30#ifdef __VMS
31#pragma message enable nosimpint
32#endif
33
34#include "wx/string.h"
35#include "wx/font.h"
36#include "wx/gdicmn.h"
37#include "wx/utils.h" // for wxGetDisplay()
38#include "wx/fontutil.h" // for wxNativeFontInfo
39#include "wx/tokenzr.h"
40#include "wx/settings.h"
41#include "wx/motif/private.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#if !wxMOTIF_NEW_FONT_HANDLING
58 WXFontStructPtr m_fontStruct; // XFontStruct
59#endif
60#if !wxMOTIF_USE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
61 WXFontList m_fontList; // Motif XmFontList
62#else // if wxUSE_RENDER_TABLE
63 WXRenderTable m_renderTable; // Motif XmRenderTable
64 WXRendition m_rendition; // Motif XmRendition
65#endif
66 WXDisplay* m_display; // XDisplay
67 int m_scale; // Scale * 100
68};
69
70class wxFontRefData: public wxGDIRefData
71{
72 friend 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#if !wxMOTIF_NEW_FONT_HANDLING
130 m_fontStruct = (WXFontStructPtr) 0;
131#endif
132#if !wxMOTIF_USE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
133 m_fontList = (WXFontList) 0;
134#else // if wxMOTIF_USE_RENDER_TABLE
135 m_renderTable = (WXRenderTable) 0;
136 m_rendition = (WXRendition) 0;
137#endif
138 m_display = (WXDisplay*) 0;
139 m_scale = 100;
140}
141
142wxXFont::~wxXFont()
143{
144#if !wxMOTIF_USE_RENDER_TABLE
145 if (m_fontList)
146 XmFontListFree ((XmFontList) m_fontList);
147 m_fontList = NULL;
148#else // if wxUSE_RENDER_TABLE
149 if (m_renderTable)
150 XmRenderTableFree ((XmRenderTable) m_renderTable);
151 m_renderTable = NULL;
152#endif
153
154 // TODO: why does freeing the font produce a segv???
155 // Note that XFreeFont wasn't called in wxWin 1.68 either.
156 // MBN: probably some interaction with fonts being still
157 // in use in some widgets...
158 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
159 // XFreeFont((Display*) m_display, fontStruct);
160}
161
162// ----------------------------------------------------------------------------
163// wxFontRefData
164// ----------------------------------------------------------------------------
165
166void wxFontRefData::Init(int pointSize,
167 int family,
168 int style,
169 int weight,
170 bool underlined,
171 const wxString& faceName,
172 wxFontEncoding encoding)
173{
174 if (family == wxDEFAULT)
175 m_family = wxSWISS;
176 else
177 m_family = family;
178
179 m_faceName = faceName;
180
181 if (style == wxDEFAULT)
182 m_style = wxNORMAL;
183 else
184 m_style = style;
185
186 if (weight == wxDEFAULT)
187 m_weight = wxNORMAL;
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;
198}
199
200wxFontRefData::~wxFontRefData()
201{
202 wxList::compatibility_iterator node = m_fonts.GetFirst();
203 while (node)
204 {
205 wxXFont* f = (wxXFont*) node->GetData();
206 delete f;
207 node = node->GetNext();
208 }
209 m_fonts.Clear();
210}
211
212// ----------------------------------------------------------------------------
213// wxFont
214// ----------------------------------------------------------------------------
215
216wxFont::wxFont(const wxNativeFontInfo& info)
217{
218 (void)Create(info.GetXFontName());
219}
220
221bool wxFont::Create(int pointSize,
222 int family,
223 int style,
224 int weight,
225 bool underlined,
226 const wxString& faceName,
227 wxFontEncoding encoding)
228{
229 UnRef();
230 m_refData = new wxFontRefData(pointSize, family, style, weight,
231 underlined, faceName, encoding);
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
370void wxFont::SetFamily(int family)
371{
372 Unshare();
373
374 M_FONTDATA->m_family = family;
375 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
376}
377
378void wxFont::SetStyle(int style)
379{
380 Unshare();
381
382 M_FONTDATA->m_style = style;
383 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
384}
385
386void wxFont::SetWeight(int weight)
387{
388 Unshare();
389
390 M_FONTDATA->m_weight = weight;
391 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
392}
393
394void wxFont::SetFaceName(const wxString& faceName)
395{
396 Unshare();
397
398 M_FONTDATA->m_faceName = faceName;
399 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
400}
401
402void wxFont::SetUnderlined(bool underlined)
403{
404 Unshare();
405
406 M_FONTDATA->m_underlined = underlined;
407}
408
409void wxFont::SetEncoding(wxFontEncoding encoding)
410{
411 Unshare();
412
413 M_FONTDATA->m_encoding = encoding;
414 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
415}
416
417void wxFont::DoSetNativeFontInfo(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
477const wxNativeFontInfo *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 &(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 wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst();
504 while (node)
505 {
506 wxXFont* f = (wxXFont*) node->GetData();
507 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
508 return f;
509 node = node->GetNext();
510 }
511
512 // not found, create a new one
513 wxString xFontSpec;
514 XFontStruct *font = (XFontStruct *)
515 wxLoadQueryNearestFont(pointSize,
516 M_FONTDATA->m_family,
517 M_FONTDATA->m_style,
518 M_FONTDATA->m_weight,
519 M_FONTDATA->m_underlined,
520 wxT(""),
521 M_FONTDATA->m_encoding,
522 &xFontSpec);
523
524 if ( !font )
525 {
526 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
527
528 return (wxXFont*) NULL;
529 }
530
531 wxXFont* f = new wxXFont;
532#if wxMOTIF_NEW_FONT_HANDLING
533 XFreeFont( (Display*) display, font );
534#else
535 f->m_fontStruct = (WXFontStructPtr)font;
536#endif
537 f->m_display = ( display ? display : wxGetDisplay() );
538 f->m_scale = intScale;
539
540#if wxMOTIF_USE_RENDER_TABLE
541 XmRendition rendition;
542 XmRenderTable renderTable;
543 Arg args[5];
544 int count = 0;
545
546#if wxMOTIF_NEW_FONT_HANDLING
547 wxChar* fontSpec = wxStrdup( xFontSpec.c_str() );
548 XtSetArg( args[count], XmNfontName, fontSpec ); ++count;
549 XtSetArg( args[count], XmNfontType, XmFONT_IS_FONTSET ); ++count;
550#else
551 XtSetArg( args[count], XmNfont, font ); ++count;
552#endif
553 XtSetArg( args[count], XmNunderlineType,
554 GetUnderlined() ? XmSINGLE_LINE : XmNO_LINE ); ++count;
555 rendition = XmRenditionCreate( XmGetXmDisplay( (Display*)f->m_display ),
556 (XmStringTag)"",
557 args, count );
558 renderTable = XmRenderTableAddRenditions( NULL, &rendition, 1,
559 XmMERGE_REPLACE );
560
561 f->m_renderTable = (WXRenderTable)renderTable;
562 f->m_rendition = (WXRendition)rendition;
563 wxASSERT( f->m_renderTable != NULL );
564#else // if !wxMOTIF_USE_RENDER_TABLE
565 f->m_fontList = XmFontListCreate ((XFontStruct*) font, XmSTRING_DEFAULT_CHARSET);
566 wxASSERT( f->m_fontList != NULL );
567#endif
568
569 M_FONTDATA->m_fonts.Append(f);
570
571 return f;
572}
573
574#if !wxMOTIF_NEW_FONT_HANDLING
575
576WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const
577{
578 wxXFont* f = GetInternalFont(scale, display);
579
580 return (f ? f->m_fontStruct : (WXFontStructPtr) 0);
581}
582
583#endif
584
585#if !wxMOTIF_USE_RENDER_TABLE
586
587WXFontList wxFont::GetFontList(double scale, WXDisplay* display) const
588{
589 wxXFont* f = GetInternalFont(scale, display);
590
591 return (f ? f->m_fontList : (WXFontList) 0);
592}
593
594#else // if wxMOTIF_USE_RENDER_TABLE
595
596WXRenderTable wxFont::GetRenderTable(WXDisplay* display) const
597{
598 wxXFont* f = GetInternalFont(1.0, display);
599
600 return (f ? f->m_renderTable : (WXRenderTable) 0);
601}
602
603#endif // wxMOTIF_USE_RENDER_TABLE
604
605WXFontType wxFont::GetFontType(WXDisplay* display) const
606{
607#if wxMOTIF_USE_RENDER_TABLE
608 return Ok() ? GetRenderTable(display) : NULL;
609#else
610 return Ok() ? GetFontList(1.0, display) : NULL;
611#endif
612}
613
614WXFontType wxFont::GetFontTypeC(WXDisplay* display) const
615{
616#if wxMOTIF_USE_RENDER_TABLE
617 return Ok() ? GetRenderTable(display) : NULL;
618#else
619 return Ok() ? XmFontListCopy( (XmFontList)GetFontList(1.0, display) ) : NULL;
620#endif
621}
622
623/*static*/ WXString wxFont::GetFontTag()
624{
625#if wxMOTIF_USE_RENDER_TABLE
626 return (WXString)XmNrenderTable;
627#else
628 return (WXString)XmNfontList;
629#endif
630}
631
632#if wxMOTIF_NEW_FONT_HANDLING
633
634WXFontSet wxFont::GetFontSet(double scale, WXDisplay* display) const
635{
636 wxXFont* f = GetInternalFont(scale, display);
637
638 if( !f ) return (WXFontSet) 0;
639
640 Arg args[2];
641 int count = 0;
642
643 XtSetArg( args[count], XmNfont, 0 ); ++count;
644 XmRenditionRetrieve( (XmRendition) f->m_rendition, args, count );
645
646 return (WXFontSet) args[0].value;
647}
648
649void wxGetTextExtent(WXDisplay* display, const wxFont& font, double scale,
650 const wxString& str,
651 int* width, int* height, int* ascent, int* descent)
652{
653 XRectangle ink, logical;
654 WXFontSet fset = font.GetFontSet(scale, display);
655
656 XmbTextExtents( (XFontSet)fset, str.c_str(), str.length(), &ink, &logical);
657
658 if( width ) *width = logical.width;
659 if( height ) *height = logical.height;
660 if( ascent ) *ascent = -logical.y;
661 if( descent ) *descent = logical.height + logical.y;
662}
663
664#else // if !wxMOTIF_NEW_FONT_HANDLING
665
666void wxGetTextExtent(WXDisplay* display, const wxFont& font,
667 double scale, const wxString& str,
668 int* width, int* height, int* ascent, int* descent)
669{
670 WXFontStructPtr pFontStruct = font.GetFontStruct(scale, display);
671
672 int direction, ascent2, descent2;
673 XCharStruct overall;
674 int slen = str.Len();
675
676 XTextExtents((XFontStruct*) pFontStruct, (char*) str.c_str(), slen,
677 &direction, &ascent2, &descent2, &overall);
678
679 if ( width )
680 *width = (overall.width);
681 if ( height )
682 *height = (ascent2 + descent2);
683 if ( descent )
684 *descent = descent2;
685 if ( ascent )
686 *ascent = ascent2;
687}
688
689#endif // !wxMOTIF_NEW_FONT_HANDLING