]> git.saurik.com Git - wxWidgets.git/blame - src/unix/fontenum.cpp
Added check for sel == -1
[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
29#include "wx/fontenum.h"
30
31#include <X11/Xlib.h>
32
33// ----------------------------------------------------------------------------
34// private functions
35// ----------------------------------------------------------------------------
36
36f210c8
VZ
37// create the list of all fonts with the given spacing and encoding
38static char **CreateFontList(wxChar spacing, wxFontEncoding encoding,
39 int *nFonts);
d111a89a
VZ
40
41// extract all font families from the given font list and call our
42// OnFontFamily() for each of them
43static 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
36f210c8
VZ
60static char **CreateFontList(wxChar spacing,
61 wxFontEncoding encoding,
62 int *nFonts)
d111a89a 63{
36f210c8 64 wxString xencoding, xregistry;
b02da6b1 65 wxGetXFontEncoding(encoding, &xregistry, &xencoding);
36f210c8 66
d111a89a 67 wxString pattern;
36f210c8
VZ
68 pattern.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
69 spacing, xregistry.c_str(), xencoding.c_str());
d111a89a
VZ
70
71 // get the list of all fonts
7dd62924 72 return XListFonts((Display *)wxGetDisplay(), pattern.mb_str(), 32767, nFonts);
d111a89a
VZ
73}
74
75static 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];
7dd62924 84 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
d111a89a
VZ
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
7dd62924 94 wxString fam(family);
d111a89a 95
7dd62924 96 if ( families.Index(fam) == wxNOT_FOUND )
d111a89a 97 {
7dd62924 98 if ( !This->OnFontFamily(fam) )
d111a89a
VZ
99 {
100 // stop enumerating
101 return FALSE;
102 }
103
7dd62924 104 families.Add(fam);
d111a89a
VZ
105 }
106 //else: already seen
107 }
108
109 return TRUE;
110}
111
112// ----------------------------------------------------------------------------
113// wxFontEnumerator
114// ----------------------------------------------------------------------------
115
36f210c8
VZ
116bool wxFontEnumerator::EnumerateFamilies(wxFontEncoding encoding,
117 bool fixedWidthOnly)
d111a89a
VZ
118{
119 int nFonts;
120 char **fonts;
121
122 if ( fixedWidthOnly )
123 {
124 bool cont = TRUE;
36f210c8 125 fonts = CreateFontList(wxT('m'), encoding, &nFonts);
d111a89a
VZ
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
36f210c8 138 fonts = CreateFontList(wxT('c'), encoding, &nFonts);
d111a89a
VZ
139 if ( !fonts )
140 {
141 return TRUE;
142 }
143 }
144 else
145 {
36f210c8 146 fonts = CreateFontList(wxT('*'), encoding, &nFonts);
d111a89a
VZ
147
148 if ( !fonts )
149 {
36f210c8
VZ
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?"));
d111a89a
VZ
154
155 return FALSE;
156 }
157 }
158
159 (void)ProcessFamiliesFromFontList(this, fonts, nFonts);
160
161 XFreeFontNames(fonts);
162
163 return TRUE;
164}
165
166bool 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;
7dd62924 174 char **fonts = XListFonts((Display *)wxGetDisplay(), pattern.mb_str(),
d111a89a
VZ
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];
7dd62924 188 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
d111a89a
VZ
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}