1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontenum.cpp
3 // Purpose: wxFontEnumerator class for X11/GDK
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "fontenum.h"
25 #include "wx/dynarray.h"
26 #include "wx/string.h"
29 #include "wx/fontenum.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // create the list of all fonts with the given spacing
38 static char **CreateFontList(wxChar spacing
, int *nFonts
);
40 // extract all font families from the given font list and call our
41 // OnFontFamily() for each of them
42 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 static char **CreateFontList(wxChar spacing
, int *nFonts
)
62 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-*-*"), spacing
);
64 // get the list of all fonts
65 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
68 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
72 // extract the list of (unique) font families
73 wxSortedArrayString families
;
74 for ( int n
= 0; n
< nFonts
; n
++ )
76 char *font
= fonts
[n
];
77 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
79 // it's not a full font name (probably an alias)
83 char *dash
= strchr(font
+ 1, '-');
84 char *family
= dash
+ 1;
85 dash
= strchr(family
, '-');
86 *dash
= '\0'; // !NULL because Matches() above succeeded
89 if ( families
.Index(fam
) == wxNOT_FOUND
)
91 if ( !This
->OnFontFamily(fam
) )
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 bool wxFontEnumerator::EnumerateFamilies(bool fixedWidthOnly
)
114 if ( fixedWidthOnly
)
117 fonts
= CreateFontList(wxT('m'), &nFonts
);
120 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
122 XFreeFontNames(fonts
);
130 fonts
= CreateFontList(wxT('c'), &nFonts
);
138 fonts
= CreateFontList(wxT('*'), &nFonts
);
142 wxFAIL_MSG(wxT("No fonts at all on this system?"));
148 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
150 XFreeFontNames(fonts
);
155 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
158 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
159 family
.IsEmpty() ? wxT("*") : family
.c_str());
161 // get the list of all fonts
163 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
172 // extract the list of (unique) encodings
173 wxSortedArrayString encodings
;
174 for ( int n
= 0; n
< nFonts
; n
++ )
176 char *font
= fonts
[n
];
177 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
179 // it's not a full font name (probably an alias)
183 // extract the family
184 char *dash
= strchr(font
+ 1, '-');
185 char *familyFont
= dash
+ 1;
186 dash
= strchr(familyFont
, '-');
187 *dash
= '\0'; // !NULL because Matches() above succeeded
189 if ( !family
.IsEmpty() && (family
!= familyFont
) )
191 // family doesn't match
195 // now extract the registry/encoding
196 char *p
= dash
+ 1; // just after the dash after family
197 dash
= strrchr(p
, '-');
199 wxString
registry(dash
+ 1);
202 dash
= strrchr(p
, '-');
203 wxString
encoding(dash
+ 1);
205 encoding
<< wxT('-') << registry
;
206 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
208 if ( !OnFontEncoding(familyFont
, encoding
) )
213 encodings
.Add(encoding
);
215 //else: already had this one
218 XFreeFontNames(fonts
);