Merge remote branch 'remotes/origin/fantempplugin'
[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.Pixmap import Pixmap
5 from Components.Sources.StaticText import StaticText
6 from Components.MenuList import MenuList
7 from enigma import 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, default = True, enable_input = True, msgBoxID = None):
16                 self.type = type
17                 Screen.__init__(self, session)
18                 
19                 self.msgBoxID = msgBoxID
20
21                 self["text"] = Label(text)
22                 self["Text"] = StaticText(text)
23                 self["selectedChoice"] = StaticText()
24
25                 self.text = text
26                 self.close_on_any_key = close_on_any_key
27
28                 self["ErrorPixmap"] = Pixmap()
29                 self["QuestionPixmap"] = Pixmap()
30                 self["InfoPixmap"] = Pixmap()
31                 self.timerRunning = False
32                 self.initTimeout(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                         if default == True:
44                                 self.list = [ (_("yes"), 0), (_("no"), 1) ]
45                         else:
46                                 self.list = [ (_("no"), 1), (_("yes"), 0) ]
47                 
48                 if self.list:
49                         self["selectedChoice"].setText(self.list[0][0])
50                 self["list"] = MenuList(self.list)
51
52                 if enable_input:
53                         self["actions"] = ActionMap(["MsgBoxActions", "DirectionActions"], 
54                                 {
55                                         "cancel": self.cancel,
56                                         "ok": self.ok,
57                                         "alwaysOK": self.alwaysOK,
58                                         "up": self.up,
59                                         "down": self.down,
60                                         "left": self.left,
61                                         "right": self.right,
62                                         "upRepeated": self.up,
63                                         "downRepeated": self.down,
64                                         "leftRepeated": self.left,
65                                         "rightRepeated": self.right
66                                 }, -1)
67
68         def initTimeout(self, timeout):
69                 self.timeout = timeout
70                 if timeout > 0:
71                         self.timer = eTimer()
72                         self.timer.callback.append(self.timerTick)
73                         self.onExecBegin.append(self.startTimer)
74                         self.origTitle = None
75                         if self.execing:
76                                 self.timerTick()
77                         else:
78                                 self.onShown.append(self.__onShown)
79                         self.timerRunning = True
80                 else:
81                         self.timerRunning = False
82
83         def __onShown(self):
84                 self.onShown.remove(self.__onShown)
85                 self.timerTick()
86
87         def startTimer(self):
88                 self.timer.start(1000)
89
90         def stopTimer(self):
91                 if self.timerRunning:
92                         del self.timer
93                         self.onExecBegin.remove(self.startTimer)
94                         self.setTitle(self.origTitle)
95                         self.timerRunning = False
96
97         def timerTick(self):
98                 if self.execing:
99                         self.timeout -= 1
100                         if self.origTitle is None:
101                                 self.origTitle = self.instance.getTitle()
102                         self.setTitle(self.origTitle + " (" + str(self.timeout) + ")")
103                         if self.timeout == 0:
104                                 self.timer.stop()
105                                 self.timerRunning = False
106                                 self.timeoutCallback()
107
108         def timeoutCallback(self):
109                 print "Timeout!"
110                 self.ok()
111
112         def cancel(self):
113                 self.close(False)
114
115         def ok(self):
116                 if self.type == self.TYPE_YESNO:
117                         self.close(self["list"].getCurrent()[1] == 0)
118                 else:
119                         self.close(True)
120
121         def alwaysOK(self):
122                 self.close(True)
123
124         def up(self):
125                 self.move(self["list"].instance.moveUp)
126
127         def down(self):
128                 self.move(self["list"].instance.moveDown)
129
130         def left(self):
131                 self.move(self["list"].instance.pageUp)
132
133         def right(self):
134                 self.move(self["list"].instance.pageDown)
135
136         def move(self, direction):
137                 if self.close_on_any_key:
138                         self.close(True)
139                 self["list"].instance.moveSelection(direction)
140                 if self.list:
141                         self["selectedChoice"].setText(self["list"].getCurrent()[0])
142                 self.stopTimer()
143
144         def __repr__(self):
145                 return str(type(self)) + "(" + self.text + ")"