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/fontenum.h"
36 #include "wx/fontmap.h"
38 #include "wx/msw/private.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
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
47 class wxFontEnumeratorHelper
50 wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
);
52 // control what exactly are we enumerating
53 // we enumerate fonts with given enocding
54 bool SetEncoding(wxFontEncoding encoding
);
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
);
60 // call to start enumeration
63 // called by our font enumeration proc
64 bool OnFont(const LPLOGFONT lf
, const LPTEXTMETRIC tm
) const;
67 // the object we forward calls to OnFont() to
68 wxFontEnumerator
*m_fontEnum
;
70 // if != -1, enum only fonts which have this encoding
73 // if not empty, enum only the fonts with this facename
76 // if not empty, enum only the fonts in this family
79 // if TRUE, enum only fixed fonts
82 // if TRUE, we enumerate the encodings, not fonts
85 // the list of charsets we already found while enumerating charsets
86 wxArrayInt m_charsets
;
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
94 DWORD dwStyle
, LONG lParam
);
96 // ============================================================================
98 // ============================================================================
100 // ----------------------------------------------------------------------------
101 // wxFontEnumeratorHelper
102 // ----------------------------------------------------------------------------
104 wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
)
106 m_fontEnum
= fontEnum
;
107 m_charset
= DEFAULT_CHARSET
;
109 m_enumEncodings
= FALSE
;
112 void wxFontEnumeratorHelper::SetFamily(const wxString
& family
)
114 m_enumEncodings
= TRUE
;
118 bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding
)
120 wxNativeEncodingInfo info
;
121 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
123 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
125 // no such encodings at all
129 m_charset
= info
.charset
;
130 m_facename
= info
.facename
;
135 #if defined(__GNUWIN32__)
136 #if wxUSE_NORLANDER_HEADERS
137 #define wxFONTENUMPROC int(*)(const LOGFONTA *, const TEXTMETRICA *, long unsigned int, LPARAM)
139 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
142 #define wxFONTENUMPROC FONTENUMPROC
145 void wxFontEnumeratorHelper::DoEnumerate()
147 HDC hDC
= ::GetDC(NULL
);
151 lf
.lfCharSet
= m_charset
;
152 wxStrncpy(lf
.lfFaceName
, m_facename
, WXSIZEOF(lf
.lfFaceName
));
153 lf
.lfPitchAndFamily
= 0;
154 ::EnumFontFamiliesEx(hDC
, &lf
, (wxFONTENUMPROC
)wxFontEnumeratorProc
,
155 (LPARAM
)this, 0 /* reserved */) ;
157 ::EnumFonts(hDC
, (LPTSTR
)NULL
, (FONTENUMPROC
)wxFontEnumeratorProc
,
166 ::ReleaseDC(NULL
, hDC
);
169 bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf
,
170 const LPTEXTMETRIC tm
) const
172 if ( m_enumEncodings
)
174 // is this a new charset?
175 int cs
= lf
->lfCharSet
;
176 if ( m_charsets
.Index(cs
) == wxNOT_FOUND
)
178 wxConstCast(this, wxFontEnumeratorHelper
)->m_charsets
.Add(cs
);
180 wxFontEncoding enc
= wxGetFontEncFromCharSet(cs
);
181 return m_fontEnum
->OnFontEncoding(lf
->lfFaceName
,
182 wxFontMapper::GetEncodingName(enc
));
186 // continue enumeration
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
)
197 // not a fixed pitch font
202 if ( m_charset
!= -1 )
204 // check that we have the right encoding
205 if ( lf
->lfCharSet
!= m_charset
)
211 return m_fontEnum
->OnFacename(lf
->lfFaceName
);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
221 wxFontEnumeratorHelper
fe(this);
222 if ( fe
.SetEncoding(encoding
) )
224 fe
.SetFixedOnly(fixedWidthOnly
);
228 // else: no such fonts, unknown encoding
233 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
235 wxFontEnumeratorHelper
fe(this);
236 fe
.SetFamily(family
);
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
247 DWORD dwStyle
, LONG lParam
)
249 // Get rid of any fonts that we don't want...
250 if ( dwStyle
!= TRUETYPE_FONTTYPE
)
252 // continue enumeration
256 wxFontEnumeratorHelper
*fontEnum
= (wxFontEnumeratorHelper
*)lParam
;
258 return fontEnum
->OnFont(lplf
, lptm
);