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" // for wxNativeFontInfo
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 // ----------------------------------------------------------------------------
189 wxFont::wxFont(const wxNativeFontInfo
& info
)
193 (void)Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
194 info
.underlined
, info
.faceName
, info
.encoding
);
200 wxTheFontList
->Append(this);
203 bool wxFont::Create(int pointSize
,
208 const wxString
& faceName
,
209 wxFontEncoding encoding
)
212 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
213 underlined
, faceName
, encoding
);
223 wxTheFontList
->DeleteObject(this);
226 // ----------------------------------------------------------------------------
227 // change the font attributes
228 // ----------------------------------------------------------------------------
230 void wxFont::Unshare()
232 // Don't change shared data
235 m_refData
= new wxFontRefData();
239 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
245 void wxFont::SetPointSize(int pointSize
)
249 M_FONTDATA
->m_pointSize
= pointSize
;
254 void wxFont::SetFamily(int family
)
258 M_FONTDATA
->m_family
= family
;
263 void wxFont::SetStyle(int style
)
267 M_FONTDATA
->m_style
= style
;
272 void wxFont::SetWeight(int weight
)
276 M_FONTDATA
->m_weight
= weight
;
281 void wxFont::SetFaceName(const wxString
& faceName
)
285 M_FONTDATA
->m_faceName
= faceName
;
290 void wxFont::SetUnderlined(bool underlined
)
294 M_FONTDATA
->m_underlined
= underlined
;
299 void wxFont::SetEncoding(wxFontEncoding encoding
)
303 M_FONTDATA
->m_encoding
= encoding
;
308 // ----------------------------------------------------------------------------
309 // query font attributes
310 // ----------------------------------------------------------------------------
312 int wxFont::GetPointSize() const
314 return M_FONTDATA
->m_pointSize
;
317 int wxFont::GetFamily() const
319 return M_FONTDATA
->m_family
;
322 int wxFont::GetStyle() const
324 return M_FONTDATA
->m_style
;
327 int wxFont::GetWeight() const
329 return M_FONTDATA
->m_weight
;
332 bool wxFont::GetUnderlined() const
334 return M_FONTDATA
->m_underlined
;
337 wxString
wxFont::GetFaceName() const
341 str
= M_FONTDATA
->m_faceName
;
345 wxFontEncoding
wxFont::GetEncoding() const
347 return M_FONTDATA
->m_encoding
;
350 // ----------------------------------------------------------------------------
351 // real implementation
352 // ----------------------------------------------------------------------------
354 // Find an existing, or create a new, XFontStruct
355 // based on this wxFont and the given scale. Append the
356 // font to list in the private data for future reference.
357 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
360 return (wxXFont
*)NULL
;
362 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
363 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
365 // search existing fonts first
366 wxNode
* node
= M_FONTDATA
->m_fonts
.First();
369 wxXFont
* f
= (wxXFont
*) node
->Data();
370 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
375 // not found, create a new one
376 XFontStruct
*font
= (XFontStruct
*)
377 wxLoadQueryNearestFont(pointSize
,
378 M_FONTDATA
->m_family
,
380 M_FONTDATA
->m_weight
,
381 M_FONTDATA
->m_underlined
,
383 M_FONTDATA
->m_encoding
);
387 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
389 return (wxXFont
*) NULL
;
392 wxXFont
* f
= new wxXFont
;
393 f
->m_fontStruct
= (WXFontStructPtr
)font
;
394 f
->m_display
= ( display
? display
: wxGetDisplay() );
395 f
->m_scale
= intScale
;
396 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
397 M_FONTDATA
->m_fonts
.Append(f
);
402 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
404 wxXFont
* f
= GetInternalFont(scale
, display
);
406 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
409 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
411 wxXFont
* f
= GetInternalFont(scale
, display
);
413 return (f
? f
->m_fontList
: (WXFontList
) 0);