]>
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 | ||
4676948b JS |
37 | #include "wx/msw/private.h" |
38 | ||
76e23cdb | 39 | #include "wx/fontutil.h" |
a1d58ddc | 40 | #include "wx/fontenum.h" |
11c7d5b6 | 41 | #include "wx/fontmap.h" |
543f08a6 | 42 | |
a1d58ddc VZ |
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; | |
98e6fc81 VZ |
90 | |
91 | // the list of facenames we already found while enumerating facenames | |
92 | wxArrayString m_facenames; | |
22f3361e VZ |
93 | |
94 | DECLARE_NO_COPY_CLASS(wxFontEnumeratorHelper) | |
a1d58ddc | 95 | }; |
543f08a6 | 96 | |
a1d58ddc VZ |
97 | // ---------------------------------------------------------------------------- |
98 | // private functions | |
99 | // ---------------------------------------------------------------------------- | |
100 | ||
04ef50df | 101 | #ifndef __WXMICROWIN__ |
a1d58ddc VZ |
102 | int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm, |
103 | DWORD dwStyle, LONG lParam); | |
04ef50df | 104 | #endif |
a1d58ddc VZ |
105 | |
106 | // ============================================================================ | |
107 | // implementation | |
108 | // ============================================================================ | |
109 | ||
110 | // ---------------------------------------------------------------------------- | |
111 | // wxFontEnumeratorHelper | |
112 | // ---------------------------------------------------------------------------- | |
113 | ||
114 | wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator *fontEnum) | |
115 | { | |
116 | m_fontEnum = fontEnum; | |
14647781 | 117 | m_charset = DEFAULT_CHARSET; |
a1d58ddc | 118 | m_fixedOnly = FALSE; |
f6bcfd97 BP |
119 | m_enumEncodings = FALSE; |
120 | } | |
121 | ||
122 | void wxFontEnumeratorHelper::SetFamily(const wxString& family) | |
123 | { | |
124 | m_enumEncodings = TRUE; | |
125 | m_family = family; | |
543f08a6 JS |
126 | } |
127 | ||
a1d58ddc VZ |
128 | bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding) |
129 | { | |
98e6fc81 | 130 | if ( encoding != wxFONTENCODING_SYSTEM ) |
a1d58ddc | 131 | { |
98e6fc81 VZ |
132 | wxNativeEncodingInfo info; |
133 | if ( !wxGetNativeFontEncoding(encoding, &info) ) | |
11c7d5b6 | 134 | { |
98e6fc81 | 135 | #if wxUSE_FONTMAP |
142b3bc2 | 136 | if ( !wxFontMapper::Get()->GetAltForEncoding(encoding, &info) ) |
98e6fc81 VZ |
137 | #endif // wxUSE_FONTMAP |
138 | { | |
139 | // no such encodings at all | |
140 | return FALSE; | |
141 | } | |
11c7d5b6 | 142 | } |
98e6fc81 VZ |
143 | |
144 | m_charset = info.charset; | |
145 | m_facename = info.facename; | |
a1d58ddc | 146 | } |
543f08a6 | 147 | |
11c7d5b6 | 148 | return TRUE; |
a1d58ddc VZ |
149 | } |
150 | ||
b4da152e JS |
151 | #if defined(__GNUWIN32__) && !defined(__CYGWIN10__) && !wxCHECK_W32API_VERSION( 1, 1 ) && !wxUSE_NORLANDER_HEADERS |
152 | #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM) | |
01dba85a | 153 | #else |
9f83044f | 154 | #define wxFONTENUMPROC FONTENUMPROC |
01dba85a JS |
155 | #endif |
156 | ||
a1d58ddc | 157 | void wxFontEnumeratorHelper::DoEnumerate() |
543f08a6 | 158 | { |
04ef50df | 159 | #ifndef __WXMICROWIN__ |
a1d58ddc | 160 | HDC hDC = ::GetDC(NULL); |
543f08a6 | 161 | |
4676948b JS |
162 | #ifdef __WXWINCE__ |
163 | ::EnumFontFamilies(hDC, m_facename, (wxFONTENUMPROC)wxFontEnumeratorProc, | |
164 | (LPARAM)this) ; | |
165 | #elif defined(__WIN32__) | |
a1d58ddc VZ |
166 | LOGFONT lf; |
167 | lf.lfCharSet = m_charset; | |
11c7d5b6 | 168 | wxStrncpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName)); |
a1d58ddc | 169 | lf.lfPitchAndFamily = 0; |
01dba85a | 170 | ::EnumFontFamiliesEx(hDC, &lf, (wxFONTENUMPROC)wxFontEnumeratorProc, |
a1d58ddc VZ |
171 | (LPARAM)this, 0 /* reserved */) ; |
172 | #else // Win16 | |
173 | ::EnumFonts(hDC, (LPTSTR)NULL, (FONTENUMPROC)wxFontEnumeratorProc, | |
8f177c8e VZ |
174 | #ifdef STRICT |
175 | (LPARAM) | |
176 | #else | |
177 | (LPSTR) | |
178 | #endif | |
179 | this); | |
a1d58ddc | 180 | #endif // Win32/16 |
543f08a6 | 181 | |
a1d58ddc | 182 | ::ReleaseDC(NULL, hDC); |
04ef50df | 183 | #endif |
a1d58ddc VZ |
184 | } |
185 | ||
186 | bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf, | |
187 | const LPTEXTMETRIC tm) const | |
188 | { | |
f6bcfd97 BP |
189 | if ( m_enumEncodings ) |
190 | { | |
191 | // is this a new charset? | |
192 | int cs = lf->lfCharSet; | |
193 | if ( m_charsets.Index(cs) == wxNOT_FOUND ) | |
194 | { | |
195 | wxConstCast(this, wxFontEnumeratorHelper)->m_charsets.Add(cs); | |
196 | ||
197 | wxFontEncoding enc = wxGetFontEncFromCharSet(cs); | |
14647781 | 198 | return m_fontEnum->OnFontEncoding(lf->lfFaceName, |
f6bcfd97 BP |
199 | wxFontMapper::GetEncodingName(enc)); |
200 | } | |
201 | else | |
202 | { | |
203 | // continue enumeration | |
204 | return TRUE; | |
205 | } | |
206 | } | |
207 | ||
a1d58ddc VZ |
208 | if ( m_fixedOnly ) |
209 | { | |
210 | // check that it's a fixed pitch font (there is *no* error here, the | |
211 | // flag name is misleading!) | |
212 | if ( tm->tmPitchAndFamily & TMPF_FIXED_PITCH ) | |
213 | { | |
214 | // not a fixed pitch font | |
215 | return TRUE; | |
216 | } | |
217 | } | |
218 | ||
98e6fc81 | 219 | if ( m_charset != DEFAULT_CHARSET ) |
a1d58ddc VZ |
220 | { |
221 | // check that we have the right encoding | |
222 | if ( lf->lfCharSet != m_charset ) | |
223 | { | |
224 | return TRUE; | |
225 | } | |
226 | } | |
98e6fc81 VZ |
227 | else // enumerating fonts in all charsets |
228 | { | |
229 | // we can get the same facename twice or more in this case because it | |
230 | // may exist in several charsets but we only want to return one copy of | |
231 | // it (note that this can't happen for m_charset != DEFAULT_CHARSET) | |
232 | if ( m_facenames.Index(lf->lfFaceName) != wxNOT_FOUND ) | |
233 | { | |
234 | // continue enumeration | |
235 | return TRUE; | |
236 | } | |
237 | ||
238 | wxConstCast(this, wxFontEnumeratorHelper)-> | |
239 | m_facenames.Add(lf->lfFaceName); | |
240 | } | |
a1d58ddc | 241 | |
3c1866e8 | 242 | return m_fontEnum->OnFacename(lf->lfFaceName); |
a1d58ddc VZ |
243 | } |
244 | ||
245 | // ---------------------------------------------------------------------------- | |
246 | // wxFontEnumerator | |
247 | // ---------------------------------------------------------------------------- | |
248 | ||
3c1866e8 VZ |
249 | bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding, |
250 | bool fixedWidthOnly) | |
543f08a6 | 251 | { |
a1d58ddc VZ |
252 | wxFontEnumeratorHelper fe(this); |
253 | if ( fe.SetEncoding(encoding) ) | |
254 | { | |
255 | fe.SetFixedOnly(fixedWidthOnly); | |
256 | ||
257 | fe.DoEnumerate(); | |
258 | } | |
259 | // else: no such fonts, unknown encoding | |
260 | ||
543f08a6 JS |
261 | return TRUE; |
262 | } | |
263 | ||
a1d58ddc VZ |
264 | bool wxFontEnumerator::EnumerateEncodings(const wxString& family) |
265 | { | |
f6bcfd97 BP |
266 | wxFontEnumeratorHelper fe(this); |
267 | fe.SetFamily(family); | |
268 | fe.DoEnumerate(); | |
a1d58ddc VZ |
269 | |
270 | return TRUE; | |
271 | } | |
272 | ||
273 | // ---------------------------------------------------------------------------- | |
274 | // Windows callbacks | |
275 | // ---------------------------------------------------------------------------- | |
276 | ||
04ef50df | 277 | #ifndef __WXMICROWIN__ |
a1d58ddc | 278 | int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm, |
574c939e | 279 | DWORD WXUNUSED(dwStyle), LONG lParam) |
a1d58ddc | 280 | { |
574c939e | 281 | |
98e6fc81 VZ |
282 | // we used to process TrueType fonts only, but there doesn't seem to be any |
283 | // reasons to restrict ourselves to them here | |
284 | #if 0 | |
a1d58ddc VZ |
285 | // Get rid of any fonts that we don't want... |
286 | if ( dwStyle != TRUETYPE_FONTTYPE ) | |
287 | { | |
288 | // continue enumeration | |
289 | return TRUE; | |
290 | } | |
98e6fc81 | 291 | #endif // 0 |
a1d58ddc VZ |
292 | |
293 | wxFontEnumeratorHelper *fontEnum = (wxFontEnumeratorHelper *)lParam; | |
294 | ||
295 | return fontEnum->OnFont(lplf, lptm); | |
296 | } | |
04ef50df | 297 | #endif |
a1d58ddc | 298 | |
1e6feb95 | 299 | #endif // wxUSE_FONTMAP |