]> git.saurik.com Git - wxWidgets.git/blob - src/msw/fontenum.cpp
Update with recent changes.
[wxWidgets.git] / src / msw / fontenum.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/fontenum.cpp
3 // Purpose: wxFontEnumerator class for Windows
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to add support for font encodings
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "fontenum.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 #if wxUSE_FONTMAP
32
33 #ifndef WX_PRECOMP
34 #include "wx/font.h"
35 #endif
36
37 #include "wx/fontutil.h"
38 #include "wx/fontenum.h"
39 #include "wx/fontmap.h"
40
41 #include "wx/msw/private.h"
42
43 // ----------------------------------------------------------------------------
44 // private classes
45 // ----------------------------------------------------------------------------
46
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
50 class wxFontEnumeratorHelper
51 {
52 public:
53 wxFontEnumeratorHelper(wxFontEnumerator *fontEnum);
54
55 // control what exactly are we enumerating
56 // we enumerate fonts with given enocding
57 bool SetEncoding(wxFontEncoding encoding);
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);
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;
72
73 // if != -1, enum only fonts which have this encoding
74 int m_charset;
75
76 // if not empty, enum only the fonts with this facename
77 wxString m_facename;
78
79 // if not empty, enum only the fonts in this family
80 wxString m_family;
81
82 // if TRUE, enum only fixed fonts
83 bool m_fixedOnly;
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;
90
91 // the list of facenames we already found while enumerating facenames
92 wxArrayString m_facenames;
93
94 DECLARE_NO_COPY_CLASS(wxFontEnumeratorHelper)
95 };
96
97 // ----------------------------------------------------------------------------
98 // private functions
99 // ----------------------------------------------------------------------------
100
101 #ifndef __WXMICROWIN__
102 int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm,
103 DWORD dwStyle, LONG lParam);
104 #endif
105
106 // ============================================================================
107 // implementation
108 // ============================================================================
109
110 // ----------------------------------------------------------------------------
111 // wxFontEnumeratorHelper
112 // ----------------------------------------------------------------------------
113
114 wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator *fontEnum)
115 {
116 m_fontEnum = fontEnum;
117 m_charset = DEFAULT_CHARSET;
118 m_fixedOnly = FALSE;
119 m_enumEncodings = FALSE;
120 }
121
122 void wxFontEnumeratorHelper::SetFamily(const wxString& family)
123 {
124 m_enumEncodings = TRUE;
125 m_family = family;
126 }
127
128 bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding)
129 {
130 if ( encoding != wxFONTENCODING_SYSTEM )
131 {
132 wxNativeEncodingInfo info;
133 if ( !wxGetNativeFontEncoding(encoding, &info) )
134 {
135 #if wxUSE_FONTMAP
136 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding, &info) )
137 #endif // wxUSE_FONTMAP
138 {
139 // no such encodings at all
140 return FALSE;
141 }
142 }
143
144 m_charset = info.charset;
145 m_facename = info.facename;
146 }
147
148 return TRUE;
149 }
150
151 #if defined(__GNUWIN32__) && !defined(__CYGWIN10__) && !wxCHECK_W32API_VERSION( 1, 1 ) && !wxUSE_NORLANDER_HEADERS
152 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
153 #else
154 #define wxFONTENUMPROC FONTENUMPROC
155 #endif
156
157 void wxFontEnumeratorHelper::DoEnumerate()
158 {
159 #ifndef __WXMICROWIN__
160 HDC hDC = ::GetDC(NULL);
161
162 #ifdef __WIN32__
163 LOGFONT lf;
164 lf.lfCharSet = m_charset;
165 wxStrncpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName));
166 lf.lfPitchAndFamily = 0;
167 ::EnumFontFamiliesEx(hDC, &lf, (wxFONTENUMPROC)wxFontEnumeratorProc,
168 (LPARAM)this, 0 /* reserved */) ;
169 #else // Win16
170 ::EnumFonts(hDC, (LPTSTR)NULL, (FONTENUMPROC)wxFontEnumeratorProc,
171 #ifdef STRICT
172 (LPARAM)
173 #else
174 (LPSTR)
175 #endif
176 this);
177 #endif // Win32/16
178
179 ::ReleaseDC(NULL, hDC);
180 #endif
181 }
182
183 bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf,
184 const LPTEXTMETRIC tm) const
185 {
186 if ( m_enumEncodings )
187 {
188 // is this a new charset?
189 int cs = lf->lfCharSet;
190 if ( m_charsets.Index(cs) == wxNOT_FOUND )
191 {
192 wxConstCast(this, wxFontEnumeratorHelper)->m_charsets.Add(cs);
193
194 wxFontEncoding enc = wxGetFontEncFromCharSet(cs);
195 return m_fontEnum->OnFontEncoding(lf->lfFaceName,
196 wxFontMapper::GetEncodingName(enc));
197 }
198 else
199 {
200 // continue enumeration
201 return TRUE;
202 }
203 }
204
205 if ( m_fixedOnly )
206 {
207 // check that it's a fixed pitch font (there is *no* error here, the
208 // flag name is misleading!)
209 if ( tm->tmPitchAndFamily & TMPF_FIXED_PITCH )
210 {
211 // not a fixed pitch font
212 return TRUE;
213 }
214 }
215
216 if ( m_charset != DEFAULT_CHARSET )
217 {
218 // check that we have the right encoding
219 if ( lf->lfCharSet != m_charset )
220 {
221 return TRUE;
222 }
223 }
224 else // enumerating fonts in all charsets
225 {
226 // we can get the same facename twice or more in this case because it
227 // may exist in several charsets but we only want to return one copy of
228 // it (note that this can't happen for m_charset != DEFAULT_CHARSET)
229 if ( m_facenames.Index(lf->lfFaceName) != wxNOT_FOUND )
230 {
231 // continue enumeration
232 return TRUE;
233 }
234
235 wxConstCast(this, wxFontEnumeratorHelper)->
236 m_facenames.Add(lf->lfFaceName);
237 }
238
239 return m_fontEnum->OnFacename(lf->lfFaceName);
240 }
241
242 // ----------------------------------------------------------------------------
243 // wxFontEnumerator
244 // ----------------------------------------------------------------------------
245
246 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
247 bool fixedWidthOnly)
248 {
249 wxFontEnumeratorHelper fe(this);
250 if ( fe.SetEncoding(encoding) )
251 {
252 fe.SetFixedOnly(fixedWidthOnly);
253
254 fe.DoEnumerate();
255 }
256 // else: no such fonts, unknown encoding
257
258 return TRUE;
259 }
260
261 bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
262 {
263 wxFontEnumeratorHelper fe(this);
264 fe.SetFamily(family);
265 fe.DoEnumerate();
266
267 return TRUE;
268 }
269
270 // ----------------------------------------------------------------------------
271 // Windows callbacks
272 // ----------------------------------------------------------------------------
273
274 #ifndef __WXMICROWIN__
275 int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm,
276 DWORD WXUNUSED(dwStyle), LONG lParam)
277 {
278
279 // we used to process TrueType fonts only, but there doesn't seem to be any
280 // reasons to restrict ourselves to them here
281 #if 0
282 // Get rid of any fonts that we don't want...
283 if ( dwStyle != TRUETYPE_FONTTYPE )
284 {
285 // continue enumeration
286 return TRUE;
287 }
288 #endif // 0
289
290 wxFontEnumeratorHelper *fontEnum = (wxFontEnumeratorHelper *)lParam;
291
292 return fontEnum->OnFont(lplf, lptm);
293 }
294 #endif
295
296 #endif // wxUSE_FONTMAP