fix cancel handling
[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(HTMLComponent, GUIComponent, VariableText):
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.setText(text)
30                 #self.setText(self.text[0:self.currPos] + "_" + self.text[self.currPos] + "_" + self.text[self.currPos + 1:])
31
32         def getText(self):
33                 return self.text
34         
35         def createWidget(self, parent):
36                 return eLabel(parent, self.currPos)
37         
38         def getSize(self):
39                 s = self.instance.calculateSize()
40                 return (s.width(), s.height())
41         
42         def right(self):
43                 self.currPos += 1
44                 if self.currPos == len(self.text):
45                         if self.maxSize:
46                                 self.currPos -= 1
47                         else:
48                                 self.text = self.text + " "
49                         
50                 self.update()
51                 
52         def left(self):
53                 self.currPos -= 1
54                 self.update()
55                 
56         def up(self):
57                 if self.text[self.currPos] == "9":
58                         newNumber = "0"
59                 else:
60                         newNumber = str(int(self.text[self.currPos]) + 1)
61                 self.text = self.text[0:self.currPos] + newNumber + self.text[self.currPos + 1:]
62                 self.update()
63                 
64         def down(self):
65                 if self.text[self.currPos] == "0":
66                         newNumber = "9"
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 number(self, number):
73                 if self.type == self.TEXT:
74                         newChar = self.numericalTextInput.getKey(number)
75                 elif self.type == self.PIN or self.type == self.NUMBER:
76                         newChar = str(number)
77                 self.text = self.text[0:self.currPos] + newChar + self.text[self.currPos + 1:]
78                 if self.type == self.PIN or self.type == self.NUMBER:
79                         self.right()
80                 self.update()
81
82         def show(self):
83                 self.instance.show()
84
85         def hide(self):
86                 self.instance.hide()