immediate update frontend status when un-suspending
[enigma2.git] / lib / python / Components / Input.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
3 from VariableText import VariableText
4
5 from enigma import eLabel
6
7 from Tools.NumericalTextInput import NumericalTextInput
8
9 class Input(VariableText, HTMLComponent, GUIComponent, NumericalTextInput):
10         TEXT = 0
11         PIN = 1
12         NUMBER = 2
13
14         def __init__(self, text="", maxSize = False, visible_width = False, type = TEXT):
15                 NumericalTextInput.__init__(self, self.right)
16                 GUIComponent.__init__(self)
17                 VariableText.__init__(self)
18                 self.type = type
19                 self.allmarked = (text != "") and (type != self.PIN)
20                 self.maxSize = maxSize
21                 self.currPos = 0
22                 self.visible_width = visible_width
23                 self.offset = 0
24                 self.overwrite = maxSize
25                 self.setText(text)
26                 
27         def __len__(self):
28                 return len(self.text)
29
30         def update(self):
31                 if self.visible_width:
32                         if self.currPos < self.offset:
33                                 self.offset = self.currPos
34                         if self.currPos >= self.offset + self.visible_width:
35                                 if self.currPos == len(self.Text):
36                                         self.offset = self.currPos - self.visible_width
37                                 else:
38                                         self.offset = self.currPos - self.visible_width + 1
39                         if self.offset > 0 and self.offset + self.visible_width > len(self.Text):
40                                 self.offset = max(0, len(self.Text) - self.visible_width)
41                 if self.allmarked:
42                         self.setMarkedPos(-2)
43                 else:
44                         self.setMarkedPos(self.currPos-self.offset)
45                 if self.visible_width:
46                         if self.type == self.PIN:
47                                 self.text = ""
48                                 for x in self.Text[self.offset:self.offset+self.visible_width]:
49                                         self.text += (x==" " and " " or "*")
50                         else:
51                                 self.text = self.Text[self.offset:self.offset+self.visible_width].encode("utf-8") + " "
52                 else:
53                         if self.type == self.PIN:
54                                 self.text = ""
55                                 for x in self.Text:
56                                         self.text += (x==" " and " " or "*")
57                         else:
58                                 self.text = self.Text.encode("utf-8") + " "
59
60         def setText(self, text):
61                 if not len(text):
62                         self.currPos = 0
63                         self.Text = u""
64                 else:
65                         try:
66                                 self.Text = text.decode("utf-8")
67                         except UnicodeDecodeError:
68                                 print "utf8 kaputt!"
69                                 self.Text = text
70                 self.update()
71
72         def getText(self):
73                 return self.Text.encode("utf-8")
74
75         def createWidget(self, parent):
76                 if self.allmarked:
77                         return eLabel(parent, -2)
78                 else:
79                         return eLabel(parent, self.currPos-self.offset)
80
81         def getSize(self):
82                 s = self.instance.calculateSize()
83                 return (s.width(), s.height())
84         
85         def innerright(self):
86                 if self.allmarked:
87                         self.currPos = 0
88                         self.allmarked = False
89                 elif self.maxSize:
90                         if self.currPos < len(self.Text)-1:
91                                 self.currPos += 1
92                 else:
93                         if self.currPos < len(self.Text):
94                                 self.currPos += 1
95
96         def right(self):
97                 if self.type == self.TEXT:
98                         self.timeout()
99                 self.innerright()
100                 self.update()
101
102         def left(self):
103                 if self.type == self.TEXT:
104                         self.timeout()
105                 if self.allmarked:
106                         if self.maxSize:
107                                 self.currPos = len(self.Text) - 1
108                         else:
109                                 self.currPos = len(self.Text)
110                         self.allmarked = False
111                 elif self.currPos > 0:
112                         self.currPos -= 1
113                 self.update()
114
115         def up(self):
116                 self.allmarked = False
117                 if self.type == self.TEXT:
118                         self.timeout()
119                 if self.currPos == len(self.Text) or self.Text[self.currPos] == "9" or self.Text[self.currPos] == " ":
120                         newNumber = "0"
121                 else:
122                         newNumber = str(int(self.Text[self.currPos]) + 1)
123                 self.Text = self.Text[0:self.currPos] + newNumber + self.Text[self.currPos + 1:]
124                 self.update()
125
126         def down(self):
127                 self.allmarked = False
128                 if self.type == self.TEXT:
129                         self.timeout()
130                 if self.currPos == len(self.Text) or self.Text[self.currPos] == "0" or self.Text[self.currPos] == " ":
131                         newNumber = "9"
132                 else:
133                         newNumber = str(int(self.Text[self.currPos]) - 1)
134                 self.Text = self.Text[0:self.currPos] + newNumber + self.Text[self.currPos + 1:]
135                 self.update()
136                 
137         def home(self):
138                 self.allmarked = False
139                 if self.type == self.TEXT:
140                         self.timeout()
141                 self.currPos = 0
142                 self.update()
143         
144         def end(self):
145                 self.allmarked = False
146                 if self.type == self.TEXT:
147                         self.timeout()
148                 if self.maxSize:
149                         self.currPos = len(self.Text) - 1
150                 else:
151                         self.currPos = len(self.Text)
152                 self.update()
153
154         def insertChar(self, ch, pos, owr, ins):
155                 if ins and not self.maxSize:
156                         self.Text = self.Text[0:pos] + ch + self.Text[pos:]
157                 elif owr or self.overwrite:
158                         self.Text = self.Text[0:pos] + ch + self.Text[pos + 1:]
159                 elif self.maxSize:
160                         self.Text = self.Text[0:pos] + ch + self.Text[pos:-1]
161                 else:
162                         self.Text = self.Text[0:pos] + ch + self.Text[pos:]
163
164         def deleteChar(self, pos):
165                 if not self.maxSize:
166                         self.Text = self.Text[0:pos] + self.Text[pos + 1:]
167                 elif self.overwrite:
168                         self.Text = self.Text[0:pos] + " " + self.Text[pos + 1:]
169                 else:
170                         self.Text = self.Text[0:pos] + self.Text[pos + 1:] + " "
171
172         def deleteAllChars(self):
173                 if self.maxSize:
174                         self.Text = " " * len(self.Text)
175                 else:
176                         self.Text = ""
177                 self.currPos = 0
178
179         def tab(self):
180                 if self.type == self.TEXT:
181                         self.timeout()
182                 if self.allmarked:
183                         self.deleteAllChars()
184                         self.allmarked = False
185                 else:
186                         self.insertChar(" ", self.currPos, False, True);
187                         self.innerright()
188                 self.update()
189
190         def delete(self):
191                 if self.type == self.TEXT:
192                         self.timeout()
193                 if self.allmarked:
194                         self.deleteAllChars()
195                         self.allmarked = False
196                 else:
197                         self.deleteChar(self.currPos);
198                         if self.maxSize and self.overwrite:
199                                 self.innerright()
200                 self.update()
201
202         def deleteBackward(self):
203                 if self.type == self.TEXT:
204                         self.timeout()
205                 if self.allmarked:
206                         self.deleteAllChars()
207                         self.allmarked = False
208                 else:
209                         if self.currPos > 0:
210                                 self.deleteChar(self.currPos-1);
211                                 if not self.maxSize and self.offset > 0:
212                                         self.offset -= 1
213                                 self.currPos -= 1
214                 self.update()
215
216         def toggleOverwrite(self):
217                 if self.type == self.TEXT:
218                         self.timeout()
219                 self.overwrite = not self.overwrite
220                 self.update()
221
222         def handleAscii(self, code):
223                 if self.type == self.TEXT:
224                         self.timeout()
225                 if self.allmarked:
226                         self.deleteAllChars()
227                         self.allmarked = False
228                 self.insertChar(unichr(code), self.currPos, False, False);
229                 self.innerright()
230                 self.update()
231
232         def number(self, number):
233                 if self.type == self.TEXT:
234                         owr = self.lastKey == number
235                         newChar = self.getKey(number)
236                 elif self.type == self.PIN or self.type == self.NUMBER:
237                         owr = False
238                         newChar = str(number)
239                 if self.allmarked:
240                         self.deleteAllChars()
241                         self.allmarked = False
242                 self.insertChar(newChar, self.currPos, owr, False);
243                 if self.type == self.PIN or self.type == self.NUMBER:
244                         self.innerright()
245                 self.update()