]>
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); | |
207 | if ( !M_FONTDATA->m_hFont ) | |
b9b3ccd9 VZ |
208 | { |
209 | wxLogLastError("CreateFont"); | |
11c7d5b6 VZ |
210 | |
211 | return FALSE; | |
b9b3ccd9 VZ |
212 | } |
213 | ||
11c7d5b6 | 214 | return TRUE; |
2bda0e17 KB |
215 | } |
216 | ||
217 | bool wxFont::FreeResource(bool force) | |
218 | { | |
0c5d3e1c VZ |
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; | |
2bda0e17 KB |
231 | } |
232 | ||
b823f5a1 | 233 | WXHANDLE wxFont::GetResourceHandle() |
2bda0e17 | 234 | { |
0c5d3e1c VZ |
235 | if ( !M_FONTDATA ) |
236 | return 0; | |
237 | else | |
11c7d5b6 | 238 | return (WXHANDLE)M_FONTDATA->m_hFont; |
2bda0e17 KB |
239 | } |
240 | ||
e90babdf | 241 | bool wxFont::IsFree() const |
2bda0e17 | 242 | { |
0c5d3e1c | 243 | return (M_FONTDATA && (M_FONTDATA->m_hFont == 0)); |
2bda0e17 KB |
244 | } |
245 | ||
b823f5a1 JS |
246 | void wxFont::Unshare() |
247 | { | |
b9b3ccd9 VZ |
248 | // Don't change shared data |
249 | if ( !m_refData ) | |
b823f5a1 | 250 | { |
b9b3ccd9 VZ |
251 | m_refData = new wxFontRefData(); |
252 | } | |
b823f5a1 JS |
253 | else |
254 | { | |
b9b3ccd9 VZ |
255 | wxFontRefData* ref = new wxFontRefData(*M_FONTDATA); |
256 | UnRef(); | |
257 | m_refData = ref; | |
258 | } | |
b823f5a1 JS |
259 | } |
260 | ||
0c5d3e1c VZ |
261 | // ---------------------------------------------------------------------------- |
262 | // change font attribute: we recreate font when doing it | |
263 | // ---------------------------------------------------------------------------- | |
264 | ||
debe6624 | 265 | void wxFont::SetPointSize(int pointSize) |
2bda0e17 | 266 | { |
b823f5a1 JS |
267 | Unshare(); |
268 | ||
2bda0e17 | 269 | M_FONTDATA->m_pointSize = pointSize; |
b823f5a1 JS |
270 | |
271 | RealizeResource(); | |
2bda0e17 KB |
272 | } |
273 | ||
debe6624 | 274 | void wxFont::SetFamily(int family) |
2bda0e17 | 275 | { |
b823f5a1 JS |
276 | Unshare(); |
277 | ||
2bda0e17 | 278 | M_FONTDATA->m_family = family; |
b823f5a1 JS |
279 | |
280 | RealizeResource(); | |
2bda0e17 KB |
281 | } |
282 | ||
debe6624 | 283 | void wxFont::SetStyle(int style) |
2bda0e17 | 284 | { |
b823f5a1 JS |
285 | Unshare(); |
286 | ||
2bda0e17 | 287 | M_FONTDATA->m_style = style; |
b823f5a1 JS |
288 | |
289 | RealizeResource(); | |
2bda0e17 KB |
290 | } |
291 | ||
debe6624 | 292 | void wxFont::SetWeight(int weight) |
2bda0e17 | 293 | { |
b823f5a1 JS |
294 | Unshare(); |
295 | ||
2bda0e17 | 296 | M_FONTDATA->m_weight = weight; |
b823f5a1 JS |
297 | |
298 | RealizeResource(); | |
2bda0e17 KB |
299 | } |
300 | ||
301 | void wxFont::SetFaceName(const wxString& faceName) | |
302 | { | |
b823f5a1 JS |
303 | Unshare(); |
304 | ||
2bda0e17 | 305 | M_FONTDATA->m_faceName = faceName; |
b823f5a1 JS |
306 | |
307 | RealizeResource(); | |
2bda0e17 KB |
308 | } |
309 | ||
debe6624 | 310 | void wxFont::SetUnderlined(bool underlined) |
2bda0e17 | 311 | { |
b823f5a1 JS |
312 | Unshare(); |
313 | ||
2bda0e17 | 314 | M_FONTDATA->m_underlined = underlined; |
b823f5a1 JS |
315 | |
316 | RealizeResource(); | |
2bda0e17 KB |
317 | } |
318 | ||
0c5d3e1c | 319 | void wxFont::SetEncoding(wxFontEncoding encoding) |
2bda0e17 | 320 | { |
0c5d3e1c VZ |
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; | |
2bda0e17 KB |
340 | } |
341 | ||
0c5d3e1c | 342 | int wxFont::GetFontId() const |
2bda0e17 | 343 | { |
0c5d3e1c | 344 | return M_FONTDATA->m_fontId; |
2bda0e17 KB |
345 | } |
346 | ||
0c5d3e1c | 347 | int wxFont::GetStyle() const |
2bda0e17 | 348 | { |
0c5d3e1c | 349 | return M_FONTDATA->m_style; |
2bda0e17 KB |
350 | } |
351 | ||
0c5d3e1c | 352 | int wxFont::GetWeight() const |
2bda0e17 | 353 | { |
0c5d3e1c | 354 | return M_FONTDATA->m_weight; |
2bda0e17 KB |
355 | } |
356 | ||
0c5d3e1c VZ |
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 ) | |
11c7d5b6 | 366 | str = M_FONTDATA->m_faceName; |
0c5d3e1c VZ |
367 | return str; |
368 | } | |
369 | ||
370 | wxFontEncoding wxFont::GetEncoding() const | |
371 | { | |
372 | return M_FONTDATA->m_encoding; | |
373 | } | |
a1d58ddc | 374 |