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