]> git.saurik.com Git - wxWidgets.git/blob - src/motif/font.cpp
include wx/defs.h before testing for wxUSE_TEXTDLG defined in it
[wxWidgets.git] / src / motif / font.cpp
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
47 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
48
49 #if wxCHECK_MOTIF_VERSION( 2, 0 ) && !wxCHECK_LESSTIF()
50 #define wxUSE_RENDER_TABLE 1
51 #else
52 #define wxUSE_RENDER_TABLE 0
53 #endif
54
55 // ----------------------------------------------------------------------------
56 // private classes
57 // ----------------------------------------------------------------------------
58
59 // For every wxFont, there must be a font for each display and scale requested.
60 // So these objects are stored in wxFontRefData::m_fonts
61 class wxXFont : public wxObject
62 {
63 public:
64 wxXFont();
65 ~wxXFont();
66
67 #if !wxMOTIF_NEW_FONT_HANDLING
68 WXFontStructPtr m_fontStruct; // XFontStruct
69 #endif
70 #if !wxUSE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
71 WXFontList m_fontList; // Motif XmFontList
72 #else // if wxUSE_RENDER_TABLE
73 WXRenderTable m_renderTable; // Motif XmRenderTable
74 WXRendition m_rendition; // Motif XmRendition
75 #endif
76 WXDisplay* m_display; // XDisplay
77 int m_scale; // Scale * 100
78 };
79
80 class wxFontRefData: public wxGDIRefData
81 {
82 friend class wxFont;
83
84 public:
85 wxFontRefData(int size = wxDEFAULT,
86 int family = wxDEFAULT,
87 int style = wxDEFAULT,
88 int weight = wxDEFAULT,
89 bool underlined = false,
90 const wxString& faceName = wxEmptyString,
91 wxFontEncoding encoding = wxFONTENCODING_DEFAULT)
92 {
93 Init(size, family, style, weight, underlined, faceName, encoding);
94 }
95
96 wxFontRefData(const wxFontRefData& data)
97 {
98 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
99 data.m_underlined, data.m_faceName, data.m_encoding);
100 }
101
102 ~wxFontRefData();
103
104 protected:
105 // common part of all ctors
106 void Init(int size,
107 int family,
108 int style,
109 int weight,
110 bool underlined,
111 const wxString& faceName,
112 wxFontEncoding encoding);
113
114 // font attributes
115 int m_pointSize;
116 int m_family;
117 int m_style;
118 int m_weight;
119 bool m_underlined;
120 wxString m_faceName;
121 wxFontEncoding m_encoding;
122
123 wxNativeFontInfo m_nativeFontInfo;
124
125 // A list of wxXFonts
126 wxList m_fonts;
127 };
128
129 // ============================================================================
130 // implementation
131 // ============================================================================
132
133 // ----------------------------------------------------------------------------
134 // wxXFont
135 // ----------------------------------------------------------------------------
136
137 wxXFont::wxXFont()
138 {
139 #if !wxMOTIF_NEW_FONT_HANDLING
140 m_fontStruct = (WXFontStructPtr) 0;
141 #endif
142 #if !wxUSE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
143 m_fontList = (WXFontList) 0;
144 #else // if wxUSE_RENDER_TABLE
145 m_renderTable = (WXRenderTable) 0;
146 m_rendition = (WXRendition) 0;
147 #endif
148 m_display = (WXDisplay*) 0;
149 m_scale = 100;
150 }
151
152 wxXFont::~wxXFont()
153 {
154 #if !wxUSE_RENDER_TABLE
155 if (m_fontList)
156 XmFontListFree ((XmFontList) m_fontList);
157 m_fontList = NULL;
158 #else // if wxUSE_RENDER_TABLE
159 if (m_renderTable)
160 XmRenderTableFree ((XmRenderTable) m_renderTable);
161 m_renderTable = NULL;
162 #endif
163
164 // TODO: why does freeing the font produce a segv???
165 // Note that XFreeFont wasn't called in wxWin 1.68 either.
166 // MBN: probably some interaction with fonts being still
167 // in use in some widgets...
168 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
169 // XFreeFont((Display*) m_display, fontStruct);
170 }
171
172 // ----------------------------------------------------------------------------
173 // wxFontRefData
174 // ----------------------------------------------------------------------------
175
176 void wxFontRefData::Init(int pointSize,
177 int family,
178 int style,
179 int weight,
180 bool underlined,
181 const wxString& faceName,
182 wxFontEncoding encoding)
183 {
184 if (family == wxDEFAULT)
185 m_family = wxSWISS;
186 else
187 m_family = family;
188
189 m_faceName = faceName;
190
191 if (style == wxDEFAULT)
192 m_style = wxNORMAL;
193 else
194 m_style = style;
195
196 if (weight == wxDEFAULT)
197 m_weight = wxNORMAL;
198 else
199 m_weight = weight;
200
201 if (pointSize == wxDEFAULT)
202 m_pointSize = 12;
203 else
204 m_pointSize = pointSize;
205
206 m_underlined = underlined;
207 m_encoding = encoding;
208 }
209
210 wxFontRefData::~wxFontRefData()
211 {
212 wxList::compatibility_iterator node = m_fonts.GetFirst();
213 while (node)
214 {
215 wxXFont* f = (wxXFont*) node->GetData();
216 delete f;
217 node = node->GetNext();
218 }
219 m_fonts.Clear();
220 }
221
222 // ----------------------------------------------------------------------------
223 // wxFont
224 // ----------------------------------------------------------------------------
225
226 wxFont::wxFont(const wxNativeFontInfo& info)
227 {
228 Init();
229
230 (void)Create(info.GetXFontName());
231 }
232
233 void wxFont::Init()
234 {
235 }
236
237 bool wxFont::Create(int pointSize,
238 int family,
239 int style,
240 int weight,
241 bool underlined,
242 const wxString& faceName,
243 wxFontEncoding encoding)
244 {
245 UnRef();
246 m_refData = new wxFontRefData(pointSize, family, style, weight,
247 underlined, faceName, encoding);
248
249 RealizeResource();
250
251 return true;
252 }
253
254 bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
255 {
256 if( !fontname )
257 {
258 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT);
259 return true;
260 }
261
262 m_refData = new wxFontRefData();
263
264 M_FONTDATA->m_nativeFontInfo.SetXFontName(fontname); // X font name
265
266 wxString tmp;
267
268 wxStringTokenizer tn( fontname, wxT("-") );
269
270 tn.GetNextToken(); // skip initial empty token
271 tn.GetNextToken(); // foundry
272
273
274 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
275
276 tmp = tn.GetNextToken().MakeUpper(); // weight
277 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
278 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD;
279 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
280 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD;
281 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
282
283 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
284 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
285
286 tmp = tn.GetNextToken().MakeUpper(); // slant
287 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
288 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
289
290 tn.GetNextToken(); // set width
291 tn.GetNextToken(); // add. style
292 tn.GetNextToken(); // pixel size
293
294 tmp = tn.GetNextToken(); // pointsize
295 if (tmp != wxT("*"))
296 {
297 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
298 M_FONTDATA->m_pointSize = (int)(num / 10);
299 }
300
301 tn.GetNextToken(); // x-res
302 tn.GetNextToken(); // y-res
303
304 tmp = tn.GetNextToken().MakeUpper(); // spacing
305
306 if (tmp == wxT("M"))
307 M_FONTDATA->m_family = wxMODERN;
308 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
309 M_FONTDATA->m_family = wxROMAN;
310 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
311 M_FONTDATA->m_family = wxSWISS;
312 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
313 M_FONTDATA->m_family = wxTELETYPE;
314 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
315 M_FONTDATA->m_family = wxDECORATIVE;
316 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
317 M_FONTDATA->m_family = wxSCRIPT;
318
319 tn.GetNextToken(); // avg width
320
321 // deal with font encoding
322 M_FONTDATA->m_encoding = enc;
323 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
324 {
325 wxString registry = tn.GetNextToken().MakeUpper(),
326 encoding = tn.GetNextToken().MakeUpper();
327
328 if ( registry == _T("ISO8859") )
329 {
330 int cp;
331 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
332 {
333 M_FONTDATA->m_encoding =
334 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
335 }
336 }
337 else if ( registry == _T("MICROSOFT") )
338 {
339 int cp;
340 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
341 {
342 M_FONTDATA->m_encoding =
343 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
344 }
345 }
346 else if ( registry == _T("KOI8") )
347 {
348 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
349 }
350 //else: unknown encoding - may be give a warning here?
351 else
352 return false;
353 }
354 return true;
355 }
356
357 wxFont::~wxFont()
358 {
359 }
360
361 // ----------------------------------------------------------------------------
362 // change the font attributes
363 // ----------------------------------------------------------------------------
364
365 void wxFont::Unshare()
366 {
367 // Don't change shared data
368 if (!m_refData)
369 {
370 m_refData = new wxFontRefData();
371 }
372 else
373 {
374 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
375 UnRef();
376 m_refData = ref;
377 }
378 }
379
380 void wxFont::SetPointSize(int pointSize)
381 {
382 Unshare();
383
384 M_FONTDATA->m_pointSize = pointSize;
385 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
386
387 RealizeResource();
388 }
389
390 void wxFont::SetFamily(int family)
391 {
392 Unshare();
393
394 M_FONTDATA->m_family = family;
395 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
396
397 RealizeResource();
398 }
399
400 void wxFont::SetStyle(int style)
401 {
402 Unshare();
403
404 M_FONTDATA->m_style = style;
405 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
406
407 RealizeResource();
408 }
409
410 void wxFont::SetWeight(int weight)
411 {
412 Unshare();
413
414 M_FONTDATA->m_weight = weight;
415 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
416
417 RealizeResource();
418 }
419
420 void wxFont::SetFaceName(const wxString& faceName)
421 {
422 Unshare();
423
424 M_FONTDATA->m_faceName = faceName;
425 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
426
427 RealizeResource();
428 }
429
430 void wxFont::SetUnderlined(bool underlined)
431 {
432 Unshare();
433
434 M_FONTDATA->m_underlined = underlined;
435
436 RealizeResource();
437 }
438
439 void wxFont::SetEncoding(wxFontEncoding encoding)
440 {
441 Unshare();
442
443 M_FONTDATA->m_encoding = encoding;
444 M_FONTDATA->m_nativeFontInfo.GetXFontName().Clear(); // invalid now
445
446 RealizeResource();
447 }
448
449 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info)
450 {
451 Unshare();
452
453 M_FONTDATA->m_nativeFontInfo = info;
454 }
455
456 // ----------------------------------------------------------------------------
457 // query font attributes
458 // ----------------------------------------------------------------------------
459
460 int wxFont::GetPointSize() const
461 {
462 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
463
464 return M_FONTDATA->m_pointSize;
465 }
466
467 wxString wxFont::GetFaceName() const
468 {
469 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
470
471 return M_FONTDATA->m_faceName ;
472 }
473
474 int wxFont::GetFamily() const
475 {
476 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
477
478 return M_FONTDATA->m_family;
479 }
480
481 int wxFont::GetStyle() const
482 {
483 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
484
485 return M_FONTDATA->m_style;
486 }
487
488 int wxFont::GetWeight() const
489 {
490 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
491
492 return M_FONTDATA->m_weight;
493 }
494
495 bool wxFont::GetUnderlined() const
496 {
497 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
498
499 return M_FONTDATA->m_underlined;
500 }
501
502 wxFontEncoding wxFont::GetEncoding() const
503 {
504 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
505
506 return M_FONTDATA->m_encoding;
507 }
508
509 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
510 {
511 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
512
513 if(M_FONTDATA->m_nativeFontInfo.GetXFontName().IsEmpty())
514 GetInternalFont();
515
516 return &(M_FONTDATA->m_nativeFontInfo);
517 }
518
519 // ----------------------------------------------------------------------------
520 // real implementation
521 // ----------------------------------------------------------------------------
522
523 // Find an existing, or create a new, XFontStruct
524 // based on this wxFont and the given scale. Append the
525 // font to list in the private data for future reference.
526 wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const
527 {
528 if ( !Ok() )
529 return (wxXFont *)NULL;
530
531 long intScale = long(scale * 100.0 + 0.5); // key for wxXFont
532 int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100;
533
534 // search existing fonts first
535 wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst();
536 while (node)
537 {
538 wxXFont* f = (wxXFont*) node->GetData();
539 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
540 return f;
541 node = node->GetNext();
542 }
543
544 // not found, create a new one
545 wxString xFontSpec;
546 XFontStruct *font = (XFontStruct *)
547 wxLoadQueryNearestFont(pointSize,
548 M_FONTDATA->m_family,
549 M_FONTDATA->m_style,
550 M_FONTDATA->m_weight,
551 M_FONTDATA->m_underlined,
552 wxT(""),
553 M_FONTDATA->m_encoding,
554 &xFontSpec);
555
556 if ( !font )
557 {
558 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
559
560 return (wxXFont*) NULL;
561 }
562
563 wxXFont* f = new wxXFont;
564 #if wxMOTIF_NEW_FONT_HANDLING
565 XFreeFont( (Display*) display, font );
566 #else
567 f->m_fontStruct = (WXFontStructPtr)font;
568 #endif
569 f->m_display = ( display ? display : wxGetDisplay() );
570 f->m_scale = intScale;
571
572 #if wxUSE_RENDER_TABLE
573 XmRendition rendition;
574 XmRenderTable renderTable;
575 Arg args[5];
576 int count = 0;
577
578 #if wxMOTIF_NEW_FONT_HANDLING
579 wxChar* fontSpec = wxStrdup( xFontSpec.c_str() );
580 XtSetArg( args[count], XmNfontName, fontSpec ); ++count;
581 XtSetArg( args[count], XmNfontType, XmFONT_IS_FONTSET ); ++count;
582 #else
583 XtSetArg( args[count], XmNfont, font ); ++count;
584 #endif
585 XtSetArg( args[count], XmNunderlineType,
586 GetUnderlined() ? XmSINGLE_LINE : XmNO_LINE ); ++count;
587 rendition = XmRenditionCreate( XmGetXmDisplay( (Display*)f->m_display ),
588 (XmStringTag)"",
589 args, count );
590 renderTable = XmRenderTableAddRenditions( NULL, &rendition, 1,
591 XmMERGE_REPLACE );
592
593 f->m_renderTable = (WXRenderTable)renderTable;
594 f->m_rendition = (WXRendition)rendition;
595 wxASSERT( f->m_renderTable != NULL );
596 #else // if !wxUSE_RENDER_TABLE
597 f->m_fontList = XmFontListCreate ((XFontStruct*) font, XmSTRING_DEFAULT_CHARSET);
598 wxASSERT( f->m_fontList != NULL );
599 #endif
600
601 M_FONTDATA->m_fonts.Append(f);
602
603 return f;
604 }
605
606 #if !wxMOTIF_NEW_FONT_HANDLING
607
608 WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const
609 {
610 wxXFont* f = GetInternalFont(scale, display);
611
612 return (f ? f->m_fontStruct : (WXFontStructPtr) 0);
613 }
614
615 WXFontList wxFont::GetFontList(double scale, WXDisplay* display) const
616 {
617 #if !wxUSE_RENDER_TABLE
618 wxXFont* f = GetInternalFont(scale, display);
619
620 return (f ? f->m_fontList : (WXFontList) 0);
621 #else
622 return NULL;
623 #endif
624 }
625
626 #endif // !wxMOTIF_NEW_FONT_HANDLING
627
628 // declared in the header, can't use wxUSE_RENDER_TABLE
629 #if wxCHECK_MOTIF_VERSION( 2, 0 )
630
631 WXRenderTable wxFont::GetRenderTable(WXDisplay* display) const
632 {
633 #if wxUSE_RENDER_TABLE
634 wxXFont* f = GetInternalFont(1.0, display);
635
636 return (f ? f->m_renderTable : (WXFontList) 0);
637 #else
638 return NULL;
639 #endif
640 }
641
642 #endif // wxCHECK_MOTIF_VERSION( 2, 0 )
643
644 WXFontType wxFont::GetFontType(WXDisplay* display) const
645 {
646 #if wxUSE_RENDER_TABLE
647 return Ok() ? GetRenderTable(display) : NULL;
648 #else
649 return Ok() ? GetFontList(1.0, display) : NULL;
650 #endif
651 }
652
653 WXFontType wxFont::GetFontTypeC(WXDisplay* display) const
654 {
655 #if wxUSE_RENDER_TABLE
656 return Ok() ? GetRenderTable(display) : NULL;
657 #else
658 return Ok() ? XmFontListCopy( (XmFontList)GetFontList(1.0, display) ) : NULL;
659 #endif
660 }
661
662 /*static*/ WXString wxFont::GetFontTag()
663 {
664 #if wxUSE_RENDER_TABLE
665 return (WXString)XmNrenderTable;
666 #else
667 return (WXString)XmNfontList;
668 #endif
669 }
670
671 #if wxMOTIF_NEW_FONT_HANDLING
672
673 WXFontSet wxFont::GetFontSet(double scale, WXDisplay* display) const
674 {
675 wxXFont* f = GetInternalFont(scale, display);
676
677 if( !f ) return (WXFontSet) 0;
678
679 Arg args[2];
680 int count = 0;
681
682 XtSetArg( args[count], XmNfont, 0 ); ++count;
683 XmRenditionRetrieve( (XmRendition) f->m_rendition, args, count );
684
685 return (WXFontSet) args[0].value;
686 }
687
688 void wxGetTextExtent(WXDisplay* display, const wxFont& font, double scale,
689 const wxString& str,
690 int* width, int* height, int* ascent, int* descent)
691 {
692 XRectangle ink, logical;
693 WXFontSet fset = font.GetFontSet(scale, display);
694
695 XmbTextExtents( (XFontSet)fset, str.c_str(), str.length(), &ink, &logical);
696
697 if( width ) *width = logical.width;
698 if( height ) *height = logical.height;
699 if( ascent ) *ascent = -logical.y;
700 if( descent ) *descent = logical.height + logical.y;
701 }
702
703 #else // if !wxMOTIF_NEW_FONT_HANDLING
704
705 void wxGetTextExtent(WXDisplay* display, const wxFont& font,
706 double scale, const wxString& str,
707 int* width, int* height, int* ascent, int* descent)
708 {
709 WXFontStructPtr pFontStruct = font.GetFontStruct(scale, display);
710
711 int direction, ascent2, descent2;
712 XCharStruct overall;
713 int slen = str.Len();
714
715 XTextExtents((XFontStruct*) pFontStruct, (char*) str.c_str(), slen,
716 &direction, &ascent2, &descent2, &overall);
717
718 if ( width )
719 *width = (overall.width);
720 if ( height )
721 *height = (ascent2 + descent2);
722 if ( descent )
723 *descent = descent2;
724 if ( ascent )
725 *ascent = ascent2;
726 }
727
728 #endif // !wxMOTIF_NEW_FONT_HANDLING