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