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