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 #if !USE_SHARED_LIBRARIES
43 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
45 #if wxUSE_PORTABLE_FONTS_IN_MSW
46 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
50 // ----------------------------------------------------------------------------
51 // wxFontRefData - the internal description of the font
52 // ----------------------------------------------------------------------------
54 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
56 friend class WXDLLEXPORT wxFont
;
61 Init(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
62 "", wxFONTENCODING_DEFAULT
);
65 wxFontRefData(const wxFontRefData
& data
)
67 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
68 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
70 m_fontId
= data
.m_fontId
;
73 wxFontRefData(int size
,
78 const wxString
& faceName
,
79 wxFontEncoding encoding
)
81 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
84 virtual ~wxFontRefData();
87 // common part of all ctors
93 const wxString
& faceName
,
94 wxFontEncoding encoding
);
96 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
97 // DELETED by destructor
102 // font characterstics
109 wxFontEncoding m_encoding
;
111 // Windows font handle
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 void wxFontRefData::Init(int pointSize
,
128 const wxString
& faceName
,
129 wxFontEncoding encoding
)
132 m_pointSize
= pointSize
;
136 m_underlined
= underlined
;
137 m_faceName
= faceName
;
138 m_encoding
= encoding
;
146 wxFontRefData::~wxFontRefData()
150 if ( !::DeleteObject((HFONT
) m_hFont
) )
152 wxLogLastError("DeleteObject(font)");
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
164 wxTheFontList
->Append(this);
167 /* Constructor for a font. Note that the real construction is done
168 * in wxDC::SetFont, when information is available about scaling etc.
170 bool wxFont::Create(int pointSize
,
175 const wxString
& faceName
,
176 wxFontEncoding encoding
)
179 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
180 underlined
, faceName
, encoding
);
190 wxTheFontList
->DeleteObject(this);
193 // ----------------------------------------------------------------------------
194 // real implementation
195 // ----------------------------------------------------------------------------
197 bool wxFont::RealizeResource()
199 if ( GetResourceHandle() )
201 // VZ: the old code returned FALSE in this case, but it doesn't seem
202 // to make sense because the font _was_ created
207 wxFillLogFont(&lf
, this);
208 M_FONTDATA
->m_hFont
= (WXHFONT
)::CreateFontIndirect(&lf
);
209 if ( !M_FONTDATA
->m_hFont
)
211 wxLogLastError("CreateFont");
219 bool wxFont::FreeResource(bool force
)
221 if ( GetResourceHandle() )
223 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
225 wxLogLastError("DeleteObject(font)");
228 M_FONTDATA
->m_hFont
= 0;
235 WXHANDLE
wxFont::GetResourceHandle()
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
;