]>
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 | // This is the best we can do since we don't have the | |
285 | // correct information at this point. | |
286 | m_family = wxSWISS; | |
287 | } | |
288 | ||
289 | wxFontRefData::~wxFontRefData() | |
290 | { | |
291 | Free(); | |
292 | } | |
293 | ||
294 | bool wxFontRefData::Alloc(wxFont *font) | |
295 | { | |
296 | if ( !m_nativeFontInfoOk ) | |
297 | { | |
298 | wxFillLogFont(&m_nativeFontInfo.lf, font); | |
299 | m_nativeFontInfoOk = TRUE; | |
300 | } | |
301 | ||
302 | HFONT hfont = ::CreateFontIndirect(&m_nativeFontInfo.lf); | |
303 | if ( !hfont ) | |
304 | { | |
305 | wxLogLastError(wxT("CreateFont")); | |
306 | ||
307 | return FALSE; | |
308 | } | |
309 | ||
310 | m_hFont = (WXHFONT)hfont; | |
311 | ||
312 | return TRUE; | |
313 | } | |
314 | ||
315 | void wxFontRefData::Free() | |
316 | { | |
317 | if ( m_hFont ) | |
318 | { | |
319 | if ( !::DeleteObject((HFONT) m_hFont) ) | |
320 | { | |
321 | wxLogLastError(wxT("DeleteObject(font)")); | |
322 | } | |
323 | ||
324 | m_hFont = 0; | |
325 | } | |
326 | } | |
327 | ||
328 | // ---------------------------------------------------------------------------- | |
329 | // wxNativeFontInfo | |
330 | // ---------------------------------------------------------------------------- | |
331 | ||
332 | void wxNativeFontInfo::Init() | |
333 | { | |
334 | wxZeroMemory(lf); | |
335 | } | |
336 | ||
337 | int wxNativeFontInfo::GetPointSize() const | |
338 | { | |
339 | // FIXME: using the screen here results in incorrect font size calculation | |
340 | // for printing! | |
341 | const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); | |
342 | ||
343 | return (int) (((72.0*(double)abs(lf.lfHeight)) / (double) ppInch) + 0.5); | |
344 | } | |
345 | ||
346 | wxFontStyle wxNativeFontInfo::GetStyle() const | |
347 | { | |
348 | return lf.lfItalic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL; | |
349 | } | |
350 | ||
351 | wxFontWeight wxNativeFontInfo::GetWeight() const | |
352 | { | |
353 | if ( lf.lfWeight <= 300 ) | |
354 | return wxFONTWEIGHT_LIGHT; | |
355 | ||
356 | if ( lf.lfWeight >= 600 ) | |
357 | return wxFONTWEIGHT_BOLD; | |
358 | ||
359 | return wxFONTWEIGHT_NORMAL; | |
360 | } | |
361 | ||
362 | bool wxNativeFontInfo::GetUnderlined() const | |
363 | { | |
364 | return lf.lfUnderline != 0; | |
365 | } | |
366 | ||
367 | wxString wxNativeFontInfo::GetFaceName() const | |
368 | { | |
369 | return lf.lfFaceName; | |
370 | } | |
371 | ||
372 | wxFontFamily wxNativeFontInfo::GetFamily() const | |
373 | { | |
374 | wxFontFamily family; | |
375 | ||
376 | // extract family from pitch-and-family | |
377 | switch ( lf.lfPitchAndFamily & ~PITCH_MASK ) | |
378 | { | |
379 | case FF_ROMAN: | |
380 | family = wxFONTFAMILY_ROMAN; | |
381 | break; | |
382 | ||
383 | default: | |
384 | wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") ); | |
385 | // fall through | |
386 | ||
387 | case FF_SWISS: | |
388 | family = wxFONTFAMILY_SWISS; | |
389 | break; | |
390 | ||
391 | case FF_SCRIPT: | |
392 | family = wxFONTFAMILY_SCRIPT; | |
393 | break; | |
394 | ||
395 | case FF_MODERN: | |
396 | family = wxFONTFAMILY_MODERN; | |
397 | break; | |
398 | ||
399 | case FF_DECORATIVE: | |
400 | family = wxFONTFAMILY_DECORATIVE; | |
401 | break; | |
402 | } | |
403 | ||
404 | return family; | |
405 | } | |
406 | ||
407 | wxFontEncoding wxNativeFontInfo::GetEncoding() const | |
408 | { | |
409 | return wxGetFontEncFromCharSet(lf.lfCharSet); | |
410 | } | |
411 | ||
412 | void wxNativeFontInfo::SetPointSize(int pointsize) | |
413 | { | |
414 | #if wxFONT_SIZE_COMPATIBILITY | |
415 | // Incorrect, but compatible with old wxWindows behaviour | |
416 | lf.lfHeight = (pointSize*ppInch)/72; | |
417 | #else // wxFONT_SIZE_COMPATIBILITY | |
418 | // FIXME: using the screen here results in incorrect font size calculation | |
419 | // for printing! | |
420 | const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); | |
421 | ||
422 | lf.lfHeight = -(int)((pointsize*((double)ppInch)/72.0) + 0.5); | |
423 | #endif // wxFONT_SIZE_COMPATIBILITY/!wxFONT_SIZE_COMPATIBILITY | |
424 | } | |
425 | ||
426 | void wxNativeFontInfo::SetStyle(wxFontStyle style) | |
427 | { | |
428 | switch ( style ) | |
429 | { | |
430 | default: | |
431 | wxFAIL_MSG( _T("unknown font style") ); | |
432 | // fall through | |
433 | ||
434 | case wxFONTSTYLE_NORMAL: | |
435 | break; | |
436 | ||
437 | case wxFONTSTYLE_ITALIC: | |
438 | case wxFONTSTYLE_SLANT: | |
439 | lf.lfItalic = TRUE; | |
440 | break; | |
441 | } | |
442 | } | |
443 | ||
444 | void wxNativeFontInfo::SetWeight(wxFontWeight weight) | |
445 | { | |
446 | switch ( weight ) | |
447 | { | |
448 | default: | |
449 | wxFAIL_MSG( _T("unknown font weight") ); | |
450 | // fall through | |
451 | ||
452 | case wxFONTWEIGHT_NORMAL: | |
453 | lf.lfWeight = FW_NORMAL; | |
454 | break; | |
455 | ||
456 | case wxFONTWEIGHT_LIGHT: | |
457 | lf.lfWeight = FW_LIGHT; | |
458 | break; | |
459 | ||
460 | case wxFONTWEIGHT_BOLD: | |
461 | lf.lfWeight = FW_BOLD; | |
462 | break; | |
463 | } | |
464 | } | |
465 | ||
466 | void wxNativeFontInfo::SetUnderlined(bool underlined) | |
467 | { | |
468 | lf.lfUnderline = underlined; | |
469 | } | |
470 | ||
471 | void wxNativeFontInfo::SetFaceName(wxString facename) | |
472 | { | |
473 | wxStrncpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName)); | |
474 | } | |
475 | ||
476 | void wxNativeFontInfo::SetFamily(wxFontFamily family) | |
477 | { | |
478 | int ff_family; | |
479 | wxString facename; | |
480 | ||
481 | switch ( family ) | |
482 | { | |
483 | case wxSCRIPT: | |
484 | ff_family = FF_SCRIPT; | |
485 | facename = _T("Script"); | |
486 | break; | |
487 | ||
488 | case wxDECORATIVE: | |
489 | ff_family = FF_DECORATIVE; | |
490 | facename = _T("Old English Text MT"); | |
491 | break; | |
492 | ||
493 | case wxROMAN: | |
494 | ff_family = FF_ROMAN; | |
495 | facename = _T("Times New Roman"); | |
496 | break; | |
497 | ||
498 | case wxTELETYPE: | |
499 | case wxMODERN: | |
500 | ff_family = FF_MODERN; | |
501 | facename = _T("Courier New"); | |
502 | break; | |
503 | ||
504 | case wxSWISS: | |
505 | ff_family = FF_SWISS; | |
506 | facename = _T("Arial"); | |
507 | break; | |
508 | ||
509 | case wxDEFAULT: | |
510 | default: | |
511 | ff_family = FF_SWISS; | |
512 | facename = _T("MS Sans Serif"); | |
513 | } | |
514 | ||
515 | lf.lfPitchAndFamily = DEFAULT_PITCH | ff_family; | |
516 | ||
517 | if ( !wxStrlen(lf.lfFaceName) ) | |
518 | { | |
519 | SetFaceName(facename); | |
520 | } | |
521 | } | |
522 | ||
523 | void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding) | |
524 | { | |
525 | wxNativeEncodingInfo info; | |
526 | if ( !wxGetNativeFontEncoding(encoding, &info) ) | |
527 | { | |
528 | #if wxUSE_FONTMAP | |
529 | if ( wxFontMapper::Get()->GetAltForEncoding(encoding, &info) ) | |
530 | { | |
531 | if ( !info.facename.empty() ) | |
532 | { | |
533 | // if we have this encoding only in some particular facename, use | |
534 | // the facename - it is better to show the correct characters in a | |
535 | // wrong facename than unreadable text in a correct one | |
536 | SetFaceName(info.facename); | |
537 | } | |
538 | } | |
539 | else | |
540 | #endif // wxUSE_FONTMAP | |
541 | { | |
542 | // unsupported encoding, replace with the default | |
543 | info.charset = DEFAULT_CHARSET; | |
544 | } | |
545 | } | |
546 | ||
547 | lf.lfCharSet = info.charset; | |
548 | } | |
549 | ||
550 | bool wxNativeFontInfo::FromString(const wxString& s) | |
551 | { | |
552 | long l; | |
553 | ||
554 | wxStringTokenizer tokenizer(s, _T(";")); | |
555 | ||
556 | // first the version | |
557 | wxString token = tokenizer.GetNextToken(); | |
558 | if ( token != _T('0') ) | |
559 | return FALSE; | |
560 | ||
561 | token = tokenizer.GetNextToken(); | |
562 | if ( !token.ToLong(&l) ) | |
563 | return FALSE; | |
564 | lf.lfHeight = l; | |
565 | ||
566 | token = tokenizer.GetNextToken(); | |
567 | if ( !token.ToLong(&l) ) | |
568 | return FALSE; | |
569 | lf.lfWidth = l; | |
570 | ||
571 | token = tokenizer.GetNextToken(); | |
572 | if ( !token.ToLong(&l) ) | |
573 | return FALSE; | |
574 | lf.lfEscapement = l; | |
575 | ||
576 | token = tokenizer.GetNextToken(); | |
577 | if ( !token.ToLong(&l) ) | |
578 | return FALSE; | |
579 | lf.lfOrientation = l; | |
580 | ||
581 | token = tokenizer.GetNextToken(); | |
582 | if ( !token.ToLong(&l) ) | |
583 | return FALSE; | |
584 | lf.lfWeight = l; | |
585 | ||
586 | token = tokenizer.GetNextToken(); | |
587 | if ( !token.ToLong(&l) ) | |
588 | return FALSE; | |
589 | lf.lfItalic = (BYTE)l; | |
590 | ||
591 | token = tokenizer.GetNextToken(); | |
592 | if ( !token.ToLong(&l) ) | |
593 | return FALSE; | |
594 | lf.lfUnderline = (BYTE)l; | |
595 | ||
596 | token = tokenizer.GetNextToken(); | |
597 | if ( !token.ToLong(&l) ) | |
598 | return FALSE; | |
599 | lf.lfStrikeOut = (BYTE)l; | |
600 | ||
601 | token = tokenizer.GetNextToken(); | |
602 | if ( !token.ToLong(&l) ) | |
603 | return FALSE; | |
604 | lf.lfCharSet = (BYTE)l; | |
605 | ||
606 | token = tokenizer.GetNextToken(); | |
607 | if ( !token.ToLong(&l) ) | |
608 | return FALSE; | |
609 | lf.lfOutPrecision = (BYTE)l; | |
610 | ||
611 | token = tokenizer.GetNextToken(); | |
612 | if ( !token.ToLong(&l) ) | |
613 | return FALSE; | |
614 | lf.lfClipPrecision = (BYTE)l; | |
615 | ||
616 | token = tokenizer.GetNextToken(); | |
617 | if ( !token.ToLong(&l) ) | |
618 | return FALSE; | |
619 | lf.lfQuality = (BYTE)l; | |
620 | ||
621 | token = tokenizer.GetNextToken(); | |
622 | if ( !token.ToLong(&l) ) | |
623 | return FALSE; | |
624 | lf.lfPitchAndFamily = (BYTE)l; | |
625 | ||
626 | token = tokenizer.GetNextToken(); | |
627 | if(!token) | |
628 | return FALSE; | |
629 | wxStrcpy(lf.lfFaceName, token.c_str()); | |
630 | ||
631 | return TRUE; | |
632 | } | |
633 | ||
634 | wxString wxNativeFontInfo::ToString() const | |
635 | { | |
636 | wxString s; | |
637 | ||
638 | s.Printf(_T("%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%s"), | |
639 | 0, // version, in case we want to change the format later | |
640 | lf.lfHeight, | |
641 | lf.lfWidth, | |
642 | lf.lfEscapement, | |
643 | lf.lfOrientation, | |
644 | lf.lfWeight, | |
645 | lf.lfItalic, | |
646 | lf.lfUnderline, | |
647 | lf.lfStrikeOut, | |
648 | lf.lfCharSet, | |
649 | lf.lfOutPrecision, | |
650 | lf.lfClipPrecision, | |
651 | lf.lfQuality, | |
652 | lf.lfPitchAndFamily, | |
653 | lf.lfFaceName); | |
654 | ||
655 | return s; | |
656 | } | |
657 | ||
658 | // ---------------------------------------------------------------------------- | |
659 | // wxFont | |
660 | // ---------------------------------------------------------------------------- | |
661 | ||
662 | void wxFont::Init() | |
663 | { | |
664 | } | |
665 | ||
666 | bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont) | |
667 | { | |
668 | UnRef(); | |
669 | ||
670 | m_refData = new wxFontRefData(info, hFont); | |
671 | ||
672 | RealizeResource(); | |
673 | ||
674 | return TRUE; | |
675 | } | |
676 | ||
677 | wxFont::wxFont(const wxString& fontdesc) | |
678 | { | |
679 | wxNativeFontInfo info; | |
680 | if ( info.FromString(fontdesc) ) | |
681 | (void)Create(info); | |
682 | } | |
683 | ||
684 | /* Constructor for a font. Note that the real construction is done | |
685 | * in wxDC::SetFont, when information is available about scaling etc. | |
686 | */ | |
687 | bool wxFont::Create(int pointSize, | |
688 | int family, | |
689 | int style, | |
690 | int weight, | |
691 | bool underlined, | |
692 | const wxString& faceName, | |
693 | wxFontEncoding encoding) | |
694 | { | |
695 | UnRef(); | |
696 | ||
697 | // wxDEFAULT is a valid value for the font size too so we must treat it | |
698 | // specially here (otherwise the size would be 70 == wxDEFAULT value) | |
699 | if ( pointSize == wxDEFAULT ) | |
700 | { | |
701 | pointSize = wxNORMAL_FONT->GetPointSize(); | |
702 | } | |
703 | ||
704 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
705 | underlined, faceName, encoding); | |
706 | ||
707 | RealizeResource(); | |
708 | ||
709 | return TRUE; | |
710 | } | |
711 | ||
712 | wxFont::~wxFont() | |
713 | { | |
714 | } | |
715 | ||
716 | // ---------------------------------------------------------------------------- | |
717 | // real implementation | |
718 | // ---------------------------------------------------------------------------- | |
719 | ||
720 | bool wxFont::RealizeResource() | |
721 | { | |
722 | if ( GetResourceHandle() ) | |
723 | { | |
724 | // VZ: the old code returned FALSE in this case, but it doesn't seem | |
725 | // to make sense because the font _was_ created | |
726 | return TRUE; | |
727 | } | |
728 | ||
729 | return M_FONTDATA->Alloc(this); | |
730 | } | |
731 | ||
732 | bool wxFont::FreeResource(bool WXUNUSED(force)) | |
733 | { | |
734 | if ( GetResourceHandle() ) | |
735 | { | |
736 | M_FONTDATA->Free(); | |
737 | ||
738 | return TRUE; | |
739 | } | |
740 | ||
741 | return FALSE; | |
742 | } | |
743 | ||
744 | WXHANDLE wxFont::GetResourceHandle() | |
745 | { | |
746 | return GetHFONT(); | |
747 | } | |
748 | ||
749 | WXHFONT wxFont::GetHFONT() const | |
750 | { | |
751 | return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0; | |
752 | } | |
753 | ||
754 | bool wxFont::IsFree() const | |
755 | { | |
756 | return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0); | |
757 | } | |
758 | ||
759 | void wxFont::Unshare() | |
760 | { | |
761 | // Don't change shared data | |
762 | if ( !m_refData ) | |
763 | { | |
764 | m_refData = new wxFontRefData(); | |
765 | } | |
766 | else | |
767 | { | |
768 | wxFontRefData* ref = new wxFontRefData(*M_FONTDATA); | |
769 | UnRef(); | |
770 | m_refData = ref; | |
771 | } | |
772 | } | |
773 | ||
774 | // ---------------------------------------------------------------------------- | |
775 | // change font attribute: we recreate font when doing it | |
776 | // ---------------------------------------------------------------------------- | |
777 | ||
778 | void wxFont::SetPointSize(int pointSize) | |
779 | { | |
780 | Unshare(); | |
781 | ||
782 | M_FONTDATA->SetPointSize(pointSize); | |
783 | ||
784 | RealizeResource(); | |
785 | } | |
786 | ||
787 | void wxFont::SetFamily(int family) | |
788 | { | |
789 | Unshare(); | |
790 | ||
791 | M_FONTDATA->SetFamily(family); | |
792 | ||
793 | RealizeResource(); | |
794 | } | |
795 | ||
796 | void wxFont::SetStyle(int style) | |
797 | { | |
798 | Unshare(); | |
799 | ||
800 | M_FONTDATA->SetStyle(style); | |
801 | ||
802 | RealizeResource(); | |
803 | } | |
804 | ||
805 | void wxFont::SetWeight(int weight) | |
806 | { | |
807 | Unshare(); | |
808 | ||
809 | M_FONTDATA->SetWeight(weight); | |
810 | ||
811 | RealizeResource(); | |
812 | } | |
813 | ||
814 | void wxFont::SetFaceName(const wxString& faceName) | |
815 | { | |
816 | Unshare(); | |
817 | ||
818 | M_FONTDATA->SetFaceName(faceName); | |
819 | ||
820 | RealizeResource(); | |
821 | } | |
822 | ||
823 | void wxFont::SetUnderlined(bool underlined) | |
824 | { | |
825 | Unshare(); | |
826 | ||
827 | M_FONTDATA->SetUnderlined(underlined); | |
828 | ||
829 | RealizeResource(); | |
830 | } | |
831 | ||
832 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
833 | { | |
834 | Unshare(); | |
835 | ||
836 | M_FONTDATA->SetEncoding(encoding); | |
837 | ||
838 | RealizeResource(); | |
839 | } | |
840 | ||
841 | void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info) | |
842 | { | |
843 | Unshare(); | |
844 | ||
845 | FreeResource(); | |
846 | ||
847 | *M_FONTDATA = wxFontRefData(info); | |
848 | ||
849 | RealizeResource(); | |
850 | } | |
851 | ||
852 | // ---------------------------------------------------------------------------- | |
853 | // accessors | |
854 | // ---------------------------------------------------------------------------- | |
855 | ||
856 | int wxFont::GetPointSize() const | |
857 | { | |
858 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
859 | ||
860 | return M_FONTDATA->GetPointSize(); | |
861 | } | |
862 | ||
863 | int wxFont::GetFamily() const | |
864 | { | |
865 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
866 | ||
867 | return M_FONTDATA->GetFamily(); | |
868 | } | |
869 | ||
870 | int wxFont::GetStyle() const | |
871 | { | |
872 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
873 | ||
874 | return M_FONTDATA->GetStyle(); | |
875 | } | |
876 | ||
877 | int wxFont::GetWeight() const | |
878 | { | |
879 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
880 | ||
881 | return M_FONTDATA->GetWeight(); | |
882 | } | |
883 | ||
884 | bool wxFont::GetUnderlined() const | |
885 | { | |
886 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
887 | ||
888 | return M_FONTDATA->GetUnderlined(); | |
889 | } | |
890 | ||
891 | wxString wxFont::GetFaceName() const | |
892 | { | |
893 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); | |
894 | ||
895 | return M_FONTDATA->GetFaceName(); | |
896 | } | |
897 | ||
898 | wxFontEncoding wxFont::GetEncoding() const | |
899 | { | |
900 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
901 | ||
902 | return M_FONTDATA->GetEncoding(); | |
903 | } | |
904 | ||
905 | wxNativeFontInfo *wxFont::GetNativeFontInfo() const | |
906 | { | |
907 | if ( M_FONTDATA->HasNativeFontInfo() ) | |
908 | return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo()); | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
913 | bool wxFont::IsFixedWidth() const | |
914 | { | |
915 | if ( M_FONTDATA->HasNativeFontInfo() ) | |
916 | { | |
917 | // the two low-order bits specify the pitch of the font, the rest is | |
918 | // family | |
919 | BYTE pitch = M_FONTDATA->GetNativeFontInfo(). | |
920 | lf.lfPitchAndFamily & PITCH_MASK; | |
921 | ||
922 | return pitch == FIXED_PITCH; | |
923 | } | |
924 | ||
925 | return wxFontBase::IsFixedWidth(); | |
926 | } | |
927 |