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