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