1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/font.cpp
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
34 #include "wx/encinfo.h"
35 #include "wx/scopeguard.h"
38 #include "wx/msw/private.h"
40 #include "wx/fontutil.h"
41 #include "wx/fontmap.h"
44 #include "wx/sysopt.h"
47 #include "wx/tokenzr.h"
49 #if wxUSE_EXTENDED_RTTI
51 wxBEGIN_ENUM( wxFontFamily
)
52 wxENUM_MEMBER( wxDEFAULT
)
53 wxENUM_MEMBER( wxDECORATIVE
)
54 wxENUM_MEMBER( wxROMAN
)
55 wxENUM_MEMBER( wxSCRIPT
)
56 wxENUM_MEMBER( wxSWISS
)
57 wxENUM_MEMBER( wxMODERN
)
58 wxENUM_MEMBER( wxTELETYPE
)
59 wxEND_ENUM( wxFontFamily
)
61 wxBEGIN_ENUM( wxFontStyle
)
62 wxENUM_MEMBER( wxNORMAL
)
63 wxENUM_MEMBER( wxITALIC
)
64 wxENUM_MEMBER( wxSLANT
)
65 wxEND_ENUM( wxFontStyle
)
67 wxBEGIN_ENUM( wxFontWeight
)
68 wxENUM_MEMBER( wxNORMAL
)
69 wxENUM_MEMBER( wxLIGHT
)
70 wxENUM_MEMBER( wxBOLD
)
71 wxEND_ENUM( wxFontWeight
)
73 IMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont
, wxGDIObject
,"wx/font.h")
75 wxBEGIN_PROPERTIES_TABLE(wxFont
)
76 wxPROPERTY( Size
,int, SetPointSize
, GetPointSize
, 12 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 wxPROPERTY( Family
, int , SetFamily
, GetFamily
, (int)wxDEFAULT
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontFamily
78 wxPROPERTY( Style
, int , SetStyle
, GetStyle
, (int)wxNORMAL
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontStyle
79 wxPROPERTY( Weight
, int , SetWeight
, GetWeight
, (int)wxNORMAL
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontWeight
80 wxPROPERTY( Underlined
, bool , SetUnderlined
, GetUnderlined
, false , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
81 wxPROPERTY( Face
, wxString
, SetFaceName
, GetFaceName
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
82 wxPROPERTY( Encoding
, wxFontEncoding
, SetEncoding
, GetEncoding
, wxFONTENCODING_DEFAULT
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
83 wxEND_PROPERTIES_TABLE()
85 wxCONSTRUCTOR_6( wxFont
, int , Size
, int , Family
, int , Style
, int , Weight
, bool , Underlined
, wxString
, Face
)
87 wxBEGIN_HANDLERS_TABLE(wxFont
)
88 wxEND_HANDLERS_TABLE()
91 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // the mask used to extract the pitch from LOGFONT::lfPitchAndFamily field
100 static const int PITCH_MASK
= FIXED_PITCH
| VARIABLE_PITCH
;
102 // ----------------------------------------------------------------------------
103 // wxFontRefData - the internal description of the font
104 // ----------------------------------------------------------------------------
106 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
112 Init(-1, wxSize(0,0), false, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
113 wxFONTWEIGHT_NORMAL
, false, wxEmptyString
,
114 wxFONTENCODING_DEFAULT
);
117 wxFontRefData(int size
,
118 const wxSize
& pixelSize
,
119 bool sizeUsingPixels
,
124 const wxString
& faceName
,
125 wxFontEncoding encoding
)
127 Init(size
, pixelSize
, sizeUsingPixels
, family
, style
, weight
,
128 underlined
, faceName
, encoding
);
131 wxFontRefData(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0)
136 wxFontRefData(const wxFontRefData
& data
) : wxGDIRefData()
138 Init(data
.m_nativeFontInfo
);
141 virtual ~wxFontRefData();
148 // all wxFont accessors
149 int GetPointSize() const
151 return m_nativeFontInfo
.GetPointSize();
154 wxSize
GetPixelSize() const
156 return m_nativeFontInfo
.GetPixelSize();
159 bool IsUsingSizeInPixels() const
161 return m_sizeUsingPixels
;
164 wxFontFamily
GetFamily() const
166 return m_nativeFontInfo
.GetFamily();
169 wxFontStyle
GetStyle() const
171 return m_nativeFontInfo
.GetStyle();
174 wxFontWeight
GetWeight() const
176 return m_nativeFontInfo
.GetWeight();
179 bool GetUnderlined() const
181 return m_nativeFontInfo
.GetUnderlined();
184 wxString
GetFaceName() const
186 wxString facename
= m_nativeFontInfo
.GetFaceName();
187 if ( facename
.empty() )
189 facename
= GetMSWFaceName();
190 if ( !facename
.empty() )
192 // cache the face name, it shouldn't change unless the family
193 // does and wxNativeFontInfo::SetFamily() resets the face name
194 const_cast<wxFontRefData
*>(this)->SetFaceName(facename
);
201 wxFontEncoding
GetEncoding() const
203 return m_nativeFontInfo
.GetEncoding();
206 WXHFONT
GetHFONT() const
209 const_cast<wxFontRefData
*>(this)->Alloc();
211 return (WXHFONT
)m_hFont
;
214 bool HasHFONT() const
219 // ... and setters: notice that all of them invalidate the currently
220 // allocated HFONT, if any, so that the next call to GetHFONT() recreates a
222 void SetPointSize(int pointSize
)
226 m_nativeFontInfo
.SetPointSize(pointSize
);
227 m_sizeUsingPixels
= false;
230 void SetPixelSize(const wxSize
& pixelSize
)
232 wxCHECK_RET( pixelSize
.GetWidth() >= 0, "negative font width" );
233 wxCHECK_RET( pixelSize
.GetHeight() != 0, "zero font height" );
237 m_nativeFontInfo
.SetPixelSize(pixelSize
);
238 m_sizeUsingPixels
= true;
241 void SetFamily(wxFontFamily family
)
245 m_nativeFontInfo
.SetFamily(family
);
248 void SetStyle(wxFontStyle style
)
252 m_nativeFontInfo
.SetStyle(style
);
255 void SetWeight(wxFontWeight weight
)
259 m_nativeFontInfo
.SetWeight(weight
);
262 bool SetFaceName(const wxString
& faceName
)
266 return m_nativeFontInfo
.SetFaceName(faceName
);
269 void SetUnderlined(bool underlined
)
273 m_nativeFontInfo
.SetUnderlined(underlined
);
276 void SetEncoding(wxFontEncoding encoding
)
280 m_nativeFontInfo
.SetEncoding(encoding
);
283 const wxNativeFontInfo
& GetNativeFontInfo() const
284 { return m_nativeFontInfo
; }
286 void SetNativeFontInfo(const wxNativeFontInfo
& nativeFontInfo
)
290 m_nativeFontInfo
= nativeFontInfo
;
294 // common part of all ctors
296 const wxSize
& pixelSize
,
297 bool sizeUsingPixels
,
302 const wxString
& faceName
,
303 wxFontEncoding encoding
);
305 void Init(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0);
307 // retrieve the face name really being used by the font: this is used to
308 // get the face name selected by the system when we don't specify it (but
309 // use just the family for example)
310 wxString
GetMSWFaceName() const
313 SelectInHDC
selectFont(hdc
, m_hFont
);
315 UINT otmSize
= GetOutlineTextMetrics(hdc
, 0, NULL
);
318 wxLogLastError("GetOutlineTextMetrics(NULL)");
322 OUTLINETEXTMETRIC
* const
323 otm
= static_cast<OUTLINETEXTMETRIC
*>(malloc(otmSize
));
324 wxON_BLOCK_EXIT1( free
, otm
);
326 otm
->otmSize
= otmSize
;
327 if ( !GetOutlineTextMetrics(hdc
, otmSize
, otm
) )
329 wxLogLastError("GetOutlineTextMetrics()");
333 // in spit of its type, the otmpFaceName field of OUTLINETEXTMETRIC
334 // gives an offset in _bytes_ of the face name from the struct start
335 // while the name itself is an array of TCHARs
336 return reinterpret_cast<wxChar
*>(otm
) +
337 wxPtrToUInt(otm
->otmpFaceName
)/sizeof(wxChar
);
340 // are we using m_nativeFontInfo.lf.lfHeight for point size or pixel size?
341 bool m_sizeUsingPixels
;
343 // Windows font handle, created on demand in GetHFONT()
347 wxNativeFontInfo m_nativeFontInfo
;
350 #define M_FONTDATA ((wxFontRefData*)m_refData)
352 // ============================================================================
354 // ============================================================================
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
360 void wxFontRefData::Init(int pointSize
,
361 const wxSize
& pixelSize
,
362 bool sizeUsingPixels
,
367 const wxString
& faceName
,
368 wxFontEncoding encoding
)
372 m_sizeUsingPixels
= sizeUsingPixels
;
373 if ( m_sizeUsingPixels
)
374 SetPixelSize(pixelSize
);
376 SetPointSize(pointSize
);
380 SetUnderlined(underlined
);
382 // set the family/facename
384 if ( !faceName
.empty() )
385 SetFaceName(faceName
);
387 // deal with encoding now (it may override the font family and facename
388 // so do it after setting them)
389 SetEncoding(encoding
);
392 void wxFontRefData::Init(const wxNativeFontInfo
& info
, WXHFONT hFont
)
394 // hFont may be zero, or it be passed in case we really want to
395 // use the exact font created in the underlying system
396 // (for example where we can't guarantee conversion from HFONT
397 // to LOGFONT back to HFONT)
398 m_hFont
= (HFONT
)hFont
;
399 m_nativeFontInfo
= info
;
401 // TODO: m_sizeUsingPixels?
404 wxFontRefData::~wxFontRefData()
409 bool wxFontRefData::Alloc()
411 m_hFont
= ::CreateFontIndirect(&m_nativeFontInfo
.lf
);
414 wxLogLastError(wxT("CreateFont"));
421 void wxFontRefData::Free()
425 if ( !::DeleteObject(m_hFont
) )
427 wxLogLastError(wxT("DeleteObject(font)"));
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
438 void wxNativeFontInfo::Init()
442 // we get better font quality if we use PROOF_QUALITY instead of
443 // DEFAULT_QUALITY but some fonts (e.g. "Terminal 6pt") are not available
444 // then so we allow to set a global option to choose between quality and
445 // wider font selection
447 lf
.lfQuality
= CLEARTYPE_QUALITY
;
449 lf
.lfQuality
= wxSystemOptions::GetOptionInt("msw.font.no-proof-quality")
455 int wxNativeFontInfo::GetPointSize() const
457 // FIXME: using the screen here results in incorrect font size calculation
459 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
461 // BC++ 2007 doesn't provide abs(long) overload, hence the cast
462 return (int) (((72.0*abs((int)lf
.lfHeight
)) / (double) ppInch
) + 0.5);
465 wxSize
wxNativeFontInfo::GetPixelSize() const
468 ret
.SetHeight(abs((int)lf
.lfHeight
));
469 ret
.SetWidth(lf
.lfWidth
);
473 wxFontStyle
wxNativeFontInfo::GetStyle() const
475 return lf
.lfItalic
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
478 wxFontWeight
wxNativeFontInfo::GetWeight() const
480 if ( lf
.lfWeight
<= 300 )
481 return wxFONTWEIGHT_LIGHT
;
483 if ( lf
.lfWeight
>= 600 )
484 return wxFONTWEIGHT_BOLD
;
486 return wxFONTWEIGHT_NORMAL
;
489 bool wxNativeFontInfo::GetUnderlined() const
491 return lf
.lfUnderline
!= 0;
494 wxString
wxNativeFontInfo::GetFaceName() const
496 return lf
.lfFaceName
;
499 wxFontFamily
wxNativeFontInfo::GetFamily() const
503 // extract family from pitch-and-family
504 switch ( lf
.lfPitchAndFamily
& ~PITCH_MASK
)
507 family
= wxFONTFAMILY_UNKNOWN
;
511 family
= wxFONTFAMILY_ROMAN
;
515 family
= wxFONTFAMILY_SWISS
;
519 family
= wxFONTFAMILY_SCRIPT
;
523 family
= wxFONTFAMILY_MODERN
;
527 family
= wxFONTFAMILY_DECORATIVE
;
531 wxFAIL_MSG( "unknown LOGFONT::lfFamily value" );
532 family
= wxFONTFAMILY_UNKNOWN
;
533 // just to avoid a warning
539 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
541 return wxGetFontEncFromCharSet(lf
.lfCharSet
);
544 void wxNativeFontInfo::SetPointSize(int pointsize
)
546 // FIXME: using the screen here results in incorrect font size calculation
548 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
550 lf
.lfHeight
= -(int)((pointsize
*((double)ppInch
)/72.0) + 0.5);
553 void wxNativeFontInfo::SetPixelSize(const wxSize
& pixelSize
)
555 // MSW accepts both positive and negative heights here but they mean
556 // different things: positive specifies the cell height while negative
557 // specifies the character height. We used to just pass the value to MSW
558 // unchanged but changed the behaviour for positive values in 2.9.1 to
559 // match other ports and, more importantly, the expected behaviour. So now
560 // passing the negative height doesn't make sense at all any more but we
561 // still accept it for compatibility with the existing code which worked
562 // around the wrong interpretation of the height argument in older wxMSW
563 // versions by passing a negative value explicitly itself.
564 lf
.lfHeight
= -abs(pixelSize
.GetHeight());
565 lf
.lfWidth
= pixelSize
.GetWidth();
568 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
573 wxFAIL_MSG( "unknown font style" );
576 case wxFONTSTYLE_NORMAL
:
580 case wxFONTSTYLE_ITALIC
:
581 case wxFONTSTYLE_SLANT
:
587 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
592 wxFAIL_MSG( "unknown font weight" );
595 case wxFONTWEIGHT_NORMAL
:
596 lf
.lfWeight
= FW_NORMAL
;
599 case wxFONTWEIGHT_LIGHT
:
600 lf
.lfWeight
= FW_LIGHT
;
603 case wxFONTWEIGHT_BOLD
:
604 lf
.lfWeight
= FW_BOLD
;
609 void wxNativeFontInfo::SetUnderlined(bool underlined
)
611 lf
.lfUnderline
= underlined
;
614 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
616 wxStrlcpy(lf
.lfFaceName
, facename
.c_str(), WXSIZEOF(lf
.lfFaceName
));
620 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
623 wxArrayString facename
;
625 // the list of fonts associated with a family was partially
626 // taken from http://www.codestyle.org/css/font-family
630 case wxFONTFAMILY_SCRIPT
:
631 ff_family
= FF_SCRIPT
;
632 facename
.Add(wxS("Script"));
633 facename
.Add(wxS("Brush Script MT"));
634 facename
.Add(wxS("Comic Sans MS"));
635 facename
.Add(wxS("Lucida Handwriting"));
638 case wxFONTFAMILY_DECORATIVE
:
639 ff_family
= FF_DECORATIVE
;
640 facename
.Add(wxS("Old English Text MT"));
641 facename
.Add(wxS("Comic Sans MS"));
642 facename
.Add(wxS("Lucida Handwriting"));
645 case wxFONTFAMILY_ROMAN
:
646 ff_family
= FF_ROMAN
;
647 facename
.Add(wxS("Times New Roman"));
648 facename
.Add(wxS("Georgia"));
649 facename
.Add(wxS("Garamond"));
650 facename
.Add(wxS("Bookman Old Style"));
651 facename
.Add(wxS("Book Antiqua"));
654 case wxFONTFAMILY_TELETYPE
:
655 case wxFONTFAMILY_MODERN
:
656 ff_family
= FF_MODERN
;
657 facename
.Add(wxS("Courier New"));
658 facename
.Add(wxS("Lucida Console"));
659 facename
.Add(wxS("Andale Mono"));
660 facename
.Add(wxS("OCR A Extended"));
661 facename
.Add(wxS("Terminal"));
664 case wxFONTFAMILY_SWISS
:
665 ff_family
= FF_SWISS
;
666 facename
.Add(wxS("Arial"));
667 facename
.Add(wxS("Century Gothic"));
668 facename
.Add(wxS("Lucida Sans Unicode"));
669 facename
.Add(wxS("Tahoma"));
670 facename
.Add(wxS("Trebuchet MS"));
671 facename
.Add(wxS("Verdana"));
674 case wxFONTFAMILY_DEFAULT
:
677 // We want Windows 2000 or later to have new fonts even MS Shell Dlg
678 // is returned as default GUI font for compatibility
680 ff_family
= FF_SWISS
;
681 if(wxGetOsVersion(&verMaj
) == wxOS_WINDOWS_NT
&& verMaj
>= 5)
682 facename
.Add(wxS("MS Shell Dlg 2"));
684 facename
.Add(wxS("MS Shell Dlg"));
687 // "MS Shell Dlg is a mapping mechanism that enables
688 // U.S. English Microsoft Windows NT, and Microsoft Windows 2000 to
689 // support locales that have characters that are not contained in code
690 // page 1252. It is not a font but a face name for a nonexistent font."
694 lf
.lfPitchAndFamily
= (BYTE
)(DEFAULT_PITCH
) | ff_family
;
696 // reset the facename so that CreateFontIndirect() will automatically choose a
697 // face name based only on the font family.
698 lf
.lfFaceName
[0] = '\0';
701 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
703 wxNativeEncodingInfo info
;
704 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
707 if ( wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
709 if ( !info
.facename
.empty() )
711 // if we have this encoding only in some particular facename, use
712 // the facename - it is better to show the correct characters in a
713 // wrong facename than unreadable text in a correct one
714 SetFaceName(info
.facename
);
718 #endif // wxUSE_FONTMAP
720 // unsupported encoding, replace with the default
721 info
.charset
= DEFAULT_CHARSET
;
725 lf
.lfCharSet
= (BYTE
)info
.charset
;
728 bool wxNativeFontInfo::FromString(const wxString
& s
)
732 wxStringTokenizer
tokenizer(s
, wxS(";"), wxTOKEN_RET_EMPTY_ALL
);
735 wxString token
= tokenizer
.GetNextToken();
736 if ( token
!= wxS('0') )
739 token
= tokenizer
.GetNextToken();
740 if ( !token
.ToLong(&l
) )
744 token
= tokenizer
.GetNextToken();
745 if ( !token
.ToLong(&l
) )
749 token
= tokenizer
.GetNextToken();
750 if ( !token
.ToLong(&l
) )
754 token
= tokenizer
.GetNextToken();
755 if ( !token
.ToLong(&l
) )
757 lf
.lfOrientation
= l
;
759 token
= tokenizer
.GetNextToken();
760 if ( !token
.ToLong(&l
) )
764 token
= tokenizer
.GetNextToken();
765 if ( !token
.ToLong(&l
) )
767 lf
.lfItalic
= (BYTE
)l
;
769 token
= tokenizer
.GetNextToken();
770 if ( !token
.ToLong(&l
) )
772 lf
.lfUnderline
= (BYTE
)l
;
774 token
= tokenizer
.GetNextToken();
775 if ( !token
.ToLong(&l
) )
777 lf
.lfStrikeOut
= (BYTE
)l
;
779 token
= tokenizer
.GetNextToken();
780 if ( !token
.ToLong(&l
) )
782 lf
.lfCharSet
= (BYTE
)l
;
784 token
= tokenizer
.GetNextToken();
785 if ( !token
.ToLong(&l
) )
787 lf
.lfOutPrecision
= (BYTE
)l
;
789 token
= tokenizer
.GetNextToken();
790 if ( !token
.ToLong(&l
) )
792 lf
.lfClipPrecision
= (BYTE
)l
;
794 token
= tokenizer
.GetNextToken();
795 if ( !token
.ToLong(&l
) )
797 lf
.lfQuality
= (BYTE
)l
;
799 token
= tokenizer
.GetNextToken();
800 if ( !token
.ToLong(&l
) )
802 lf
.lfPitchAndFamily
= (BYTE
)l
;
804 if ( !tokenizer
.HasMoreTokens() )
807 // the face name may be empty
808 wxStrcpy(lf
.lfFaceName
, tokenizer
.GetNextToken());
813 wxString
wxNativeFontInfo::ToString() const
817 s
.Printf(wxS("%d;%ld;%ld;%ld;%ld;%ld;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
818 0, // version, in case we want to change the format later
837 // ----------------------------------------------------------------------------
839 // ----------------------------------------------------------------------------
841 wxFont::wxFont(const wxString
& fontdesc
)
843 wxNativeFontInfo info
;
844 if ( info
.FromString(fontdesc
) )
848 bool wxFont::Create(const wxNativeFontInfo
& info
, WXHFONT hFont
)
852 m_refData
= new wxFontRefData(info
, hFont
);
854 return RealizeResource();
857 bool wxFont::DoCreate(int pointSize
,
858 const wxSize
& pixelSize
,
859 bool sizeUsingPixels
,
864 const wxString
& faceName
,
865 wxFontEncoding encoding
)
869 // wxDEFAULT is a valid value for the font size too so we must treat it
870 // specially here (otherwise the size would be 70 == wxDEFAULT value)
871 if ( pointSize
== wxDEFAULT
)
873 pointSize
= wxNORMAL_FONT
->GetPointSize();
876 m_refData
= new wxFontRefData(pointSize
, pixelSize
, sizeUsingPixels
,
877 family
, style
, weight
,
878 underlined
, faceName
, encoding
);
880 return RealizeResource();
887 // ----------------------------------------------------------------------------
888 // real implementation
889 // ----------------------------------------------------------------------------
891 wxGDIRefData
*wxFont::CreateGDIRefData() const
893 return new wxFontRefData();
896 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
898 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
901 bool wxFont::RealizeResource()
903 // NOTE: the GetHFONT() call automatically triggers a reallocation of
904 // the HFONT if necessary (will do nothing if we already have the resource);
905 // it returns NULL only if there is a failure in wxFontRefData::Alloc()...
906 return GetHFONT() != NULL
;
909 bool wxFont::FreeResource(bool WXUNUSED(force
))
919 WXHANDLE
wxFont::GetResourceHandle() const
921 return (WXHANDLE
)GetHFONT();
924 WXHFONT
wxFont::GetHFONT() const
926 // NOTE: wxFontRefData::GetHFONT() will automatically call
927 // wxFontRefData::Alloc() if necessary
928 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
931 bool wxFont::IsFree() const
933 return M_FONTDATA
&& !M_FONTDATA
->HasHFONT();
936 // ----------------------------------------------------------------------------
937 // change font attribute: we recreate font when doing it
938 // ----------------------------------------------------------------------------
940 void wxFont::SetPointSize(int pointSize
)
945 M_FONTDATA
->SetPointSize(pointSize
);
948 void wxFont::SetPixelSize(const wxSize
& pixelSize
)
952 M_FONTDATA
->SetPixelSize(pixelSize
);
955 void wxFont::SetFamily(wxFontFamily family
)
959 M_FONTDATA
->SetFamily(family
);
962 void wxFont::SetStyle(wxFontStyle style
)
966 M_FONTDATA
->SetStyle(style
);
969 void wxFont::SetWeight(wxFontWeight weight
)
973 M_FONTDATA
->SetWeight(weight
);
976 bool wxFont::SetFaceName(const wxString
& faceName
)
980 if ( !M_FONTDATA
->SetFaceName(faceName
) )
983 // NB: using win32's GetObject() API on M_FONTDATA->GetHFONT()
984 // to retrieve a LOGFONT and then compare lf.lfFaceName
985 // with given facename is not reliable at all:
986 // Windows copies the facename given to ::CreateFontIndirect()
987 // without any validity check.
988 // Thus we use wxFontBase::SetFaceName to check if facename
990 return wxFontBase::SetFaceName(faceName
);
993 void wxFont::SetUnderlined(bool underlined
)
997 M_FONTDATA
->SetUnderlined(underlined
);
1000 void wxFont::SetEncoding(wxFontEncoding encoding
)
1004 M_FONTDATA
->SetEncoding(encoding
);
1007 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
1011 M_FONTDATA
->SetNativeFontInfo(info
);
1014 // ----------------------------------------------------------------------------
1016 // ----------------------------------------------------------------------------
1018 int wxFont::GetPointSize() const
1020 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
1022 return M_FONTDATA
->GetPointSize();
1025 wxSize
wxFont::GetPixelSize() const
1027 wxCHECK_MSG( IsOk(), wxDefaultSize
, wxT("invalid font") );
1029 return M_FONTDATA
->GetPixelSize();
1032 bool wxFont::IsUsingSizeInPixels() const
1034 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
1036 return M_FONTDATA
->IsUsingSizeInPixels();
1039 wxFontFamily
wxFont::GetFamily() const
1041 wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX
, wxT("invalid font") );
1043 return M_FONTDATA
->GetFamily();
1046 wxFontStyle
wxFont::GetStyle() const
1048 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX
, wxT("invalid font") );
1050 return M_FONTDATA
->GetStyle();
1053 wxFontWeight
wxFont::GetWeight() const
1055 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
1057 return M_FONTDATA
->GetWeight();
1060 bool wxFont::GetUnderlined() const
1062 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
1064 return M_FONTDATA
->GetUnderlined();
1067 wxString
wxFont::GetFaceName() const
1069 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
1071 return M_FONTDATA
->GetFaceName();
1074 wxFontEncoding
wxFont::GetEncoding() const
1076 wxCHECK_MSG( IsOk(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
1078 return M_FONTDATA
->GetEncoding();
1081 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
1083 return IsOk() ? &(M_FONTDATA
->GetNativeFontInfo()) : NULL
;
1086 wxString
wxFont::GetNativeFontInfoDesc() const
1088 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
1090 // be sure we have an HFONT associated...
1091 const_cast<wxFont
*>(this)->RealizeResource();
1092 return wxFontBase::GetNativeFontInfoDesc();
1095 wxString
wxFont::GetNativeFontInfoUserDesc() const
1097 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
1099 // be sure we have an HFONT associated...
1100 const_cast<wxFont
*>(this)->RealizeResource();
1101 return wxFontBase::GetNativeFontInfoUserDesc();
1104 bool wxFont::IsFixedWidth() const
1106 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
1108 // the two low-order bits specify the pitch of the font, the rest is
1111 (BYTE
)(M_FONTDATA
->GetNativeFontInfo().lf
.lfPitchAndFamily
& PITCH_MASK
);
1113 return pitch
== FIXED_PITCH
;