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 #if wxUSE_PORTABLE_FONTS_IN_MSW
45 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
48 // ----------------------------------------------------------------------------
49 // wxFontRefData - the internal description of the font
50 // ----------------------------------------------------------------------------
52 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
54 friend class WXDLLEXPORT wxFont
;
59 Init(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
60 "", wxFONTENCODING_DEFAULT
);
63 wxFontRefData(const wxFontRefData
& data
)
65 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
66 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
68 m_fontId
= data
.m_fontId
;
71 wxFontRefData(int size
,
76 const wxString
& faceName
,
77 wxFontEncoding encoding
)
79 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
82 virtual ~wxFontRefData();
85 // common part of all ctors
91 const wxString
& faceName
,
92 wxFontEncoding encoding
);
94 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
95 // DELETED by destructor
100 // font characterstics
107 wxFontEncoding m_encoding
;
109 // Windows font handle
113 // ============================================================================
115 // ============================================================================
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 void wxFontRefData::Init(int pointSize
,
126 const wxString
& faceName
,
127 wxFontEncoding encoding
)
130 m_pointSize
= pointSize
;
134 m_underlined
= underlined
;
135 m_faceName
= faceName
;
136 m_encoding
= encoding
;
144 wxFontRefData::~wxFontRefData()
148 if ( !::DeleteObject((HFONT
) m_hFont
) )
150 wxLogLastError("DeleteObject(font)");
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
162 wxTheFontList
->Append(this);
165 /* Constructor for a font. Note that the real construction is done
166 * in wxDC::SetFont, when information is available about scaling etc.
168 bool wxFont::Create(int pointSize
,
173 const wxString
& faceName
,
174 wxFontEncoding encoding
)
177 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
178 underlined
, faceName
, encoding
);
188 wxTheFontList
->DeleteObject(this);
191 // ----------------------------------------------------------------------------
192 // real implementation
193 // ----------------------------------------------------------------------------
195 bool wxFont::RealizeResource()
197 if ( GetResourceHandle() )
199 // VZ: the old code returned FALSE in this case, but it doesn't seem
200 // to make sense because the font _was_ created
205 wxFillLogFont(&lf
, this);
206 M_FONTDATA
->m_hFont
= (WXHFONT
)::CreateFontIndirect(&lf
);
207 if ( !M_FONTDATA
->m_hFont
)
209 wxLogLastError("CreateFont");
217 bool wxFont::FreeResource(bool force
)
219 if ( GetResourceHandle() )
221 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
223 wxLogLastError("DeleteObject(font)");
226 M_FONTDATA
->m_hFont
= 0;
233 WXHANDLE
wxFont::GetResourceHandle()
238 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
241 bool wxFont::IsFree() const
243 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
246 void wxFont::Unshare()
248 // Don't change shared data
251 m_refData
= new wxFontRefData();
255 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
261 // ----------------------------------------------------------------------------
262 // change font attribute: we recreate font when doing it
263 // ----------------------------------------------------------------------------
265 void wxFont::SetPointSize(int pointSize
)
269 M_FONTDATA
->m_pointSize
= pointSize
;
274 void wxFont::SetFamily(int family
)
278 M_FONTDATA
->m_family
= family
;
283 void wxFont::SetStyle(int style
)
287 M_FONTDATA
->m_style
= style
;
292 void wxFont::SetWeight(int weight
)
296 M_FONTDATA
->m_weight
= weight
;
301 void wxFont::SetFaceName(const wxString
& faceName
)
305 M_FONTDATA
->m_faceName
= faceName
;
310 void wxFont::SetUnderlined(bool underlined
)
314 M_FONTDATA
->m_underlined
= underlined
;
319 void wxFont::SetEncoding(wxFontEncoding encoding
)
323 M_FONTDATA
->m_encoding
= encoding
;
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 int wxFont::GetPointSize() const
334 return M_FONTDATA
->m_pointSize
;
337 int wxFont::GetFamily() const
339 return M_FONTDATA
->m_family
;
342 int wxFont::GetFontId() const
344 return M_FONTDATA
->m_fontId
;
347 int wxFont::GetStyle() const
349 return M_FONTDATA
->m_style
;
352 int wxFont::GetWeight() const
354 return M_FONTDATA
->m_weight
;
357 bool wxFont::GetUnderlined() const
359 return M_FONTDATA
->m_underlined
;
362 wxString
wxFont::GetFaceName() const
366 str
= M_FONTDATA
->m_faceName
;
370 wxFontEncoding
wxFont::GetEncoding() const
372 return M_FONTDATA
->m_encoding
;