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 #include "wx/string.h"
27 #include "wx/gdicmn.h"
32 #if !USE_SHARED_LIBRARIES
33 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // For every wxFont, there must be a font for each display and scale requested.
41 // So these objects are stored in wxFontRefData::m_fonts
42 class wxXFont
: public wxObject
48 WXFontStructPtr m_fontStruct
; // XFontStruct
49 WXFontList m_fontList
; // Motif XmFontList
50 WXDisplay
* m_display
; // XDisplay
51 int m_scale
; // Scale * 100
54 class wxFontRefData
: public wxGDIRefData
59 wxFontRefData(int size
= wxDEFAULT
,
60 int family
= wxDEFAULT
,
61 int style
= wxDEFAULT
,
62 int weight
= wxDEFAULT
,
63 bool underlined
= FALSE
,
64 const wxString
& faceName
= wxEmptyString
,
65 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
67 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
70 wxFontRefData(const wxFontRefData
& data
)
72 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
73 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
79 // common part of all ctors
85 const wxString
& faceName
,
86 wxFontEncoding encoding
);
95 wxFontEncoding m_encoding
;
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
111 m_fontStruct
= (WXFontStructPtr
) 0;
112 m_fontList
= (WXFontList
) 0;
113 m_display
= (WXDisplay
*) 0;
119 XmFontList fontList
= (XmFontList
) m_fontList
;
121 XmFontListFree (fontList
);
123 // TODO: why does freeing the font produce a segv???
124 // Note that XFreeFont wasn't called in wxWin 1.68 either.
125 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
126 // XFreeFont((Display*) m_display, fontStruct);
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 void wxFontRefData::Init(int pointSize
,
138 const wxString
& faceName
,
139 wxFontEncoding encoding
)
141 if (family
== wxDEFAULT
)
146 m_faceName
= faceName
;
148 if (style
== wxDEFAULT
)
153 if (weight
== wxDEFAULT
)
158 if (pointSize
== wxDEFAULT
)
161 m_pointSize
= pointSize
;
163 m_underlined
= underlined
;
164 m_encoding
= encoding
;
167 wxFontRefData::~wxFontRefData()
169 wxNode
* node
= m_fonts
.First();
172 wxXFont
* f
= (wxXFont
*) node
->Data();
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
186 wxTheFontList
->Append(this);
189 bool wxFont::Create(int pointSize
,
194 const wxString
& faceName
,
195 wxFontEncoding encoding
)
198 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
199 underlined
, faceName
, encoding
);
209 wxTheFontList
->DeleteObject(this);
212 // ----------------------------------------------------------------------------
213 // change the font attributes
214 // ----------------------------------------------------------------------------
216 void wxFont::Unshare()
218 // Don't change shared data
221 m_refData
= new wxFontRefData();
225 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
231 void wxFont::SetPointSize(int pointSize
)
235 M_FONTDATA
->m_pointSize
= pointSize
;
240 void wxFont::SetFamily(int family
)
244 M_FONTDATA
->m_family
= family
;
249 void wxFont::SetStyle(int style
)
253 M_FONTDATA
->m_style
= style
;
258 void wxFont::SetWeight(int weight
)
262 M_FONTDATA
->m_weight
= weight
;
267 void wxFont::SetFaceName(const wxString
& faceName
)
271 M_FONTDATA
->m_faceName
= faceName
;
276 void wxFont::SetUnderlined(bool underlined
)
280 M_FONTDATA
->m_underlined
= underlined
;
285 void wxFont::SetEncoding(wxFontEncoding encoding
)
289 M_FONTDATA
->m_encoding
= encoding
;
294 // ----------------------------------------------------------------------------
295 // query font attributes
296 // ----------------------------------------------------------------------------
298 int wxFont::GetPointSize() const
300 return M_FONTDATA
->m_pointSize
;
303 int wxFont::GetFamily() const
305 return M_FONTDATA
->m_family
;
308 int wxFont::GetStyle() const
310 return M_FONTDATA
->m_style
;
313 int wxFont::GetWeight() const
315 return M_FONTDATA
->m_weight
;
318 bool wxFont::GetUnderlined() const
320 return M_FONTDATA
->m_underlined
;
323 wxString
wxFont::GetFaceName() const
327 str
= M_FONTDATA
->m_faceName
;
331 wxFontEncoding
wxFont::GetEncoding() const
333 return M_FONTDATA
->m_encoding
;
336 // ----------------------------------------------------------------------------
337 // real implementation
338 // ----------------------------------------------------------------------------
340 // Find an existing, or create a new, XFontStruct
341 // based on this wxFont and the given scale. Append the
342 // font to list in the private data for future reference.
343 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
346 return (wxXFont
*)NULL
;
348 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
349 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
351 // search existing fonts first
352 wxNode
* node
= M_FONTDATA
->m_fonts
.First();
355 wxXFont
* f
= (wxXFont
*) node
->Data();
356 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
361 // not found, create a new one
362 XFontStruct
*font
= wxLoadQueryNearestFont(pointSize
,
363 M_FONTDATA
->m_family
,
365 M_FONTDATA
->m_weight
,
366 M_FONTDATA
->m_underlined
,
368 M_FONTDATA
->m_encoding
);
372 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
374 return (wxXFont
*) NULL
;
377 wxXFont
* f
= new wxXFont
;
378 f
->m_fontStruct
= (WXFontStructPtr
)font
;
379 f
->m_display
= ( display
? display
: wxGetDisplay() );
380 f
->m_scale
= intScale
;
381 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
382 M_FONTDATA
->m_fonts
.Append(f
);
387 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
389 wxXFont
* f
= GetInternalFont(scale
, display
);
391 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
394 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
396 wxXFont
* f
= GetInternalFont(scale
, display
);
398 return (f
? f
->m_fontList
: (WXFontList
) 0);