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