4.2.1r3
[enigma2.git] / usr / lib / enigma2 / 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 enigma import eTimer, eActionMap
8
9 class MessageBox(Screen):
10         TYPE_YESNO = 0
11         TYPE_INFO = 1
12         TYPE_WARNING = 2
13         TYPE_ERROR = 3
14
15         IS_DIALOG = True
16
17         def __init__(self, session, text, type = TYPE_YESNO, timeout = -1, close_on_any_key = False, default = True, enable_input = True, msgBoxID = None, title = None, additionalActionMap=None):
18                 self.type = type
19                 Screen.__init__(self, session)
20                 
21                 self.msgBoxID = msgBoxID
22
23                 self["text"] = Label(text)
24                 self["Text"] = StaticText(text)
25                 self["selectedChoice"] = StaticText()
26
27                 self.text = text
28                 self.close_on_any_key = close_on_any_key
29                 self.title = title
30                 self.tmr = eTimer()
31                 self.tmr_conn = self.tmr.timeout.connect(self.delayedAdditionalActionMapEnd)
32
33                 self["ErrorPixmap"] = Pixmap()
34                 self["QuestionPixmap"] = Pixmap()
35                 self["InfoPixmap"] = Pixmap()
36                 self.timerRunning = False
37                 self.initTimeout(timeout)
38
39                 self.list = []
40                 if type != self.TYPE_ERROR:
41                         self["ErrorPixmap"].hide()
42                 if type != self.TYPE_YESNO:
43                         self["QuestionPixmap"].hide()
44                 if type != self.TYPE_INFO:
45                         self["InfoPixmap"].hide()
46
47                 if type == self.TYPE_YESNO:
48                         if default == True:
49                                 self.list = [ (_("yes"), 0), (_("no"), 1) ]
50                         else:
51                                 self.list = [ (_("no"), 1), (_("yes"), 0) ]
52                 
53                 if self.list:
54                         self["selectedChoice"].setText(self.list[0][0])
55                 self["list"] = MenuList(self.list)
56
57                 self.onFirstExecBegin.append(self._onFirstExecBegin)
58
59                 self.additionalActionMap = None
60                 if enable_input:
61                         if close_on_any_key:
62                                 self.additionalActionMap = additionalActionMap
63                                 self.onShow.append(self.connectHighPrioAction)
64                                 self.onHide.append(self.disconnectHighPrioAction)
65                         else:
66                                 self["actions"] = ActionMap(["MsgBoxActions", "DirectionActions"], 
67                                         {
68                                                 "cancel": self.cancel,
69                                                 "ok": self.ok,
70                                                 "alwaysOK": self.alwaysOK,
71                                                 "up": self.up,
72                                                 "down": self.down,
73                                                 "left": self.left,
74                                                 "right": self.right,
75                                                 "upRepeated": self.up,
76                                                 "downRepeated": self.down,
77                                                 "leftRepeated": self.left,
78                                                 "rightRepeated": self.right
79                                         }, -1)
80
81         def connectHighPrioAction(self):
82                 self.highPrioActionSlot = eActionMap.getInstance().bindAction('', -0x7FFFFFFF, self.closeAnyKey)
83                 if self.additionalActionMap:
84                         self.additionalActionMap.execBegin()
85
86         def disconnectHighPrioAction(self):
87                 self.highPrioActionSlot = None
88                 self.tmr.start(0, True)
89
90         def delayedAdditionalActionMapEnd(self):
91                 if self.additionalActionMap:
92                         self.additionalActionMap.execEnd()
93
94         def _onFirstExecBegin(self):
95                 if self.title != None:
96                         self.setTitle(self.title)
97
98         def initTimeout(self, timeout):
99                 self.timeout = timeout
100                 if timeout > 0:
101                         self.timer = eTimer()
102                         self.timer_conn = self.timer.timeout.connect(self.timerTick)
103                         self.onExecBegin.append(self.startTimer)
104                         self.origTitle = None
105                         if self.execing:
106                                 self.timerTick()
107                         else:
108                                 self.onShown.append(self.__onShown)
109                         self.timerRunning = True
110                 else:
111                         self.timerRunning = False
112
113         def __onShown(self):
114                 self.onShown.remove(self.__onShown)
115                 self.timerTick()
116
117         def startTimer(self):
118                 self.timer.start(1000)
119
120         def stopTimer(self):
121                 if self.timerRunning:
122                         del self.timer
123                         self.onExecBegin.remove(self.startTimer)
124                         self.setTitle(self.origTitle)
125                         self.timerRunning = False
126
127         def timerTick(self):
128                 if self.execing:
129                         self.timeout -= 1
130                         if self.origTitle is None:
131                                 self.origTitle = self.instance.getTitle()
132                         self.setTitle(self.origTitle + " (" + str(self.timeout) + ")")
133                         if self.timeout == 0:
134                                 self.timer.stop()
135                                 self.timerRunning = False
136                                 self.timeoutCallback()
137
138         def closeAnyKey(self, key, flag):
139                 self.close(True)
140                 return 1 if not self.additionalActionMap or flag else 0
141
142         def timeoutCallback(self):
143                 print "Timeout!"
144                 self.ok()
145
146         def cancel(self):
147                 self.close(False)
148
149         def ok(self):
150                 if self.type == self.TYPE_YESNO:
151                         self.close(self["list"].getCurrent()[1] == 0)
152                 else:
153                         self.close(True)
154
155         def alwaysOK(self):
156                 self.close(True)
157
158         def up(self):
159                 self.move(self["list"].instance.moveUp)
160
161         def down(self):
162                 self.move(self["list"].instance.moveDown)
163
164         def left(self):
165                 self.move(self["list"].instance.pageUp)
166
167         def right(self):
168                 self.move(self["list"].instance.pageDown)
169
170         def move(self, direction):
171                 self["list"].instance.moveSelection(direction)
172                 if self.list:
173                         self["selectedChoice"].setText(self["list"].getCurrent()[0])
174                 self.stopTimer()
175
176         def __repr__(self):
177                 return str(type(self)) + "(" + self.text + ")"