1. added encoding param to wxFontEncoding::EnumFamilies() which allows to get
[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 #ifdef __GNUG__
21 #pragma implementation "fontenum.h"
22 #endif
23
24 #include "wx/defs.h"
25 #include "wx/dynarray.h"
26 #include "wx/string.h"
27 #include "wx/utils.h"
28
29 #include "wx/fontenum.h"
30
31 #include <X11/Xlib.h>
32
33 // ----------------------------------------------------------------------------
34 // private functions
35 // ----------------------------------------------------------------------------
36
37 // create the list of all fonts with the given spacing and encoding
38 static char **CreateFontList(wxChar spacing, wxFontEncoding encoding,
39 int *nFonts);
40
41 // extract all font families from the given font list and call our
42 // OnFontFamily() for each of them
43 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
44 char **fonts,
45 int nFonts);
46
47
48 // ----------------------------------------------------------------------------
49 // private types
50 // ----------------------------------------------------------------------------
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // helpers
58 // ----------------------------------------------------------------------------
59
60 static char **CreateFontList(wxChar spacing,
61 wxFontEncoding encoding,
62 int *nFonts)
63 {
64 wxString xencoding, xregistry;
65 wxGetXFontEncoding(encoding, &xencoding, &xregistry);
66
67 wxString pattern;
68 pattern.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
69 spacing, xregistry.c_str(), xencoding.c_str());
70
71 // get the list of all fonts
72 return XListFonts((Display *)wxGetDisplay(), pattern.mb_str(), 32767, nFonts);
73 }
74
75 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
76 char **fonts,
77 int nFonts)
78 {
79 // extract the list of (unique) font families
80 wxSortedArrayString families;
81 for ( int n = 0; n < nFonts; n++ )
82 {
83 char *font = fonts[n];
84 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
85 {
86 // it's not a full font name (probably an alias)
87 continue;
88 }
89
90 char *dash = strchr(font + 1, '-');
91 char *family = dash + 1;
92 dash = strchr(family, '-');
93 *dash = '\0'; // !NULL because Matches() above succeeded
94 wxString fam(family);
95
96 if ( families.Index(fam) == wxNOT_FOUND )
97 {
98 if ( !This->OnFontFamily(fam) )
99 {
100 // stop enumerating
101 return FALSE;
102 }
103
104 families.Add(fam);
105 }
106 //else: already seen
107 }
108
109 return TRUE;
110 }
111
112 // ----------------------------------------------------------------------------
113 // wxFontEnumerator
114 // ----------------------------------------------------------------------------
115
116 bool wxFontEnumerator::EnumerateFamilies(wxFontEncoding encoding,
117 bool fixedWidthOnly)
118 {
119 int nFonts;
120 char **fonts;
121
122 if ( fixedWidthOnly )
123 {
124 bool cont = TRUE;
125 fonts = CreateFontList(wxT('m'), encoding, &nFonts);
126 if ( fonts )
127 {
128 cont = ProcessFamiliesFromFontList(this, fonts, nFonts);
129
130 XFreeFontNames(fonts);
131 }
132
133 if ( !cont )
134 {
135 return TRUE;
136 }
137
138 fonts = CreateFontList(wxT('c'), encoding, &nFonts);
139 if ( !fonts )
140 {
141 return TRUE;
142 }
143 }
144 else
145 {
146 fonts = CreateFontList(wxT('*'), encoding, &nFonts);
147
148 if ( !fonts )
149 {
150 // it's ok if there are no fonts in given encoding - but it's not
151 // ok if there are no fonts at all
152 wxASSERT_MSG(encoding != wxFONTENCODING_SYSTEM,
153 wxT("No fonts at all on this system?"));
154
155 return FALSE;
156 }
157 }
158
159 (void)ProcessFamiliesFromFontList(this, fonts, nFonts);
160
161 XFreeFontNames(fonts);
162
163 return TRUE;
164 }
165
166 bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
167 {
168 wxString pattern;
169 pattern.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
170 family.IsEmpty() ? wxT("*") : family.c_str());
171
172 // get the list of all fonts
173 int nFonts;
174 char **fonts = XListFonts((Display *)wxGetDisplay(), pattern.mb_str(),
175 32767, &nFonts);
176
177 if ( !fonts )
178 {
179 // unknown family?
180 return FALSE;
181 }
182
183 // extract the list of (unique) encodings
184 wxSortedArrayString encodings;
185 for ( int n = 0; n < nFonts; n++ )
186 {
187 char *font = fonts[n];
188 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
189 {
190 // it's not a full font name (probably an alias)
191 continue;
192 }
193
194 // extract the family
195 char *dash = strchr(font + 1, '-');
196 char *familyFont = dash + 1;
197 dash = strchr(familyFont, '-');
198 *dash = '\0'; // !NULL because Matches() above succeeded
199
200 if ( !family.IsEmpty() && (family != familyFont) )
201 {
202 // family doesn't match
203 continue;
204 }
205
206 // now extract the registry/encoding
207 char *p = dash + 1; // just after the dash after family
208 dash = strrchr(p, '-');
209
210 wxString registry(dash + 1);
211 *dash = '\0';
212
213 dash = strrchr(p, '-');
214 wxString encoding(dash + 1);
215
216 encoding << wxT('-') << registry;
217 if ( encodings.Index(encoding) == wxNOT_FOUND )
218 {
219 if ( !OnFontEncoding(familyFont, encoding) )
220 {
221 break;
222 }
223
224 encodings.Add(encoding);
225 }
226 //else: already had this one
227 }
228
229 XFreeFontNames(fonts);
230
231 return TRUE;
232 }