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