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
, 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("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
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
88 if ( families
.Index(family
) == wxNOT_FOUND
)
90 if ( !This
->OnFontFamily(family
) )
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 bool wxFontEnumerator::EnumerateFamilies(bool fixedWidthOnly
)
113 if ( fixedWidthOnly
)
116 fonts
= CreateFontList(wxT('m'), &nFonts
);
119 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
121 XFreeFontNames(fonts
);
129 fonts
= CreateFontList(wxT('c'), &nFonts
);
137 fonts
= CreateFontList(wxT('*'), &nFonts
);
141 wxFAIL_MSG(wxT("No fonts at all on this system?"));
147 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
149 XFreeFontNames(fonts
);
154 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
157 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
158 family
.IsEmpty() ? wxT("*") : family
.c_str());
160 // get the list of all fonts
162 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
,
171 // extract the list of (unique) encodings
172 wxSortedArrayString encodings
;
173 for ( int n
= 0; n
< nFonts
; n
++ )
175 char *font
= fonts
[n
];
176 if ( !wxString(font
).Matches("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
178 // it's not a full font name (probably an alias)
182 // extract the family
183 char *dash
= strchr(font
+ 1, '-');
184 char *familyFont
= dash
+ 1;
185 dash
= strchr(familyFont
, '-');
186 *dash
= '\0'; // !NULL because Matches() above succeeded
188 if ( !family
.IsEmpty() && (family
!= familyFont
) )
190 // family doesn't match
194 // now extract the registry/encoding
195 char *p
= dash
+ 1; // just after the dash after family
196 dash
= strrchr(p
, '-');
198 wxString
registry(dash
+ 1);
201 dash
= strrchr(p
, '-');
202 wxString
encoding(dash
+ 1);
204 encoding
<< wxT('-') << registry
;
205 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
207 if ( !OnFontEncoding(familyFont
, encoding
) )
212 encodings
.Add(encoding
);
214 //else: already had this one
217 XFreeFontNames(fonts
);