don't display clock on LCD/OLED for MessageBoxes anymore. instead display more of...
[vuplus_dvbapp] / 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.Sources.StaticText import StaticText
6 from Components.MenuList import MenuList
7 from Components.Sources.StaticText import StaticText
8 from enigma import eTimer
9
10 class MessageBox(Screen):
11         TYPE_YESNO = 0
12         TYPE_INFO = 1
13         TYPE_WARNING = 2
14         TYPE_ERROR = 3
15
16         def __init__(self, session, text, type = TYPE_YESNO, timeout = -1, close_on_any_key = False, default = True):
17                 self.type = type
18                 Screen.__init__(self, session)
19
20                 self["text"] = Label(text)
21                 self["Text"] = StaticText(text)
22                 self["selectedChoice"] = StaticText()
23
24                 self.text = text
25                 self.close_on_any_key = close_on_any_key
26
27                 self["ErrorPixmap"] = Pixmap()
28                 self["QuestionPixmap"] = Pixmap()
29                 self["InfoPixmap"] = Pixmap()
30                 self.timerRunning = False
31                 self.initTimeout(timeout)
32
33                 self.list = []
34                 if type != self.TYPE_ERROR:
35                         self["ErrorPixmap"].hide()
36                 if type != self.TYPE_YESNO:
37                         self["QuestionPixmap"].hide()
38                 if type != self.TYPE_INFO:
39                         self["InfoPixmap"].hide()
40
41                 if type == self.TYPE_YESNO:
42                         if default == True:
43                                 self.list = [ (_("yes"), 0), (_("no"), 1) ]
44                         else:
45                                 self.list = [ (_("no"), 1), (_("yes"), 0) ]
46                 
47                 if len(self.list):
48                         self["selectedChoice"].setText(self.list[0][0])
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         def initTimeout(self, timeout):
67                 self.timeout = timeout
68                 if timeout > 0:
69                         self.timer = eTimer()
70                         self.timer.callback.append(self.timerTick)
71                         self.onExecBegin.append(self.startTimer)
72                         self.origTitle = None
73                         if self.execing:
74                                 self.timerTick()
75                         else:
76                                 self.onShown.append(self.__onShown)
77                         self.timerRunning = True
78                 else:
79                         self.timerRunning = False
80
81         def __onShown(self):
82                 self.onShown.remove(self.__onShown)
83                 self.timerTick()
84
85         def startTimer(self):
86                 self.timer.start(1000)
87
88         def stopTimer(self):
89                 if self.timerRunning:
90                         del self.timer
91                         self.setTitle(self.origTitle)
92                         self.timerRunning = False
93
94         def timerTick(self):
95                 if self.execing:
96                         self.timeout -= 1
97                         if self.origTitle is None:
98                                 self.origTitle = self.instance.getTitle()
99                         self.setTitle(self.origTitle + " (" + str(self.timeout) + ")")
100                         if self.timeout == 0:
101                                 self.timer.stop()
102                                 self.timerRunning = False
103                                 self.timeoutCallback()
104
105         def timeoutCallback(self):
106                 print "Timeout!"
107                 self.ok()
108
109         def cancel(self):
110                 self.close(False)
111
112         def ok(self):
113                 if self.type == self.TYPE_YESNO:
114                         self.close(self["list"].getCurrent()[1] == 0)
115                 else:
116                         self.close(True)
117
118         def alwaysOK(self):
119                 self.close(True)
120
121         def up(self):
122                 self.move(self["list"].instance.moveUp)
123
124         def down(self):
125                 self.move(self["list"].instance.moveDown)
126
127         def left(self):
128                 self.move(self["list"].instance.pageUp)
129
130         def right(self):
131                 self.move(self["list"].instance.pageDown)
132
133         def move(self, direction):
134                 if self.close_on_any_key:
135                         self.close(True)
136                 self["list"].instance.moveSelection(direction)
137                 if len(self.list):
138                         self["selectedChoice"].setText(self["list"].getCurrent()[0])
139                 self.stopTimer()
140
141         def __repr__(self):
142                 return str(type(self)) + "(" + self.text + ")"