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/msw/private.h"
42 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
44 // ----------------------------------------------------------------------------
45 // wxFontRefData - the internal description of the font
46 // ----------------------------------------------------------------------------
48 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
50 friend class WXDLLEXPORT wxFont
;
55 Init(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
56 "", wxFONTENCODING_DEFAULT
);
59 wxFontRefData(const wxFontRefData
& data
)
61 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
62 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
64 m_fontId
= data
.m_fontId
;
67 wxFontRefData(int size
,
72 const wxString
& faceName
,
73 wxFontEncoding encoding
)
75 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
78 virtual ~wxFontRefData();
81 // common part of all ctors
87 const wxString
& faceName
,
88 wxFontEncoding encoding
);
90 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
91 // DELETED by destructor
96 // font characterstics
103 wxFontEncoding m_encoding
;
105 // Windows font handle
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 void wxFontRefData::Init(int pointSize
,
122 const wxString
& faceName
,
123 wxFontEncoding encoding
)
126 m_pointSize
= pointSize
;
130 m_underlined
= underlined
;
131 m_faceName
= faceName
;
132 m_encoding
= encoding
;
140 wxFontRefData::~wxFontRefData()
144 if ( !::DeleteObject((HFONT
) m_hFont
) )
146 wxLogLastError(wxT("DeleteObject(font)"));
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
158 wxTheFontList
->Append(this);
161 /* Constructor for a font. Note that the real construction is done
162 * in wxDC::SetFont, when information is available about scaling etc.
164 bool wxFont::Create(int pointSize
,
169 const wxString
& faceName
,
170 wxFontEncoding encoding
)
173 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
174 underlined
, faceName
, encoding
);
184 wxTheFontList
->DeleteObject(this);
187 // ----------------------------------------------------------------------------
188 // real implementation
189 // ----------------------------------------------------------------------------
191 bool wxFont::RealizeResource()
193 if ( GetResourceHandle() )
195 // VZ: the old code returned FALSE in this case, but it doesn't seem
196 // to make sense because the font _was_ created
201 wxFillLogFont(&lf
, this);
202 M_FONTDATA
->m_hFont
= (WXHFONT
)::CreateFontIndirect(&lf
);
203 M_FONTDATA
->m_faceName
= lf
.lfFaceName
;
204 if ( !M_FONTDATA
->m_hFont
)
206 wxLogLastError(wxT("CreateFont"));
214 bool wxFont::FreeResource(bool force
)
216 if ( GetResourceHandle() )
218 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
220 wxLogLastError(wxT("DeleteObject(font)"));
223 M_FONTDATA
->m_hFont
= 0;
230 WXHANDLE
wxFont::GetResourceHandle()
235 WXHFONT
wxFont::GetHFONT() const
240 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
243 bool wxFont::IsFree() const
245 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
248 void wxFont::Unshare()
250 // Don't change shared data
253 m_refData
= new wxFontRefData();
257 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
263 // ----------------------------------------------------------------------------
264 // change font attribute: we recreate font when doing it
265 // ----------------------------------------------------------------------------
267 void wxFont::SetPointSize(int pointSize
)
271 M_FONTDATA
->m_pointSize
= pointSize
;
276 void wxFont::SetFamily(int family
)
280 M_FONTDATA
->m_family
= family
;
285 void wxFont::SetStyle(int style
)
289 M_FONTDATA
->m_style
= style
;
294 void wxFont::SetWeight(int weight
)
298 M_FONTDATA
->m_weight
= weight
;
303 void wxFont::SetFaceName(const wxString
& faceName
)
307 M_FONTDATA
->m_faceName
= faceName
;
312 void wxFont::SetUnderlined(bool underlined
)
316 M_FONTDATA
->m_underlined
= underlined
;
321 void wxFont::SetEncoding(wxFontEncoding encoding
)
325 M_FONTDATA
->m_encoding
= encoding
;
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 int wxFont::GetPointSize() const
336 return M_FONTDATA
->m_pointSize
;
339 int wxFont::GetFamily() const
341 return M_FONTDATA
->m_family
;
344 int wxFont::GetFontId() const
346 return M_FONTDATA
->m_fontId
;
349 int wxFont::GetStyle() const
351 return M_FONTDATA
->m_style
;
354 int wxFont::GetWeight() const
356 return M_FONTDATA
->m_weight
;
359 bool wxFont::GetUnderlined() const
361 return M_FONTDATA
->m_underlined
;
364 wxString
wxFont::GetFaceName() const
368 str
= M_FONTDATA
->m_faceName
;
372 wxFontEncoding
wxFont::GetEncoding() const
374 return M_FONTDATA
->m_encoding
;