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