]>
Commit | Line | Data |
---|---|---|
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 | ||
44 | class wxFontEnumeratorHelper | |
543f08a6 | 45 | { |
a1d58ddc VZ |
46 | public: |
47 | wxFontEnumeratorHelper(wxFontEnumerator *fontEnum); | |
48 | ||
49 | // control what exactly are we enumerating | |
50 | bool SetEncoding(wxFontEncoding encoding); | |
51 | void SetFixedOnly(bool fixedOnly) | |
52 | { m_fixedOnly = fixedOnly; } | |
53 | ||
54 | // call to start enumeration | |
55 | void DoEnumerate(); | |
56 | ||
57 | // called by our font enumeration proc | |
58 | bool OnFont(const LPLOGFONT lf, const LPTEXTMETRIC tm) const; | |
59 | ||
60 | private: | |
61 | // the object we forward calls to OnFont() to | |
62 | wxFontEnumerator *m_fontEnum; | |
543f08a6 | 63 | |
a1d58ddc VZ |
64 | // if != -1, enum only fonts which have this encoding |
65 | int m_charset; | |
543f08a6 | 66 | |
11c7d5b6 VZ |
67 | // if not empty, enum only the fonts with this facename |
68 | wxString m_facename; | |
69 | ||
a1d58ddc VZ |
70 | // if TRUE, enum only fixed fonts |
71 | bool m_fixedOnly; | |
72 | }; | |
543f08a6 | 73 | |
a1d58ddc VZ |
74 | // ---------------------------------------------------------------------------- |
75 | // private functions | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
78 | int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm, | |
79 | DWORD dwStyle, LONG lParam); | |
80 | ||
81 | // ============================================================================ | |
82 | // implementation | |
83 | // ============================================================================ | |
84 | ||
85 | // ---------------------------------------------------------------------------- | |
86 | // wxFontEnumeratorHelper | |
87 | // ---------------------------------------------------------------------------- | |
88 | ||
89 | wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator *fontEnum) | |
90 | { | |
91 | m_fontEnum = fontEnum; | |
92 | m_charset = -1; | |
93 | m_fixedOnly = FALSE; | |
543f08a6 JS |
94 | } |
95 | ||
a1d58ddc VZ |
96 | bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding) |
97 | { | |
11c7d5b6 VZ |
98 | wxNativeEncodingInfo info; |
99 | if ( !wxGetNativeFontEncoding(encoding, &info) ) | |
a1d58ddc | 100 | { |
11c7d5b6 VZ |
101 | if ( !wxTheFontMapper->GetAltForEncoding(encoding, &info) ) |
102 | { | |
103 | // no such encodings at all | |
104 | return FALSE; | |
105 | } | |
106 | ||
107 | m_charset = info.charset; | |
108 | m_facename = info.facename; | |
a1d58ddc | 109 | } |
543f08a6 | 110 | |
11c7d5b6 | 111 | return TRUE; |
a1d58ddc VZ |
112 | } |
113 | ||
114 | void wxFontEnumeratorHelper::DoEnumerate() | |
543f08a6 | 115 | { |
a1d58ddc | 116 | HDC hDC = ::GetDC(NULL); |
543f08a6 | 117 | |
543f08a6 | 118 | #ifdef __WIN32__ |
a1d58ddc VZ |
119 | LOGFONT lf; |
120 | lf.lfCharSet = m_charset; | |
11c7d5b6 | 121 | wxStrncpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName)); |
a1d58ddc VZ |
122 | lf.lfPitchAndFamily = 0; |
123 | ::EnumFontFamiliesEx(hDC, &lf, (FONTENUMPROC)wxFontEnumeratorProc, | |
124 | (LPARAM)this, 0 /* reserved */) ; | |
125 | #else // Win16 | |
126 | ::EnumFonts(hDC, (LPTSTR)NULL, (FONTENUMPROC)wxFontEnumeratorProc, | |
127 | (LPARAM) (void*) this) ; | |
128 | #endif // Win32/16 | |
543f08a6 | 129 | |
a1d58ddc VZ |
130 | ::ReleaseDC(NULL, hDC); |
131 | } | |
132 | ||
133 | bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf, | |
134 | const LPTEXTMETRIC tm) const | |
135 | { | |
136 | if ( m_fixedOnly ) | |
137 | { | |
138 | // check that it's a fixed pitch font (there is *no* error here, the | |
139 | // flag name is misleading!) | |
140 | if ( tm->tmPitchAndFamily & TMPF_FIXED_PITCH ) | |
141 | { | |
142 | // not a fixed pitch font | |
143 | return TRUE; | |
144 | } | |
145 | } | |
146 | ||
147 | if ( m_charset != -1 ) | |
148 | { | |
149 | // check that we have the right encoding | |
150 | if ( lf->lfCharSet != m_charset ) | |
151 | { | |
152 | return TRUE; | |
153 | } | |
154 | } | |
155 | ||
3c1866e8 | 156 | return m_fontEnum->OnFacename(lf->lfFaceName); |
a1d58ddc VZ |
157 | } |
158 | ||
159 | // ---------------------------------------------------------------------------- | |
160 | // wxFontEnumerator | |
161 | // ---------------------------------------------------------------------------- | |
162 | ||
3c1866e8 VZ |
163 | bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding, |
164 | bool fixedWidthOnly) | |
543f08a6 | 165 | { |
a1d58ddc VZ |
166 | wxFontEnumeratorHelper fe(this); |
167 | if ( fe.SetEncoding(encoding) ) | |
168 | { | |
169 | fe.SetFixedOnly(fixedWidthOnly); | |
170 | ||
171 | fe.DoEnumerate(); | |
172 | } | |
173 | // else: no such fonts, unknown encoding | |
174 | ||
543f08a6 JS |
175 | return TRUE; |
176 | } | |
177 | ||
a1d58ddc VZ |
178 | bool wxFontEnumerator::EnumerateEncodings(const wxString& family) |
179 | { | |
180 | wxFAIL_MSG(wxT("TODO")); | |
181 | ||
182 | return TRUE; | |
183 | } | |
184 | ||
185 | // ---------------------------------------------------------------------------- | |
186 | // Windows callbacks | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
189 | int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm, | |
190 | DWORD dwStyle, LONG lParam) | |
191 | { | |
192 | // Get rid of any fonts that we don't want... | |
193 | if ( dwStyle != TRUETYPE_FONTTYPE ) | |
194 | { | |
195 | // continue enumeration | |
196 | return TRUE; | |
197 | } | |
198 | ||
199 | wxFontEnumeratorHelper *fontEnum = (wxFontEnumeratorHelper *)lParam; | |
200 | ||
201 | return fontEnum->OnFont(lplf, lptm); | |
202 | } | |
203 |