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"
37 #include "wx/fontutil.h"
38 #include "wx/fontenum.h"
39 #include "wx/fontmap.h"
41 #include "wx/msw/private.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // the helper class which calls ::EnumFontFamilies() and whose OnFont() is
48 // called from the callback passed to this function and, in its turn, calls the
49 // appropariate wxFontEnumerator method
50 class wxFontEnumeratorHelper
53 wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
);
55 // control what exactly are we enumerating
56 // we enumerate fonts with given enocding
57 bool SetEncoding(wxFontEncoding encoding
);
58 // we enumerate fixed-width fonts
59 void SetFixedOnly(bool fixedOnly
) { m_fixedOnly
= fixedOnly
; }
60 // we enumerate the encodings available in this family
61 void SetFamily(const wxString
& family
);
63 // call to start enumeration
66 // called by our font enumeration proc
67 bool OnFont(const LPLOGFONT lf
, const LPTEXTMETRIC tm
) const;
70 // the object we forward calls to OnFont() to
71 wxFontEnumerator
*m_fontEnum
;
73 // if != -1, enum only fonts which have this encoding
76 // if not empty, enum only the fonts with this facename
79 // if not empty, enum only the fonts in this family
82 // if TRUE, enum only fixed fonts
85 // if TRUE, we enumerate the encodings, not fonts
88 // the list of charsets we already found while enumerating charsets
89 wxArrayInt m_charsets
;
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
97 DWORD dwStyle
, LONG lParam
);
99 // ============================================================================
101 // ============================================================================
103 // ----------------------------------------------------------------------------
104 // wxFontEnumeratorHelper
105 // ----------------------------------------------------------------------------
107 wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
)
109 m_fontEnum
= fontEnum
;
110 m_charset
= DEFAULT_CHARSET
;
112 m_enumEncodings
= FALSE
;
115 void wxFontEnumeratorHelper::SetFamily(const wxString
& family
)
117 m_enumEncodings
= TRUE
;
121 bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding
)
123 wxNativeEncodingInfo info
;
124 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
126 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
128 // no such encodings at all
132 m_charset
= info
.charset
;
133 m_facename
= info
.facename
;
138 #if defined(__GNUWIN32__) && !defined(__CYGWIN10__)
139 #if wxUSE_NORLANDER_HEADERS
140 #define wxFONTENUMPROC int(*)(const LOGFONT *, const TEXTMETRIC *, long unsigned int, LPARAM)
142 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
145 #define wxFONTENUMPROC FONTENUMPROC
148 void wxFontEnumeratorHelper::DoEnumerate()
150 HDC hDC
= ::GetDC(NULL
);
154 lf
.lfCharSet
= m_charset
;
155 wxStrncpy(lf
.lfFaceName
, m_facename
, WXSIZEOF(lf
.lfFaceName
));
156 lf
.lfPitchAndFamily
= 0;
157 ::EnumFontFamiliesEx(hDC
, &lf
, (wxFONTENUMPROC
)wxFontEnumeratorProc
,
158 (LPARAM
)this, 0 /* reserved */) ;
160 ::EnumFonts(hDC
, (LPTSTR
)NULL
, (FONTENUMPROC
)wxFontEnumeratorProc
,
169 ::ReleaseDC(NULL
, hDC
);
172 bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf
,
173 const LPTEXTMETRIC tm
) const
175 if ( m_enumEncodings
)
177 // is this a new charset?
178 int cs
= lf
->lfCharSet
;
179 if ( m_charsets
.Index(cs
) == wxNOT_FOUND
)
181 wxConstCast(this, wxFontEnumeratorHelper
)->m_charsets
.Add(cs
);
183 wxFontEncoding enc
= wxGetFontEncFromCharSet(cs
);
184 return m_fontEnum
->OnFontEncoding(lf
->lfFaceName
,
185 wxFontMapper::GetEncodingName(enc
));
189 // continue enumeration
196 // check that it's a fixed pitch font (there is *no* error here, the
197 // flag name is misleading!)
198 if ( tm
->tmPitchAndFamily
& TMPF_FIXED_PITCH
)
200 // not a fixed pitch font
205 if ( m_charset
!= -1 )
207 // check that we have the right encoding
208 if ( lf
->lfCharSet
!= m_charset
)
214 return m_fontEnum
->OnFacename(lf
->lfFaceName
);
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
224 wxFontEnumeratorHelper
fe(this);
225 if ( fe
.SetEncoding(encoding
) )
227 fe
.SetFixedOnly(fixedWidthOnly
);
231 // else: no such fonts, unknown encoding
236 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
238 wxFontEnumeratorHelper
fe(this);
239 fe
.SetFamily(family
);
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
250 DWORD dwStyle
, LONG lParam
)
252 // Get rid of any fonts that we don't want...
253 if ( dwStyle
!= TRUETYPE_FONTTYPE
)
255 // continue enumeration
259 wxFontEnumeratorHelper
*fontEnum
= (wxFontEnumeratorHelper
*)lParam
;
261 return fontEnum
->OnFont(lplf
, lptm
);
264 #endif // wxUSE_FONTMAP