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