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
203 wxLogDebug(wxT("Calling wxFont::RealizeResource() twice"));
209 wxFillLogFont(&lf
, this);
210 M_FONTDATA
->m_hFont
= (WXHFONT
)::CreateFontIndirect(&lf
);
211 if ( !M_FONTDATA
->m_hFont
)
213 wxLogLastError("CreateFont");
221 bool wxFont::FreeResource(bool force
)
223 if ( GetResourceHandle() )
225 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
227 wxLogLastError("DeleteObject(font)");
230 M_FONTDATA
->m_hFont
= 0;
237 WXHANDLE
wxFont::GetResourceHandle()
242 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
245 bool wxFont::IsFree() const
247 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
250 void wxFont::Unshare()
252 // Don't change shared data
255 m_refData
= new wxFontRefData();
259 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
265 // ----------------------------------------------------------------------------
266 // change font attribute: we recreate font when doing it
267 // ----------------------------------------------------------------------------
269 void wxFont::SetPointSize(int pointSize
)
273 M_FONTDATA
->m_pointSize
= pointSize
;
278 void wxFont::SetFamily(int family
)
282 M_FONTDATA
->m_family
= family
;
287 void wxFont::SetStyle(int style
)
291 M_FONTDATA
->m_style
= style
;
296 void wxFont::SetWeight(int weight
)
300 M_FONTDATA
->m_weight
= weight
;
305 void wxFont::SetFaceName(const wxString
& faceName
)
309 M_FONTDATA
->m_faceName
= faceName
;
314 void wxFont::SetUnderlined(bool underlined
)
318 M_FONTDATA
->m_underlined
= underlined
;
323 void wxFont::SetEncoding(wxFontEncoding encoding
)
327 M_FONTDATA
->m_encoding
= encoding
;
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
336 int wxFont::GetPointSize() const
338 return M_FONTDATA
->m_pointSize
;
341 int wxFont::GetFamily() const
343 return M_FONTDATA
->m_family
;
346 int wxFont::GetFontId() const
348 return M_FONTDATA
->m_fontId
;
351 int wxFont::GetStyle() const
353 return M_FONTDATA
->m_style
;
356 int wxFont::GetWeight() const
358 return M_FONTDATA
->m_weight
;
361 bool wxFont::GetUnderlined() const
363 return M_FONTDATA
->m_underlined
;
366 wxString
wxFont::GetFaceName() const
370 str
= M_FONTDATA
->m_faceName
;
374 wxFontEncoding
wxFont::GetEncoding() const
376 return M_FONTDATA
->m_encoding
;