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