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