rewrite ServiceEventTracker and PerServiceBase service event handle code
[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.MenuList import MenuList
6 from Components.Sources.StaticText import StaticText
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
22                 self.text = text
23                 self.close_on_any_key = close_on_any_key
24
25                 self["ErrorPixmap"] = Pixmap()
26                 self["QuestionPixmap"] = Pixmap()
27                 self["InfoPixmap"] = Pixmap()
28                 self.timerRunning = False
29                 self.initTimeout(timeout)
30
31                 self.list = []
32                 if type != self.TYPE_ERROR:
33                         self["ErrorPixmap"].hide()
34                 if type != self.TYPE_YESNO:
35                         self["QuestionPixmap"].hide()
36                 if type != self.TYPE_INFO:
37                         self["InfoPixmap"].hide()
38
39                 if type == self.TYPE_YESNO:
40                         if default == True:
41                                 self.list = [ (_("yes"), 0), (_("no"), 1) ]
42                         else:
43                                 self.list = [ (_("no"), 1), (_("yes"), 0) ]
44
45                 self["list"] = MenuList(self.list)
46
47                 self["actions"] = ActionMap(["MsgBoxActions", "DirectionActions"], 
48                         {
49                                 "cancel": self.cancel,
50                                 "ok": self.ok,
51                                 "alwaysOK": self.alwaysOK,
52                                 "up": self.up,
53                                 "down": self.down,
54                                 "left": self.left,
55                                 "right": self.right,
56                                 "upRepeated": self.up,
57                                 "downRepeated": self.down,
58                                 "leftRepeated": self.left,
59                                 "rightRepeated": self.right
60                         }, -1)
61
62         def initTimeout(self, timeout):
63                 self.timeout = timeout
64                 if timeout > 0:
65                         self.timer = eTimer()
66                         self.timer.callback.append(self.timerTick)
67                         self.onExecBegin.append(self.startTimer)
68                         self.origTitle = None
69                         if self.execing:
70                                 self.timerTick()
71                         else:
72                                 self.onShown.append(self.__onShown)
73                         self.timerRunning = True
74                 else:
75                         self.timerRunning = False
76
77         def __onShown(self):
78                 self.onShown.remove(self.__onShown)
79                 self.timerTick()
80
81         def startTimer(self):
82                 self.timer.start(1000)
83
84         def stopTimer(self):
85                 if self.timerRunning:
86                         del self.timer
87                         self.setTitle(self.origTitle)
88                         self.timerRunning = False
89
90         def timerTick(self):
91                 if self.execing:
92                         self.timeout -= 1
93                         if self.origTitle is None:
94                                 self.origTitle = self.instance.getTitle()
95                         self.setTitle(self.origTitle + " (" + str(self.timeout) + ")")
96                         if self.timeout == 0:
97                                 self.timer.stop()
98                                 self.timerRunning = False
99                                 self.timeoutCallback()
100
101         def timeoutCallback(self):
102                 print "Timeout!"
103                 self.ok()
104
105         def cancel(self):
106                 self.close(False)
107
108         def ok(self):
109                 if self.type == self.TYPE_YESNO:
110                         self.close(self["list"].getCurrent()[1] == 0)
111                 else:
112                         self.close(True)
113
114         def alwaysOK(self):
115                 self.close(True)
116
117         def up(self):
118                 self.move(self["list"].instance.moveUp)
119
120         def down(self):
121                 self.move(self["list"].instance.moveDown)
122
123         def left(self):
124                 self.move(self["list"].instance.pageUp)
125
126         def right(self):
127                 self.move(self["list"].instance.pageDown)
128
129         def move(self, direction):
130                 if self.close_on_any_key:
131                         self.close(True)
132                 self["list"].instance.moveSelection(direction)
133                 self.stopTimer()
134
135         def __repr__(self):
136                 return str(type(self)) + "(" + self.text + ")"