]> git.saurik.com Git - wxWidgets.git/blob - src/msw/font.cpp
workarounded false memory leak report
[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 // ----------------------------------------------------------------------------
45 // constants
46 // ----------------------------------------------------------------------------
47
48 // the default font size in points
49 static const int wxDEFAULT_FONT_SIZE = 12;
50
51 // ----------------------------------------------------------------------------
52 // wxFontRefData - the internal description of the font
53 // ----------------------------------------------------------------------------
54
55 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
56 {
57 friend class WXDLLEXPORT wxFont;
58
59 public:
60 wxFontRefData()
61 {
62 Init(wxDEFAULT_FONT_SIZE, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE,
63 "", wxFONTENCODING_DEFAULT);
64 }
65
66 wxFontRefData(const wxFontRefData& data)
67 {
68 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
69 data.m_underlined, data.m_faceName, data.m_encoding);
70
71 m_fontId = data.m_fontId;
72 }
73
74 wxFontRefData(int size,
75 int family,
76 int style,
77 int weight,
78 bool underlined,
79 const wxString& faceName,
80 wxFontEncoding encoding)
81 {
82 Init(size, family, style, weight, underlined, faceName, encoding);
83 }
84
85 virtual ~wxFontRefData();
86
87 protected:
88 // common part of all ctors
89 void Init(int size,
90 int family,
91 int style,
92 int weight,
93 bool underlined,
94 const wxString& faceName,
95 wxFontEncoding encoding);
96
97 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
98 // DELETED by destructor
99 bool m_temporary;
100
101 int m_fontId;
102
103 // font characterstics
104 int m_pointSize;
105 int m_family;
106 int m_style;
107 int m_weight;
108 bool m_underlined;
109 wxString m_faceName;
110 wxFontEncoding m_encoding;
111
112 // Windows font handle
113 WXHFONT m_hFont;
114 };
115
116 // ============================================================================
117 // implementation
118 // ============================================================================
119
120 // ----------------------------------------------------------------------------
121 // wxFontRefData
122 // ----------------------------------------------------------------------------
123
124 void wxFontRefData::Init(int pointSize,
125 int family,
126 int style,
127 int weight,
128 bool underlined,
129 const wxString& faceName,
130 wxFontEncoding encoding)
131 {
132 m_style = style;
133 m_pointSize = pointSize;
134 m_family = family;
135 m_style = style;
136 m_weight = weight;
137 m_underlined = underlined;
138 m_faceName = faceName;
139 m_encoding = encoding;
140
141 m_fontId = 0;
142 m_temporary = FALSE;
143
144 m_hFont = 0;
145 }
146
147 wxFontRefData::~wxFontRefData()
148 {
149 if ( m_hFont )
150 {
151 if ( !::DeleteObject((HFONT) m_hFont) )
152 {
153 wxLogLastError(wxT("DeleteObject(font)"));
154 }
155 }
156 }
157
158 // ----------------------------------------------------------------------------
159 // wxFont
160 // ----------------------------------------------------------------------------
161
162 void wxFont::Init()
163 {
164 if ( wxTheFontList )
165 wxTheFontList->Append(this);
166 }
167
168 /* Constructor for a font. Note that the real construction is done
169 * in wxDC::SetFont, when information is available about scaling etc.
170 */
171 bool wxFont::Create(int pointSize,
172 int family,
173 int style,
174 int weight,
175 bool underlined,
176 const wxString& faceName,
177 wxFontEncoding encoding)
178 {
179 UnRef();
180
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;
185
186 m_refData = new wxFontRefData(pointSize, family, style, weight,
187 underlined, faceName, encoding);
188
189 RealizeResource();
190
191 return TRUE;
192 }
193
194 wxFont::~wxFont()
195 {
196 if ( wxTheFontList )
197 wxTheFontList->DeleteObject(this);
198 }
199
200 // ----------------------------------------------------------------------------
201 // real implementation
202 // ----------------------------------------------------------------------------
203
204 bool wxFont::RealizeResource()
205 {
206 if ( GetResourceHandle() )
207 {
208 // VZ: the old code returned FALSE in this case, but it doesn't seem
209 // to make sense because the font _was_ created
210 return TRUE;
211 }
212
213 LOGFONT lf;
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 )
218 {
219 wxLogLastError(wxT("CreateFont"));
220
221 return FALSE;
222 }
223
224 return TRUE;
225 }
226
227 bool wxFont::FreeResource(bool force)
228 {
229 if ( GetResourceHandle() )
230 {
231 if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) )
232 {
233 wxLogLastError(wxT("DeleteObject(font)"));
234 }
235
236 M_FONTDATA->m_hFont = 0;
237
238 return TRUE;
239 }
240 return FALSE;
241 }
242
243 WXHANDLE wxFont::GetResourceHandle()
244 {
245 return GetHFONT();
246 }
247
248 WXHFONT wxFont::GetHFONT() const
249 {
250 if ( !M_FONTDATA )
251 return 0;
252 else
253 return (WXHANDLE)M_FONTDATA->m_hFont;
254 }
255
256 bool wxFont::IsFree() const
257 {
258 return (M_FONTDATA && (M_FONTDATA->m_hFont == 0));
259 }
260
261 void wxFont::Unshare()
262 {
263 // Don't change shared data
264 if ( !m_refData )
265 {
266 m_refData = new wxFontRefData();
267 }
268 else
269 {
270 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
271 UnRef();
272 m_refData = ref;
273 }
274 }
275
276 // ----------------------------------------------------------------------------
277 // change font attribute: we recreate font when doing it
278 // ----------------------------------------------------------------------------
279
280 void wxFont::SetPointSize(int pointSize)
281 {
282 Unshare();
283
284 M_FONTDATA->m_pointSize = pointSize;
285
286 RealizeResource();
287 }
288
289 void wxFont::SetFamily(int family)
290 {
291 Unshare();
292
293 M_FONTDATA->m_family = family;
294
295 RealizeResource();
296 }
297
298 void wxFont::SetStyle(int style)
299 {
300 Unshare();
301
302 M_FONTDATA->m_style = style;
303
304 RealizeResource();
305 }
306
307 void wxFont::SetWeight(int weight)
308 {
309 Unshare();
310
311 M_FONTDATA->m_weight = weight;
312
313 RealizeResource();
314 }
315
316 void wxFont::SetFaceName(const wxString& faceName)
317 {
318 Unshare();
319
320 M_FONTDATA->m_faceName = faceName;
321
322 RealizeResource();
323 }
324
325 void wxFont::SetUnderlined(bool underlined)
326 {
327 Unshare();
328
329 M_FONTDATA->m_underlined = underlined;
330
331 RealizeResource();
332 }
333
334 void wxFont::SetEncoding(wxFontEncoding encoding)
335 {
336 Unshare();
337
338 M_FONTDATA->m_encoding = encoding;
339
340 RealizeResource();
341 }
342
343 // ----------------------------------------------------------------------------
344 // accessors
345 // ----------------------------------------------------------------------------
346
347 int wxFont::GetPointSize() const
348 {
349 return M_FONTDATA->m_pointSize;
350 }
351
352 int wxFont::GetFamily() const
353 {
354 return M_FONTDATA->m_family;
355 }
356
357 int wxFont::GetFontId() const
358 {
359 return M_FONTDATA->m_fontId;
360 }
361
362 int wxFont::GetStyle() const
363 {
364 return M_FONTDATA->m_style;
365 }
366
367 int wxFont::GetWeight() const
368 {
369 return M_FONTDATA->m_weight;
370 }
371
372 bool wxFont::GetUnderlined() const
373 {
374 return M_FONTDATA->m_underlined;
375 }
376
377 wxString wxFont::GetFaceName() const
378 {
379 wxString str;
380 if ( M_FONTDATA )
381 str = M_FONTDATA->m_faceName;
382 return str;
383 }
384
385 wxFontEncoding wxFont::GetEncoding() const
386 {
387 return M_FONTDATA->m_encoding;
388 }
389