]> git.saurik.com Git - wxWidgets.git/blob - src/msw/font.cpp
wxFont remembers facename of actual font used
[wxWidgets.git] / src / msw / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: font.cpp
3 // Purpose: wxFont class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "font.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include <stdio.h>
33 #include "wx/setup.h"
34 #include "wx/list.h"
35 #include "wx/utils.h"
36 #include "wx/app.h"
37 #include "wx/font.h"
38 #endif // WX_PRECOMP
39
40 #include "wx/msw/private.h"
41
42 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
43
44 #if wxUSE_PORTABLE_FONTS_IN_MSW
45 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory, wxObject)
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // wxFontRefData - the internal description of the font
50 // ----------------------------------------------------------------------------
51
52 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
53 {
54 friend class WXDLLEXPORT wxFont;
55
56 public:
57 wxFontRefData()
58 {
59 Init(12, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE,
60 "", wxFONTENCODING_DEFAULT);
61 }
62
63 wxFontRefData(const wxFontRefData& data)
64 {
65 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
66 data.m_underlined, data.m_faceName, data.m_encoding);
67
68 m_fontId = data.m_fontId;
69 }
70
71 wxFontRefData(int size,
72 int family,
73 int style,
74 int weight,
75 bool underlined,
76 const wxString& faceName,
77 wxFontEncoding encoding)
78 {
79 Init(size, family, style, weight, underlined, faceName, encoding);
80 }
81
82 virtual ~wxFontRefData();
83
84 protected:
85 // common part of all ctors
86 void Init(int size,
87 int family,
88 int style,
89 int weight,
90 bool underlined,
91 const wxString& faceName,
92 wxFontEncoding encoding);
93
94 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
95 // DELETED by destructor
96 bool m_temporary;
97
98 int m_fontId;
99
100 // font characterstics
101 int m_pointSize;
102 int m_family;
103 int m_style;
104 int m_weight;
105 bool m_underlined;
106 wxString m_faceName;
107 wxFontEncoding m_encoding;
108
109 // Windows font handle
110 WXHFONT m_hFont;
111 };
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 // ----------------------------------------------------------------------------
118 // wxFontRefData
119 // ----------------------------------------------------------------------------
120
121 void wxFontRefData::Init(int pointSize,
122 int family,
123 int style,
124 int weight,
125 bool underlined,
126 const wxString& faceName,
127 wxFontEncoding encoding)
128 {
129 m_style = style;
130 m_pointSize = pointSize;
131 m_family = family;
132 m_style = style;
133 m_weight = weight;
134 m_underlined = underlined;
135 m_faceName = faceName;
136 m_encoding = encoding;
137
138 m_fontId = 0;
139 m_temporary = FALSE;
140
141 m_hFont = 0;
142 }
143
144 wxFontRefData::~wxFontRefData()
145 {
146 if ( m_hFont )
147 {
148 if ( !::DeleteObject((HFONT) m_hFont) )
149 {
150 wxLogLastError("DeleteObject(font)");
151 }
152 }
153 }
154
155 // ----------------------------------------------------------------------------
156 // wxFont
157 // ----------------------------------------------------------------------------
158
159 void wxFont::Init()
160 {
161 if ( wxTheFontList )
162 wxTheFontList->Append(this);
163 }
164
165 /* Constructor for a font. Note that the real construction is done
166 * in wxDC::SetFont, when information is available about scaling etc.
167 */
168 bool wxFont::Create(int pointSize,
169 int family,
170 int style,
171 int weight,
172 bool underlined,
173 const wxString& faceName,
174 wxFontEncoding encoding)
175 {
176 UnRef();
177 m_refData = new wxFontRefData(pointSize, family, style, weight,
178 underlined, faceName, encoding);
179
180 RealizeResource();
181
182 return TRUE;
183 }
184
185 wxFont::~wxFont()
186 {
187 if ( wxTheFontList )
188 wxTheFontList->DeleteObject(this);
189 }
190
191 // ----------------------------------------------------------------------------
192 // real implementation
193 // ----------------------------------------------------------------------------
194
195 bool wxFont::RealizeResource()
196 {
197 if ( GetResourceHandle() )
198 {
199 // VZ: the old code returned FALSE in this case, but it doesn't seem
200 // to make sense because the font _was_ created
201 return TRUE;
202 }
203
204 LOGFONT lf;
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 )
209 {
210 wxLogLastError("CreateFont");
211
212 return FALSE;
213 }
214
215 return TRUE;
216 }
217
218 bool wxFont::FreeResource(bool force)
219 {
220 if ( GetResourceHandle() )
221 {
222 if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) )
223 {
224 wxLogLastError("DeleteObject(font)");
225 }
226
227 M_FONTDATA->m_hFont = 0;
228
229 return TRUE;
230 }
231 return FALSE;
232 }
233
234 WXHANDLE wxFont::GetResourceHandle()
235 {
236 if ( !M_FONTDATA )
237 return 0;
238 else
239 return (WXHANDLE)M_FONTDATA->m_hFont;
240 }
241
242 bool wxFont::IsFree() const
243 {
244 return (M_FONTDATA && (M_FONTDATA->m_hFont == 0));
245 }
246
247 void wxFont::Unshare()
248 {
249 // Don't change shared data
250 if ( !m_refData )
251 {
252 m_refData = new wxFontRefData();
253 }
254 else
255 {
256 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
257 UnRef();
258 m_refData = ref;
259 }
260 }
261
262 // ----------------------------------------------------------------------------
263 // change font attribute: we recreate font when doing it
264 // ----------------------------------------------------------------------------
265
266 void wxFont::SetPointSize(int pointSize)
267 {
268 Unshare();
269
270 M_FONTDATA->m_pointSize = pointSize;
271
272 RealizeResource();
273 }
274
275 void wxFont::SetFamily(int family)
276 {
277 Unshare();
278
279 M_FONTDATA->m_family = family;
280
281 RealizeResource();
282 }
283
284 void wxFont::SetStyle(int style)
285 {
286 Unshare();
287
288 M_FONTDATA->m_style = style;
289
290 RealizeResource();
291 }
292
293 void wxFont::SetWeight(int weight)
294 {
295 Unshare();
296
297 M_FONTDATA->m_weight = weight;
298
299 RealizeResource();
300 }
301
302 void wxFont::SetFaceName(const wxString& faceName)
303 {
304 Unshare();
305
306 M_FONTDATA->m_faceName = faceName;
307
308 RealizeResource();
309 }
310
311 void wxFont::SetUnderlined(bool underlined)
312 {
313 Unshare();
314
315 M_FONTDATA->m_underlined = underlined;
316
317 RealizeResource();
318 }
319
320 void wxFont::SetEncoding(wxFontEncoding encoding)
321 {
322 Unshare();
323
324 M_FONTDATA->m_encoding = encoding;
325
326 RealizeResource();
327 }
328
329 // ----------------------------------------------------------------------------
330 // accessors
331 // ----------------------------------------------------------------------------
332
333 int wxFont::GetPointSize() const
334 {
335 return M_FONTDATA->m_pointSize;
336 }
337
338 int wxFont::GetFamily() const
339 {
340 return M_FONTDATA->m_family;
341 }
342
343 int wxFont::GetFontId() const
344 {
345 return M_FONTDATA->m_fontId;
346 }
347
348 int wxFont::GetStyle() const
349 {
350 return M_FONTDATA->m_style;
351 }
352
353 int wxFont::GetWeight() const
354 {
355 return M_FONTDATA->m_weight;
356 }
357
358 bool wxFont::GetUnderlined() const
359 {
360 return M_FONTDATA->m_underlined;
361 }
362
363 wxString wxFont::GetFaceName() const
364 {
365 wxString str;
366 if ( M_FONTDATA )
367 str = M_FONTDATA->m_faceName;
368 return str;
369 }
370
371 wxFontEncoding wxFont::GetEncoding() const
372 {
373 return M_FONTDATA->m_encoding;
374 }
375