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