fix setText("")
[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):
10         TEXT = 0
11         PIN = 1
12         NUMBER = 2      
13         
14         def __init__(self, text="", maxSize = False, type = TEXT):
15                 GUIComponent.__init__(self)
16                 VariableText.__init__(self)
17                 self.numericalTextInput = NumericalTextInput(self.right)
18                 self.type = type
19                 self.maxSize = maxSize
20                 self.currPos = 0
21                 self.text = text
22                 self.update()
23
24         def update(self):
25                 self.setMarkedPos(self.currPos)
26                 text = self.text
27                 if self.type == self.PIN:
28                         text = "*" * len(self.text)
29                 self.message = text
30                 if self.instance:
31                         self.instance.setText(self.message)
32
33         def setText(self, text):
34                 if not len(text):
35                         self.currPos = 0
36                 self.text = text
37                 self.update()
38
39         def getText(self):
40                 return self.text
41         
42         def createWidget(self, parent):
43                 return eLabel(parent, self.currPos)
44         
45         def getSize(self):
46                 s = self.instance.calculateSize()
47                 return (s.width(), s.height())
48         
49         def right(self):
50                 self.currPos += 1
51                 if self.currPos == len(self.text):
52                         if self.maxSize:
53                                 self.currPos -= 1
54                         else:
55                                 self.text = self.text + " "
56                         
57                 self.update()
58                 
59         def left(self):
60                 if self.currPos > 0:
61                         self.currPos -= 1
62                         self.update()
63                 
64         def up(self):
65                 if self.text[self.currPos] == "9" or self.text[self.currPos] == " ":
66                         newNumber = "0"
67                 else:
68                         newNumber = str(int(self.text[self.currPos]) + 1)
69                 self.text = self.text[0:self.currPos] + newNumber + self.text[self.currPos + 1:]
70                 self.update()
71                 
72         def down(self):
73                 if self.text[self.currPos] == "0" or self.text[self.currPos] == " ":
74                         newNumber = "9"
75                 else:
76                         newNumber = str(int(self.text[self.currPos]) - 1)
77
78                 self.text = self.text[0:self.currPos] + newNumber + self.text[self.currPos + 1:]
79                 self.update()
80                 
81         def delete(self):
82                 self.text = self.text[:self.currPos] + self.text[self.currPos + 1:]
83                 self.update()
84
85         def handleAscii(self, code):
86                 newChar = chr(code)
87                 self.text = self.text[0:self.currPos] + newChar + self.text[self.currPos + 1:]
88                 self.right()
89                 self.update()
90
91         def number(self, number):
92                 if self.type == self.TEXT:
93                         newChar = self.numericalTextInput.getKey(number)
94                 elif self.type == self.PIN or self.type == self.NUMBER:
95                         newChar = str(number)
96                 self.text = self.text[0:self.currPos] + newChar + self.text[self.currPos + 1:]
97                 if self.type == self.PIN or self.type == self.NUMBER:
98                         self.right()
99                 self.update()