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