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