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 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // the default font size in points
49 static const int wxDEFAULT_FONT_SIZE
= 12;
51 // ----------------------------------------------------------------------------
52 // wxFontRefData - the internal description of the font
53 // ----------------------------------------------------------------------------
55 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
57 friend class WXDLLEXPORT wxFont
;
62 Init(wxDEFAULT_FONT_SIZE
, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
63 "", wxFONTENCODING_DEFAULT
);
66 wxFontRefData(const wxFontRefData
& data
)
68 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
69 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
71 m_fontId
= data
.m_fontId
;
74 wxFontRefData(int size
,
79 const wxString
& faceName
,
80 wxFontEncoding encoding
)
82 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
85 virtual ~wxFontRefData();
88 // common part of all ctors
94 const wxString
& faceName
,
95 wxFontEncoding encoding
);
97 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
98 // DELETED by destructor
103 // font characterstics
110 wxFontEncoding m_encoding
;
112 // Windows font handle
116 // ============================================================================
118 // ============================================================================
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 void wxFontRefData::Init(int pointSize
,
129 const wxString
& faceName
,
130 wxFontEncoding encoding
)
133 m_pointSize
= pointSize
;
137 m_underlined
= underlined
;
138 m_faceName
= faceName
;
139 m_encoding
= encoding
;
147 wxFontRefData::~wxFontRefData()
151 if ( !::DeleteObject((HFONT
) m_hFont
) )
153 wxLogLastError(wxT("DeleteObject(font)"));
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
165 wxTheFontList
->Append(this);
168 /* Constructor for a font. Note that the real construction is done
169 * in wxDC::SetFont, when information is available about scaling etc.
171 bool wxFont::Create(int pointSize
,
176 const wxString
& faceName
,
177 wxFontEncoding encoding
)
181 // wxDEFAULT is a valid value for the font size too so we must treat it
182 // specially here (otherwise the size would be 70 == wxDEFAULT value)
183 if ( pointSize
== wxDEFAULT
)
184 pointSize
= wxDEFAULT_FONT_SIZE
;
186 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
187 underlined
, faceName
, encoding
);
197 wxTheFontList
->DeleteObject(this);
200 // ----------------------------------------------------------------------------
201 // real implementation
202 // ----------------------------------------------------------------------------
204 bool wxFont::RealizeResource()
206 if ( GetResourceHandle() )
208 // VZ: the old code returned FALSE in this case, but it doesn't seem
209 // to make sense because the font _was_ created
214 wxFillLogFont(&lf
, this);
215 M_FONTDATA
->m_hFont
= (WXHFONT
)::CreateFontIndirect(&lf
);
216 M_FONTDATA
->m_faceName
= lf
.lfFaceName
;
217 if ( !M_FONTDATA
->m_hFont
)
219 wxLogLastError(wxT("CreateFont"));
227 bool wxFont::FreeResource(bool force
)
229 if ( GetResourceHandle() )
231 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
233 wxLogLastError(wxT("DeleteObject(font)"));
236 M_FONTDATA
->m_hFont
= 0;
243 WXHANDLE
wxFont::GetResourceHandle()
248 WXHFONT
wxFont::GetHFONT() const
253 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
256 bool wxFont::IsFree() const
258 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
261 void wxFont::Unshare()
263 // Don't change shared data
266 m_refData
= new wxFontRefData();
270 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
276 // ----------------------------------------------------------------------------
277 // change font attribute: we recreate font when doing it
278 // ----------------------------------------------------------------------------
280 void wxFont::SetPointSize(int pointSize
)
284 M_FONTDATA
->m_pointSize
= pointSize
;
289 void wxFont::SetFamily(int family
)
293 M_FONTDATA
->m_family
= family
;
298 void wxFont::SetStyle(int style
)
302 M_FONTDATA
->m_style
= style
;
307 void wxFont::SetWeight(int weight
)
311 M_FONTDATA
->m_weight
= weight
;
316 void wxFont::SetFaceName(const wxString
& faceName
)
320 M_FONTDATA
->m_faceName
= faceName
;
325 void wxFont::SetUnderlined(bool underlined
)
329 M_FONTDATA
->m_underlined
= underlined
;
334 void wxFont::SetEncoding(wxFontEncoding encoding
)
338 M_FONTDATA
->m_encoding
= encoding
;
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
347 int wxFont::GetPointSize() const
349 return M_FONTDATA
->m_pointSize
;
352 int wxFont::GetFamily() const
354 return M_FONTDATA
->m_family
;
357 int wxFont::GetFontId() const
359 return M_FONTDATA
->m_fontId
;
362 int wxFont::GetStyle() const
364 return M_FONTDATA
->m_style
;
367 int wxFont::GetWeight() const
369 return M_FONTDATA
->m_weight
;
372 bool wxFont::GetUnderlined() const
374 return M_FONTDATA
->m_underlined
;
377 wxString
wxFont::GetFaceName() const
381 str
= M_FONTDATA
->m_faceName
;
385 wxFontEncoding
wxFont::GetEncoding() const
387 return M_FONTDATA
->m_encoding
;