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
26 #include "wx/vms_x_fix.h"
30 #pragma message enable nosimpint
34 #include "wx/string.h"
36 #include "wx/gdicmn.h"
37 #include "wx/utils.h" // for wxGetDisplay()
38 #include "wx/fontutil.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // For every wxFont, there must be a font for each display and scale requested.
47 // So these objects are stored in wxFontRefData::m_fonts
48 class wxXFont
: public wxObject
54 WXFontStructPtr m_fontStruct
; // XFontStruct
55 WXFontList m_fontList
; // Motif XmFontList
56 WXDisplay
* m_display
; // XDisplay
57 int m_scale
; // Scale * 100
60 class wxFontRefData
: public wxGDIRefData
65 wxFontRefData(int size
= wxDEFAULT
,
66 int family
= wxDEFAULT
,
67 int style
= wxDEFAULT
,
68 int weight
= wxDEFAULT
,
69 bool underlined
= FALSE
,
70 const wxString
& faceName
= wxEmptyString
,
71 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
73 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
76 wxFontRefData(const wxFontRefData
& data
)
78 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
79 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
85 // common part of all ctors
91 const wxString
& faceName
,
92 wxFontEncoding encoding
);
101 wxFontEncoding m_encoding
;
103 // A list of wxXFonts
107 // ============================================================================
109 // ============================================================================
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
117 m_fontStruct
= (WXFontStructPtr
) 0;
118 m_fontList
= (WXFontList
) 0;
119 m_display
= (WXDisplay
*) 0;
125 XmFontList fontList
= (XmFontList
) m_fontList
;
127 XmFontListFree (fontList
);
129 // TODO: why does freeing the font produce a segv???
130 // Note that XFreeFont wasn't called in wxWin 1.68 either.
131 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
132 // XFreeFont((Display*) m_display, fontStruct);
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 void wxFontRefData::Init(int pointSize
,
144 const wxString
& faceName
,
145 wxFontEncoding encoding
)
147 if (family
== wxDEFAULT
)
152 m_faceName
= faceName
;
154 if (style
== wxDEFAULT
)
159 if (weight
== wxDEFAULT
)
164 if (pointSize
== wxDEFAULT
)
167 m_pointSize
= pointSize
;
169 m_underlined
= underlined
;
170 m_encoding
= encoding
;
173 wxFontRefData::~wxFontRefData()
175 wxNode
* node
= m_fonts
.First();
178 wxXFont
* f
= (wxXFont
*) node
->Data();
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
192 wxTheFontList
->Append(this);
195 bool wxFont::Create(int pointSize
,
200 const wxString
& faceName
,
201 wxFontEncoding encoding
)
204 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
205 underlined
, faceName
, encoding
);
215 wxTheFontList
->DeleteObject(this);
218 // ----------------------------------------------------------------------------
219 // change the font attributes
220 // ----------------------------------------------------------------------------
222 void wxFont::Unshare()
224 // Don't change shared data
227 m_refData
= new wxFontRefData();
231 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
237 void wxFont::SetPointSize(int pointSize
)
241 M_FONTDATA
->m_pointSize
= pointSize
;
246 void wxFont::SetFamily(int family
)
250 M_FONTDATA
->m_family
= family
;
255 void wxFont::SetStyle(int style
)
259 M_FONTDATA
->m_style
= style
;
264 void wxFont::SetWeight(int weight
)
268 M_FONTDATA
->m_weight
= weight
;
273 void wxFont::SetFaceName(const wxString
& faceName
)
277 M_FONTDATA
->m_faceName
= faceName
;
282 void wxFont::SetUnderlined(bool underlined
)
286 M_FONTDATA
->m_underlined
= underlined
;
291 void wxFont::SetEncoding(wxFontEncoding encoding
)
295 M_FONTDATA
->m_encoding
= encoding
;
300 // ----------------------------------------------------------------------------
301 // query font attributes
302 // ----------------------------------------------------------------------------
304 int wxFont::GetPointSize() const
306 return M_FONTDATA
->m_pointSize
;
309 int wxFont::GetFamily() const
311 return M_FONTDATA
->m_family
;
314 int wxFont::GetStyle() const
316 return M_FONTDATA
->m_style
;
319 int wxFont::GetWeight() const
321 return M_FONTDATA
->m_weight
;
324 bool wxFont::GetUnderlined() const
326 return M_FONTDATA
->m_underlined
;
329 wxString
wxFont::GetFaceName() const
333 str
= M_FONTDATA
->m_faceName
;
337 wxFontEncoding
wxFont::GetEncoding() const
339 return M_FONTDATA
->m_encoding
;
342 // ----------------------------------------------------------------------------
343 // real implementation
344 // ----------------------------------------------------------------------------
346 // Find an existing, or create a new, XFontStruct
347 // based on this wxFont and the given scale. Append the
348 // font to list in the private data for future reference.
349 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
352 return (wxXFont
*)NULL
;
354 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
355 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
357 // search existing fonts first
358 wxNode
* node
= M_FONTDATA
->m_fonts
.First();
361 wxXFont
* f
= (wxXFont
*) node
->Data();
362 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
367 // not found, create a new one
368 XFontStruct
*font
= (XFontStruct
*)
369 wxLoadQueryNearestFont(pointSize
,
370 M_FONTDATA
->m_family
,
372 M_FONTDATA
->m_weight
,
373 M_FONTDATA
->m_underlined
,
375 M_FONTDATA
->m_encoding
);
379 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
381 return (wxXFont
*) NULL
;
384 wxXFont
* f
= new wxXFont
;
385 f
->m_fontStruct
= (WXFontStructPtr
)font
;
386 f
->m_display
= ( display
? display
: wxGetDisplay() );
387 f
->m_scale
= intScale
;
388 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
389 M_FONTDATA
->m_fonts
.Append(f
);
394 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
396 wxXFont
* f
= GetInternalFont(scale
, display
);
398 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
401 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
403 wxXFont
* f
= GetInternalFont(scale
, display
);
405 return (f
? f
->m_fontList
: (WXFontList
) 0);