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