]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
a9249b2e | 2 | // Name: src/msw/font.cpp |
2bda0e17 KB |
3 | // Purpose: wxFont class |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
a9249b2e VZ |
8 | // Copyright: (c) wxWindows team |
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 "wx/setup.h" |
33 | #include "wx/list.h" | |
34 | #include "wx/utils.h" | |
35 | #include "wx/app.h" | |
36 | #include "wx/font.h" | |
f94dfb38 | 37 | #include "wx/log.h" |
0c5d3e1c | 38 | #endif // WX_PRECOMP |
2bda0e17 | 39 | |
bff67a6a VZ |
40 | #include "wx/msw/private.h" |
41 | ||
76e23cdb | 42 | #include "wx/fontutil.h" |
bff67a6a | 43 | #include "wx/fontmap.h" |
76e23cdb | 44 | |
1e6feb95 | 45 | #include "wx/tokenzr.h" |
2bda0e17 | 46 | |
7fee680b | 47 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
2bda0e17 | 48 | |
3ca6a5f0 BP |
49 | // ---------------------------------------------------------------------------- |
50 | // constants | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
0c5d3e1c VZ |
53 | // ---------------------------------------------------------------------------- |
54 | // wxFontRefData - the internal description of the font | |
55 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 56 | |
0c5d3e1c | 57 | class WXDLLEXPORT wxFontRefData: public wxGDIRefData |
2bda0e17 | 58 | { |
0c5d3e1c | 59 | public: |
a9249b2e | 60 | // constructors |
0c5d3e1c VZ |
61 | wxFontRefData() |
62 | { | |
a9249b2e VZ |
63 | Init(-1, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, |
64 | FALSE, _T(""), wxFONTENCODING_DEFAULT); | |
0c5d3e1c VZ |
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 | |
04ef50df | 78 | wxFontRefData(const wxNativeFontInfo& info, WXHFONT hFont = 0) |
09fcd889 | 79 | { |
04ef50df | 80 | Init(info, hFont); |
09fcd889 VZ |
81 | } |
82 | ||
a9249b2e VZ |
83 | wxFontRefData(const wxFontRefData& data) |
84 | { | |
85 | if ( data.m_nativeFontInfoOk ) | |
86 | { | |
87 | Init(data.m_nativeFontInfo); | |
88 | } | |
89 | else | |
90 | { | |
91 | Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, | |
92 | data.m_underlined, data.m_faceName, data.m_encoding); | |
93 | } | |
94 | } | |
95 | ||
0c5d3e1c VZ |
96 | virtual ~wxFontRefData(); |
97 | ||
a9249b2e VZ |
98 | // operations |
99 | bool Alloc(wxFont *font); | |
100 | ||
101 | void Free(); | |
102 | ||
103 | // all wxFont accessors | |
104 | int GetPointSize() const | |
105 | { | |
106 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetPointSize() | |
107 | : m_pointSize; | |
108 | } | |
109 | ||
110 | int GetFamily() const | |
111 | { | |
112 | return m_family; | |
113 | } | |
114 | ||
115 | int GetStyle() const | |
116 | { | |
117 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetStyle() | |
118 | : m_style; | |
119 | } | |
120 | ||
121 | int GetWeight() const | |
122 | { | |
123 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetWeight() | |
124 | : m_weight; | |
125 | } | |
126 | ||
127 | bool GetUnderlined() const | |
128 | { | |
129 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetUnderlined() | |
130 | : m_underlined; | |
131 | } | |
132 | ||
133 | wxString GetFaceName() const | |
134 | { | |
135 | wxString s; | |
136 | if ( m_nativeFontInfoOk ) | |
137 | s = m_nativeFontInfo.GetFaceName(); | |
138 | else | |
139 | s = m_faceName; | |
140 | ||
141 | return s; | |
142 | } | |
143 | ||
144 | wxFontEncoding GetEncoding() const | |
145 | { | |
146 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetEncoding() | |
147 | : m_encoding; | |
148 | } | |
149 | ||
150 | WXHFONT GetHFONT() const { return m_hFont; } | |
151 | ||
152 | // ... and setters | |
153 | void SetPointSize(int pointSize) | |
154 | { | |
155 | if ( m_nativeFontInfoOk ) | |
156 | m_nativeFontInfo.SetPointSize(pointSize); | |
157 | else | |
158 | m_pointSize = pointSize; | |
159 | } | |
160 | ||
161 | void SetFamily(int family) | |
162 | { | |
163 | m_family = family; | |
164 | } | |
165 | ||
166 | void SetStyle(int style) | |
167 | { | |
168 | if ( m_nativeFontInfoOk ) | |
169 | m_nativeFontInfo.SetStyle((wxFontStyle)style); | |
170 | else | |
171 | m_style = style; | |
172 | } | |
173 | ||
174 | void SetWeight(int weight) | |
175 | { | |
176 | if ( m_nativeFontInfoOk ) | |
177 | m_nativeFontInfo.SetWeight((wxFontWeight)weight); | |
178 | else | |
179 | m_weight = weight; | |
180 | } | |
181 | ||
182 | void SetFaceName(const wxString& faceName) | |
183 | { | |
184 | if ( m_nativeFontInfoOk ) | |
185 | m_nativeFontInfo.SetFaceName(faceName); | |
186 | else | |
187 | m_faceName = faceName; | |
188 | } | |
189 | ||
190 | void SetUnderlined(bool underlined) | |
191 | { | |
192 | if ( m_nativeFontInfoOk ) | |
193 | m_nativeFontInfo.SetUnderlined(underlined); | |
194 | else | |
195 | m_underlined = underlined; | |
196 | } | |
197 | ||
198 | void SetEncoding(wxFontEncoding encoding) | |
199 | { | |
200 | if ( m_nativeFontInfoOk ) | |
201 | m_nativeFontInfo.SetEncoding(encoding); | |
202 | else | |
203 | m_encoding = encoding; | |
204 | } | |
205 | ||
206 | // native font info tests | |
207 | bool HasNativeFontInfo() const { return m_nativeFontInfoOk; } | |
208 | ||
209 | const wxNativeFontInfo& GetNativeFontInfo() const | |
210 | { return m_nativeFontInfo; } | |
211 | ||
0c5d3e1c VZ |
212 | protected: |
213 | // common part of all ctors | |
214 | void Init(int size, | |
215 | int family, | |
216 | int style, | |
217 | int weight, | |
218 | bool underlined, | |
219 | const wxString& faceName, | |
220 | wxFontEncoding encoding); | |
221 | ||
04ef50df | 222 | void Init(const wxNativeFontInfo& info, WXHFONT hFont = 0); |
09fcd889 | 223 | |
0c5d3e1c VZ |
224 | // font characterstics |
225 | int m_pointSize; | |
226 | int m_family; | |
227 | int m_style; | |
228 | int m_weight; | |
229 | bool m_underlined; | |
230 | wxString m_faceName; | |
231 | wxFontEncoding m_encoding; | |
232 | ||
233 | // Windows font handle | |
234 | WXHFONT m_hFont; | |
789034a0 | 235 | |
09fcd889 VZ |
236 | // Native font info |
237 | wxNativeFontInfo m_nativeFontInfo; | |
238 | bool m_nativeFontInfoOk; | |
0c5d3e1c VZ |
239 | }; |
240 | ||
241 | // ============================================================================ | |
242 | // implementation | |
243 | // ============================================================================ | |
244 | ||
245 | // ---------------------------------------------------------------------------- | |
246 | // wxFontRefData | |
247 | // ---------------------------------------------------------------------------- | |
248 | ||
249 | void wxFontRefData::Init(int pointSize, | |
250 | int family, | |
251 | int style, | |
252 | int weight, | |
253 | bool underlined, | |
254 | const wxString& faceName, | |
255 | wxFontEncoding encoding) | |
b823f5a1 | 256 | { |
b9b3ccd9 | 257 | m_style = style; |
a9249b2e | 258 | m_pointSize = pointSize == -1 ? wxNORMAL_FONT->GetPointSize() : pointSize; |
b9b3ccd9 VZ |
259 | m_family = family; |
260 | m_style = style; | |
261 | m_weight = weight; | |
262 | m_underlined = underlined; | |
263 | m_faceName = faceName; | |
0c5d3e1c VZ |
264 | m_encoding = encoding; |
265 | ||
b9b3ccd9 | 266 | m_hFont = 0; |
789034a0 | 267 | |
09fcd889 VZ |
268 | m_nativeFontInfoOk = FALSE; |
269 | } | |
270 | ||
04ef50df | 271 | void wxFontRefData::Init(const wxNativeFontInfo& info, WXHFONT hFont) |
09fcd889 | 272 | { |
a9249b2e VZ |
273 | // we don't really need the family, what for? |
274 | #if 0 | |
09fcd889 VZ |
275 | // extract family from pitch-and-family |
276 | int lfFamily = info.lf.lfPitchAndFamily; | |
277 | if ( lfFamily & FIXED_PITCH ) | |
278 | lfFamily -= FIXED_PITCH; | |
279 | if ( lfFamily & VARIABLE_PITCH ) | |
280 | lfFamily -= VARIABLE_PITCH; | |
281 | ||
282 | switch ( lfFamily ) | |
283 | { | |
284 | case FF_ROMAN: | |
285 | m_family = wxROMAN; | |
286 | break; | |
287 | ||
288 | case FF_SWISS: | |
289 | m_family = wxSWISS; | |
290 | break; | |
291 | ||
292 | case FF_SCRIPT: | |
293 | m_family = wxSCRIPT; | |
294 | break; | |
295 | ||
296 | case FF_MODERN: | |
297 | m_family = wxMODERN; | |
298 | break; | |
299 | ||
300 | case FF_DECORATIVE: | |
301 | m_family = wxDECORATIVE; | |
302 | break; | |
303 | ||
304 | default: | |
305 | m_family = wxSWISS; | |
306 | } | |
a9249b2e | 307 | #endif // 0 |
09fcd889 | 308 | |
04ef50df JS |
309 | // hFont may be zero, or it be passed in case we really want to |
310 | // use the exact font created in the underlying system | |
311 | // (for example where we can't guarantee conversion from HFONT | |
312 | // to LOGFONT back to HFONT) | |
313 | m_hFont = hFont; | |
789034a0 | 314 | |
09fcd889 VZ |
315 | m_nativeFontInfoOk = TRUE; |
316 | m_nativeFontInfo = info; | |
b823f5a1 JS |
317 | } |
318 | ||
0c5d3e1c | 319 | wxFontRefData::~wxFontRefData() |
a9249b2e VZ |
320 | { |
321 | Free(); | |
322 | } | |
323 | ||
324 | bool wxFontRefData::Alloc(wxFont *font) | |
325 | { | |
326 | if ( !m_nativeFontInfoOk ) | |
327 | { | |
328 | wxFillLogFont(&m_nativeFontInfo.lf, font); | |
329 | m_nativeFontInfoOk = TRUE; | |
330 | } | |
331 | ||
332 | HFONT hfont = ::CreateFontIndirect(&m_nativeFontInfo.lf); | |
333 | if ( !hfont ) | |
334 | { | |
335 | wxLogLastError(wxT("CreateFont")); | |
336 | ||
337 | return FALSE; | |
338 | } | |
339 | ||
340 | m_hFont = (WXHFONT)hfont; | |
341 | ||
342 | return TRUE; | |
343 | } | |
344 | ||
345 | void wxFontRefData::Free() | |
2bda0e17 | 346 | { |
b9b3ccd9 | 347 | if ( m_hFont ) |
0c5d3e1c | 348 | { |
b9b3ccd9 | 349 | if ( !::DeleteObject((HFONT) m_hFont) ) |
0c5d3e1c | 350 | { |
f6bcfd97 | 351 | wxLogLastError(wxT("DeleteObject(font)")); |
0c5d3e1c | 352 | } |
a9249b2e VZ |
353 | |
354 | m_hFont = 0; | |
0c5d3e1c | 355 | } |
2bda0e17 KB |
356 | } |
357 | ||
09fcd889 VZ |
358 | // ---------------------------------------------------------------------------- |
359 | // wxNativeFontInfo | |
360 | // ---------------------------------------------------------------------------- | |
361 | ||
a9249b2e VZ |
362 | void wxNativeFontInfo::Init() |
363 | { | |
364 | wxZeroMemory(lf); | |
365 | } | |
366 | ||
367 | int wxNativeFontInfo::GetPointSize() const | |
368 | { | |
7936354d VZ |
369 | // FIXME: using the screen here results in incorrect font size calculation |
370 | // for printing! | |
a9249b2e VZ |
371 | const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); |
372 | ||
373 | return (int) (((72.0*(double)abs(lf.lfHeight)) / (double) ppInch) + 0.5); | |
374 | } | |
375 | ||
376 | wxFontStyle wxNativeFontInfo::GetStyle() const | |
377 | { | |
378 | return lf.lfItalic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL; | |
379 | } | |
380 | ||
381 | wxFontWeight wxNativeFontInfo::GetWeight() const | |
382 | { | |
383 | if ( lf.lfWeight <= 300 ) | |
384 | return wxFONTWEIGHT_LIGHT; | |
385 | ||
386 | if ( lf.lfWeight >= 600 ) | |
387 | return wxFONTWEIGHT_BOLD; | |
388 | ||
389 | return wxFONTWEIGHT_NORMAL; | |
390 | } | |
391 | ||
392 | bool wxNativeFontInfo::GetUnderlined() const | |
393 | { | |
394 | return lf.lfUnderline != 0; | |
395 | } | |
396 | ||
397 | wxString wxNativeFontInfo::GetFaceName() const | |
398 | { | |
399 | return lf.lfFaceName; | |
400 | } | |
401 | ||
36e2bb4e RD |
402 | wxFontFamily wxNativeFontInfo::GetFamily() const |
403 | { | |
404 | // extract family from pitch-and-family | |
405 | int lfFamily = lf.lfPitchAndFamily; | |
406 | int family; | |
407 | ||
408 | if ( lfFamily & FIXED_PITCH ) | |
409 | lfFamily -= FIXED_PITCH; | |
410 | if ( lfFamily & VARIABLE_PITCH ) | |
411 | lfFamily -= VARIABLE_PITCH; | |
412 | ||
413 | switch ( lfFamily ) | |
414 | { | |
415 | case FF_ROMAN: | |
416 | family = wxROMAN; | |
417 | break; | |
418 | ||
419 | case FF_SWISS: | |
420 | family = wxSWISS; | |
421 | break; | |
422 | ||
423 | case FF_SCRIPT: | |
424 | family = wxSCRIPT; | |
425 | break; | |
426 | ||
427 | case FF_MODERN: | |
428 | family = wxMODERN; | |
429 | break; | |
430 | ||
431 | case FF_DECORATIVE: | |
432 | family = wxDECORATIVE; | |
433 | break; | |
434 | ||
435 | default: | |
436 | family = wxSWISS; | |
437 | } | |
438 | return (wxFontFamily)family; | |
439 | } | |
440 | ||
a9249b2e VZ |
441 | wxFontEncoding wxNativeFontInfo::GetEncoding() const |
442 | { | |
443 | return wxGetFontEncFromCharSet(lf.lfCharSet); | |
444 | } | |
445 | ||
446 | void wxNativeFontInfo::SetPointSize(int pointsize) | |
447 | { | |
7936354d VZ |
448 | #if wxFONT_SIZE_COMPATIBILITY |
449 | // Incorrect, but compatible with old wxWindows behaviour | |
450 | lf.lfHeight = (pointSize*ppInch)/72; | |
451 | #else // wxFONT_SIZE_COMPATIBILITY | |
452 | // FIXME: using the screen here results in incorrect font size calculation | |
453 | // for printing! | |
a9249b2e VZ |
454 | const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); |
455 | ||
456 | lf.lfHeight = -(int)((pointsize*((double)ppInch)/72.0) + 0.5); | |
7936354d | 457 | #endif // wxFONT_SIZE_COMPATIBILITY/!wxFONT_SIZE_COMPATIBILITY |
a9249b2e VZ |
458 | } |
459 | ||
460 | void wxNativeFontInfo::SetStyle(wxFontStyle style) | |
461 | { | |
462 | switch ( style ) | |
463 | { | |
464 | default: | |
465 | wxFAIL_MSG( _T("unknown font style") ); | |
466 | // fall through | |
467 | ||
468 | case wxFONTSTYLE_NORMAL: | |
469 | break; | |
470 | ||
471 | case wxFONTSTYLE_ITALIC: | |
472 | case wxFONTSTYLE_SLANT: | |
473 | lf.lfItalic = TRUE; | |
474 | break; | |
475 | } | |
476 | } | |
477 | ||
478 | void wxNativeFontInfo::SetWeight(wxFontWeight weight) | |
479 | { | |
480 | switch ( weight ) | |
481 | { | |
482 | default: | |
483 | wxFAIL_MSG( _T("unknown font weight") ); | |
484 | // fall through | |
485 | ||
486 | case wxFONTWEIGHT_NORMAL: | |
487 | lf.lfWeight = FW_NORMAL; | |
488 | break; | |
489 | ||
490 | case wxFONTWEIGHT_LIGHT: | |
491 | lf.lfWeight = FW_LIGHT; | |
492 | break; | |
493 | ||
494 | case wxFONTWEIGHT_BOLD: | |
495 | lf.lfWeight = FW_BOLD; | |
496 | break; | |
497 | } | |
498 | } | |
499 | ||
500 | void wxNativeFontInfo::SetUnderlined(bool underlined) | |
501 | { | |
502 | lf.lfUnderline = underlined; | |
503 | } | |
504 | ||
505 | void wxNativeFontInfo::SetFaceName(wxString facename) | |
506 | { | |
49eb2344 | 507 | wxStrncpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName)); |
a9249b2e VZ |
508 | } |
509 | ||
7936354d VZ |
510 | void wxNativeFontInfo::SetFamily(wxFontFamily family) |
511 | { | |
512 | int ff_family; | |
513 | wxString facename; | |
514 | ||
515 | switch ( family ) | |
516 | { | |
517 | case wxSCRIPT: | |
518 | ff_family = FF_SCRIPT; | |
519 | facename = _T("Script"); | |
520 | break; | |
521 | ||
522 | case wxDECORATIVE: | |
523 | ff_family = FF_DECORATIVE; | |
5e968b74 | 524 | facename = _T("Old English Text MT"); |
7936354d VZ |
525 | break; |
526 | ||
527 | case wxROMAN: | |
528 | ff_family = FF_ROMAN; | |
529 | facename = _T("Times New Roman"); | |
530 | break; | |
531 | ||
532 | case wxTELETYPE: | |
533 | case wxMODERN: | |
534 | ff_family = FF_MODERN; | |
535 | facename = _T("Courier New"); | |
536 | break; | |
537 | ||
538 | case wxSWISS: | |
539 | ff_family = FF_SWISS; | |
540 | facename = _T("Arial"); | |
541 | break; | |
542 | ||
543 | case wxDEFAULT: | |
544 | default: | |
545 | ff_family = FF_SWISS; | |
546 | facename = _T("MS Sans Serif"); | |
547 | } | |
548 | ||
549 | lf.lfPitchAndFamily = DEFAULT_PITCH | ff_family; | |
550 | ||
551 | if ( !wxStrlen(lf.lfFaceName) ) | |
552 | { | |
553 | SetFaceName(facename); | |
554 | } | |
555 | } | |
556 | ||
a9249b2e VZ |
557 | void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding) |
558 | { | |
559 | wxNativeEncodingInfo info; | |
560 | if ( !wxGetNativeFontEncoding(encoding, &info) ) | |
561 | { | |
bff67a6a | 562 | #if wxUSE_FONTMAP |
e7e52b6d VZ |
563 | if ( wxTheFontMapper->GetAltForEncoding(encoding, &info) ) |
564 | { | |
565 | if ( !info.facename.empty() ) | |
566 | { | |
567 | // if we have this encoding only in some particular facename, use | |
568 | // the facename - it is better to show the correct characters in a | |
569 | // wrong facename than unreadable text in a correct one | |
570 | SetFaceName(info.facename); | |
571 | } | |
572 | } | |
573 | else | |
bff67a6a VZ |
574 | #endif // wxUSE_FONTMAP |
575 | { | |
576 | // unsupported encoding, replace with the default | |
577 | info.charset = ANSI_CHARSET; | |
578 | } | |
a9249b2e VZ |
579 | } |
580 | ||
581 | lf.lfCharSet = info.charset; | |
582 | } | |
583 | ||
09fcd889 VZ |
584 | bool wxNativeFontInfo::FromString(const wxString& s) |
585 | { | |
586 | long l; | |
587 | ||
588 | wxStringTokenizer tokenizer(s, _T(";")); | |
589 | ||
a9249b2e | 590 | // first the version |
09fcd889 | 591 | wxString token = tokenizer.GetNextToken(); |
a9249b2e VZ |
592 | if ( token != _T('0') ) |
593 | return FALSE; | |
09fcd889 VZ |
594 | |
595 | token = tokenizer.GetNextToken(); | |
596 | if ( !token.ToLong(&l) ) | |
597 | return FALSE; | |
598 | lf.lfHeight = l; | |
599 | ||
600 | token = tokenizer.GetNextToken(); | |
601 | if ( !token.ToLong(&l) ) | |
602 | return FALSE; | |
603 | lf.lfWidth = l; | |
604 | ||
605 | token = tokenizer.GetNextToken(); | |
606 | if ( !token.ToLong(&l) ) | |
607 | return FALSE; | |
608 | lf.lfEscapement = l; | |
609 | ||
610 | token = tokenizer.GetNextToken(); | |
611 | if ( !token.ToLong(&l) ) | |
612 | return FALSE; | |
613 | lf.lfOrientation = l; | |
614 | ||
615 | token = tokenizer.GetNextToken(); | |
616 | if ( !token.ToLong(&l) ) | |
617 | return FALSE; | |
618 | lf.lfWeight = l; | |
619 | ||
620 | token = tokenizer.GetNextToken(); | |
621 | if ( !token.ToLong(&l) ) | |
622 | return FALSE; | |
33ac7e6f | 623 | lf.lfItalic = (BYTE)l; |
09fcd889 VZ |
624 | |
625 | token = tokenizer.GetNextToken(); | |
626 | if ( !token.ToLong(&l) ) | |
627 | return FALSE; | |
33ac7e6f | 628 | lf.lfUnderline = (BYTE)l; |
09fcd889 VZ |
629 | |
630 | token = tokenizer.GetNextToken(); | |
631 | if ( !token.ToLong(&l) ) | |
632 | return FALSE; | |
33ac7e6f | 633 | lf.lfStrikeOut = (BYTE)l; |
09fcd889 VZ |
634 | |
635 | token = tokenizer.GetNextToken(); | |
636 | if ( !token.ToLong(&l) ) | |
637 | return FALSE; | |
33ac7e6f | 638 | lf.lfCharSet = (BYTE)l; |
09fcd889 VZ |
639 | |
640 | token = tokenizer.GetNextToken(); | |
641 | if ( !token.ToLong(&l) ) | |
642 | return FALSE; | |
33ac7e6f | 643 | lf.lfOutPrecision = (BYTE)l; |
09fcd889 VZ |
644 | |
645 | token = tokenizer.GetNextToken(); | |
646 | if ( !token.ToLong(&l) ) | |
647 | return FALSE; | |
33ac7e6f | 648 | lf.lfClipPrecision = (BYTE)l; |
09fcd889 VZ |
649 | |
650 | token = tokenizer.GetNextToken(); | |
651 | if ( !token.ToLong(&l) ) | |
652 | return FALSE; | |
33ac7e6f | 653 | lf.lfQuality = (BYTE)l; |
09fcd889 VZ |
654 | |
655 | token = tokenizer.GetNextToken(); | |
656 | if ( !token.ToLong(&l) ) | |
657 | return FALSE; | |
33ac7e6f | 658 | lf.lfPitchAndFamily = (BYTE)l; |
09fcd889 VZ |
659 | |
660 | token = tokenizer.GetNextToken(); | |
661 | if(!token) | |
662 | return FALSE; | |
663 | wxStrcpy(lf.lfFaceName, token.c_str()); | |
664 | ||
665 | return TRUE; | |
666 | } | |
667 | ||
668 | wxString wxNativeFontInfo::ToString() const | |
669 | { | |
670 | wxString s; | |
671 | ||
672 | s.Printf(_T("%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%s"), | |
673 | 0, // version, in case we want to change the format later | |
674 | lf.lfHeight, | |
675 | lf.lfWidth, | |
676 | lf.lfEscapement, | |
677 | lf.lfOrientation, | |
678 | lf.lfWeight, | |
679 | lf.lfItalic, | |
680 | lf.lfUnderline, | |
681 | lf.lfStrikeOut, | |
682 | lf.lfCharSet, | |
683 | lf.lfOutPrecision, | |
684 | lf.lfClipPrecision, | |
685 | lf.lfQuality, | |
686 | lf.lfPitchAndFamily, | |
687 | lf.lfFaceName); | |
688 | ||
689 | return s; | |
690 | } | |
691 | ||
0c5d3e1c VZ |
692 | // ---------------------------------------------------------------------------- |
693 | // wxFont | |
694 | // ---------------------------------------------------------------------------- | |
695 | ||
696 | void wxFont::Init() | |
2bda0e17 | 697 | { |
2bda0e17 KB |
698 | } |
699 | ||
04ef50df | 700 | bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont) |
76e23cdb | 701 | { |
09fcd889 VZ |
702 | UnRef(); |
703 | ||
04ef50df | 704 | m_refData = new wxFontRefData(info, hFont); |
09fcd889 VZ |
705 | |
706 | RealizeResource(); | |
707 | ||
708 | return TRUE; | |
76e23cdb VZ |
709 | } |
710 | ||
711 | wxFont::wxFont(const wxString& fontdesc) | |
712 | { | |
713 | wxNativeFontInfo info; | |
714 | if ( info.FromString(fontdesc) ) | |
715 | (void)Create(info); | |
716 | } | |
717 | ||
2bda0e17 KB |
718 | /* Constructor for a font. Note that the real construction is done |
719 | * in wxDC::SetFont, when information is available about scaling etc. | |
720 | */ | |
0c5d3e1c VZ |
721 | bool wxFont::Create(int pointSize, |
722 | int family, | |
723 | int style, | |
724 | int weight, | |
725 | bool underlined, | |
726 | const wxString& faceName, | |
727 | wxFontEncoding encoding) | |
2bda0e17 | 728 | { |
0c5d3e1c | 729 | UnRef(); |
3ca6a5f0 BP |
730 | |
731 | // wxDEFAULT is a valid value for the font size too so we must treat it | |
732 | // specially here (otherwise the size would be 70 == wxDEFAULT value) | |
733 | if ( pointSize == wxDEFAULT ) | |
a9249b2e VZ |
734 | { |
735 | pointSize = wxNORMAL_FONT->GetPointSize(); | |
736 | } | |
3ca6a5f0 | 737 | |
0c5d3e1c VZ |
738 | m_refData = new wxFontRefData(pointSize, family, style, weight, |
739 | underlined, faceName, encoding); | |
2bda0e17 | 740 | |
0c5d3e1c | 741 | RealizeResource(); |
2bda0e17 | 742 | |
0c5d3e1c | 743 | return TRUE; |
2bda0e17 KB |
744 | } |
745 | ||
746 | wxFont::~wxFont() | |
747 | { | |
2bda0e17 KB |
748 | } |
749 | ||
0c5d3e1c VZ |
750 | // ---------------------------------------------------------------------------- |
751 | // real implementation | |
752 | // ---------------------------------------------------------------------------- | |
753 | ||
754 | bool wxFont::RealizeResource() | |
2bda0e17 | 755 | { |
0c5d3e1c VZ |
756 | if ( GetResourceHandle() ) |
757 | { | |
758 | // VZ: the old code returned FALSE in this case, but it doesn't seem | |
759 | // to make sense because the font _was_ created | |
0c5d3e1c VZ |
760 | return TRUE; |
761 | } | |
762 | ||
a9249b2e | 763 | return M_FONTDATA->Alloc(this); |
2bda0e17 KB |
764 | } |
765 | ||
33ac7e6f | 766 | bool wxFont::FreeResource(bool WXUNUSED(force)) |
2bda0e17 | 767 | { |
0c5d3e1c VZ |
768 | if ( GetResourceHandle() ) |
769 | { | |
a9249b2e | 770 | M_FONTDATA->Free(); |
0c5d3e1c VZ |
771 | |
772 | return TRUE; | |
773 | } | |
a9249b2e | 774 | |
0c5d3e1c | 775 | return FALSE; |
2bda0e17 KB |
776 | } |
777 | ||
b823f5a1 | 778 | WXHANDLE wxFont::GetResourceHandle() |
f6bcfd97 BP |
779 | { |
780 | return GetHFONT(); | |
781 | } | |
782 | ||
783 | WXHFONT wxFont::GetHFONT() const | |
2bda0e17 | 784 | { |
a9249b2e | 785 | return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0; |
2bda0e17 KB |
786 | } |
787 | ||
e90babdf | 788 | bool wxFont::IsFree() const |
2bda0e17 | 789 | { |
a9249b2e | 790 | return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0); |
2bda0e17 KB |
791 | } |
792 | ||
b823f5a1 JS |
793 | void wxFont::Unshare() |
794 | { | |
b9b3ccd9 VZ |
795 | // Don't change shared data |
796 | if ( !m_refData ) | |
b823f5a1 | 797 | { |
b9b3ccd9 VZ |
798 | m_refData = new wxFontRefData(); |
799 | } | |
b823f5a1 JS |
800 | else |
801 | { | |
b9b3ccd9 VZ |
802 | wxFontRefData* ref = new wxFontRefData(*M_FONTDATA); |
803 | UnRef(); | |
804 | m_refData = ref; | |
805 | } | |
b823f5a1 JS |
806 | } |
807 | ||
0c5d3e1c VZ |
808 | // ---------------------------------------------------------------------------- |
809 | // change font attribute: we recreate font when doing it | |
810 | // ---------------------------------------------------------------------------- | |
811 | ||
debe6624 | 812 | void wxFont::SetPointSize(int pointSize) |
2bda0e17 | 813 | { |
b823f5a1 JS |
814 | Unshare(); |
815 | ||
a9249b2e | 816 | M_FONTDATA->SetPointSize(pointSize); |
b823f5a1 JS |
817 | |
818 | RealizeResource(); | |
2bda0e17 KB |
819 | } |
820 | ||
debe6624 | 821 | void wxFont::SetFamily(int family) |
2bda0e17 | 822 | { |
b823f5a1 JS |
823 | Unshare(); |
824 | ||
a9249b2e | 825 | M_FONTDATA->SetFamily(family); |
b823f5a1 JS |
826 | |
827 | RealizeResource(); | |
2bda0e17 KB |
828 | } |
829 | ||
debe6624 | 830 | void wxFont::SetStyle(int style) |
2bda0e17 | 831 | { |
b823f5a1 JS |
832 | Unshare(); |
833 | ||
a9249b2e | 834 | M_FONTDATA->SetStyle(style); |
b823f5a1 JS |
835 | |
836 | RealizeResource(); | |
2bda0e17 KB |
837 | } |
838 | ||
debe6624 | 839 | void wxFont::SetWeight(int weight) |
2bda0e17 | 840 | { |
b823f5a1 JS |
841 | Unshare(); |
842 | ||
a9249b2e | 843 | M_FONTDATA->SetWeight(weight); |
b823f5a1 JS |
844 | |
845 | RealizeResource(); | |
2bda0e17 KB |
846 | } |
847 | ||
848 | void wxFont::SetFaceName(const wxString& faceName) | |
849 | { | |
b823f5a1 JS |
850 | Unshare(); |
851 | ||
a9249b2e | 852 | M_FONTDATA->SetFaceName(faceName); |
b823f5a1 JS |
853 | |
854 | RealizeResource(); | |
2bda0e17 KB |
855 | } |
856 | ||
debe6624 | 857 | void wxFont::SetUnderlined(bool underlined) |
2bda0e17 | 858 | { |
b823f5a1 JS |
859 | Unshare(); |
860 | ||
a9249b2e | 861 | M_FONTDATA->SetUnderlined(underlined); |
b823f5a1 JS |
862 | |
863 | RealizeResource(); | |
2bda0e17 KB |
864 | } |
865 | ||
0c5d3e1c | 866 | void wxFont::SetEncoding(wxFontEncoding encoding) |
2bda0e17 | 867 | { |
0c5d3e1c VZ |
868 | Unshare(); |
869 | ||
a9249b2e | 870 | M_FONTDATA->SetEncoding(encoding); |
09fcd889 VZ |
871 | |
872 | RealizeResource(); | |
873 | } | |
874 | ||
875 | void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info) | |
876 | { | |
877 | Unshare(); | |
789034a0 VZ |
878 | |
879 | FreeResource(); | |
09fcd889 | 880 | |
a9249b2e | 881 | *M_FONTDATA = wxFontRefData(info); |
0c5d3e1c VZ |
882 | |
883 | RealizeResource(); | |
884 | } | |
885 | ||
886 | // ---------------------------------------------------------------------------- | |
887 | // accessors | |
888 | // ---------------------------------------------------------------------------- | |
889 | ||
890 | int wxFont::GetPointSize() const | |
891 | { | |
789034a0 VZ |
892 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
893 | ||
a9249b2e | 894 | return M_FONTDATA->GetPointSize(); |
0c5d3e1c VZ |
895 | } |
896 | ||
897 | int wxFont::GetFamily() const | |
898 | { | |
789034a0 VZ |
899 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
900 | ||
a9249b2e | 901 | return M_FONTDATA->GetFamily(); |
2bda0e17 KB |
902 | } |
903 | ||
0c5d3e1c | 904 | int wxFont::GetStyle() const |
2bda0e17 | 905 | { |
789034a0 VZ |
906 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
907 | ||
a9249b2e | 908 | return M_FONTDATA->GetStyle(); |
2bda0e17 KB |
909 | } |
910 | ||
0c5d3e1c | 911 | int wxFont::GetWeight() const |
2bda0e17 | 912 | { |
789034a0 VZ |
913 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
914 | ||
a9249b2e | 915 | return M_FONTDATA->GetWeight(); |
2bda0e17 KB |
916 | } |
917 | ||
0c5d3e1c VZ |
918 | bool wxFont::GetUnderlined() const |
919 | { | |
789034a0 VZ |
920 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
921 | ||
a9249b2e | 922 | return M_FONTDATA->GetUnderlined(); |
0c5d3e1c VZ |
923 | } |
924 | ||
925 | wxString wxFont::GetFaceName() const | |
926 | { | |
789034a0 VZ |
927 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); |
928 | ||
a9249b2e | 929 | return M_FONTDATA->GetFaceName(); |
0c5d3e1c VZ |
930 | } |
931 | ||
932 | wxFontEncoding wxFont::GetEncoding() const | |
933 | { | |
789034a0 VZ |
934 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
935 | ||
a9249b2e | 936 | return M_FONTDATA->GetEncoding(); |
0c5d3e1c | 937 | } |
a1d58ddc | 938 | |
e421922f | 939 | wxNativeFontInfo *wxFont::GetNativeFontInfo() const |
1e6feb95 | 940 | { |
a9249b2e VZ |
941 | if ( M_FONTDATA->HasNativeFontInfo() ) |
942 | return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo()); | |
1e6feb95 | 943 | |
e421922f | 944 | return 0; |
09fcd889 VZ |
945 | } |
946 |