]> git.saurik.com Git - wxWidgets.git/blob - src/msw/fontenum.cpp
wxFontEnumerator mostly works for wxMSW
[wxWidgets.git] / src / msw / fontenum.cpp
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
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "fontenum.h"
22 #endif
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
35 #include "wx/fontenum.h"
36
37 #include "wx/msw/private.h"
38
39 // ----------------------------------------------------------------------------
40 // private classes
41 // ----------------------------------------------------------------------------
42
43 class wxFontEnumeratorHelper
44 {
45 public:
46 wxFontEnumeratorHelper(wxFontEnumerator *fontEnum);
47
48 // control what exactly are we enumerating
49 bool SetEncoding(wxFontEncoding encoding);
50 void SetFixedOnly(bool fixedOnly)
51 { m_fixedOnly = fixedOnly; }
52
53 // call to start enumeration
54 void DoEnumerate();
55
56 // called by our font enumeration proc
57 bool OnFont(const LPLOGFONT lf, const LPTEXTMETRIC tm) const;
58
59 private:
60 // the object we forward calls to OnFont() to
61 wxFontEnumerator *m_fontEnum;
62
63 // if != -1, enum only fonts which have this encoding
64 int m_charset;
65
66 // if TRUE, enum only fixed fonts
67 bool m_fixedOnly;
68 };
69
70 // ----------------------------------------------------------------------------
71 // private functions
72 // ----------------------------------------------------------------------------
73
74 int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm,
75 DWORD dwStyle, LONG lParam);
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // wxFontEnumeratorHelper
83 // ----------------------------------------------------------------------------
84
85 wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator *fontEnum)
86 {
87 m_fontEnum = fontEnum;
88 m_charset = -1;
89 m_fixedOnly = FALSE;
90 }
91
92 bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding)
93 {
94 bool exact;
95 m_charset = wxCharsetFromEncoding(encoding, &exact);
96 #ifdef __WIN32__
97 if ( !exact )
98 {
99 m_charset = DEFAULT_CHARSET;
100 }
101 #endif // Win32
102
103 return exact;
104 }
105
106 void wxFontEnumeratorHelper::DoEnumerate()
107 {
108 HDC hDC = ::GetDC(NULL);
109
110 #ifdef __WIN32__
111 LOGFONT lf;
112 lf.lfCharSet = m_charset;
113 lf.lfFaceName[0] = _T('\0');
114 lf.lfPitchAndFamily = 0;
115 ::EnumFontFamiliesEx(hDC, &lf, (FONTENUMPROC)wxFontEnumeratorProc,
116 (LPARAM)this, 0 /* reserved */) ;
117 #else // Win16
118 ::EnumFonts(hDC, (LPTSTR)NULL, (FONTENUMPROC)wxFontEnumeratorProc,
119 (LPARAM) (void*) this) ;
120 #endif // Win32/16
121
122 ::ReleaseDC(NULL, hDC);
123 }
124
125 bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf,
126 const LPTEXTMETRIC tm) const
127 {
128 if ( m_fixedOnly )
129 {
130 // check that it's a fixed pitch font (there is *no* error here, the
131 // flag name is misleading!)
132 if ( tm->tmPitchAndFamily & TMPF_FIXED_PITCH )
133 {
134 // not a fixed pitch font
135 return TRUE;
136 }
137 }
138
139 if ( m_charset != -1 )
140 {
141 // check that we have the right encoding
142 if ( lf->lfCharSet != m_charset )
143 {
144 return TRUE;
145 }
146 }
147
148 return m_fontEnum->OnFontFamily(lf->lfFaceName);
149 }
150
151 // ----------------------------------------------------------------------------
152 // wxFontEnumerator
153 // ----------------------------------------------------------------------------
154
155 bool wxFontEnumerator::EnumerateFamilies(wxFontEncoding encoding,
156 bool fixedWidthOnly)
157 {
158 wxFontEnumeratorHelper fe(this);
159 if ( fe.SetEncoding(encoding) )
160 {
161 fe.SetFixedOnly(fixedWidthOnly);
162
163 fe.DoEnumerate();
164 }
165 // else: no such fonts, unknown encoding
166
167 return TRUE;
168 }
169
170 bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
171 {
172 wxFAIL_MSG(wxT("TODO"));
173
174 return TRUE;
175 }
176
177 // ----------------------------------------------------------------------------
178 // Windows callbacks
179 // ----------------------------------------------------------------------------
180
181 int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm,
182 DWORD dwStyle, LONG lParam)
183 {
184 // Get rid of any fonts that we don't want...
185 if ( dwStyle != TRUETYPE_FONTTYPE )
186 {
187 // continue enumeration
188 return TRUE;
189 }
190
191 wxFontEnumeratorHelper *fontEnum = (wxFontEnumeratorHelper *)lParam;
192
193 return fontEnum->OnFont(lplf, lptm);
194 }
195