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