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