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