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