Define _CRT_NONSTDC_NO_WARNINGS for zlib compilation with MSVC.
[wxWidgets.git] / src / common / fontenumcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontenumcmn.cpp
3 // Purpose: wxFontEnumerator class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 7/5/2006
7 // Copyright: (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_FONTENUM
27
28 #include "wx/fontenum.h"
29
30 // ============================================================================
31 // implementation
32 // ============================================================================
33
34 // A simple wxFontEnumerator which doesn't perform any filtering and
35 // just returns all facenames and encodings found in the system
36 class WXDLLEXPORT wxSimpleFontEnumerator : public wxFontEnumerator
37 {
38 public:
39 wxSimpleFontEnumerator() { }
40
41 // called by EnumerateFacenames
42 virtual bool OnFacename(const wxString& facename)
43 {
44 m_arrFacenames.Add(facename);
45 return true;
46 }
47
48 // called by EnumerateEncodings
49 virtual bool OnFontEncoding(const wxString& WXUNUSED(facename),
50 const wxString& encoding)
51 {
52 m_arrEncodings.Add(encoding);
53 return true;
54 }
55
56 public:
57 wxArrayString m_arrFacenames, m_arrEncodings;
58 };
59
60
61 /* static */
62 wxArrayString wxFontEnumerator::GetFacenames(wxFontEncoding encoding, bool fixedWidthOnly)
63 {
64 wxSimpleFontEnumerator temp;
65 temp.EnumerateFacenames(encoding, fixedWidthOnly);
66 return temp.m_arrFacenames;
67 }
68
69 /* static */
70 wxArrayString wxFontEnumerator::GetEncodings(const wxString& facename)
71 {
72 wxSimpleFontEnumerator temp;
73 temp.EnumerateEncodings(facename);
74 return temp.m_arrEncodings;
75 }
76
77 /* static */
78 bool wxFontEnumerator::IsValidFacename(const wxString &facename)
79 {
80 // we cache the result of wxFontEnumerator::GetFacenames supposing that
81 // the array of face names won't change in the session of this program
82 static wxArrayString s_arr = wxFontEnumerator::GetFacenames();
83
84 #ifdef __WXMSW__
85 // Quoting the MSDN:
86 // "MS Shell Dlg is a mapping mechanism that enables
87 // U.S. English Microsoft Windows NT, and Microsoft Windows 2000 to
88 // support locales that have characters that are not contained in code
89 // page 1252. It is not a font but a face name for a nonexistent font."
90 // Thus we need to consider "Ms Shell Dlg" and "Ms Shell Dlg 2" as valid
91 // font face names even if they are not enumerated by wxFontEnumerator
92 if (facename.IsSameAs(wxT("Ms Shell Dlg"), false) ||
93 facename.IsSameAs(wxT("Ms Shell Dlg 2"), false))
94 return true;
95 #endif
96
97 // is given font face name a valid one ?
98 if (s_arr.Index(facename, false) == wxNOT_FOUND)
99 return false;
100
101 return true;
102 }
103
104 #ifdef wxHAS_UTF8_FONTS
105 bool wxFontEnumerator::EnumerateEncodingsUTF8(const wxString& facename)
106 {
107 // name of UTF-8 encoding: no need to use wxFontMapper for it as it's
108 // unlikely to change
109 const wxString utf8(wxT("UTF-8"));
110
111 // all fonts are in UTF-8 only if this code is used
112 if ( !facename.empty() )
113 {
114 OnFontEncoding(facename, utf8);
115 return true;
116 }
117
118 // so enumerating all facenames supporting this encoding is the same as
119 // enumerating all facenames
120 const wxArrayString facenames(GetFacenames(wxFONTENCODING_UTF8));
121 const size_t count = facenames.size();
122 if ( !count )
123 return false;
124
125 for ( size_t n = 0; n < count; n++ )
126 {
127 OnFontEncoding(facenames[n], utf8);
128 }
129
130 return true;
131 }
132 #endif // wxHAS_UTF8_FONTS
133
134 #endif // wxUSE_FONTENUM