f951eebfdacb22c8fe5625c5ae1dda0c41717e21
[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.Button import Button
5 from Components.Pixmap import Pixmap
6 from Components.MenuList import MenuList
7 from enigma import eSize, ePoint, 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):
16                 self.type = type
17                 Screen.__init__(self, session)
18                 
19                 self["text"] = Label(text)
20                 
21                 self.text = text
22                 self.close_on_any_key = close_on_any_key
23                 
24                 self["ErrorPixmap"] = Pixmap()
25                 self["QuestionPixmap"] = Pixmap()
26                 self["InfoPixmap"] = Pixmap()
27                 self.timerRunning = False
28                 if timeout > 0:
29                         self.timer = eTimer()
30                         self.timer.timeout.get().append(self.timerTick)
31                         self.timer.start(1000)
32                         self.origTitle = None
33                         self.onShown.append(self.timerTick)
34                         self.timerRunning = True
35                 self.timeout = timeout
36                 
37                 self.list = []
38                 if type != self.TYPE_ERROR:
39                         self["ErrorPixmap"].hide()
40                 if type != self.TYPE_YESNO:
41                         self["QuestionPixmap"].hide()
42                 if type != self.TYPE_INFO:
43                         self["InfoPixmap"].hide()
44                         
45                 if type == self.TYPE_YESNO:
46                         self.list = [ (_("yes"), 0), (_("no"), 1) ]
47
48
49                 self["list"] = MenuList(self.list)
50                 
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         
67         def timerTick(self):
68                 self.timeout -= 1
69                 if self.origTitle is None:
70                         self.origTitle = self.instance.getTitle()
71                 self.setTitle(self.origTitle + " (" + str(self.timeout) + ")")
72                 if self.timeout == 0:
73                         self.timer.stop()
74                         self.timerRunning = False
75                         self.timeoutCallback()
76                         
77         def timeoutCallback(self):
78                 print "Timeout!"
79                 self.ok()
80         
81         def cancel(self):
82                 self.close(False)
83         
84         def ok(self):
85                 if self.type == self.TYPE_YESNO:
86                         self.close(self["list"].getCurrent()[1] == 0)
87                 else:
88                         self.close(True)
89
90         def alwaysOK(self):
91                 self.close(True)
92
93         def up(self):
94                 self.move(self["list"].instance.moveUp)
95                 
96         def down(self):
97                 self.move(self["list"].instance.moveDown)
98
99         def left(self):
100                 self.move(self["list"].instance.pageUp)
101                 
102         def right(self):
103                 self.move(self["list"].instance.pageDown)
104
105         def move(self, direction):
106                 if self.close_on_any_key:
107                         self.close(True)
108
109                 self["list"].instance.moveSelection(direction)
110                 if self.timerRunning:
111                         self.timer.stop()
112                         self.setTitle(self.origTitle)
113                         self.timerRunning = False
114
115         def __repr__(self):
116                 return str(type(self)) + "(" + self.text + ")"