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"
41 #include "wx/msw/private.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // the default font size in points
50 static const int wxDEFAULT_FONT_SIZE
= 12;
52 // ----------------------------------------------------------------------------
53 // wxFontRefData - the internal description of the font
54 // ----------------------------------------------------------------------------
56 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
58 friend class WXDLLEXPORT wxFont
;
63 Init(wxDEFAULT_FONT_SIZE
, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
64 "", wxFONTENCODING_DEFAULT
);
67 wxFontRefData(const wxFontRefData
& data
)
69 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
70 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
72 m_fontId
= data
.m_fontId
;
75 wxFontRefData(int size
,
80 const wxString
& faceName
,
81 wxFontEncoding encoding
)
83 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
86 virtual ~wxFontRefData();
89 // common part of all ctors
95 const wxString
& faceName
,
96 wxFontEncoding encoding
);
98 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
99 // DELETED by destructor
104 // font characterstics
111 wxFontEncoding m_encoding
;
113 // Windows font handle
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 void wxFontRefData::Init(int pointSize
,
130 const wxString
& faceName
,
131 wxFontEncoding encoding
)
134 m_pointSize
= pointSize
;
138 m_underlined
= underlined
;
139 m_faceName
= faceName
;
140 m_encoding
= encoding
;
148 wxFontRefData::~wxFontRefData()
152 if ( !::DeleteObject((HFONT
) m_hFont
) )
154 wxLogLastError(wxT("DeleteObject(font)"));
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
166 wxTheFontList
->Append(this);
169 /* Constructor for a font. Note that the real construction is done
170 * in wxDC::SetFont, when information is available about scaling etc.
172 bool wxFont::Create(int pointSize
,
177 const wxString
& faceName
,
178 wxFontEncoding encoding
)
182 // wxDEFAULT is a valid value for the font size too so we must treat it
183 // specially here (otherwise the size would be 70 == wxDEFAULT value)
184 if ( pointSize
== wxDEFAULT
)
185 pointSize
= wxDEFAULT_FONT_SIZE
;
187 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
188 underlined
, faceName
, encoding
);
198 wxTheFontList
->DeleteObject(this);
201 // ----------------------------------------------------------------------------
202 // real implementation
203 // ----------------------------------------------------------------------------
205 bool wxFont::RealizeResource()
207 if ( GetResourceHandle() )
209 // VZ: the old code returned FALSE in this case, but it doesn't seem
210 // to make sense because the font _was_ created
215 wxFillLogFont(&lf
, this);
216 M_FONTDATA
->m_hFont
= (WXHFONT
)::CreateFontIndirect(&lf
);
217 M_FONTDATA
->m_faceName
= lf
.lfFaceName
;
218 if ( !M_FONTDATA
->m_hFont
)
220 wxLogLastError(wxT("CreateFont"));
228 bool wxFont::FreeResource(bool force
)
230 if ( GetResourceHandle() )
232 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
234 wxLogLastError(wxT("DeleteObject(font)"));
237 M_FONTDATA
->m_hFont
= 0;
244 WXHANDLE
wxFont::GetResourceHandle()
249 WXHFONT
wxFont::GetHFONT() const
254 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
257 bool wxFont::IsFree() const
259 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
262 void wxFont::Unshare()
264 // Don't change shared data
267 m_refData
= new wxFontRefData();
271 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
277 // ----------------------------------------------------------------------------
278 // change font attribute: we recreate font when doing it
279 // ----------------------------------------------------------------------------
281 void wxFont::SetPointSize(int pointSize
)
285 M_FONTDATA
->m_pointSize
= pointSize
;
290 void wxFont::SetFamily(int family
)
294 M_FONTDATA
->m_family
= family
;
299 void wxFont::SetStyle(int style
)
303 M_FONTDATA
->m_style
= style
;
308 void wxFont::SetWeight(int weight
)
312 M_FONTDATA
->m_weight
= weight
;
317 void wxFont::SetFaceName(const wxString
& faceName
)
321 M_FONTDATA
->m_faceName
= faceName
;
326 void wxFont::SetUnderlined(bool underlined
)
330 M_FONTDATA
->m_underlined
= underlined
;
335 void wxFont::SetEncoding(wxFontEncoding encoding
)
339 M_FONTDATA
->m_encoding
= encoding
;
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
348 int wxFont::GetPointSize() const
350 return M_FONTDATA
->m_pointSize
;
353 int wxFont::GetFamily() const
355 return M_FONTDATA
->m_family
;
358 int wxFont::GetFontId() const
360 return M_FONTDATA
->m_fontId
;
363 int wxFont::GetStyle() const
365 return M_FONTDATA
->m_style
;
368 int wxFont::GetWeight() const
370 return M_FONTDATA
->m_weight
;
373 bool wxFont::GetUnderlined() const
375 return M_FONTDATA
->m_underlined
;
378 wxString
wxFont::GetFaceName() const
382 str
= M_FONTDATA
->m_faceName
;
386 wxFontEncoding
wxFont::GetEncoding() const
388 return M_FONTDATA
->m_encoding
;