]> git.saurik.com Git - wxWidgets.git/blame - src/msw/fontenum.cpp
compilation fix for WXWIN_COMPATIBILITY_2 == 1
[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;
22f3361e
VZ
93
94 DECLARE_NO_COPY_CLASS(wxFontEnumeratorHelper)
a1d58ddc 95};
543f08a6 96
a1d58ddc
VZ
97// ----------------------------------------------------------------------------
98// private functions
99// ----------------------------------------------------------------------------
100
04ef50df 101#ifndef __WXMICROWIN__
a1d58ddc
VZ
102int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm,
103 DWORD dwStyle, LONG lParam);
04ef50df 104#endif
a1d58ddc
VZ
105
106// ============================================================================
107// implementation
108// ============================================================================
109
110// ----------------------------------------------------------------------------
111// wxFontEnumeratorHelper
112// ----------------------------------------------------------------------------
113
114wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator *fontEnum)
115{
116 m_fontEnum = fontEnum;
14647781 117 m_charset = DEFAULT_CHARSET;
a1d58ddc 118 m_fixedOnly = FALSE;
f6bcfd97
BP
119 m_enumEncodings = FALSE;
120}
121
122void wxFontEnumeratorHelper::SetFamily(const wxString& family)
123{
124 m_enumEncodings = TRUE;
125 m_family = family;
543f08a6
JS
126}
127
a1d58ddc
VZ
128bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding)
129{
98e6fc81 130 if ( encoding != wxFONTENCODING_SYSTEM )
a1d58ddc 131 {
98e6fc81
VZ
132 wxNativeEncodingInfo info;
133 if ( !wxGetNativeFontEncoding(encoding, &info) )
11c7d5b6 134 {
98e6fc81 135#if wxUSE_FONTMAP
142b3bc2 136 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding, &info) )
98e6fc81
VZ
137#endif // wxUSE_FONTMAP
138 {
139 // no such encodings at all
140 return FALSE;
141 }
11c7d5b6 142 }
98e6fc81
VZ
143
144 m_charset = info.charset;
145 m_facename = info.facename;
a1d58ddc 146 }
543f08a6 147
11c7d5b6 148 return TRUE;
a1d58ddc
VZ
149}
150
b4da152e
JS
151#if defined(__GNUWIN32__) && !defined(__CYGWIN10__) && !wxCHECK_W32API_VERSION( 1, 1 ) && !wxUSE_NORLANDER_HEADERS
152 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
01dba85a 153#else
9f83044f 154 #define wxFONTENUMPROC FONTENUMPROC
01dba85a
JS
155#endif
156
a1d58ddc 157void wxFontEnumeratorHelper::DoEnumerate()
543f08a6 158{
04ef50df 159#ifndef __WXMICROWIN__
a1d58ddc 160 HDC hDC = ::GetDC(NULL);
543f08a6 161
543f08a6 162#ifdef __WIN32__
a1d58ddc
VZ
163 LOGFONT lf;
164 lf.lfCharSet = m_charset;
11c7d5b6 165 wxStrncpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName));
a1d58ddc 166 lf.lfPitchAndFamily = 0;
01dba85a 167 ::EnumFontFamiliesEx(hDC, &lf, (wxFONTENUMPROC)wxFontEnumeratorProc,
a1d58ddc
VZ
168 (LPARAM)this, 0 /* reserved */) ;
169#else // Win16
170 ::EnumFonts(hDC, (LPTSTR)NULL, (FONTENUMPROC)wxFontEnumeratorProc,
8f177c8e
VZ
171 #ifdef STRICT
172 (LPARAM)
173 #else
174 (LPSTR)
175 #endif
176 this);
a1d58ddc 177#endif // Win32/16
543f08a6 178
a1d58ddc 179 ::ReleaseDC(NULL, hDC);
04ef50df 180#endif
a1d58ddc
VZ
181}
182
183bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf,
184 const LPTEXTMETRIC tm) const
185{
f6bcfd97
BP
186 if ( m_enumEncodings )
187 {
188 // is this a new charset?
189 int cs = lf->lfCharSet;
190 if ( m_charsets.Index(cs) == wxNOT_FOUND )
191 {
192 wxConstCast(this, wxFontEnumeratorHelper)->m_charsets.Add(cs);
193
194 wxFontEncoding enc = wxGetFontEncFromCharSet(cs);
14647781 195 return m_fontEnum->OnFontEncoding(lf->lfFaceName,
f6bcfd97
BP
196 wxFontMapper::GetEncodingName(enc));
197 }
198 else
199 {
200 // continue enumeration
201 return TRUE;
202 }
203 }
204
a1d58ddc
VZ
205 if ( m_fixedOnly )
206 {
207 // check that it's a fixed pitch font (there is *no* error here, the
208 // flag name is misleading!)
209 if ( tm->tmPitchAndFamily & TMPF_FIXED_PITCH )
210 {
211 // not a fixed pitch font
212 return TRUE;
213 }
214 }
215
98e6fc81 216 if ( m_charset != DEFAULT_CHARSET )
a1d58ddc
VZ
217 {
218 // check that we have the right encoding
219 if ( lf->lfCharSet != m_charset )
220 {
221 return TRUE;
222 }
223 }
98e6fc81
VZ
224 else // enumerating fonts in all charsets
225 {
226 // we can get the same facename twice or more in this case because it
227 // may exist in several charsets but we only want to return one copy of
228 // it (note that this can't happen for m_charset != DEFAULT_CHARSET)
229 if ( m_facenames.Index(lf->lfFaceName) != wxNOT_FOUND )
230 {
231 // continue enumeration
232 return TRUE;
233 }
234
235 wxConstCast(this, wxFontEnumeratorHelper)->
236 m_facenames.Add(lf->lfFaceName);
237 }
a1d58ddc 238
3c1866e8 239 return m_fontEnum->OnFacename(lf->lfFaceName);
a1d58ddc
VZ
240}
241
242// ----------------------------------------------------------------------------
243// wxFontEnumerator
244// ----------------------------------------------------------------------------
245
3c1866e8
VZ
246bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
247 bool fixedWidthOnly)
543f08a6 248{
a1d58ddc
VZ
249 wxFontEnumeratorHelper fe(this);
250 if ( fe.SetEncoding(encoding) )
251 {
252 fe.SetFixedOnly(fixedWidthOnly);
253
254 fe.DoEnumerate();
255 }
256 // else: no such fonts, unknown encoding
257
543f08a6
JS
258 return TRUE;
259}
260
a1d58ddc
VZ
261bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
262{
f6bcfd97
BP
263 wxFontEnumeratorHelper fe(this);
264 fe.SetFamily(family);
265 fe.DoEnumerate();
a1d58ddc
VZ
266
267 return TRUE;
268}
269
270// ----------------------------------------------------------------------------
271// Windows callbacks
272// ----------------------------------------------------------------------------
273
04ef50df 274#ifndef __WXMICROWIN__
a1d58ddc 275int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm,
574c939e 276 DWORD WXUNUSED(dwStyle), LONG lParam)
a1d58ddc 277{
574c939e 278
98e6fc81
VZ
279 // we used to process TrueType fonts only, but there doesn't seem to be any
280 // reasons to restrict ourselves to them here
281#if 0
a1d58ddc
VZ
282 // Get rid of any fonts that we don't want...
283 if ( dwStyle != TRUETYPE_FONTTYPE )
284 {
285 // continue enumeration
286 return TRUE;
287 }
98e6fc81 288#endif // 0
a1d58ddc
VZ
289
290 wxFontEnumeratorHelper *fontEnum = (wxFontEnumeratorHelper *)lParam;
291
292 return fontEnum->OnFont(lplf, lptm);
293}
04ef50df 294#endif
a1d58ddc 295
1e6feb95 296#endif // wxUSE_FONTMAP