]> git.saurik.com Git - wxWidgets.git/blame - src/msw/fontenum.cpp
Added chapter on collection and container classes to contents
[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
31#ifndef WX_PRECOMP
32 #include "wx/font.h"
33#endif
34
a1d58ddc 35#include "wx/fontenum.h"
11c7d5b6 36#include "wx/fontmap.h"
543f08a6 37
a1d58ddc
VZ
38#include "wx/msw/private.h"
39
40// ----------------------------------------------------------------------------
41// private classes
42// ----------------------------------------------------------------------------
43
f6bcfd97
BP
44// the helper class which calls ::EnumFontFamilies() and whose OnFont() is
45// called from the callback passed to this function and, in its turn, calls the
46// appropariate wxFontEnumerator method
a1d58ddc 47class wxFontEnumeratorHelper
543f08a6 48{
a1d58ddc
VZ
49public:
50 wxFontEnumeratorHelper(wxFontEnumerator *fontEnum);
51
52 // control what exactly are we enumerating
f6bcfd97 53 // we enumerate fonts with given enocding
a1d58ddc 54 bool SetEncoding(wxFontEncoding encoding);
f6bcfd97
BP
55 // we enumerate fixed-width fonts
56 void SetFixedOnly(bool fixedOnly) { m_fixedOnly = fixedOnly; }
57 // we enumerate the encodings available in this family
58 void SetFamily(const wxString& family);
a1d58ddc
VZ
59
60 // call to start enumeration
61 void DoEnumerate();
62
63 // called by our font enumeration proc
64 bool OnFont(const LPLOGFONT lf, const LPTEXTMETRIC tm) const;
65
66private:
67 // the object we forward calls to OnFont() to
68 wxFontEnumerator *m_fontEnum;
543f08a6 69
a1d58ddc
VZ
70 // if != -1, enum only fonts which have this encoding
71 int m_charset;
543f08a6 72
11c7d5b6
VZ
73 // if not empty, enum only the fonts with this facename
74 wxString m_facename;
75
f6bcfd97
BP
76 // if not empty, enum only the fonts in this family
77 wxString m_family;
78
a1d58ddc
VZ
79 // if TRUE, enum only fixed fonts
80 bool m_fixedOnly;
f6bcfd97
BP
81
82 // if TRUE, we enumerate the encodings, not fonts
83 bool m_enumEncodings;
84
85 // the list of charsets we already found while enumerating charsets
86 wxArrayInt m_charsets;
a1d58ddc 87};
543f08a6 88
a1d58ddc
VZ
89// ----------------------------------------------------------------------------
90// private functions
91// ----------------------------------------------------------------------------
92
93int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm,
94 DWORD dwStyle, LONG lParam);
95
96// ============================================================================
97// implementation
98// ============================================================================
99
100// ----------------------------------------------------------------------------
101// wxFontEnumeratorHelper
102// ----------------------------------------------------------------------------
103
104wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator *fontEnum)
105{
106 m_fontEnum = fontEnum;
107 m_charset = -1;
108 m_fixedOnly = FALSE;
f6bcfd97
BP
109 m_enumEncodings = FALSE;
110}
111
112void wxFontEnumeratorHelper::SetFamily(const wxString& family)
113{
114 m_enumEncodings = TRUE;
115 m_family = family;
543f08a6
JS
116}
117
a1d58ddc
VZ
118bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding)
119{
11c7d5b6
VZ
120 wxNativeEncodingInfo info;
121 if ( !wxGetNativeFontEncoding(encoding, &info) )
a1d58ddc 122 {
11c7d5b6
VZ
123 if ( !wxTheFontMapper->GetAltForEncoding(encoding, &info) )
124 {
125 // no such encodings at all
126 return FALSE;
127 }
a1d58ddc 128 }
024fb9b9
RD
129 m_charset = info.charset;
130 m_facename = info.facename;
543f08a6 131
11c7d5b6 132 return TRUE;
a1d58ddc
VZ
133}
134
c5c32d72 135#if defined(__GNUWIN32__)
f6bcfd97 136 #if wxUSE_NORLANDER_HEADERS
c5c32d72
VZ
137 #define wxFONTENUMPROC int(*)(const LOGFONTA *, const TEXTMETRICA *, long unsigned int, LPARAM)
138 #else
139 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
140 #endif
01dba85a 141#else
9f83044f 142 #define wxFONTENUMPROC FONTENUMPROC
01dba85a
JS
143#endif
144
a1d58ddc 145void wxFontEnumeratorHelper::DoEnumerate()
543f08a6 146{
a1d58ddc 147 HDC hDC = ::GetDC(NULL);
543f08a6 148
543f08a6 149#ifdef __WIN32__
a1d58ddc
VZ
150 LOGFONT lf;
151 lf.lfCharSet = m_charset;
11c7d5b6 152 wxStrncpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName));
a1d58ddc 153 lf.lfPitchAndFamily = 0;
01dba85a 154 ::EnumFontFamiliesEx(hDC, &lf, (wxFONTENUMPROC)wxFontEnumeratorProc,
a1d58ddc
VZ
155 (LPARAM)this, 0 /* reserved */) ;
156#else // Win16
157 ::EnumFonts(hDC, (LPTSTR)NULL, (FONTENUMPROC)wxFontEnumeratorProc,
8f177c8e
VZ
158 #ifdef STRICT
159 (LPARAM)
160 #else
161 (LPSTR)
162 #endif
163 this);
a1d58ddc 164#endif // Win32/16
543f08a6 165
a1d58ddc
VZ
166 ::ReleaseDC(NULL, hDC);
167}
168
169bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf,
170 const LPTEXTMETRIC tm) const
171{
f6bcfd97
BP
172 if ( m_enumEncodings )
173 {
174 // is this a new charset?
175 int cs = lf->lfCharSet;
176 if ( m_charsets.Index(cs) == wxNOT_FOUND )
177 {
178 wxConstCast(this, wxFontEnumeratorHelper)->m_charsets.Add(cs);
179
180 wxFontEncoding enc = wxGetFontEncFromCharSet(cs);
181 return m_fontEnum->OnFontEncoding(m_family,
182 wxFontMapper::GetEncodingName(enc));
183 }
184 else
185 {
186 // continue enumeration
187 return TRUE;
188 }
189 }
190
a1d58ddc
VZ
191 if ( m_fixedOnly )
192 {
193 // check that it's a fixed pitch font (there is *no* error here, the
194 // flag name is misleading!)
195 if ( tm->tmPitchAndFamily & TMPF_FIXED_PITCH )
196 {
197 // not a fixed pitch font
198 return TRUE;
199 }
200 }
201
202 if ( m_charset != -1 )
203 {
204 // check that we have the right encoding
205 if ( lf->lfCharSet != m_charset )
206 {
207 return TRUE;
208 }
209 }
210
3c1866e8 211 return m_fontEnum->OnFacename(lf->lfFaceName);
a1d58ddc
VZ
212}
213
214// ----------------------------------------------------------------------------
215// wxFontEnumerator
216// ----------------------------------------------------------------------------
217
3c1866e8
VZ
218bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
219 bool fixedWidthOnly)
543f08a6 220{
a1d58ddc
VZ
221 wxFontEnumeratorHelper fe(this);
222 if ( fe.SetEncoding(encoding) )
223 {
224 fe.SetFixedOnly(fixedWidthOnly);
225
226 fe.DoEnumerate();
227 }
228 // else: no such fonts, unknown encoding
229
543f08a6
JS
230 return TRUE;
231}
232
a1d58ddc
VZ
233bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
234{
f6bcfd97
BP
235 wxFontEnumeratorHelper fe(this);
236 fe.SetFamily(family);
237 fe.DoEnumerate();
a1d58ddc
VZ
238
239 return TRUE;
240}
241
242// ----------------------------------------------------------------------------
243// Windows callbacks
244// ----------------------------------------------------------------------------
245
246int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm,
247 DWORD dwStyle, LONG lParam)
248{
249 // Get rid of any fonts that we don't want...
250 if ( dwStyle != TRUETYPE_FONTTYPE )
251 {
252 // continue enumeration
253 return TRUE;
254 }
255
256 wxFontEnumeratorHelper *fontEnum = (wxFontEnumeratorHelper *)lParam;
257
258 return fontEnum->OnFont(lplf, lptm);
259}
260