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