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 #if !USE_SHARED_LIBRARIES
40 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // For every wxFont, there must be a font for each display and scale requested.
48 // So these objects are stored in wxFontRefData::m_fonts
49 class wxXFont
: public wxObject
55 WXFontStructPtr m_fontStruct
; // XFontStruct
56 WXFontList m_fontList
; // Motif XmFontList
57 WXDisplay
* m_display
; // XDisplay
58 int m_scale
; // Scale * 100
61 class wxFontRefData
: public wxGDIRefData
66 wxFontRefData(int size
= wxDEFAULT
,
67 int family
= wxDEFAULT
,
68 int style
= wxDEFAULT
,
69 int weight
= wxDEFAULT
,
70 bool underlined
= FALSE
,
71 const wxString
& faceName
= wxEmptyString
,
72 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
74 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
77 wxFontRefData(const wxFontRefData
& data
)
79 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
80 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
86 // common part of all ctors
92 const wxString
& faceName
,
93 wxFontEncoding encoding
);
102 wxFontEncoding m_encoding
;
104 // A list of wxXFonts
108 // ============================================================================
110 // ============================================================================
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
118 m_fontStruct
= (WXFontStructPtr
) 0;
119 m_fontList
= (WXFontList
) 0;
120 m_display
= (WXDisplay
*) 0;
126 XmFontList fontList
= (XmFontList
) m_fontList
;
128 XmFontListFree (fontList
);
130 // TODO: why does freeing the font produce a segv???
131 // Note that XFreeFont wasn't called in wxWin 1.68 either.
132 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
133 // XFreeFont((Display*) m_display, fontStruct);
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 void wxFontRefData::Init(int pointSize
,
145 const wxString
& faceName
,
146 wxFontEncoding encoding
)
148 if (family
== wxDEFAULT
)
153 m_faceName
= faceName
;
155 if (style
== wxDEFAULT
)
160 if (weight
== wxDEFAULT
)
165 if (pointSize
== wxDEFAULT
)
168 m_pointSize
= pointSize
;
170 m_underlined
= underlined
;
171 m_encoding
= encoding
;
174 wxFontRefData::~wxFontRefData()
176 wxNode
* node
= m_fonts
.First();
179 wxXFont
* f
= (wxXFont
*) node
->Data();
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
193 wxTheFontList
->Append(this);
196 bool wxFont::Create(int pointSize
,
201 const wxString
& faceName
,
202 wxFontEncoding encoding
)
205 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
206 underlined
, faceName
, encoding
);
216 wxTheFontList
->DeleteObject(this);
219 // ----------------------------------------------------------------------------
220 // change the font attributes
221 // ----------------------------------------------------------------------------
223 void wxFont::Unshare()
225 // Don't change shared data
228 m_refData
= new wxFontRefData();
232 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
238 void wxFont::SetPointSize(int pointSize
)
242 M_FONTDATA
->m_pointSize
= pointSize
;
247 void wxFont::SetFamily(int family
)
251 M_FONTDATA
->m_family
= family
;
256 void wxFont::SetStyle(int style
)
260 M_FONTDATA
->m_style
= style
;
265 void wxFont::SetWeight(int weight
)
269 M_FONTDATA
->m_weight
= weight
;
274 void wxFont::SetFaceName(const wxString
& faceName
)
278 M_FONTDATA
->m_faceName
= faceName
;
283 void wxFont::SetUnderlined(bool underlined
)
287 M_FONTDATA
->m_underlined
= underlined
;
292 void wxFont::SetEncoding(wxFontEncoding encoding
)
296 M_FONTDATA
->m_encoding
= encoding
;
301 // ----------------------------------------------------------------------------
302 // query font attributes
303 // ----------------------------------------------------------------------------
305 int wxFont::GetPointSize() const
307 return M_FONTDATA
->m_pointSize
;
310 int wxFont::GetFamily() const
312 return M_FONTDATA
->m_family
;
315 int wxFont::GetStyle() const
317 return M_FONTDATA
->m_style
;
320 int wxFont::GetWeight() const
322 return M_FONTDATA
->m_weight
;
325 bool wxFont::GetUnderlined() const
327 return M_FONTDATA
->m_underlined
;
330 wxString
wxFont::GetFaceName() const
334 str
= M_FONTDATA
->m_faceName
;
338 wxFontEncoding
wxFont::GetEncoding() const
340 return M_FONTDATA
->m_encoding
;
343 // ----------------------------------------------------------------------------
344 // real implementation
345 // ----------------------------------------------------------------------------
347 // Find an existing, or create a new, XFontStruct
348 // based on this wxFont and the given scale. Append the
349 // font to list in the private data for future reference.
350 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
353 return (wxXFont
*)NULL
;
355 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
356 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
358 // search existing fonts first
359 wxNode
* node
= M_FONTDATA
->m_fonts
.First();
362 wxXFont
* f
= (wxXFont
*) node
->Data();
363 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
368 // not found, create a new one
369 XFontStruct
*font
= (XFontStruct
*)
370 wxLoadQueryNearestFont(pointSize
,
371 M_FONTDATA
->m_family
,
373 M_FONTDATA
->m_weight
,
374 M_FONTDATA
->m_underlined
,
376 M_FONTDATA
->m_encoding
);
380 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
382 return (wxXFont
*) NULL
;
385 wxXFont
* f
= new wxXFont
;
386 f
->m_fontStruct
= (WXFontStructPtr
)font
;
387 f
->m_display
= ( display
? display
: wxGetDisplay() );
388 f
->m_scale
= intScale
;
389 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
390 M_FONTDATA
->m_fonts
.Append(f
);
395 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
397 wxXFont
* f
= GetInternalFont(scale
, display
);
399 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
402 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
404 wxXFont
* f
= GetInternalFont(scale
, display
);
406 return (f
? f
->m_fontList
: (WXFontList
) 0);