remove double import
[enigma2.git] / lib / python / Screens / MessageBox.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.Label import Label
4 from Components.Pixmap import Pixmap
5 from Components.Sources.StaticText import StaticText
6 from Components.MenuList import MenuList
7 from enigma import eTimer
8
9 class MessageBox(Screen):
10         TYPE_YESNO = 0
11         TYPE_INFO = 1
12         TYPE_WARNING = 2
13         TYPE_ERROR = 3
14
15         def __init__(self, session, text, type = TYPE_YESNO, timeout = -1, close_on_any_key = False, default = True):
16                 self.type = type
17                 Screen.__init__(self, session)
18
19                 self["text"] = Label(text)
20                 self["Text"] = StaticText(text)
21                 self["selectedChoice"] = StaticText()
22
23                 self.text = text
24                 self.close_on_any_key = close_on_any_key
25
26                 self["ErrorPixmap"] = Pixmap()
27                 self["QuestionPixmap"] = Pixmap()
28                 self["InfoPixmap"] = Pixmap()
29                 self.timerRunning = False
30                 self.initTimeout(timeout)
31
32                 self.list = []
33                 if type != self.TYPE_ERROR:
34                         self["ErrorPixmap"].hide()
35                 if type != self.TYPE_YESNO:
36                         self["QuestionPixmap"].hide()
37                 if type != self.TYPE_INFO:
38                         self["InfoPixmap"].hide()
39
40                 if type == self.TYPE_YESNO:
41                         if default == True:
42                                 self.list = [ (_("yes"), 0), (_("no"), 1) ]
43                         else:
44                                 self.list = [ (_("no"), 1), (_("yes"), 0) ]
45                 
46                 if len(self.list):
47                         self["selectedChoice"].setText(self.list[0][0])
48                 self["list"] = MenuList(self.list)
49
50                 self["actions"] = ActionMap(["MsgBoxActions", "DirectionActions"], 
51                         {
52                                 "cancel": self.cancel,
53                                 "ok": self.ok,
54                                 "alwaysOK": self.alwaysOK,
55                                 "up": self.up,
56                                 "down": self.down,
57                                 "left": self.left,
58                                 "right": self.right,
59                                 "upRepeated": self.up,
60                                 "downRepeated": self.down,
61                                 "leftRepeated": self.left,
62                                 "rightRepeated": self.right
63                         }, -1)
64
65         def initTimeout(self, timeout):
66                 self.timeout = timeout
67                 if timeout > 0:
68                         self.timer = eTimer()
69                         self.timer.callback.append(self.timerTick)
70                         self.onExecBegin.append(self.startTimer)
71                         self.origTitle = None
72                         if self.execing:
73                                 self.timerTick()
74                         else:
75                                 self.onShown.append(self.__onShown)
76                         self.timerRunning = True
77                 else:
78                         self.timerRunning = False
79
80         def __onShown(self):
81                 self.onShown.remove(self.__onShown)
82                 self.timerTick()
83
84         def startTimer(self):
85                 self.timer.start(1000)
86
87         def stopTimer(self):
88                 if self.timerRunning:
89                         del self.timer
90                         self.setTitle(self.origTitle)
91                         self.timerRunning = False
92
93         def timerTick(self):
94                 if self.execing:
95                         self.timeout -= 1
96                         if self.origTitle is None:
97                                 self.origTitle = self.instance.getTitle()
98                         self.setTitle(self.origTitle + " (" + str(self.timeout) + ")")
99                         if self.timeout == 0:
100                                 self.timer.stop()
101                                 self.timerRunning = False
102                                 self.timeoutCallback()
103
104         def timeoutCallback(self):
105                 print "Timeout!"
106                 self.ok()
107
108         def cancel(self):
109                 self.close(False)
110
111         def ok(self):
112                 if self.type == self.TYPE_YESNO:
113                         self.close(self["list"].getCurrent()[1] == 0)
114                 else:
115                         self.close(True)
116
117         def alwaysOK(self):
118                 self.close(True)
119
120         def up(self):
121                 self.move(self["list"].instance.moveUp)
122
123         def down(self):
124                 self.move(self["list"].instance.moveDown)
125
126         def left(self):
127                 self.move(self["list"].instance.pageUp)
128
129         def right(self):
130                 self.move(self["list"].instance.pageDown)
131
132         def move(self, direction):
133                 if self.close_on_any_key:
134                         self.close(True)
135                 self["list"].instance.moveSelection(direction)
136                 if len(self.list):
137                         self["selectedChoice"].setText(self["list"].getCurrent()[0])
138                 self.stopTimer()
139
140         def __repr__(self):
141                 return str(type(self)) + "(" + self.text + ")"