invalidate listboxcontent on epglist sort
[enigma2.git] / lib / python / Screens / VirtualKeyBoard.py
1 # -*- coding: iso-8859-1 -*-\r
2 from Components.Language import language\r
3 from Components.ActionMap import ActionMap\r
4 from Components.Label import Label\r
5 from Components.Pixmap import Pixmap\r
6 from Components.MenuList import MenuList\r
7 from Components.MultiContent import MultiContentEntryText, MultiContentEntryPixmapAlphaTest\r
8 from enigma import eListboxPythonMultiContent, gFont, loadPNG, RT_HALIGN_CENTER, RT_VALIGN_CENTER\r
9 from Screen import Screen\r
10 from Tools.Directories import resolveFilename, SCOPE_SKIN_IMAGE\r
11 \r
12 key_backspace = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_backspace.png"))\r
13 key_bg = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_bg.png"))\r
14 key_clr = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_clr.png"))\r
15 key_esc = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_esc.png"))\r
16 key_ok = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_ok.png"))\r
17 key_sel = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_sel.png"))\r
18 key_shift = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_shift.png"))\r
19 key_shift_sel = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_shift_sel.png"))\r
20 key_space = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_space.png"))\r
21 \r
22 class VirtualKeyBoardList(MenuList):\r
23         def __init__(self, list, enableWrapAround=False):\r
24                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)\r
25                 self.l.setFont(0, gFont("Regular", 22))\r
26                 self.l.setItemHeight(45)\r
27 \r
28 def VirtualKeyBoardEntryComponent(keys, selectedKey,shiftMode=False):\r
29         res = [ (keys) ]\r
30         \r
31         x = 0\r
32         count = 0\r
33         if shiftMode:\r
34                 shiftkey_png = key_shift_sel\r
35         else:\r
36                 shiftkey_png = key_shift\r
37         for key in keys:\r
38                 if key == "EXIT":\r
39                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_esc))\r
40                 elif key == "BACKSPACE":\r
41                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_backspace))\r
42                 elif key == "CLEAR":\r
43                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_clr))\r
44                 elif key == "SHIFT":\r
45                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=shiftkey_png))\r
46                 elif key == "SPACE":\r
47                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_space))\r
48                 elif key == "OK":\r
49                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_ok))\r
50                 #elif key == "<-":\r
51                 #       res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_left))\r
52                 #elif key == "->":\r
53                 #       res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_right))\r
54                 \r
55                 else:\r
56                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_bg))\r
57                         res.append(MultiContentEntryText(pos=(x, 0), size=(45, 45), font=0, text=key.encode("utf-8"), flags=RT_HALIGN_CENTER | RT_VALIGN_CENTER))\r
58                 \r
59                 if selectedKey == count:\r
60                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_sel))\r
61                 \r
62                 x += 45\r
63                 count += 1\r
64         \r
65         return res\r
66 \r
67 \r
68 class VirtualKeyBoard(Screen):\r
69 \r
70         def __init__(self, session, title="", text=""):\r
71                 Screen.__init__(self, session)\r
72                 self.keys_list = []\r
73                 self.shiftkeys_list = []\r
74                 self.lang = language.getLanguage()\r
75                 if self.lang == 'de_DE':\r
76                         self.keys_list = [\r
77                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],\r
78                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"ü", u"+"],\r
79                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ö", u"ä", u"#"],\r
80                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],\r
81                                 [u"SHIFT", u"SPACE", u"@", u"ß", u"OK"]]\r
82                         \r
83                         self.shiftkeys_list = [\r
84                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],\r
85                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"Ü", u"*"],\r
86                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ö", u"Ä", u"'"],\r
87                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],\r
88                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"OK"]]\r
89                         \r
90                 elif self.lang == 'es_ES':\r
91                         #still missing keys (u"ùÙ")\r
92                         self.keys_list = [\r
93                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],\r
94                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"ú", u"+"],\r
95                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ó", u"á", u"#"],\r
96                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],\r
97                                 [u"SHIFT", u"SPACE", u"@", u"£", u"à", u"é", u"è", u"í", u"ì", u"ñ", u"ò", u"OK"]]\r
98                         \r
99                         self.shiftkeys_list = [\r
100                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],\r
101                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"Ú", u"*"],\r
102                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ó", u"Á", u"'"],\r
103                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],\r
104                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"À", u"É", u"È",  u"Í", u"Ì", u"Ñ", u"Ò", u"OK"]]\r
105                                 \r
106                 elif self.lang in ['sv_SE', 'fi_FI']:\r
107                         self.keys_list = [\r
108                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],\r
109                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"é", u"+"],\r
110                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ö", u"ä", u"#"],\r
111                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],\r
112                                 [u"SHIFT", u"SPACE", u"@", u"ß", u"å", u"OK"]]\r
113                                 \r
114                         self.shiftkeys_list = [\r
115                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],\r
116                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"É", u"*"],\r
117                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ö", u"Ä", u"'"],\r
118                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],\r
119                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"Å", u"OK"]]\r
120                 else:\r
121                         self.keys_list = [\r
122                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],\r
123                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"+", u"@"],\r
124                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"#", u"\\"],\r
125                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],\r
126                                 [u"SHIFT", u"SPACE", u"OK"]]\r
127                         \r
128                         self.shiftkeys_list = [\r
129                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],\r
130                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"*"],\r
131                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"'", u"?"],\r
132                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],\r
133                                 [u"SHIFT", u"SPACE", u"OK"]]\r
134                 \r
135                 self.shiftMode = False\r
136                 self.text = text\r
137                 self.selectedKey = 0\r
138                 \r
139                 self["header"] = Label(title)\r
140                 self["text"] = Label(self.text)\r
141                 self["list"] = VirtualKeyBoardList([])\r
142                 \r
143                 self["actions"] = ActionMap(["OkCancelActions", "WizardActions", "ColorActions"],\r
144                         {\r
145                                 "ok": self.okClicked,\r
146                                 "cancel": self.exit,\r
147                                 "left": self.left,\r
148                                 "right": self.right,\r
149                                 "up": self.up,\r
150                                 "down": self.down,\r
151                                 "red": self.backClicked,\r
152                                 "green": self.ok\r
153                         }, -2)\r
154                 \r
155                 self.onLayoutFinish.append(self.buildVirtualKeyBoard)\r
156 \r
157         def buildVirtualKeyBoard(self, selectedKey=0):\r
158                 list = []\r
159                 \r
160                 if self.shiftMode:\r
161                         self.k_list = self.shiftkeys_list\r
162                         for keys in self.k_list:\r
163                                 if selectedKey < 12 and selectedKey > -1:\r
164                                         list.append(VirtualKeyBoardEntryComponent(keys, selectedKey,True))\r
165                                 else:\r
166                                         list.append(VirtualKeyBoardEntryComponent(keys, -1,True))\r
167                                 selectedKey -= 12\r
168                 else:\r
169                         self.k_list = self.keys_list\r
170                         for keys in self.k_list:\r
171                                 if selectedKey < 12 and selectedKey > -1:\r
172                                         list.append(VirtualKeyBoardEntryComponent(keys, selectedKey))\r
173                                 else:\r
174                                         list.append(VirtualKeyBoardEntryComponent(keys, -1))\r
175                                 selectedKey -= 12\r
176                 \r
177                 self["list"].setList(list)\r
178 \r
179         \r
180         def backClicked(self):\r
181                 self.text = self["text"].getText()[:-1]\r
182                 self["text"].setText(self.text)\r
183                         \r
184         def okClicked(self):\r
185                 if self.shiftMode:\r
186                         list = self.shiftkeys_list\r
187                 else:\r
188                         list = self.keys_list\r
189                 \r
190                 selectedKey = self.selectedKey\r
191 \r
192                 for x in list:\r
193                         if selectedKey < 12:\r
194                                 text = x[selectedKey]\r
195                                 break\r
196                         else:\r
197                                 selectedKey -= 12\r
198 \r
199                 text = text.encode("utf-8")\r
200 \r
201                 if text == "EXIT":\r
202                         self.close(None)\r
203                 \r
204                 elif text == "BACKSPACE":\r
205                         self.text = self["text"].getText()[:-1]\r
206                         self["text"].setText(self.text)\r
207                 \r
208                 elif text == "CLEAR":\r
209                         self.text = ""\r
210                         self["text"].setText(self.text)\r
211                 \r
212                 elif text == "SHIFT":\r
213                         if self.shiftMode:\r
214                                 self.shiftMode = False\r
215                         else:\r
216                                 self.shiftMode = True\r
217                         \r
218                         self.buildVirtualKeyBoard(self.selectedKey)\r
219                 \r
220                 elif text == "SPACE":\r
221                         self.text += " "\r
222                         self["text"].setText(self.text)\r
223                 \r
224                 elif text == "OK":\r
225                         self.close(self["text"].getText())\r
226                 \r
227                 else:\r
228                         self.text = self["text"].getText()\r
229                         self.text += text\r
230                         self["text"].setText(self.text)\r
231 \r
232         def ok(self):\r
233                 self.close(self["text"].getText())\r
234 \r
235         def exit(self):\r
236                 self.close(None)\r
237 \r
238         def left(self):\r
239                 self.selectedKey -= 1\r
240                 \r
241                 if self.selectedKey == -1:\r
242                         self.selectedKey = 11\r
243                 elif self.selectedKey == 11:\r
244                         self.selectedKey = 23\r
245                 elif self.selectedKey == 23:\r
246                         self.selectedKey = 35\r
247                 elif self.selectedKey == 35:\r
248                         self.selectedKey = 47\r
249                 elif self.selectedKey == 47:\r
250                         self.selectedKey = 59\r
251                 \r
252                 self.showActiveKey()\r
253 \r
254         def right(self):\r
255                 self.selectedKey += 1\r
256                 \r
257                 if self.selectedKey == 12:\r
258                         self.selectedKey = 0\r
259                 elif self.selectedKey == 24:\r
260                         self.selectedKey = 12\r
261                 elif self.selectedKey == 36:\r
262                         self.selectedKey = 24\r
263                 elif self.selectedKey == 48:\r
264                         self.selectedKey = 36\r
265                 elif self.selectedKey == 60:\r
266                         self.selectedKey = 48\r
267                 \r
268                 self.showActiveKey()\r
269 \r
270         def up(self):\r
271                 self.selectedKey -= 12\r
272                 \r
273                 if self.selectedKey < 0:\r
274                         self.selectedKey += 60\r
275                 \r
276                 self.showActiveKey()\r
277 \r
278         def down(self):\r
279                 self.selectedKey += 12\r
280                 \r
281                 if self.selectedKey > 59:\r
282                         self.selectedKey -= 60\r
283                 \r
284                 self.showActiveKey()\r
285 \r
286         def showActiveKey(self):\r
287                 self.buildVirtualKeyBoard(self.selectedKey)\r