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