]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/langlistctrl.py
Patches from Andrea
[wxWidgets.git] / wxPython / wx / lib / langlistctrl.py
1 #-----------------------------------------------------------------------------
2 # Name: languagectrls.py
3 # Purpose:
4 #
5 # Author: Riaan Booysen
6 #
7 # Created: 2006
8 # RCS-ID: $Id$
9 # Copyright: (c) 2006 Riaan Booysen
10 # License: wxPython
11 #-----------------------------------------------------------------------------
12 """ ListCtrl and functions to display languages and the flags of their countries
13 """
14 import wx
15
16 from wx.lib.art import flagart
17
18 langIdCountryMap = {
19 # generated from wx.Locale info and locale.windows_locale
20 wx.LANGUAGE_AFRIKAANS: 'ZA',
21 wx.LANGUAGE_ALBANIAN: 'AL',
22 wx.LANGUAGE_ARABIC_ALGERIA: 'DZ',
23 wx.LANGUAGE_ARABIC_BAHRAIN: 'BH',
24 wx.LANGUAGE_ARABIC_EGYPT: 'EG',
25 wx.LANGUAGE_ARABIC_IRAQ: 'IQ',
26 wx.LANGUAGE_ARABIC_JORDAN: 'JO',
27 wx.LANGUAGE_ARABIC_KUWAIT: 'KW',
28 wx.LANGUAGE_ARABIC_LEBANON: 'LB',
29 wx.LANGUAGE_ARABIC_LIBYA: 'LY',
30 wx.LANGUAGE_ARABIC_MOROCCO: 'MA',
31 wx.LANGUAGE_ARABIC_OMAN: 'OM',
32 wx.LANGUAGE_ARABIC_QATAR: 'QA',
33 wx.LANGUAGE_ARABIC_SAUDI_ARABIA: 'SA',
34 wx.LANGUAGE_ARABIC_SUDAN: 'SD',
35 wx.LANGUAGE_ARABIC_SYRIA: 'SY',
36 wx.LANGUAGE_ARABIC_TUNISIA: 'TN',
37 wx.LANGUAGE_ARABIC_UAE: 'AE',
38 wx.LANGUAGE_ARABIC_YEMEN: 'YE',
39 wx.LANGUAGE_ARMENIAN: 'AM',
40 wx.LANGUAGE_AZERI: 'AZ',
41 wx.LANGUAGE_AZERI_CYRILLIC: 'AZ',
42 wx.LANGUAGE_AZERI_LATIN: 'AZ',
43 wx.LANGUAGE_BASQUE: 'ES',
44 wx.LANGUAGE_BELARUSIAN: 'BY',
45 wx.LANGUAGE_BENGALI: 'IN',
46 wx.LANGUAGE_BRETON: 'FR',
47 wx.LANGUAGE_BULGARIAN: 'BG',
48 wx.LANGUAGE_CATALAN: 'ES',
49 wx.LANGUAGE_CHINESE: 'CN',
50 wx.LANGUAGE_CHINESE_HONGKONG: 'HK',
51 wx.LANGUAGE_CHINESE_MACAU: 'MO',
52 wx.LANGUAGE_CHINESE_SIMPLIFIED: 'CN',
53 wx.LANGUAGE_CHINESE_SINGAPORE: 'SG',
54 wx.LANGUAGE_CHINESE_TAIWAN: 'TW',
55 wx.LANGUAGE_CHINESE_TRADITIONAL: 'CN',
56 wx.LANGUAGE_CROATIAN: 'HR',
57 wx.LANGUAGE_CZECH: 'CZ',
58 wx.LANGUAGE_DANISH: 'DK',
59 # wx.LANGUAGE_DEFAULT: 'ZA',
60 wx.LANGUAGE_DUTCH: 'NL',
61 wx.LANGUAGE_DUTCH_BELGIAN: 'BE',
62 wx.LANGUAGE_ENGLISH: 'GB',
63 wx.LANGUAGE_ENGLISH_AUSTRALIA: 'AU',
64 wx.LANGUAGE_ENGLISH_BELIZE: 'BZ',
65 wx.LANGUAGE_ENGLISH_BOTSWANA: 'BW',
66 wx.LANGUAGE_ENGLISH_CANADA: 'CA',
67 wx.LANGUAGE_ENGLISH_CARIBBEAN: 'CB',
68 wx.LANGUAGE_ENGLISH_DENMARK: 'DK',
69 wx.LANGUAGE_ENGLISH_EIRE: 'IE',
70 wx.LANGUAGE_ENGLISH_JAMAICA: 'JM',
71 wx.LANGUAGE_ENGLISH_NEW_ZEALAND: 'NZ',
72 wx.LANGUAGE_ENGLISH_PHILIPPINES: 'PH',
73 wx.LANGUAGE_ENGLISH_SOUTH_AFRICA: 'ZA',
74 wx.LANGUAGE_ENGLISH_TRINIDAD: 'TT',
75 wx.LANGUAGE_ENGLISH_UK: 'GB',
76 wx.LANGUAGE_ENGLISH_US: 'US',
77 wx.LANGUAGE_ENGLISH_ZIMBABWE: 'ZW',
78 wx.LANGUAGE_ESTONIAN: 'EE',
79 wx.LANGUAGE_FAEROESE: 'FO',
80 wx.LANGUAGE_FARSI: 'IR',
81 wx.LANGUAGE_FINNISH: 'FI',
82 wx.LANGUAGE_FRENCH: 'FR',
83 wx.LANGUAGE_FRENCH_BELGIAN: 'BE',
84 wx.LANGUAGE_FRENCH_CANADIAN: 'CA',
85 wx.LANGUAGE_FRENCH_LUXEMBOURG: 'LU',
86 wx.LANGUAGE_FRENCH_MONACO: 'MC',
87 wx.LANGUAGE_FRENCH_SWISS: 'CH',
88 wx.LANGUAGE_FRISIAN: 'NL',
89 wx.LANGUAGE_GALICIAN: 'ES',
90 wx.LANGUAGE_GEORGIAN: 'GE',
91 wx.LANGUAGE_GERMAN: 'DE',
92 wx.LANGUAGE_GERMAN_AUSTRIAN: 'AT',
93 wx.LANGUAGE_GERMAN_BELGIUM: 'BE',
94 wx.LANGUAGE_GERMAN_LIECHTENSTEIN: 'LI',
95 wx.LANGUAGE_GERMAN_LUXEMBOURG: 'LU',
96 wx.LANGUAGE_GERMAN_SWISS: 'CH',
97 wx.LANGUAGE_GREEK: 'GR',
98 wx.LANGUAGE_GREENLANDIC: 'GL',
99 wx.LANGUAGE_GUJARATI: 'IN',
100 wx.LANGUAGE_HEBREW: 'IL',
101 wx.LANGUAGE_HINDI: 'IN',
102 wx.LANGUAGE_HUNGARIAN: 'HU',
103 wx.LANGUAGE_ICELANDIC: 'IS',
104 wx.LANGUAGE_INDONESIAN: 'ID',
105 wx.LANGUAGE_INUKTITUT: 'CA',
106 wx.LANGUAGE_IRISH: 'IE',
107 wx.LANGUAGE_ITALIAN: 'IT',
108 wx.LANGUAGE_ITALIAN_SWISS: 'CH',
109 wx.LANGUAGE_JAPANESE: 'JP',
110 wx.LANGUAGE_KANNADA: 'IN',
111 wx.LANGUAGE_KASHMIRI_INDIA: 'IN',
112 wx.LANGUAGE_KAZAKH: 'KZ',
113 wx.LANGUAGE_KERNEWEK: 'GB',
114 wx.LANGUAGE_KIRGHIZ: 'KG',
115 wx.LANGUAGE_KOREAN: 'KR',
116 wx.LANGUAGE_LATVIAN: 'LV',
117 wx.LANGUAGE_LITHUANIAN: 'LT',
118 wx.LANGUAGE_MACEDONIAN: 'MK',
119 wx.LANGUAGE_MALAY: 'MY',
120 wx.LANGUAGE_MALAYALAM: 'IN',
121 wx.LANGUAGE_MALAY_BRUNEI_DARUSSALAM: 'BN',
122 wx.LANGUAGE_MALAY_MALAYSIA: 'MY',
123 wx.LANGUAGE_MALTESE: 'MT',
124 wx.LANGUAGE_MAORI: 'NZ',
125 wx.LANGUAGE_MARATHI: 'IN',
126 wx.LANGUAGE_MONGOLIAN: 'MN',
127 wx.LANGUAGE_NEPALI: 'NP',
128 wx.LANGUAGE_NEPALI_INDIA: 'IN',
129 wx.LANGUAGE_NORWEGIAN_BOKMAL: 'NO',
130 wx.LANGUAGE_NORWEGIAN_NYNORSK: 'NO',
131 wx.LANGUAGE_OCCITAN: 'FR',
132 wx.LANGUAGE_ORIYA: 'IN',
133 wx.LANGUAGE_PASHTO: 'AF',
134 wx.LANGUAGE_POLISH: 'PL',
135 wx.LANGUAGE_PORTUGUESE: 'PT',
136 wx.LANGUAGE_PORTUGUESE_BRAZILIAN: 'BR',
137 wx.LANGUAGE_PUNJABI: 'IN',
138 wx.LANGUAGE_RHAETO_ROMANCE: 'CH',
139 wx.LANGUAGE_ROMANIAN: 'RO',
140 wx.LANGUAGE_RUSSIAN: 'RU',
141 wx.LANGUAGE_RUSSIAN_UKRAINE: 'UA',
142 wx.LANGUAGE_SANSKRIT: 'IN',
143 wx.LANGUAGE_SERBIAN_CYRILLIC: 'YU',
144 wx.LANGUAGE_SERBIAN_LATIN: 'YU',
145 wx.LANGUAGE_SETSWANA: 'ZA',
146 wx.LANGUAGE_SLOVAK: 'SK',
147 wx.LANGUAGE_SLOVENIAN: 'SI',
148 wx.LANGUAGE_SPANISH: 'ES',
149 wx.LANGUAGE_SPANISH_ARGENTINA: 'AR',
150 wx.LANGUAGE_SPANISH_BOLIVIA: 'BO',
151 wx.LANGUAGE_SPANISH_CHILE: 'CL',
152 wx.LANGUAGE_SPANISH_COLOMBIA: 'CO',
153 wx.LANGUAGE_SPANISH_COSTA_RICA: 'CR',
154 wx.LANGUAGE_SPANISH_DOMINICAN_REPUBLIC: 'DO',
155 wx.LANGUAGE_SPANISH_ECUADOR: 'EC',
156 wx.LANGUAGE_SPANISH_EL_SALVADOR: 'SV',
157 wx.LANGUAGE_SPANISH_GUATEMALA: 'GT',
158 wx.LANGUAGE_SPANISH_HONDURAS: 'HN',
159 wx.LANGUAGE_SPANISH_MEXICAN: 'MX',
160 wx.LANGUAGE_SPANISH_MODERN: 'ES',
161 wx.LANGUAGE_SPANISH_NICARAGUA: 'NI',
162 wx.LANGUAGE_SPANISH_PANAMA: 'PA',
163 wx.LANGUAGE_SPANISH_PARAGUAY: 'PY',
164 wx.LANGUAGE_SPANISH_PERU: 'PE',
165 wx.LANGUAGE_SPANISH_PUERTO_RICO: 'PR',
166 wx.LANGUAGE_SPANISH_URUGUAY: 'UY',
167 wx.LANGUAGE_SPANISH_US: 'US',
168 wx.LANGUAGE_SPANISH_VENEZUELA: 'VE',
169 wx.LANGUAGE_SWAHILI: 'KE',
170 wx.LANGUAGE_SWEDISH: 'SE',
171 wx.LANGUAGE_SWEDISH_FINLAND: 'FI',
172 wx.LANGUAGE_TAGALOG: 'PH',
173 wx.LANGUAGE_TAMIL: 'IN',
174 wx.LANGUAGE_TATAR: 'RU',
175 wx.LANGUAGE_TELUGU: 'IN',
176 wx.LANGUAGE_THAI: 'TH',
177 wx.LANGUAGE_TURKISH: 'TR',
178 wx.LANGUAGE_UKRAINIAN: 'UA',
179 wx.LANGUAGE_URDU: 'PK',
180 wx.LANGUAGE_URDU_INDIA: 'IN',
181 wx.LANGUAGE_URDU_PAKISTAN: 'PK',
182 wx.LANGUAGE_UZBEK: 'UZ',
183 wx.LANGUAGE_UZBEK_CYRILLIC: 'UZ',
184 wx.LANGUAGE_UZBEK_LATIN: 'UZ',
185 wx.LANGUAGE_VIETNAMESE: 'VN',
186 wx.LANGUAGE_WELSH: 'GB',
187 wx.LANGUAGE_XHOSA: 'ZA',
188 wx.LANGUAGE_ZULU: 'ZA',
189 # manually defined language/country mapping
190 wx.LANGUAGE_ABKHAZIAN: 'GE',
191 wx.LANGUAGE_AFAR: 'ET',
192 wx.LANGUAGE_AMHARIC: 'ET',
193 wx.LANGUAGE_ASSAMESE: 'IN',
194 wx.LANGUAGE_AYMARA: 'BO',
195 wx.LANGUAGE_ARABIC: 'SA',
196 wx.LANGUAGE_BASHKIR: 'RU',
197 wx.LANGUAGE_BHUTANI: 'BT',
198 wx.LANGUAGE_BIHARI: 'IN',
199 wx.LANGUAGE_BISLAMA: 'VU',
200 wx.LANGUAGE_BURMESE: 'MM',
201 wx.LANGUAGE_CAMBODIAN: 'KH',
202 wx.LANGUAGE_CORSICAN: 'FR',
203 wx.LANGUAGE_ESPERANTO: 'ESPERANTO',
204 wx.LANGUAGE_FIJI: 'FJ',
205 wx.LANGUAGE_GUARANI: 'PY',
206 wx.LANGUAGE_HAUSA: 'NG',
207 wx.LANGUAGE_INTERLINGUA: 'US',
208 wx.LANGUAGE_INTERLINGUE: 'US',
209 wx.LANGUAGE_INUPIAK: 'US',
210 wx.LANGUAGE_JAVANESE: 'IN',
211 wx.LANGUAGE_KASHMIRI: 'IN',
212 wx.LANGUAGE_KINYARWANDA: 'RW',
213 wx.LANGUAGE_KIRUNDI: 'BI',
214 wx.LANGUAGE_KONKANI: 'IN',
215 wx.LANGUAGE_KURDISH: 'IQ',
216 wx.LANGUAGE_LAOTHIAN: 'LA',
217 wx.LANGUAGE_LATIN: 'VA',
218 wx.LANGUAGE_LINGALA: 'CD',
219 wx.LANGUAGE_MALAGASY: 'MG',
220 wx.LANGUAGE_MANIPURI: 'IN',
221 wx.LANGUAGE_MOLDAVIAN: 'MD',
222 wx.LANGUAGE_NAURU: 'NR',
223 wx.LANGUAGE_OROMO: 'ET',
224 wx.LANGUAGE_QUECHUA: 'BO',
225 wx.LANGUAGE_SAMOAN: 'WS',
226 wx.LANGUAGE_SANGHO: 'CF',
227 wx.LANGUAGE_SCOTS_GAELIC: 'GB',
228 wx.LANGUAGE_SERBO_CROATIAN: 'HR',
229 wx.LANGUAGE_SESOTHO: 'ZA',
230 wx.LANGUAGE_SHONA: 'ZW',
231 wx.LANGUAGE_SINDHI: 'PK',
232 wx.LANGUAGE_SINHALESE: 'IN',
233 wx.LANGUAGE_SISWATI: 'SZ',
234 wx.LANGUAGE_SOMALI: 'SB',
235 wx.LANGUAGE_SUNDANESE: 'SD',
236 wx.LANGUAGE_TAJIK: 'TJ',
237 wx.LANGUAGE_TIBETAN: 'CN',
238 wx.LANGUAGE_TIGRINYA: 'ET',
239 wx.LANGUAGE_TONGA: 'TO',
240 wx.LANGUAGE_TSONGA: 'MZ',
241 wx.LANGUAGE_TURKMEN: 'TM',
242 wx.LANGUAGE_TWI: 'GH',
243 wx.LANGUAGE_UIGHUR: 'CN',
244 wx.LANGUAGE_VOLAPUK: 'VOLAPUK',
245 wx.LANGUAGE_WOLOF: 'SN',
246 wx.LANGUAGE_YIDDISH: 'IL',
247 wx.LANGUAGE_YORUBA: 'NG',
248 wx.LANGUAGE_ZHUANG: 'CN',
249 }
250
251 LC_AVAILABLE, LC_ALL, LC_ONLY = 1, 2, 4
252
253
254 # wx.LANGUAGE_SERBIAN gives an error for me
255 _wxLangIds = [n for n in dir(wx) if n.startswith('LANGUAGE_')]
256 for _l in ('LANGUAGE_UNKNOWN', 'LANGUAGE_USER_DEFINED', 'LANGUAGE_SERBIAN'):
257 if _l in _wxLangIds:
258 _wxLangIds.remove(_l)
259
260
261 def CreateLanguagesResourceLists(filter=LC_AVAILABLE, only=()):
262 """ Returns a tuple of (bitmaps, language descriptions, language ids) """
263 icons = wx.ImageList(16, 11)
264 names = []
265 langs = []
266
267 langIdNameMap = BuildLanguageCountryMapping()
268
269 wxLangIds = []
270 for li in _wxLangIds:
271 wxLI = getattr(wx, li)
272 try:
273 if (filter == LC_ONLY and wxLI in only) or \
274 (filter == LC_AVAILABLE and wx.Locale.IsAvailable(wxLI)) or \
275 (filter == LC_ALL):
276 wxLangIds.append(wxLI)
277 except wx.PyAssertionError:
278 # invalid language assertions
279 pass
280 except AttributeError:
281 # wx 2.6
282 wxLangIds.append(wxLI)
283
284 langCodes = [(langIdNameMap[wxLangId], wxLangId)
285 for wxLangId in wxLangIds
286 if wxLangId in langIdNameMap]
287
288 for lc, wxli in langCodes:
289 l, cnt = lc.split('_')
290
291 if cnt in flagart.catalog:
292 bmp = flagart.catalog[cnt].getBitmap()
293 else:
294 bmp = flagart.catalog['BLANK'].getBitmap()
295
296 icons.Add(bmp)
297 name = wx.Locale.GetLanguageName(wxli)
298 if wxli == wx.LANGUAGE_DEFAULT:
299 #print cnt, name, lc, wxli
300 name = 'Default: '+name
301
302 names.append(name)
303 langs.append(wxli)
304
305 return icons, names, langs
306
307
308 def GetLanguageFlag(lang):
309 """ Returns a bitmap of the flag for the country of the language id """
310 langIdNameMap = BuildLanguageCountryMapping()
311 if lang in langIdNameMap:
312 cnt = langIdNameMap[lang].split('_')[1]
313 if cnt in flagart.catalog:
314 return flagart.catalog[cnt].getBitmap()
315 return flagart.catalog['BLANK'].getBitmap()
316
317
318 def BuildLanguageCountryMapping():
319 """ Builds a mapping of language ids to LANG_COUNTRY codes """
320 res = {}
321 for name in _wxLangIds:
322 n = 'wx.'+name
323 wn = getattr(wx, name)
324
325 li = wx.Locale.GetLanguageInfo(wn)
326 if li:
327 code = li.CanonicalName
328
329 if wn in langIdCountryMap:
330 # override, drop country
331 if '_' in code:
332 code = code.split('_')[0]
333 code += '_'+langIdCountryMap[wn]
334
335 # map unhandled to blank images
336 elif '_' not in code:
337 code += '_BLANK'
338
339 res[wn] = code
340 return res
341
342 def GetWxIdentifierForLanguage(lang):
343 """ Returns the language id as a string """
344 for n in dir(wx):
345 if n.startswith('LANGUAGE_') and getattr(wx, n) == lang:
346 return n
347 raise Exception, 'Language %s not found'%lang
348
349
350 #-------------------------------------------------------------------------------
351
352 class LanguageListCtrl(wx.ListCtrl):
353 """ wx.ListCtrl derived control that displays languages and flags """
354 def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
355 size=wx.DefaultSize, style=wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL,
356 filter=LC_AVAILABLE, only=(), select=None, name='languagelistctrl'):
357
358 wx.ListCtrl.__init__(self, parent, id, pos, size, style, name=name)
359
360 self.SetUpFilter(filter, only)
361 self.Language = select
362
363 def SetUpFilter(self, filter=LC_AVAILABLE, only=()):
364 """ Filters the languages displayed in the control """
365 lang = self.GetLanguage()
366
367 self.filter, self.only = filter, only
368 self.icons, self.choices, self.langs = CreateLanguagesResourceLists(filter, only)
369
370 self.AssignImageList(self.icons, wx.IMAGE_LIST_SMALL)
371
372 self.ClearAll()
373 self.InsertColumn(0, '', width=175)
374 for i in range(len(self.choices)):
375 self.InsertImageStringItem(i, self.choices[i], i)
376
377 self.SetLanguage(lang)
378
379 def GetLanguage(self):
380 """ Returns the language id for the currently selected language in the control """
381 idx = self.GetFirstSelected()
382 if idx != -1:
383 return self.langs[idx]
384 else:
385 None
386
387 def SetLanguage(self, lang):
388 """ Selects the given language ids item in the control """
389 if lang is not None:
390 if lang in self.langs:
391 idx = self.langs.index(lang)
392 self.Select(idx)
393 self.Focus(idx)
394
395 Language = property(GetLanguage, SetLanguage, doc="See `GetLanguage` and `SetLanguage`")
396
397 #-------------------------------------------------------------------------------
398
399
400 if __name__ == '__main__':
401 a = wx.PySimpleApp()
402
403 print GetLanguageFlag(wx.LANGUAGE_AFRIKAANS)
404
405 f=wx.Frame(None, -1)
406 f.p=wx.Panel(f, -1)
407 s=wx.BoxSizer(wx.VERTICAL)
408 f.p.SetSizer(s)
409 try:
410 f.lc=LanguageChoice(f.p, pos = (220, 10), size = (200, 25))
411 s.Add(f.lc, 0, wx.GROW)
412 except:
413 pass
414 f.llc=LanguageListCtrl(f.p, pos = (10, 10), size = (200, 200),
415 filter=LC_ONLY,
416 only=(wx.LANGUAGE_AFRIKAANS, wx.LANGUAGE_ENGLISH,
417 wx.LANGUAGE_FRENCH, wx.LANGUAGE_GERMAN, wx.LANGUAGE_ITALIAN,
418 wx.LANGUAGE_PORTUGUESE_BRAZILIAN, wx.LANGUAGE_SPANISH),
419 select=wx.LANGUAGE_ENGLISH)
420 ## filter=LC_ALL)
421 s.Add(f.llc, 1, wx.GROW)
422 f.Show()
423
424 a.MainLoop()