]>
Commit | Line | Data |
---|---|---|
a1d58ddc VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/fontenum.cpp | |
543f08a6 JS |
3 | // Purpose: wxFontEnumerator class for Windows |
4 | // Author: Julian Smart | |
a1d58ddc | 5 | // Modified by: Vadim Zeitlin to add support for font encodings |
543f08a6 JS |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
a1d58ddc VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "fontenum.h" | |
22 | #endif | |
543f08a6 JS |
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 | ||
1e6feb95 VZ |
31 | #if wxUSE_FONTMAP |
32 | ||
543f08a6 JS |
33 | #ifndef WX_PRECOMP |
34 | #include "wx/font.h" | |
35 | #endif | |
36 | ||
76e23cdb | 37 | #include "wx/fontutil.h" |
a1d58ddc | 38 | #include "wx/fontenum.h" |
11c7d5b6 | 39 | #include "wx/fontmap.h" |
543f08a6 | 40 | |
a1d58ddc VZ |
41 | #include "wx/msw/private.h" |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // private classes | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
f6bcfd97 BP |
47 | // the helper class which calls ::EnumFontFamilies() and whose OnFont() is |
48 | // called from the callback passed to this function and, in its turn, calls the | |
49 | // appropariate wxFontEnumerator method | |
a1d58ddc | 50 | class wxFontEnumeratorHelper |
543f08a6 | 51 | { |
a1d58ddc VZ |
52 | public: |
53 | wxFontEnumeratorHelper(wxFontEnumerator *fontEnum); | |
54 | ||
55 | // control what exactly are we enumerating | |
f6bcfd97 | 56 | // we enumerate fonts with given enocding |
a1d58ddc | 57 | bool SetEncoding(wxFontEncoding encoding); |
f6bcfd97 BP |
58 | // we enumerate fixed-width fonts |
59 | void SetFixedOnly(bool fixedOnly) { m_fixedOnly = fixedOnly; } | |
60 | // we enumerate the encodings available in this family | |
61 | void SetFamily(const wxString& family); | |
a1d58ddc VZ |
62 | |
63 | // call to start enumeration | |
64 | void DoEnumerate(); | |
65 | ||
66 | // called by our font enumeration proc | |
67 | bool OnFont(const LPLOGFONT lf, const LPTEXTMETRIC tm) const; | |
68 | ||
69 | private: | |
70 | // the object we forward calls to OnFont() to | |
71 | wxFontEnumerator *m_fontEnum; | |
543f08a6 | 72 | |
a1d58ddc VZ |
73 | // if != -1, enum only fonts which have this encoding |
74 | int m_charset; | |
543f08a6 | 75 | |
11c7d5b6 VZ |
76 | // if not empty, enum only the fonts with this facename |
77 | wxString m_facename; | |
78 | ||
f6bcfd97 BP |
79 | // if not empty, enum only the fonts in this family |
80 | wxString m_family; | |
81 | ||
a1d58ddc VZ |
82 | // if TRUE, enum only fixed fonts |
83 | bool m_fixedOnly; | |
f6bcfd97 BP |
84 | |
85 | // if TRUE, we enumerate the encodings, not fonts | |
86 | bool m_enumEncodings; | |
87 | ||
88 | // the list of charsets we already found while enumerating charsets | |
89 | wxArrayInt m_charsets; | |
a1d58ddc | 90 | }; |
543f08a6 | 91 | |
a1d58ddc VZ |
92 | // ---------------------------------------------------------------------------- |
93 | // private functions | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm, | |
97 | DWORD dwStyle, LONG lParam); | |
98 | ||
99 | // ============================================================================ | |
100 | // implementation | |
101 | // ============================================================================ | |
102 | ||
103 | // ---------------------------------------------------------------------------- | |
104 | // wxFontEnumeratorHelper | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
107 | wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator *fontEnum) | |
108 | { | |
109 | m_fontEnum = fontEnum; | |
14647781 | 110 | m_charset = DEFAULT_CHARSET; |
a1d58ddc | 111 | m_fixedOnly = FALSE; |
f6bcfd97 BP |
112 | m_enumEncodings = FALSE; |
113 | } | |
114 | ||
115 | void wxFontEnumeratorHelper::SetFamily(const wxString& family) | |
116 | { | |
117 | m_enumEncodings = TRUE; | |
118 | m_family = family; | |
543f08a6 JS |
119 | } |
120 | ||
a1d58ddc VZ |
121 | bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding) |
122 | { | |
11c7d5b6 VZ |
123 | wxNativeEncodingInfo info; |
124 | if ( !wxGetNativeFontEncoding(encoding, &info) ) | |
a1d58ddc | 125 | { |
11c7d5b6 VZ |
126 | if ( !wxTheFontMapper->GetAltForEncoding(encoding, &info) ) |
127 | { | |
128 | // no such encodings at all | |
129 | return FALSE; | |
130 | } | |
a1d58ddc | 131 | } |
024fb9b9 RD |
132 | m_charset = info.charset; |
133 | m_facename = info.facename; | |
543f08a6 | 134 | |
11c7d5b6 | 135 | return TRUE; |
a1d58ddc VZ |
136 | } |
137 | ||
ae090fdb | 138 | #if defined(__GNUWIN32__) && !defined(__CYGWIN10__) |
f6bcfd97 | 139 | #if wxUSE_NORLANDER_HEADERS |
161f4f73 | 140 | #define wxFONTENUMPROC int(*)(const LOGFONT *, const TEXTMETRIC *, long unsigned int, LPARAM) |
c5c32d72 VZ |
141 | #else |
142 | #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM) | |
143 | #endif | |
01dba85a | 144 | #else |
9f83044f | 145 | #define wxFONTENUMPROC FONTENUMPROC |
01dba85a JS |
146 | #endif |
147 | ||
a1d58ddc | 148 | void wxFontEnumeratorHelper::DoEnumerate() |
543f08a6 | 149 | { |
a1d58ddc | 150 | HDC hDC = ::GetDC(NULL); |
543f08a6 | 151 | |
543f08a6 | 152 | #ifdef __WIN32__ |
a1d58ddc VZ |
153 | LOGFONT lf; |
154 | lf.lfCharSet = m_charset; | |
11c7d5b6 | 155 | wxStrncpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName)); |
a1d58ddc | 156 | lf.lfPitchAndFamily = 0; |
01dba85a | 157 | ::EnumFontFamiliesEx(hDC, &lf, (wxFONTENUMPROC)wxFontEnumeratorProc, |
a1d58ddc VZ |
158 | (LPARAM)this, 0 /* reserved */) ; |
159 | #else // Win16 | |
160 | ::EnumFonts(hDC, (LPTSTR)NULL, (FONTENUMPROC)wxFontEnumeratorProc, | |
8f177c8e VZ |
161 | #ifdef STRICT |
162 | (LPARAM) | |
163 | #else | |
164 | (LPSTR) | |
165 | #endif | |
166 | this); | |
a1d58ddc | 167 | #endif // Win32/16 |
543f08a6 | 168 | |
a1d58ddc VZ |
169 | ::ReleaseDC(NULL, hDC); |
170 | } | |
171 | ||
172 | bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf, | |
173 | const LPTEXTMETRIC tm) const | |
174 | { | |
f6bcfd97 BP |
175 | if ( m_enumEncodings ) |
176 | { | |
177 | // is this a new charset? | |
178 | int cs = lf->lfCharSet; | |
179 | if ( m_charsets.Index(cs) == wxNOT_FOUND ) | |
180 | { | |
181 | wxConstCast(this, wxFontEnumeratorHelper)->m_charsets.Add(cs); | |
182 | ||
183 | wxFontEncoding enc = wxGetFontEncFromCharSet(cs); | |
14647781 | 184 | return m_fontEnum->OnFontEncoding(lf->lfFaceName, |
f6bcfd97 BP |
185 | wxFontMapper::GetEncodingName(enc)); |
186 | } | |
187 | else | |
188 | { | |
189 | // continue enumeration | |
190 | return TRUE; | |
191 | } | |
192 | } | |
193 | ||
a1d58ddc VZ |
194 | if ( m_fixedOnly ) |
195 | { | |
196 | // check that it's a fixed pitch font (there is *no* error here, the | |
197 | // flag name is misleading!) | |
198 | if ( tm->tmPitchAndFamily & TMPF_FIXED_PITCH ) | |
199 | { | |
200 | // not a fixed pitch font | |
201 | return TRUE; | |
202 | } | |
203 | } | |
204 | ||
205 | if ( m_charset != -1 ) | |
206 | { | |
207 | // check that we have the right encoding | |
208 | if ( lf->lfCharSet != m_charset ) | |
209 | { | |
210 | return TRUE; | |
211 | } | |
212 | } | |
213 | ||
3c1866e8 | 214 | return m_fontEnum->OnFacename(lf->lfFaceName); |
a1d58ddc VZ |
215 | } |
216 | ||
217 | // ---------------------------------------------------------------------------- | |
218 | // wxFontEnumerator | |
219 | // ---------------------------------------------------------------------------- | |
220 | ||
3c1866e8 VZ |
221 | bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding, |
222 | bool fixedWidthOnly) | |
543f08a6 | 223 | { |
a1d58ddc VZ |
224 | wxFontEnumeratorHelper fe(this); |
225 | if ( fe.SetEncoding(encoding) ) | |
226 | { | |
227 | fe.SetFixedOnly(fixedWidthOnly); | |
228 | ||
229 | fe.DoEnumerate(); | |
230 | } | |
231 | // else: no such fonts, unknown encoding | |
232 | ||
543f08a6 JS |
233 | return TRUE; |
234 | } | |
235 | ||
a1d58ddc VZ |
236 | bool wxFontEnumerator::EnumerateEncodings(const wxString& family) |
237 | { | |
f6bcfd97 BP |
238 | wxFontEnumeratorHelper fe(this); |
239 | fe.SetFamily(family); | |
240 | fe.DoEnumerate(); | |
a1d58ddc VZ |
241 | |
242 | return TRUE; | |
243 | } | |
244 | ||
245 | // ---------------------------------------------------------------------------- | |
246 | // Windows callbacks | |
247 | // ---------------------------------------------------------------------------- | |
248 | ||
249 | int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm, | |
250 | DWORD dwStyle, LONG lParam) | |
251 | { | |
252 | // Get rid of any fonts that we don't want... | |
253 | if ( dwStyle != TRUETYPE_FONTTYPE ) | |
254 | { | |
255 | // continue enumeration | |
256 | return TRUE; | |
257 | } | |
258 | ||
259 | wxFontEnumeratorHelper *fontEnum = (wxFontEnumeratorHelper *)lParam; | |
260 | ||
261 | return fontEnum->OnFont(lplf, lptm); | |
262 | } | |
263 | ||
1e6feb95 | 264 | #endif // wxUSE_FONTMAP |