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 M_FONTDATA
->m_faceName
= lf
.lfFaceName
;
208 if ( !M_FONTDATA
->m_hFont
)
210 wxLogLastError("CreateFont");
218 bool wxFont::FreeResource(bool force
)
220 if ( GetResourceHandle() )
222 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
224 wxLogLastError("DeleteObject(font)");
227 M_FONTDATA
->m_hFont
= 0;
234 WXHANDLE
wxFont::GetResourceHandle()
239 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
242 bool wxFont::IsFree() const
244 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
247 void wxFont::Unshare()
249 // Don't change shared data
252 m_refData
= new wxFontRefData();
256 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
262 // ----------------------------------------------------------------------------
263 // change font attribute: we recreate font when doing it
264 // ----------------------------------------------------------------------------
266 void wxFont::SetPointSize(int pointSize
)
270 M_FONTDATA
->m_pointSize
= pointSize
;
275 void wxFont::SetFamily(int family
)
279 M_FONTDATA
->m_family
= family
;
284 void wxFont::SetStyle(int style
)
288 M_FONTDATA
->m_style
= style
;
293 void wxFont::SetWeight(int weight
)
297 M_FONTDATA
->m_weight
= weight
;
302 void wxFont::SetFaceName(const wxString
& faceName
)
306 M_FONTDATA
->m_faceName
= faceName
;
311 void wxFont::SetUnderlined(bool underlined
)
315 M_FONTDATA
->m_underlined
= underlined
;
320 void wxFont::SetEncoding(wxFontEncoding encoding
)
324 M_FONTDATA
->m_encoding
= encoding
;
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 int wxFont::GetPointSize() const
335 return M_FONTDATA
->m_pointSize
;
338 int wxFont::GetFamily() const
340 return M_FONTDATA
->m_family
;
343 int wxFont::GetFontId() const
345 return M_FONTDATA
->m_fontId
;
348 int wxFont::GetStyle() const
350 return M_FONTDATA
->m_style
;
353 int wxFont::GetWeight() const
355 return M_FONTDATA
->m_weight
;
358 bool wxFont::GetUnderlined() const
360 return M_FONTDATA
->m_underlined
;
363 wxString
wxFont::GetFaceName() const
367 str
= M_FONTDATA
->m_faceName
;
371 wxFontEncoding
wxFont::GetEncoding() const
373 return M_FONTDATA
->m_encoding
;