some NumericalInput and uncode/utf-8 fixes
[enigma2.git] / lib / python / Components / Input.py
1 from HTMLComponent import *
2 from GUIComponent import *
3 from VariableText import *
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, type = TEXT):
15                 NumericalTextInput.__init__(self, self.right)
16                 GUIComponent.__init__(self)
17                 VariableText.__init__(self)
18                 self.type = type
19                 self.maxSize = maxSize
20                 self.currPos = 0
21                 self.overwrite = 0
22                 self.setText(text)
23
24         def update(self):
25                 self.setMarkedPos(self.currPos)
26                 if self.type == self.PIN:
27                         self.text = "*" * len(self.Text)
28                 else:
29                         self.text = self.Text.encode("utf-8")
30
31         def setText(self, text):
32                 if not len(text):
33                         self.currPos = 0
34                         self.Text = u""
35                 else:
36                         try:
37                                 self.Text = text.decode("utf-8")
38                         except UnicodeDecodeError:
39                                 print "utf8 kaputt!"
40                                 self.Text = text
41                 self.update()
42
43         def getText(self):
44                 return self.Text.encode("utf-8")
45
46         def createWidget(self, parent):
47                 return eLabel(parent, self.currPos)
48
49         def getSize(self):
50                 s = self.instance.calculateSize()
51                 return (s.width(), s.height())
52         
53         def right(self):
54                 self.currPos += 1
55                 if self.currPos == len(self.Text):
56                         if self.maxSize:
57                                 self.currPos -= 1
58                         else:
59                                 self.Text = self.Text + " "
60                 self.update()
61
62         def left(self):
63                 if self.currPos > 0:
64                         self.currPos -= 1
65                         self.update()
66
67         def up(self):
68                 if self.Text[self.currPos] == "9" or self.Text[self.currPos] == " ":
69                         newNumber = "0"
70                 else:
71                         newNumber = str(int(self.Text[self.currPos]) + 1)
72                 self.Text = self.Text[0:self.currPos] + newNumber + self.Text[self.currPos + 1:]
73                 self.update()
74
75         def down(self):
76                 if self.Text[self.currPos] == "0" or self.Text[self.currPos] == " ":
77                         newNumber = "9"
78                 else:
79                         newNumber = str(int(self.Text[self.currPos]) - 1)
80                 self.Text = self.Text[0:self.currPos] + newNumber + self.Text[self.currPos + 1:]
81                 self.update()
82                 
83         def home(self):
84                 self.currPos = 0
85                 self.update()
86         
87         def end(self):
88                 self.currPos = len(self.Text) - 1
89                 self.update()
90
91         def tab(self):
92                 if self.currPos == len(self.Text) - 1:
93                         self.Text=self.Text+ " "
94                         self.end()
95                 else:
96                         self.Text = self.Text[0:self.currPos] + " " + self.Text[self.currPos:]
97                 self.update()
98
99         def delete(self):
100                 self.Text = self.Text[:self.currPos] + self.Text[self.currPos + 1:]
101                 self.update()
102
103         def toggleOverwrite(self):
104                 if self.overwrite==1:
105                         self.overwrite=0
106                 else:
107                         self.overwrite=1
108                 self.update()
109
110         def deleteBackward(self):
111                 self.Text = self.Text[:self.currPos - 1] + self.Text[self.currPos:]
112                 self.left()
113                 self.update()
114
115         def handleAscii(self, code):
116                 newChar = unichr(code)
117                 if self.overwrite==1:
118                         self.Text = self.Text[0:self.currPos] + newChar + self.Text[self.currPos + 1:]
119                 else:
120                         self.Text = self.Text[0:self.currPos] + newChar + self.Text[self.currPos:]
121                 self.right()
122
123         def number(self, number):
124                 if self.type == self.TEXT:
125                         newChar = self.getKey(number)
126                 elif self.type == self.PIN or self.type == self.NUMBER:
127                         newChar = str(number)
128                 self.Text = self.Text[0:self.currPos] + newChar + self.Text[self.currPos + 1:]
129                 if self.type == self.PIN or self.type == self.NUMBER:
130                         self.right()
131                 self.update()