]> git.saurik.com Git - wxWidgets.git/blame - src/unix/fontenum.cpp
More small fixes
[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
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
79e4b627 29#include "wx/fontmap.h"
d111a89a 30#include "wx/fontenum.h"
7beba2fc 31#include "wx/fontutil.h"
d111a89a 32
3fa056ab
JJ
33#ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
34 // The resulting warnings are switched off here
35#pragma message disable nosimpint
36#endif
d111a89a 37#include <X11/Xlib.h>
3fa056ab
JJ
38#ifdef __VMS__
39#pragma message enable nosimpint
40#endif
d111a89a
VZ
41
42// ----------------------------------------------------------------------------
43// private functions
44// ----------------------------------------------------------------------------
45
36f210c8
VZ
46// create the list of all fonts with the given spacing and encoding
47static char **CreateFontList(wxChar spacing, wxFontEncoding encoding,
48 int *nFonts);
d111a89a
VZ
49
50// extract all font families from the given font list and call our
3c1866e8 51// OnFacename() for each of them
d111a89a
VZ
52static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
53 char **fonts,
54 int nFonts);
55
56
57// ----------------------------------------------------------------------------
58// private types
59// ----------------------------------------------------------------------------
60
61// ============================================================================
62// implementation
63// ============================================================================
64
65// ----------------------------------------------------------------------------
66// helpers
67// ----------------------------------------------------------------------------
68
36f210c8
VZ
69static char **CreateFontList(wxChar spacing,
70 wxFontEncoding encoding,
71 int *nFonts)
d111a89a 72{
7beba2fc
VZ
73 wxNativeEncodingInfo info;
74 wxGetNativeFontEncoding(encoding, &info);
36f210c8 75
1e6feb95 76#if wxUSE_FONTMAP
79e4b627
VZ
77 if ( !wxTestFontEncoding(info) )
78 {
79 // ask font mapper for a replacement
80 (void)wxTheFontMapper->GetAltForEncoding(encoding, &info);
81 }
1e6feb95 82#endif // wxUSE_FONTMAP
79e4b627 83
d111a89a 84 wxString pattern;
36f210c8 85 pattern.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
7beba2fc
VZ
86 spacing,
87 info.xregistry.c_str(),
88 info.xencoding.c_str());
d111a89a
VZ
89
90 // get the list of all fonts
7dd62924 91 return XListFonts((Display *)wxGetDisplay(), pattern.mb_str(), 32767, nFonts);
d111a89a
VZ
92}
93
94static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
95 char **fonts,
96 int nFonts)
97{
98 // extract the list of (unique) font families
99 wxSortedArrayString families;
100 for ( int n = 0; n < nFonts; n++ )
101 {
102 char *font = fonts[n];
7dd62924 103 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
d111a89a
VZ
104 {
105 // it's not a full font name (probably an alias)
106 continue;
107 }
3c1866e8 108
d111a89a
VZ
109 char *dash = strchr(font + 1, '-');
110 char *family = dash + 1;
111 dash = strchr(family, '-');
112 *dash = '\0'; // !NULL because Matches() above succeeded
7dd62924 113 wxString fam(family);
d111a89a 114
7dd62924 115 if ( families.Index(fam) == wxNOT_FOUND )
d111a89a 116 {
3c1866e8 117 if ( !This->OnFacename(fam) )
d111a89a
VZ
118 {
119 // stop enumerating
120 return FALSE;
121 }
122
7dd62924 123 families.Add(fam);
d111a89a
VZ
124 }
125 //else: already seen
126 }
127
128 return TRUE;
129}
130
131// ----------------------------------------------------------------------------
132// wxFontEnumerator
133// ----------------------------------------------------------------------------
134
3c1866e8
VZ
135bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
136 bool fixedWidthOnly)
d111a89a
VZ
137{
138 int nFonts;
139 char **fonts;
140
141 if ( fixedWidthOnly )
142 {
143 bool cont = TRUE;
36f210c8 144 fonts = CreateFontList(wxT('m'), encoding, &nFonts);
d111a89a
VZ
145 if ( fonts )
146 {
147 cont = ProcessFamiliesFromFontList(this, fonts, nFonts);
148
149 XFreeFontNames(fonts);
150 }
151
152 if ( !cont )
153 {
154 return TRUE;
155 }
156
36f210c8 157 fonts = CreateFontList(wxT('c'), encoding, &nFonts);
d111a89a
VZ
158 if ( !fonts )
159 {
160 return TRUE;
161 }
162 }
163 else
164 {
36f210c8 165 fonts = CreateFontList(wxT('*'), encoding, &nFonts);
d111a89a
VZ
166
167 if ( !fonts )
168 {
36f210c8
VZ
169 // it's ok if there are no fonts in given encoding - but it's not
170 // ok if there are no fonts at all
171 wxASSERT_MSG(encoding != wxFONTENCODING_SYSTEM,
172 wxT("No fonts at all on this system?"));
d111a89a
VZ
173
174 return FALSE;
175 }
176 }
177
178 (void)ProcessFamiliesFromFontList(this, fonts, nFonts);
179
180 XFreeFontNames(fonts);
181
182 return TRUE;
183}
184
185bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
186{
187 wxString pattern;
188 pattern.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
189 family.IsEmpty() ? wxT("*") : family.c_str());
190
191 // get the list of all fonts
192 int nFonts;
7dd62924 193 char **fonts = XListFonts((Display *)wxGetDisplay(), pattern.mb_str(),
d111a89a
VZ
194 32767, &nFonts);
195
196 if ( !fonts )
197 {
198 // unknown family?
199 return FALSE;
200 }
201
202 // extract the list of (unique) encodings
203 wxSortedArrayString encodings;
204 for ( int n = 0; n < nFonts; n++ )
205 {
206 char *font = fonts[n];
7dd62924 207 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
d111a89a
VZ
208 {
209 // it's not a full font name (probably an alias)
210 continue;
211 }
212
213 // extract the family
214 char *dash = strchr(font + 1, '-');
215 char *familyFont = dash + 1;
216 dash = strchr(familyFont, '-');
217 *dash = '\0'; // !NULL because Matches() above succeeded
218
219 if ( !family.IsEmpty() && (family != familyFont) )
220 {
221 // family doesn't match
222 continue;
223 }
224
225 // now extract the registry/encoding
226 char *p = dash + 1; // just after the dash after family
227 dash = strrchr(p, '-');
228
229 wxString registry(dash + 1);
230 *dash = '\0';
231
232 dash = strrchr(p, '-');
233 wxString encoding(dash + 1);
234
235 encoding << wxT('-') << registry;
236 if ( encodings.Index(encoding) == wxNOT_FOUND )
237 {
238 if ( !OnFontEncoding(familyFont, encoding) )
239 {
240 break;
241 }
242
243 encodings.Add(encoding);
244 }
245 //else: already had this one
246 }
247
248 XFreeFontNames(fonts);
249
250 return TRUE;
251}