added wxUSE_FONTENUM for wxFontEnumerator
[wxWidgets.git] / src / unix / fontenum.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontenum.cpp
3 // Purpose: wxFontEnumerator class for X11/GDK
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 01.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if wxUSE_FONTENUM
24
25 #include "wx/fontenum.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/dynarray.h"
29 #include "wx/string.h"
30 #include "wx/app.h"
31 #include "wx/utils.h"
32 #endif
33
34 #include "wx/regex.h"
35 #include "wx/fontmap.h"
36 #include "wx/fontutil.h"
37 #include "wx/encinfo.h"
38
39 // ----------------------------------------------------------------------------
40 // Pango
41 // ----------------------------------------------------------------------------
42
43 #if wxUSE_PANGO
44
45 #include "pango/pango.h"
46
47 #ifdef __WXGTK20__
48 #include "gtk/gtk.h"
49 extern GtkWidget *wxGetRootWindow();
50 #endif // __WXGTK20__
51
52 extern "C" int wxCMPFUNC_CONV
53 wxCompareFamilies (const void *a, const void *b)
54 {
55 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
56 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
57
58 return g_utf8_collate (a_name, b_name);
59 }
60
61 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
62 bool fixedWidthOnly)
63 {
64 if ( encoding != wxFONTENCODING_SYSTEM && encoding != wxFONTENCODING_UTF8 )
65 {
66 // Pango supports only UTF-8 encoding (and system means any, so we
67 // accept it too)
68 return false;
69 }
70
71 #if defined(__WXGTK20__) || !defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
72 if ( fixedWidthOnly
73 #if defined(__WXGTK24__)
74 && (gtk_check_version(2,4,0) != NULL)
75 #endif
76 )
77 {
78 OnFacename( wxT("monospace") );
79 }
80 else // !fixedWidthOnly
81 #endif // __WXGTK20__ || !HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
82 {
83 PangoFontFamily **families = NULL;
84 gint n_families = 0;
85 pango_context_list_families (
86 #ifdef __WXGTK20__
87 gtk_widget_get_pango_context( wxGetRootWindow() ),
88 #else
89 wxTheApp->GetPangoContext(),
90 #endif
91 &families, &n_families );
92 qsort (families, n_families, sizeof (PangoFontFamily *), wxCompareFamilies);
93
94 for (int i=0; i<n_families; i++)
95 {
96 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
97 if (!fixedWidthOnly || (
98 #ifdef __WXGTK24__
99 !gtk_check_version(2,4,0) &&
100 #endif
101 pango_font_family_is_monospace(families[i])
102 ) )
103 #endif
104 {
105 const gchar *name = pango_font_family_get_name(families[i]);
106 OnFacename(wxString(name, wxConvUTF8));
107 }
108 }
109 g_free(families);
110 }
111
112 return true;
113 }
114
115 bool wxFontEnumerator::EnumerateEncodings(const wxString& facename)
116 {
117 return EnumerateEncodingsUTF8(facename);
118 }
119
120
121 #else // !wxUSE_PANGO
122
123 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
124 // The resulting warnings are switched off here
125 #pragma message disable nosimpint
126 #endif
127 #include <X11/Xlib.h>
128 #ifdef __VMS__
129 #pragma message enable nosimpint
130 #endif
131
132 // ----------------------------------------------------------------------------
133 // private functions
134 // ----------------------------------------------------------------------------
135
136 // create the list of all fonts with the given spacing and encoding
137 static char **CreateFontList(wxChar spacing, wxFontEncoding encoding,
138 int *nFonts);
139
140 // extract all font families from the given font list and call our
141 // OnFacename() for each of them
142 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
143 char **fonts,
144 int nFonts);
145
146
147 // ----------------------------------------------------------------------------
148 // private types
149 // ----------------------------------------------------------------------------
150
151 // ============================================================================
152 // implementation
153 // ============================================================================
154
155 // ----------------------------------------------------------------------------
156 // helpers
157 // ----------------------------------------------------------------------------
158
159 #if !wxUSE_NANOX
160 static char **CreateFontList(wxChar spacing,
161 wxFontEncoding encoding,
162 int *nFonts)
163 {
164 wxNativeEncodingInfo info;
165 wxGetNativeFontEncoding(encoding, &info);
166
167 #if wxUSE_FONTMAP
168 if ( !wxTestFontEncoding(info) )
169 {
170 // ask font mapper for a replacement
171 (void)wxFontMapper::Get()->GetAltForEncoding(encoding, &info);
172 }
173 #endif // wxUSE_FONTMAP
174
175 wxString pattern;
176 pattern.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
177 spacing,
178 info.xregistry.c_str(),
179 info.xencoding.c_str());
180
181 // get the list of all fonts
182 return XListFonts((Display *)wxGetDisplay(), pattern.mb_str(), 32767, nFonts);
183 }
184
185 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
186 char **fonts,
187 int nFonts)
188 {
189 #if wxUSE_REGEX
190 wxRegEx re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB);
191 #endif // wxUSE_REGEX
192
193 // extract the list of (unique) font families
194 wxSortedArrayString families;
195 for ( int n = 0; n < nFonts; n++ )
196 {
197 char *font = fonts[n];
198 #if wxUSE_REGEX
199 if ( !re.Matches(font) )
200 #else // !wxUSE_REGEX
201 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
202 #endif // wxUSE_REGEX/!wxUSE_REGEX
203 {
204 // it's not a full font name (probably an alias)
205 continue;
206 }
207
208 // coverity[returned_null]
209 char *dash = strchr(font + 1, '-');
210 char *family = dash + 1;
211 dash = strchr(family, '-');
212 *dash = '\0'; // !NULL because Matches() above succeeded
213 wxString fam(family);
214
215 if ( families.Index(fam) == wxNOT_FOUND )
216 {
217 if ( !This->OnFacename(fam) )
218 {
219 // stop enumerating
220 return false;
221 }
222
223 families.Add(fam);
224 }
225 //else: already seen
226 }
227
228 return true;
229 }
230 #endif
231 // wxUSE_NANOX
232
233 // ----------------------------------------------------------------------------
234 // wxFontEnumerator
235 // ----------------------------------------------------------------------------
236
237 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
238 bool fixedWidthOnly)
239 {
240 #if wxUSE_NANOX
241 return false;
242 #else
243 int nFonts;
244 char **fonts;
245
246 if ( fixedWidthOnly )
247 {
248 bool cont = true;
249 fonts = CreateFontList(wxT('m'), encoding, &nFonts);
250 if ( fonts )
251 {
252 cont = ProcessFamiliesFromFontList(this, fonts, nFonts);
253
254 XFreeFontNames(fonts);
255 }
256
257 if ( !cont )
258 {
259 return true;
260 }
261
262 fonts = CreateFontList(wxT('c'), encoding, &nFonts);
263 if ( !fonts )
264 {
265 return true;
266 }
267 }
268 else
269 {
270 fonts = CreateFontList(wxT('*'), encoding, &nFonts);
271
272 if ( !fonts )
273 {
274 // it's ok if there are no fonts in given encoding - but it's not
275 // ok if there are no fonts at all
276 wxASSERT_MSG(encoding != wxFONTENCODING_SYSTEM,
277 wxT("No fonts at all on this system?"));
278
279 return false;
280 }
281 }
282
283 (void)ProcessFamiliesFromFontList(this, fonts, nFonts);
284
285 XFreeFontNames(fonts);
286 return true;
287 #endif
288 // wxUSE_NANOX
289 }
290
291 bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
292 {
293 #if wxUSE_NANOX
294 return false;
295 #else
296 wxString pattern;
297 pattern.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
298 family.empty() ? wxT("*") : family.c_str());
299
300 // get the list of all fonts
301 int nFonts;
302 char **fonts = XListFonts((Display *)wxGetDisplay(), pattern.mb_str(),
303 32767, &nFonts);
304
305 if ( !fonts )
306 {
307 // unknown family?
308 return false;
309 }
310
311 // extract the list of (unique) encodings
312 wxSortedArrayString encodings;
313 for ( int n = 0; n < nFonts; n++ )
314 {
315 char *font = fonts[n];
316 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
317 {
318 // it's not a full font name (probably an alias)
319 continue;
320 }
321
322 // extract the family
323 char *dash = strchr(font + 1, '-');
324 char *familyFont = dash + 1;
325 dash = strchr(familyFont, '-');
326 *dash = '\0'; // !NULL because Matches() above succeeded
327
328 if ( !family.empty() && (family != familyFont) )
329 {
330 // family doesn't match
331 continue;
332 }
333
334 // now extract the registry/encoding
335 char *p = dash + 1; // just after the dash after family
336 dash = strrchr(p, '-');
337
338 wxString registry(dash + 1);
339 *dash = '\0';
340
341 dash = strrchr(p, '-');
342 wxString encoding(dash + 1);
343
344 encoding << wxT('-') << registry;
345 if ( encodings.Index(encoding) == wxNOT_FOUND )
346 {
347 if ( !OnFontEncoding(familyFont, encoding) )
348 {
349 break;
350 }
351
352 encodings.Add(encoding);
353 }
354 //else: already had this one
355 }
356
357 XFreeFontNames(fonts);
358
359 return true;
360 #endif
361 // wxUSE_NANOX
362 }
363
364 #endif // !wxUSE_PANGO
365
366 #endif // wxUSE_FONTENUM