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