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