ed3bb4f4a4b688003ea4d542fe4d7bce6b98411b
[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):
16                 self.type = type
17                 Screen.__init__(self, session)
18                 
19                 self["text"] = Label(text)
20                 
21                 self["ErrorPixmap"] = Pixmap()
22                 self["QuestionPixmap"] = Pixmap()
23                 self["InfoPixmap"] = Pixmap()
24                 self.timerRunning = False
25                 if timeout > 0:
26                         self.timer = eTimer()
27                         self.timer.timeout.get().append(self.timerTick)
28                         self.timer.start(1000)
29                         self.origTitle = None
30                         self.onShown.append(self.timerTick)
31                         self.timerRunning = True
32                 self.timeout = timeout
33                 
34                 self.list = []
35                 if type != self.TYPE_ERROR:
36                         self["ErrorPixmap"].hide()
37                 if type != self.TYPE_YESNO:
38                         self["QuestionPixmap"].hide()
39                 if type != self.TYPE_INFO:
40                         self["InfoPixmap"].hide()
41                         
42                 if type == self.TYPE_YESNO:
43                         self.list = [ (_("yes"), 0), (_("no"), 1) ]
44
45
46                 self["list"] = MenuList(self.list)
47                 
48                 self["actions"] = ActionMap(["MsgBoxActions", "DirectionActions"], 
49                         {
50                                 "cancel": self.cancel,
51                                 "ok": self.ok,
52                                 "alwaysOK": self.alwaysOK,
53                                 "up": self.up,
54                                 "down": self.down,
55                                 "left": self.left,
56                                 "right": self.right,
57                                 "upRepeated": self.up,
58                                 "downRepeated": self.down,
59                                 "leftRepeated": self.left,
60                                 "rightRepeated": self.right
61                         }, -1)
62                         
63         
64         def timerTick(self):
65                 self.timeout -= 1
66                 if self.origTitle is None:
67                         self.origTitle = self.instance.getTitle()
68                 self.setTitle(self.origTitle + " (" + _("Timeout: ") + str(self.timeout) + ")")
69                 if self.timeout == 0:
70                         self.timer.stop()
71                         self.timerRunning = False
72                         self.timeoutCallback()
73                         
74         def timeoutCallback(self):
75                 print "Timeout!"
76                 self.ok()
77         
78         def cancel(self):
79                 self.close(False)
80         
81         def ok(self):
82                 if self.type == self.TYPE_YESNO:
83                         self.close(self["list"].getCurrent()[1] == 0)
84                 else:
85                         self.close(True)
86
87         def alwaysOK(self):
88                 self.close(True)
89
90         def up(self):
91                 self.move(self["list"].instance.moveUp)
92                 
93         def down(self):
94                 self.move(self["list"].instance.moveDown)
95
96         def left(self):
97                 self.move(self["list"].instance.pageUp)
98                 
99
100                 
101         def right(self):
102                 self.move(self["list"].instance.pageDown)
103
104         def move(self, direction):
105                 self["list"].instance.moveSelection(direction)
106                 if self.timerRunning:
107                         self.timer.stop()
108                         self.setTitle(self.origTitle)
109                         self.timerRunning = False