]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
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 | |
b9b3ccd9 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
0c5d3e1c VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
0c5d3e1c | 21 | #pragma implementation "font.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
0c5d3e1c | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
0c5d3e1c VZ |
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 | |
2bda0e17 KB |
39 | |
40 | #include "wx/msw/private.h" | |
2bda0e17 | 41 | |
0c5d3e1c | 42 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
2bda0e17 | 43 | |
0c5d3e1c VZ |
44 | #if wxUSE_PORTABLE_FONTS_IN_MSW |
45 | IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory, wxObject) | |
46 | #endif | |
2bda0e17 | 47 | |
0c5d3e1c VZ |
48 | // ---------------------------------------------------------------------------- |
49 | // wxFontRefData - the internal description of the font | |
50 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 51 | |
0c5d3e1c | 52 | class WXDLLEXPORT wxFontRefData: public wxGDIRefData |
2bda0e17 | 53 | { |
0c5d3e1c VZ |
54 | friend class WXDLLEXPORT wxFont; |
55 | ||
56 | public: | |
57 | wxFontRefData() | |
58 | { | |
74b31181 VZ |
59 | Init(12, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE, |
60 | "", wxFONTENCODING_DEFAULT); | |
0c5d3e1c VZ |
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 | } | |
2bda0e17 | 81 | |
0c5d3e1c VZ |
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) | |
b823f5a1 | 128 | { |
b9b3ccd9 VZ |
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; | |
0c5d3e1c VZ |
136 | m_encoding = encoding; |
137 | ||
b9b3ccd9 VZ |
138 | m_fontId = 0; |
139 | m_temporary = FALSE; | |
0c5d3e1c | 140 | |
b9b3ccd9 | 141 | m_hFont = 0; |
b823f5a1 JS |
142 | } |
143 | ||
0c5d3e1c | 144 | wxFontRefData::~wxFontRefData() |
2bda0e17 | 145 | { |
b9b3ccd9 | 146 | if ( m_hFont ) |
0c5d3e1c | 147 | { |
b9b3ccd9 | 148 | if ( !::DeleteObject((HFONT) m_hFont) ) |
0c5d3e1c VZ |
149 | { |
150 | wxLogLastError("DeleteObject(font)"); | |
151 | } | |
152 | } | |
2bda0e17 KB |
153 | } |
154 | ||
0c5d3e1c VZ |
155 | // ---------------------------------------------------------------------------- |
156 | // wxFont | |
157 | // ---------------------------------------------------------------------------- | |
158 | ||
159 | void wxFont::Init() | |
2bda0e17 KB |
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 | */ | |
0c5d3e1c VZ |
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) | |
2bda0e17 | 175 | { |
0c5d3e1c VZ |
176 | UnRef(); |
177 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
178 | underlined, faceName, encoding); | |
2bda0e17 | 179 | |
0c5d3e1c | 180 | RealizeResource(); |
2bda0e17 | 181 | |
0c5d3e1c | 182 | return TRUE; |
2bda0e17 KB |
183 | } |
184 | ||
185 | wxFont::~wxFont() | |
186 | { | |
0c5d3e1c VZ |
187 | if ( wxTheFontList ) |
188 | wxTheFontList->DeleteObject(this); | |
2bda0e17 KB |
189 | } |
190 | ||
0c5d3e1c VZ |
191 | // ---------------------------------------------------------------------------- |
192 | // real implementation | |
193 | // ---------------------------------------------------------------------------- | |
194 | ||
195 | bool wxFont::RealizeResource() | |
2bda0e17 | 196 | { |
0c5d3e1c VZ |
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 | |
0c5d3e1c VZ |
201 | return TRUE; |
202 | } | |
203 | ||
11c7d5b6 VZ |
204 | LOGFONT lf; |
205 | wxFillLogFont(&lf, this); | |
206 | M_FONTDATA->m_hFont = (WXHFONT)::CreateFontIndirect(&lf); | |
fb04b343 | 207 | M_FONTDATA->m_faceName = lf.lfFaceName; |
11c7d5b6 | 208 | if ( !M_FONTDATA->m_hFont ) |
b9b3ccd9 VZ |
209 | { |
210 | wxLogLastError("CreateFont"); | |
11c7d5b6 VZ |
211 | |
212 | return FALSE; | |
b9b3ccd9 VZ |
213 | } |
214 | ||
11c7d5b6 | 215 | return TRUE; |
2bda0e17 KB |
216 | } |
217 | ||
218 | bool wxFont::FreeResource(bool force) | |
219 | { | |
0c5d3e1c VZ |
220 | if ( GetResourceHandle() ) |
221 | { | |
222 | if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) ) | |
223 | { | |
224 | wxLogLastError("DeleteObject(font)"); | |
225 | } | |
226 | ||
227 | M_FONTDATA->m_hFont = 0; | |
228 | ||
229 | return TRUE; | |
230 | } | |
231 | return FALSE; | |
2bda0e17 KB |
232 | } |
233 | ||
b823f5a1 | 234 | WXHANDLE wxFont::GetResourceHandle() |
2bda0e17 | 235 | { |
0c5d3e1c VZ |
236 | if ( !M_FONTDATA ) |
237 | return 0; | |
238 | else | |
11c7d5b6 | 239 | return (WXHANDLE)M_FONTDATA->m_hFont; |
2bda0e17 KB |
240 | } |
241 | ||
e90babdf | 242 | bool wxFont::IsFree() const |
2bda0e17 | 243 | { |
0c5d3e1c | 244 | return (M_FONTDATA && (M_FONTDATA->m_hFont == 0)); |
2bda0e17 KB |
245 | } |
246 | ||
b823f5a1 JS |
247 | void wxFont::Unshare() |
248 | { | |
b9b3ccd9 VZ |
249 | // Don't change shared data |
250 | if ( !m_refData ) | |
b823f5a1 | 251 | { |
b9b3ccd9 VZ |
252 | m_refData = new wxFontRefData(); |
253 | } | |
b823f5a1 JS |
254 | else |
255 | { | |
b9b3ccd9 VZ |
256 | wxFontRefData* ref = new wxFontRefData(*M_FONTDATA); |
257 | UnRef(); | |
258 | m_refData = ref; | |
259 | } | |
b823f5a1 JS |
260 | } |
261 | ||
0c5d3e1c VZ |
262 | // ---------------------------------------------------------------------------- |
263 | // change font attribute: we recreate font when doing it | |
264 | // ---------------------------------------------------------------------------- | |
265 | ||
debe6624 | 266 | void wxFont::SetPointSize(int pointSize) |
2bda0e17 | 267 | { |
b823f5a1 JS |
268 | Unshare(); |
269 | ||
2bda0e17 | 270 | M_FONTDATA->m_pointSize = pointSize; |
b823f5a1 JS |
271 | |
272 | RealizeResource(); | |
2bda0e17 KB |
273 | } |
274 | ||
debe6624 | 275 | void wxFont::SetFamily(int family) |
2bda0e17 | 276 | { |
b823f5a1 JS |
277 | Unshare(); |
278 | ||
2bda0e17 | 279 | M_FONTDATA->m_family = family; |
b823f5a1 JS |
280 | |
281 | RealizeResource(); | |
2bda0e17 KB |
282 | } |
283 | ||
debe6624 | 284 | void wxFont::SetStyle(int style) |
2bda0e17 | 285 | { |
b823f5a1 JS |
286 | Unshare(); |
287 | ||
2bda0e17 | 288 | M_FONTDATA->m_style = style; |
b823f5a1 JS |
289 | |
290 | RealizeResource(); | |
2bda0e17 KB |
291 | } |
292 | ||
debe6624 | 293 | void wxFont::SetWeight(int weight) |
2bda0e17 | 294 | { |
b823f5a1 JS |
295 | Unshare(); |
296 | ||
2bda0e17 | 297 | M_FONTDATA->m_weight = weight; |
b823f5a1 JS |
298 | |
299 | RealizeResource(); | |
2bda0e17 KB |
300 | } |
301 | ||
302 | void wxFont::SetFaceName(const wxString& faceName) | |
303 | { | |
b823f5a1 JS |
304 | Unshare(); |
305 | ||
2bda0e17 | 306 | M_FONTDATA->m_faceName = faceName; |
b823f5a1 JS |
307 | |
308 | RealizeResource(); | |
2bda0e17 KB |
309 | } |
310 | ||
debe6624 | 311 | void wxFont::SetUnderlined(bool underlined) |
2bda0e17 | 312 | { |
b823f5a1 JS |
313 | Unshare(); |
314 | ||
2bda0e17 | 315 | M_FONTDATA->m_underlined = underlined; |
b823f5a1 JS |
316 | |
317 | RealizeResource(); | |
2bda0e17 KB |
318 | } |
319 | ||
0c5d3e1c | 320 | void wxFont::SetEncoding(wxFontEncoding encoding) |
2bda0e17 | 321 | { |
0c5d3e1c VZ |
322 | Unshare(); |
323 | ||
324 | M_FONTDATA->m_encoding = encoding; | |
325 | ||
326 | RealizeResource(); | |
327 | } | |
328 | ||
329 | // ---------------------------------------------------------------------------- | |
330 | // accessors | |
331 | // ---------------------------------------------------------------------------- | |
332 | ||
333 | int wxFont::GetPointSize() const | |
334 | { | |
335 | return M_FONTDATA->m_pointSize; | |
336 | } | |
337 | ||
338 | int wxFont::GetFamily() const | |
339 | { | |
340 | return M_FONTDATA->m_family; | |
2bda0e17 KB |
341 | } |
342 | ||
0c5d3e1c | 343 | int wxFont::GetFontId() const |
2bda0e17 | 344 | { |
0c5d3e1c | 345 | return M_FONTDATA->m_fontId; |
2bda0e17 KB |
346 | } |
347 | ||
0c5d3e1c | 348 | int wxFont::GetStyle() const |
2bda0e17 | 349 | { |
0c5d3e1c | 350 | return M_FONTDATA->m_style; |
2bda0e17 KB |
351 | } |
352 | ||
0c5d3e1c | 353 | int wxFont::GetWeight() const |
2bda0e17 | 354 | { |
0c5d3e1c | 355 | return M_FONTDATA->m_weight; |
2bda0e17 KB |
356 | } |
357 | ||
0c5d3e1c VZ |
358 | bool wxFont::GetUnderlined() const |
359 | { | |
360 | return M_FONTDATA->m_underlined; | |
361 | } | |
362 | ||
363 | wxString wxFont::GetFaceName() const | |
364 | { | |
365 | wxString str; | |
366 | if ( M_FONTDATA ) | |
11c7d5b6 | 367 | str = M_FONTDATA->m_faceName; |
0c5d3e1c VZ |
368 | return str; |
369 | } | |
370 | ||
371 | wxFontEncoding wxFont::GetEncoding() const | |
372 | { | |
373 | return M_FONTDATA->m_encoding; | |
374 | } | |
a1d58ddc | 375 |