1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/fontenum.cpp
3 // Purpose: wxFontEnumerator class for Windows
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to add support for font encodings
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "fontenum.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/fontutil.h"
36 #include "wx/fontenum.h"
37 #include "wx/fontmap.h"
39 #include "wx/msw/private.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
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
48 class wxFontEnumeratorHelper
51 wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
);
53 // control what exactly are we enumerating
54 // we enumerate fonts with given enocding
55 bool SetEncoding(wxFontEncoding encoding
);
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
);
61 // call to start enumeration
64 // called by our font enumeration proc
65 bool OnFont(const LPLOGFONT lf
, const LPTEXTMETRIC tm
) const;
68 // the object we forward calls to OnFont() to
69 wxFontEnumerator
*m_fontEnum
;
71 // if != -1, enum only fonts which have this encoding
74 // if not empty, enum only the fonts with this facename
77 // if not empty, enum only the fonts in this family
80 // if TRUE, enum only fixed fonts
83 // if TRUE, we enumerate the encodings, not fonts
86 // the list of charsets we already found while enumerating charsets
87 wxArrayInt m_charsets
;
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
95 DWORD dwStyle
, LONG lParam
);
97 // ============================================================================
99 // ============================================================================
101 // ----------------------------------------------------------------------------
102 // wxFontEnumeratorHelper
103 // ----------------------------------------------------------------------------
105 wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
)
107 m_fontEnum
= fontEnum
;
108 m_charset
= DEFAULT_CHARSET
;
110 m_enumEncodings
= FALSE
;
113 void wxFontEnumeratorHelper::SetFamily(const wxString
& family
)
115 m_enumEncodings
= TRUE
;
119 bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding
)
121 wxNativeEncodingInfo info
;
122 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
124 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
126 // no such encodings at all
130 m_charset
= info
.charset
;
131 m_facename
= info
.facename
;
136 #if defined(__GNUWIN32__)
137 #if wxUSE_NORLANDER_HEADERS
138 #define wxFONTENUMPROC int(*)(const LOGFONTA *, const TEXTMETRICA *, long unsigned int, LPARAM)
140 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
143 #define wxFONTENUMPROC FONTENUMPROC
146 void wxFontEnumeratorHelper::DoEnumerate()
148 HDC hDC
= ::GetDC(NULL
);
152 lf
.lfCharSet
= m_charset
;
153 wxStrncpy(lf
.lfFaceName
, m_facename
, WXSIZEOF(lf
.lfFaceName
));
154 lf
.lfPitchAndFamily
= 0;
155 ::EnumFontFamiliesEx(hDC
, &lf
, (wxFONTENUMPROC
)wxFontEnumeratorProc
,
156 (LPARAM
)this, 0 /* reserved */) ;
158 ::EnumFonts(hDC
, (LPTSTR
)NULL
, (FONTENUMPROC
)wxFontEnumeratorProc
,
167 ::ReleaseDC(NULL
, hDC
);
170 bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf
,
171 const LPTEXTMETRIC tm
) const
173 if ( m_enumEncodings
)
175 // is this a new charset?
176 int cs
= lf
->lfCharSet
;
177 if ( m_charsets
.Index(cs
) == wxNOT_FOUND
)
179 wxConstCast(this, wxFontEnumeratorHelper
)->m_charsets
.Add(cs
);
181 wxFontEncoding enc
= wxGetFontEncFromCharSet(cs
);
182 return m_fontEnum
->OnFontEncoding(lf
->lfFaceName
,
183 wxFontMapper::GetEncodingName(enc
));
187 // continue enumeration
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
)
198 // not a fixed pitch font
203 if ( m_charset
!= -1 )
205 // check that we have the right encoding
206 if ( lf
->lfCharSet
!= m_charset
)
212 return m_fontEnum
->OnFacename(lf
->lfFaceName
);
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
222 wxFontEnumeratorHelper
fe(this);
223 if ( fe
.SetEncoding(encoding
) )
225 fe
.SetFixedOnly(fixedWidthOnly
);
229 // else: no such fonts, unknown encoding
234 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
236 wxFontEnumeratorHelper
fe(this);
237 fe
.SetFamily(family
);
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
248 DWORD dwStyle
, LONG lParam
)
250 // Get rid of any fonts that we don't want...
251 if ( dwStyle
!= TRUETYPE_FONTTYPE
)
253 // continue enumeration
257 wxFontEnumeratorHelper
*fontEnum
= (wxFontEnumeratorHelper
*)lParam
;
259 return fontEnum
->OnFont(lplf
, lptm
);