1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "font.h"
25 #pragma message disable nosimpint
29 #pragma message enable nosimpint
33 #include "wx/string.h"
35 #include "wx/gdicmn.h"
36 #include "wx/utils.h" // for wxGetDisplay()
37 #include "wx/fontutil.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // For every wxFont, there must be a font for each display and scale requested.
46 // So these objects are stored in wxFontRefData::m_fonts
47 class wxXFont
: public wxObject
53 WXFontStructPtr m_fontStruct
; // XFontStruct
54 WXFontList m_fontList
; // Motif XmFontList
55 WXDisplay
* m_display
; // XDisplay
56 int m_scale
; // Scale * 100
59 class wxFontRefData
: public wxGDIRefData
64 wxFontRefData(int size
= wxDEFAULT
,
65 int family
= wxDEFAULT
,
66 int style
= wxDEFAULT
,
67 int weight
= wxDEFAULT
,
68 bool underlined
= FALSE
,
69 const wxString
& faceName
= wxEmptyString
,
70 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
72 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
75 wxFontRefData(const wxFontRefData
& data
)
77 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
78 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
84 // common part of all ctors
90 const wxString
& faceName
,
91 wxFontEncoding encoding
);
100 wxFontEncoding m_encoding
;
102 // A list of wxXFonts
106 // ============================================================================
108 // ============================================================================
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
116 m_fontStruct
= (WXFontStructPtr
) 0;
117 m_fontList
= (WXFontList
) 0;
118 m_display
= (WXDisplay
*) 0;
124 XmFontList fontList
= (XmFontList
) m_fontList
;
126 XmFontListFree (fontList
);
128 // TODO: why does freeing the font produce a segv???
129 // Note that XFreeFont wasn't called in wxWin 1.68 either.
130 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
131 // XFreeFont((Display*) m_display, fontStruct);
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 void wxFontRefData::Init(int pointSize
,
143 const wxString
& faceName
,
144 wxFontEncoding encoding
)
146 if (family
== wxDEFAULT
)
151 m_faceName
= faceName
;
153 if (style
== wxDEFAULT
)
158 if (weight
== wxDEFAULT
)
163 if (pointSize
== wxDEFAULT
)
166 m_pointSize
= pointSize
;
168 m_underlined
= underlined
;
169 m_encoding
= encoding
;
172 wxFontRefData::~wxFontRefData()
174 wxNode
* node
= m_fonts
.First();
177 wxXFont
* f
= (wxXFont
*) node
->Data();
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
191 wxTheFontList
->Append(this);
194 bool wxFont::Create(int pointSize
,
199 const wxString
& faceName
,
200 wxFontEncoding encoding
)
203 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
204 underlined
, faceName
, encoding
);
214 wxTheFontList
->DeleteObject(this);
217 // ----------------------------------------------------------------------------
218 // change the font attributes
219 // ----------------------------------------------------------------------------
221 void wxFont::Unshare()
223 // Don't change shared data
226 m_refData
= new wxFontRefData();
230 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
236 void wxFont::SetPointSize(int pointSize
)
240 M_FONTDATA
->m_pointSize
= pointSize
;
245 void wxFont::SetFamily(int family
)
249 M_FONTDATA
->m_family
= family
;
254 void wxFont::SetStyle(int style
)
258 M_FONTDATA
->m_style
= style
;
263 void wxFont::SetWeight(int weight
)
267 M_FONTDATA
->m_weight
= weight
;
272 void wxFont::SetFaceName(const wxString
& faceName
)
276 M_FONTDATA
->m_faceName
= faceName
;
281 void wxFont::SetUnderlined(bool underlined
)
285 M_FONTDATA
->m_underlined
= underlined
;
290 void wxFont::SetEncoding(wxFontEncoding encoding
)
294 M_FONTDATA
->m_encoding
= encoding
;
299 // ----------------------------------------------------------------------------
300 // query font attributes
301 // ----------------------------------------------------------------------------
303 int wxFont::GetPointSize() const
305 return M_FONTDATA
->m_pointSize
;
308 int wxFont::GetFamily() const
310 return M_FONTDATA
->m_family
;
313 int wxFont::GetStyle() const
315 return M_FONTDATA
->m_style
;
318 int wxFont::GetWeight() const
320 return M_FONTDATA
->m_weight
;
323 bool wxFont::GetUnderlined() const
325 return M_FONTDATA
->m_underlined
;
328 wxString
wxFont::GetFaceName() const
332 str
= M_FONTDATA
->m_faceName
;
336 wxFontEncoding
wxFont::GetEncoding() const
338 return M_FONTDATA
->m_encoding
;
341 // ----------------------------------------------------------------------------
342 // real implementation
343 // ----------------------------------------------------------------------------
345 // Find an existing, or create a new, XFontStruct
346 // based on this wxFont and the given scale. Append the
347 // font to list in the private data for future reference.
348 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
351 return (wxXFont
*)NULL
;
353 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
354 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
356 // search existing fonts first
357 wxNode
* node
= M_FONTDATA
->m_fonts
.First();
360 wxXFont
* f
= (wxXFont
*) node
->Data();
361 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
366 // not found, create a new one
367 XFontStruct
*font
= (XFontStruct
*)
368 wxLoadQueryNearestFont(pointSize
,
369 M_FONTDATA
->m_family
,
371 M_FONTDATA
->m_weight
,
372 M_FONTDATA
->m_underlined
,
374 M_FONTDATA
->m_encoding
);
378 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
380 return (wxXFont
*) NULL
;
383 wxXFont
* f
= new wxXFont
;
384 f
->m_fontStruct
= (WXFontStructPtr
)font
;
385 f
->m_display
= ( display
? display
: wxGetDisplay() );
386 f
->m_scale
= intScale
;
387 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
388 M_FONTDATA
->m_fonts
.Append(f
);
393 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
395 wxXFont
* f
= GetInternalFont(scale
, display
);
397 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
400 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
402 wxXFont
* f
= GetInternalFont(scale
, display
);
404 return (f
? f
->m_fontList
: (WXFontList
) 0);