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