]>
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 | lf.lfItalic = FALSE; | |
436 | break; | |
437 | ||
438 | case wxFONTSTYLE_ITALIC: | |
439 | case wxFONTSTYLE_SLANT: | |
440 | lf.lfItalic = TRUE; | |
441 | break; | |
442 | } | |
443 | } | |
444 | ||
445 | void wxNativeFontInfo::SetWeight(wxFontWeight weight) | |
446 | { | |
447 | switch ( weight ) | |
448 | { | |
449 | default: | |
450 | wxFAIL_MSG( _T("unknown font weight") ); | |
451 | // fall through | |
452 | ||
453 | case wxFONTWEIGHT_NORMAL: | |
454 | lf.lfWeight = FW_NORMAL; | |
455 | break; | |
456 | ||
457 | case wxFONTWEIGHT_LIGHT: | |
458 | lf.lfWeight = FW_LIGHT; | |
459 | break; | |
460 | ||
461 | case wxFONTWEIGHT_BOLD: | |
462 | lf.lfWeight = FW_BOLD; | |
463 | break; | |
464 | } | |
465 | } | |
466 | ||
467 | void wxNativeFontInfo::SetUnderlined(bool underlined) | |
468 | { | |
469 | lf.lfUnderline = underlined; | |
470 | } | |
471 | ||
472 | void wxNativeFontInfo::SetFaceName(wxString facename) | |
473 | { | |
474 | wxStrncpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName)); | |
475 | } | |
476 | ||
477 | void wxNativeFontInfo::SetFamily(wxFontFamily family) | |
478 | { | |
479 | int ff_family; | |
480 | wxString facename; | |
481 | ||
482 | switch ( family ) | |
483 | { | |
484 | case wxSCRIPT: | |
485 | ff_family = FF_SCRIPT; | |
486 | facename = _T("Script"); | |
487 | break; | |
488 | ||
489 | case wxDECORATIVE: | |
490 | ff_family = FF_DECORATIVE; | |
491 | facename = _T("Old English Text MT"); | |
492 | break; | |
493 | ||
494 | case wxROMAN: | |
495 | ff_family = FF_ROMAN; | |
496 | facename = _T("Times New Roman"); | |
497 | break; | |
498 | ||
499 | case wxTELETYPE: | |
500 | case wxMODERN: | |
501 | ff_family = FF_MODERN; | |
502 | facename = _T("Courier New"); | |
503 | break; | |
504 | ||
505 | case wxSWISS: | |
506 | ff_family = FF_SWISS; | |
507 | facename = _T("Arial"); | |
508 | break; | |
509 | ||
510 | case wxDEFAULT: | |
511 | default: | |
512 | ff_family = FF_SWISS; | |
513 | facename = _T("MS Sans Serif"); | |
514 | } | |
515 | ||
516 | lf.lfPitchAndFamily = DEFAULT_PITCH | ff_family; | |
517 | ||
518 | if ( !wxStrlen(lf.lfFaceName) ) | |
519 | { | |
520 | SetFaceName(facename); | |
521 | } | |
522 | } | |
523 | ||
524 | void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding) | |
525 | { | |
526 | wxNativeEncodingInfo info; | |
527 | if ( !wxGetNativeFontEncoding(encoding, &info) ) | |
528 | { | |
529 | #if wxUSE_FONTMAP | |
530 | if ( wxFontMapper::Get()->GetAltForEncoding(encoding, &info) ) | |
531 | { | |
532 | if ( !info.facename.empty() ) | |
533 | { | |
534 | // if we have this encoding only in some particular facename, use | |
535 | // the facename - it is better to show the correct characters in a | |
536 | // wrong facename than unreadable text in a correct one | |
537 | SetFaceName(info.facename); | |
538 | } | |
539 | } | |
540 | else | |
541 | #endif // wxUSE_FONTMAP | |
542 | { | |
543 | // unsupported encoding, replace with the default | |
544 | info.charset = DEFAULT_CHARSET; | |
545 | } | |
546 | } | |
547 | ||
548 | lf.lfCharSet = info.charset; | |
549 | } | |
550 | ||
551 | bool wxNativeFontInfo::FromString(const wxString& s) | |
552 | { | |
553 | long l; | |
554 | ||
555 | wxStringTokenizer tokenizer(s, _T(";")); | |
556 | ||
557 | // first the version | |
558 | wxString token = tokenizer.GetNextToken(); | |
559 | if ( token != _T('0') ) | |
560 | return FALSE; | |
561 | ||
562 | token = tokenizer.GetNextToken(); | |
563 | if ( !token.ToLong(&l) ) | |
564 | return FALSE; | |
565 | lf.lfHeight = l; | |
566 | ||
567 | token = tokenizer.GetNextToken(); | |
568 | if ( !token.ToLong(&l) ) | |
569 | return FALSE; | |
570 | lf.lfWidth = l; | |
571 | ||
572 | token = tokenizer.GetNextToken(); | |
573 | if ( !token.ToLong(&l) ) | |
574 | return FALSE; | |
575 | lf.lfEscapement = l; | |
576 | ||
577 | token = tokenizer.GetNextToken(); | |
578 | if ( !token.ToLong(&l) ) | |
579 | return FALSE; | |
580 | lf.lfOrientation = l; | |
581 | ||
582 | token = tokenizer.GetNextToken(); | |
583 | if ( !token.ToLong(&l) ) | |
584 | return FALSE; | |
585 | lf.lfWeight = l; | |
586 | ||
587 | token = tokenizer.GetNextToken(); | |
588 | if ( !token.ToLong(&l) ) | |
589 | return FALSE; | |
590 | lf.lfItalic = (BYTE)l; | |
591 | ||
592 | token = tokenizer.GetNextToken(); | |
593 | if ( !token.ToLong(&l) ) | |
594 | return FALSE; | |
595 | lf.lfUnderline = (BYTE)l; | |
596 | ||
597 | token = tokenizer.GetNextToken(); | |
598 | if ( !token.ToLong(&l) ) | |
599 | return FALSE; | |
600 | lf.lfStrikeOut = (BYTE)l; | |
601 | ||
602 | token = tokenizer.GetNextToken(); | |
603 | if ( !token.ToLong(&l) ) | |
604 | return FALSE; | |
605 | lf.lfCharSet = (BYTE)l; | |
606 | ||
607 | token = tokenizer.GetNextToken(); | |
608 | if ( !token.ToLong(&l) ) | |
609 | return FALSE; | |
610 | lf.lfOutPrecision = (BYTE)l; | |
611 | ||
612 | token = tokenizer.GetNextToken(); | |
613 | if ( !token.ToLong(&l) ) | |
614 | return FALSE; | |
615 | lf.lfClipPrecision = (BYTE)l; | |
616 | ||
617 | token = tokenizer.GetNextToken(); | |
618 | if ( !token.ToLong(&l) ) | |
619 | return FALSE; | |
620 | lf.lfQuality = (BYTE)l; | |
621 | ||
622 | token = tokenizer.GetNextToken(); | |
623 | if ( !token.ToLong(&l) ) | |
624 | return FALSE; | |
625 | lf.lfPitchAndFamily = (BYTE)l; | |
626 | ||
627 | token = tokenizer.GetNextToken(); | |
628 | if(!token) | |
629 | return FALSE; | |
630 | wxStrcpy(lf.lfFaceName, token.c_str()); | |
631 | ||
632 | return TRUE; | |
633 | } | |
634 | ||
635 | wxString wxNativeFontInfo::ToString() const | |
636 | { | |
637 | wxString s; | |
638 | ||
639 | s.Printf(_T("%d;%ld;%ld;%ld;%ld;%ld;%d;%d;%d;%d;%d;%d;%d;%d;%s"), | |
640 | 0, // version, in case we want to change the format later | |
641 | lf.lfHeight, | |
642 | lf.lfWidth, | |
643 | lf.lfEscapement, | |
644 | lf.lfOrientation, | |
645 | lf.lfWeight, | |
646 | lf.lfItalic, | |
647 | lf.lfUnderline, | |
648 | lf.lfStrikeOut, | |
649 | lf.lfCharSet, | |
650 | lf.lfOutPrecision, | |
651 | lf.lfClipPrecision, | |
652 | lf.lfQuality, | |
653 | lf.lfPitchAndFamily, | |
654 | lf.lfFaceName); | |
655 | ||
656 | return s; | |
657 | } | |
658 | ||
659 | // ---------------------------------------------------------------------------- | |
660 | // wxFont | |
661 | // ---------------------------------------------------------------------------- | |
662 | ||
663 | void wxFont::Init() | |
664 | { | |
665 | } | |
666 | ||
667 | bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont) | |
668 | { | |
669 | UnRef(); | |
670 | ||
671 | m_refData = new wxFontRefData(info, hFont); | |
672 | ||
673 | RealizeResource(); | |
674 | ||
675 | return TRUE; | |
676 | } | |
677 | ||
678 | wxFont::wxFont(const wxString& fontdesc) | |
679 | { | |
680 | wxNativeFontInfo info; | |
681 | if ( info.FromString(fontdesc) ) | |
682 | (void)Create(info); | |
683 | } | |
684 | ||
685 | /* Constructor for a font. Note that the real construction is done | |
686 | * in wxDC::SetFont, when information is available about scaling etc. | |
687 | */ | |
688 | bool wxFont::Create(int pointSize, | |
689 | int family, | |
690 | int style, | |
691 | int weight, | |
692 | bool underlined, | |
693 | const wxString& faceName, | |
694 | wxFontEncoding encoding) | |
695 | { | |
696 | UnRef(); | |
697 | ||
698 | // wxDEFAULT is a valid value for the font size too so we must treat it | |
699 | // specially here (otherwise the size would be 70 == wxDEFAULT value) | |
700 | if ( pointSize == wxDEFAULT ) | |
701 | { | |
702 | pointSize = wxNORMAL_FONT->GetPointSize(); | |
703 | } | |
704 | ||
705 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
706 | underlined, faceName, encoding); | |
707 | ||
708 | RealizeResource(); | |
709 | ||
710 | return TRUE; | |
711 | } | |
712 | ||
713 | wxFont::~wxFont() | |
714 | { | |
715 | } | |
716 | ||
717 | // ---------------------------------------------------------------------------- | |
718 | // real implementation | |
719 | // ---------------------------------------------------------------------------- | |
720 | ||
721 | bool wxFont::RealizeResource() | |
722 | { | |
723 | if ( GetResourceHandle() ) | |
724 | { | |
725 | // VZ: the old code returned FALSE in this case, but it doesn't seem | |
726 | // to make sense because the font _was_ created | |
727 | return TRUE; | |
728 | } | |
729 | ||
730 | return M_FONTDATA->Alloc(this); | |
731 | } | |
732 | ||
733 | bool wxFont::FreeResource(bool WXUNUSED(force)) | |
734 | { | |
735 | if ( GetResourceHandle() ) | |
736 | { | |
737 | M_FONTDATA->Free(); | |
738 | ||
739 | return TRUE; | |
740 | } | |
741 | ||
742 | return FALSE; | |
743 | } | |
744 | ||
745 | WXHANDLE wxFont::GetResourceHandle() const | |
746 | { | |
747 | return (WXHANDLE)GetHFONT(); | |
748 | } | |
749 | ||
750 | WXHFONT wxFont::GetHFONT() const | |
751 | { | |
752 | return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0; | |
753 | } | |
754 | ||
755 | bool wxFont::IsFree() const | |
756 | { | |
757 | return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0); | |
758 | } | |
759 | ||
760 | void wxFont::Unshare() | |
761 | { | |
762 | // Don't change shared data | |
763 | if ( !m_refData ) | |
764 | { | |
765 | m_refData = new wxFontRefData(); | |
766 | } | |
767 | else | |
768 | { | |
769 | wxFontRefData* ref = new wxFontRefData(*M_FONTDATA); | |
770 | UnRef(); | |
771 | m_refData = ref; | |
772 | } | |
773 | } | |
774 | ||
775 | // ---------------------------------------------------------------------------- | |
776 | // change font attribute: we recreate font when doing it | |
777 | // ---------------------------------------------------------------------------- | |
778 | ||
779 | void wxFont::SetPointSize(int pointSize) | |
780 | { | |
781 | Unshare(); | |
782 | ||
783 | M_FONTDATA->SetPointSize(pointSize); | |
784 | ||
785 | RealizeResource(); | |
786 | } | |
787 | ||
788 | void wxFont::SetFamily(int family) | |
789 | { | |
790 | Unshare(); | |
791 | ||
792 | M_FONTDATA->SetFamily(family); | |
793 | ||
794 | RealizeResource(); | |
795 | } | |
796 | ||
797 | void wxFont::SetStyle(int style) | |
798 | { | |
799 | Unshare(); | |
800 | ||
801 | M_FONTDATA->SetStyle(style); | |
802 | ||
803 | RealizeResource(); | |
804 | } | |
805 | ||
806 | void wxFont::SetWeight(int weight) | |
807 | { | |
808 | Unshare(); | |
809 | ||
810 | M_FONTDATA->SetWeight(weight); | |
811 | ||
812 | RealizeResource(); | |
813 | } | |
814 | ||
815 | void wxFont::SetFaceName(const wxString& faceName) | |
816 | { | |
817 | Unshare(); | |
818 | ||
819 | M_FONTDATA->SetFaceName(faceName); | |
820 | ||
821 | RealizeResource(); | |
822 | } | |
823 | ||
824 | void wxFont::SetUnderlined(bool underlined) | |
825 | { | |
826 | Unshare(); | |
827 | ||
828 | M_FONTDATA->SetUnderlined(underlined); | |
829 | ||
830 | RealizeResource(); | |
831 | } | |
832 | ||
833 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
834 | { | |
835 | Unshare(); | |
836 | ||
837 | M_FONTDATA->SetEncoding(encoding); | |
838 | ||
839 | RealizeResource(); | |
840 | } | |
841 | ||
842 | void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info) | |
843 | { | |
844 | Unshare(); | |
845 | ||
846 | FreeResource(); | |
847 | ||
848 | *M_FONTDATA = wxFontRefData(info); | |
849 | ||
850 | RealizeResource(); | |
851 | } | |
852 | ||
853 | // ---------------------------------------------------------------------------- | |
854 | // accessors | |
855 | // ---------------------------------------------------------------------------- | |
856 | ||
857 | int wxFont::GetPointSize() const | |
858 | { | |
859 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
860 | ||
861 | return M_FONTDATA->GetPointSize(); | |
862 | } | |
863 | ||
864 | int wxFont::GetFamily() const | |
865 | { | |
866 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
867 | ||
868 | return M_FONTDATA->GetFamily(); | |
869 | } | |
870 | ||
871 | int wxFont::GetStyle() const | |
872 | { | |
873 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
874 | ||
875 | return M_FONTDATA->GetStyle(); | |
876 | } | |
877 | ||
878 | int wxFont::GetWeight() const | |
879 | { | |
880 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
881 | ||
882 | return M_FONTDATA->GetWeight(); | |
883 | } | |
884 | ||
885 | bool wxFont::GetUnderlined() const | |
886 | { | |
887 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
888 | ||
889 | return M_FONTDATA->GetUnderlined(); | |
890 | } | |
891 | ||
892 | wxString wxFont::GetFaceName() const | |
893 | { | |
894 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); | |
895 | ||
896 | return M_FONTDATA->GetFaceName(); | |
897 | } | |
898 | ||
899 | wxFontEncoding wxFont::GetEncoding() const | |
900 | { | |
901 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
902 | ||
903 | return M_FONTDATA->GetEncoding(); | |
904 | } | |
905 | ||
906 | wxNativeFontInfo *wxFont::GetNativeFontInfo() const | |
907 | { | |
908 | if ( M_FONTDATA->HasNativeFontInfo() ) | |
909 | return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo()); | |
910 | ||
911 | return 0; | |
912 | } | |
913 | ||
914 | bool wxFont::IsFixedWidth() const | |
915 | { | |
916 | if ( M_FONTDATA->HasNativeFontInfo() ) | |
917 | { | |
918 | // the two low-order bits specify the pitch of the font, the rest is | |
919 | // family | |
920 | BYTE pitch = M_FONTDATA->GetNativeFontInfo(). | |
921 | lf.lfPitchAndFamily & PITCH_MASK; | |
922 | ||
923 | return pitch == FIXED_PITCH; | |
924 | } | |
925 | ||
926 | return wxFontBase::IsFixedWidth(); | |
927 | } | |
928 |