]> git.saurik.com Git - wxWidgets.git/blob - src/msw/font.cpp
more backwards compatible kbd handling in wxScrolledWindow
[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 if ( !M_FONTDATA->m_hFont )
208 {
209 wxLogLastError("CreateFont");
210
211 return FALSE;
212 }
213
214 return TRUE;
215 }
216
217 bool wxFont::FreeResource(bool force)
218 {
219 if ( GetResourceHandle() )
220 {
221 if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) )
222 {
223 wxLogLastError("DeleteObject(font)");
224 }
225
226 M_FONTDATA->m_hFont = 0;
227
228 return TRUE;
229 }
230 return FALSE;
231 }
232
233 WXHANDLE wxFont::GetResourceHandle()
234 {
235 if ( !M_FONTDATA )
236 return 0;
237 else
238 return (WXHANDLE)M_FONTDATA->m_hFont;
239 }
240
241 bool wxFont::IsFree() const
242 {
243 return (M_FONTDATA && (M_FONTDATA->m_hFont == 0));
244 }
245
246 void wxFont::Unshare()
247 {
248 // Don't change shared data
249 if ( !m_refData )
250 {
251 m_refData = new wxFontRefData();
252 }
253 else
254 {
255 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
256 UnRef();
257 m_refData = ref;
258 }
259 }
260
261 // ----------------------------------------------------------------------------
262 // change font attribute: we recreate font when doing it
263 // ----------------------------------------------------------------------------
264
265 void wxFont::SetPointSize(int pointSize)
266 {
267 Unshare();
268
269 M_FONTDATA->m_pointSize = pointSize;
270
271 RealizeResource();
272 }
273
274 void wxFont::SetFamily(int family)
275 {
276 Unshare();
277
278 M_FONTDATA->m_family = family;
279
280 RealizeResource();
281 }
282
283 void wxFont::SetStyle(int style)
284 {
285 Unshare();
286
287 M_FONTDATA->m_style = style;
288
289 RealizeResource();
290 }
291
292 void wxFont::SetWeight(int weight)
293 {
294 Unshare();
295
296 M_FONTDATA->m_weight = weight;
297
298 RealizeResource();
299 }
300
301 void wxFont::SetFaceName(const wxString& faceName)
302 {
303 Unshare();
304
305 M_FONTDATA->m_faceName = faceName;
306
307 RealizeResource();
308 }
309
310 void wxFont::SetUnderlined(bool underlined)
311 {
312 Unshare();
313
314 M_FONTDATA->m_underlined = underlined;
315
316 RealizeResource();
317 }
318
319 void wxFont::SetEncoding(wxFontEncoding encoding)
320 {
321 Unshare();
322
323 M_FONTDATA->m_encoding = encoding;
324
325 RealizeResource();
326 }
327
328 // ----------------------------------------------------------------------------
329 // accessors
330 // ----------------------------------------------------------------------------
331
332 int wxFont::GetPointSize() const
333 {
334 return M_FONTDATA->m_pointSize;
335 }
336
337 int wxFont::GetFamily() const
338 {
339 return M_FONTDATA->m_family;
340 }
341
342 int wxFont::GetFontId() const
343 {
344 return M_FONTDATA->m_fontId;
345 }
346
347 int wxFont::GetStyle() const
348 {
349 return M_FONTDATA->m_style;
350 }
351
352 int wxFont::GetWeight() const
353 {
354 return M_FONTDATA->m_weight;
355 }
356
357 bool wxFont::GetUnderlined() const
358 {
359 return M_FONTDATA->m_underlined;
360 }
361
362 wxString wxFont::GetFaceName() const
363 {
364 wxString str;
365 if ( M_FONTDATA )
366 str = M_FONTDATA->m_faceName;
367 return str;
368 }
369
370 wxFontEncoding wxFont::GetEncoding() const
371 {
372 return M_FONTDATA->m_encoding;
373 }
374