fix exec-begin after timer stop
[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, enable_input = 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 self.list:
47                         self["selectedChoice"].setText(self.list[0][0])
48                 self["list"] = MenuList(self.list)
49
50                 if enable_input:
51                         self["actions"] = ActionMap(["MsgBoxActions", "DirectionActions"], 
52                                 {
53                                         "cancel": self.cancel,
54                                         "ok": self.ok,
55                                         "alwaysOK": self.alwaysOK,
56                                         "up": self.up,
57                                         "down": self.down,
58                                         "left": self.left,
59                                         "right": self.right,
60                                         "upRepeated": self.up,
61                                         "downRepeated": self.down,
62                                         "leftRepeated": self.left,
63                                         "rightRepeated": self.right
64                                 }, -1)
65
66         def initTimeout(self, timeout):
67                 self.timeout = timeout
68                 if timeout > 0:
69                         self.timer = eTimer()
70                         self.timer.callback.append(self.timerTick)
71                         self.onExecBegin.append(self.startTimer)
72                         self.origTitle = None
73                         if self.execing:
74                                 self.timerTick()
75                         else:
76                                 self.onShown.append(self.__onShown)
77                         self.timerRunning = True
78                 else:
79                         self.timerRunning = False
80
81         def __onShown(self):
82                 self.onShown.remove(self.__onShown)
83                 self.timerTick()
84
85         def startTimer(self):
86                 self.timer.start(1000)
87
88         def stopTimer(self):
89                 if self.timerRunning:
90                         del self.timer
91                         self.onExecBegin.remove(self.startTimer)
92                         self.setTitle(self.origTitle)
93                         self.timerRunning = False
94
95         def timerTick(self):
96                 if self.execing:
97                         self.timeout -= 1
98                         if self.origTitle is None:
99                                 self.origTitle = self.instance.getTitle()
100                         self.setTitle(self.origTitle + " (" + str(self.timeout) + ")")
101                         if self.timeout == 0:
102                                 self.timer.stop()
103                                 self.timerRunning = False
104                                 self.timeoutCallback()
105
106         def timeoutCallback(self):
107                 print "Timeout!"
108                 self.ok()
109
110         def cancel(self):
111                 self.close(False)
112
113         def ok(self):
114                 if self.type == self.TYPE_YESNO:
115                         self.close(self["list"].getCurrent()[1] == 0)
116                 else:
117                         self.close(True)
118
119         def alwaysOK(self):
120                 self.close(True)
121
122         def up(self):
123                 self.move(self["list"].instance.moveUp)
124
125         def down(self):
126                 self.move(self["list"].instance.moveDown)
127
128         def left(self):
129                 self.move(self["list"].instance.pageUp)
130
131         def right(self):
132                 self.move(self["list"].instance.pageDown)
133
134         def move(self, direction):
135                 if self.close_on_any_key:
136                         self.close(True)
137                 self["list"].instance.moveSelection(direction)
138                 if self.list:
139                         self["selectedChoice"].setText(self["list"].getCurrent()[0])
140                 self.stopTimer()
141
142         def __repr__(self):
143                 return str(type(self)) + "(" + self.text + ")"