]> git.saurik.com Git - wxWidgets.git/blob - src/motif/fontenum.cpp
wxFontEnumerator class for Motif/X
[wxWidgets.git] / src / motif / fontenum.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/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 // compare function for sorted array of strings
38 static int CMPFUNC_CONV CompareStrings(const char *s1, const char *s2);
39
40 // create the list of all fonts with the given spacing
41 static char **CreateFontList(wxChar spacing, int *nFonts);
42
43 // extract all font families from the given font list and call our
44 // OnFontFamily() for each of them
45 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
46 char **fonts,
47 int nFonts);
48
49
50 // ----------------------------------------------------------------------------
51 // private types
52 // ----------------------------------------------------------------------------
53
54 WX_DEFINE_SORTED_ARRAY(const char *, wxSortedStringArray);
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // helpers
62 // ----------------------------------------------------------------------------
63
64 static int CMPFUNC_CONV CompareStrings(const char *s1, const char *s2)
65 {
66 return strcmp(s1, s2);
67 }
68
69 static char **CreateFontList(wxChar spacing, int *nFonts)
70 {
71 wxString pattern;
72 pattern.Printf(_T("-*-*-*-*-*-*-*-*-*-*-%c-*-*-*"), spacing);
73
74 // get the list of all fonts
75 return XListFonts((Display *)wxGetDisplay(), pattern, 32767, nFonts);
76 }
77
78 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
79 char **fonts,
80 int nFonts)
81 {
82 // extract the list of (unique) font families
83 wxSortedStringArray families(CompareStrings);
84 for ( int n = 0; n < nFonts; n++ )
85 {
86 char *font = fonts[n];
87 if ( !wxString(font).Matches("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
88 {
89 // it's not a full font name (probably an alias)
90 continue;
91 }
92
93 char *dash = strchr(font + 1, '-');
94 char *family = dash + 1;
95 dash = strchr(family, '-');
96 *dash = '\0'; // !NULL because Matches() above succeeded
97
98 if ( families.Index(family) == wxNOT_FOUND )
99 {
100 if ( !This->OnFontFamily(family) )
101 {
102 // stop enumerating
103 return FALSE;
104 }
105
106 families.Add(family);
107 }
108 //else: already seen
109 }
110
111 return TRUE;
112 }
113
114 // ----------------------------------------------------------------------------
115 // wxFontEnumerator
116 // ----------------------------------------------------------------------------
117
118 bool wxFontEnumerator::EnumerateFamilies(bool fixedWidthOnly)
119 {
120 int nFonts;
121 char **fonts;
122
123 if ( fixedWidthOnly )
124 {
125 bool cont = TRUE;
126 fonts = CreateFontList(_T('m'), &nFonts);
127 if ( fonts )
128 {
129 cont = ProcessFamiliesFromFontList(this, fonts, nFonts);
130
131 XFreeFontNames(fonts);
132 }
133
134 if ( !cont )
135 {
136 return TRUE;
137 }
138
139 fonts = CreateFontList(_T('c'), &nFonts);
140 if ( !fonts )
141 {
142 return TRUE;
143 }
144 }
145 else
146 {
147 fonts = CreateFontList(_T('*'), &nFonts);
148
149 if ( !fonts )
150 {
151 wxFAIL_MSG(_T("No fonts at all on this system?"));
152
153 return FALSE;
154 }
155 }
156
157 (void)ProcessFamiliesFromFontList(this, fonts, nFonts);
158
159 XFreeFontNames(fonts);
160
161 return TRUE;
162 }
163
164 bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
165 {
166 #if 0
167 wxString pattern;
168 pattern.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
169 family.IsEmpty() ? _T("*") : family.c_str());
170
171 // get the list of all fonts
172 int nFonts;
173 char **fonts = XListFonts((Display *)wxGetDisplay(), pattern,
174 32767, nFonts);
175
176 if ( !fonts )
177 {
178 // unknown family?
179 return FALSE;
180 }
181
182 // extract the list of (unique) encodings
183 wxSortedStringArray families(CompareStrings);
184 for ( int n = 0; n < nFonts; n++ )
185 {
186 char *font = fonts[n];
187 if ( !wxString(font).Matches("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
188 {
189 // it's not a full font name (probably an alias)
190 continue;
191 }
192
193 // extract the family
194 char *dash = strchr(font + 1, '-');
195 char *family = dash + 1;
196 dash = strchr(family, '-');
197 *dash = '\0'; // !NULL because Matches() above succeeded
198
199 // now extract the registry/encoding
200 }
201
202 return TRUE;
203 #endif // 0
204
205 return FALSE; // TODO
206 }