1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "font.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
40 #include "wx/fontutil.h"
42 #include "wx/msw/private.h"
44 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // the default font size in points
51 static const int wxDEFAULT_FONT_SIZE
= 12;
53 // ----------------------------------------------------------------------------
54 // wxFontRefData - the internal description of the font
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
59 friend class WXDLLEXPORT wxFont
;
64 Init(wxDEFAULT_FONT_SIZE
, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
65 "", wxFONTENCODING_DEFAULT
);
68 wxFontRefData(const wxFontRefData
& data
)
70 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
71 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
73 m_fontId
= data
.m_fontId
;
76 wxFontRefData(int size
,
81 const wxString
& faceName
,
82 wxFontEncoding encoding
)
84 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
87 virtual ~wxFontRefData();
90 // common part of all ctors
96 const wxString
& faceName
,
97 wxFontEncoding encoding
);
99 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
100 // DELETED by destructor
105 // font characterstics
112 wxFontEncoding m_encoding
;
114 // Windows font handle
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 void wxFontRefData::Init(int pointSize
,
131 const wxString
& faceName
,
132 wxFontEncoding encoding
)
135 m_pointSize
= pointSize
;
139 m_underlined
= underlined
;
140 m_faceName
= faceName
;
141 m_encoding
= encoding
;
149 wxFontRefData::~wxFontRefData()
153 if ( !::DeleteObject((HFONT
) m_hFont
) )
155 wxLogLastError(wxT("DeleteObject(font)"));
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
167 wxTheFontList
->Append(this);
170 bool wxFont::Create(const wxNativeFontInfo
& info
)
172 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
173 info
.underlined
, info
.faceName
, info
.encoding
);
176 wxFont::wxFont(const wxString
& fontdesc
)
178 wxNativeFontInfo info
;
179 if ( info
.FromString(fontdesc
) )
183 /* Constructor for a font. Note that the real construction is done
184 * in wxDC::SetFont, when information is available about scaling etc.
186 bool wxFont::Create(int pointSize
,
191 const wxString
& faceName
,
192 wxFontEncoding encoding
)
196 // wxDEFAULT is a valid value for the font size too so we must treat it
197 // specially here (otherwise the size would be 70 == wxDEFAULT value)
198 if ( pointSize
== wxDEFAULT
)
199 pointSize
= wxDEFAULT_FONT_SIZE
;
201 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
202 underlined
, faceName
, encoding
);
212 wxTheFontList
->DeleteObject(this);
215 // ----------------------------------------------------------------------------
216 // real implementation
217 // ----------------------------------------------------------------------------
219 bool wxFont::RealizeResource()
221 if ( GetResourceHandle() )
223 // VZ: the old code returned FALSE in this case, but it doesn't seem
224 // to make sense because the font _was_ created
229 wxFillLogFont(&lf
, this);
230 M_FONTDATA
->m_hFont
= (WXHFONT
)::CreateFontIndirect(&lf
);
231 M_FONTDATA
->m_faceName
= lf
.lfFaceName
;
232 if ( !M_FONTDATA
->m_hFont
)
234 wxLogLastError(wxT("CreateFont"));
242 bool wxFont::FreeResource(bool force
)
244 if ( GetResourceHandle() )
246 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
248 wxLogLastError(wxT("DeleteObject(font)"));
251 M_FONTDATA
->m_hFont
= 0;
258 WXHANDLE
wxFont::GetResourceHandle()
263 WXHFONT
wxFont::GetHFONT() const
268 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
271 bool wxFont::IsFree() const
273 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
276 void wxFont::Unshare()
278 // Don't change shared data
281 m_refData
= new wxFontRefData();
285 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
291 // ----------------------------------------------------------------------------
292 // change font attribute: we recreate font when doing it
293 // ----------------------------------------------------------------------------
295 void wxFont::SetPointSize(int pointSize
)
299 M_FONTDATA
->m_pointSize
= pointSize
;
304 void wxFont::SetFamily(int family
)
308 M_FONTDATA
->m_family
= family
;
313 void wxFont::SetStyle(int style
)
317 M_FONTDATA
->m_style
= style
;
322 void wxFont::SetWeight(int weight
)
326 M_FONTDATA
->m_weight
= weight
;
331 void wxFont::SetFaceName(const wxString
& faceName
)
335 M_FONTDATA
->m_faceName
= faceName
;
340 void wxFont::SetUnderlined(bool underlined
)
344 M_FONTDATA
->m_underlined
= underlined
;
349 void wxFont::SetEncoding(wxFontEncoding encoding
)
353 M_FONTDATA
->m_encoding
= encoding
;
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 int wxFont::GetPointSize() const
364 return M_FONTDATA
->m_pointSize
;
367 int wxFont::GetFamily() const
369 return M_FONTDATA
->m_family
;
372 int wxFont::GetFontId() const
374 return M_FONTDATA
->m_fontId
;
377 int wxFont::GetStyle() const
379 return M_FONTDATA
->m_style
;
382 int wxFont::GetWeight() const
384 return M_FONTDATA
->m_weight
;
387 bool wxFont::GetUnderlined() const
389 return M_FONTDATA
->m_underlined
;
392 wxString
wxFont::GetFaceName() const
396 str
= M_FONTDATA
->m_faceName
;
400 wxFontEncoding
wxFont::GetEncoding() const
402 return M_FONTDATA
->m_encoding
;