whitespace fixes
[openblackhole/openblackhole-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, picon=None, simple=False, list=[], timeout_default=None):
16                 self.type = type
17                 Screen.__init__(self, session)
18
19                 if simple:
20                         self.skinName="MessageBoxSimple"
21
22                 self.msgBoxID = msgBoxID
23
24                 self["text"] = Label(text)
25                 self["Text"] = StaticText(text)
26                 self["selectedChoice"] = StaticText()
27
28                 self.text = text
29                 self.close_on_any_key = close_on_any_key
30                 self.timeout_default = timeout_default
31
32                 self["ErrorPixmap"] = Pixmap()
33                 self["QuestionPixmap"] = Pixmap()
34                 self["InfoPixmap"] = Pixmap()
35                 self.timerRunning = False
36                 self.initTimeout(timeout)
37
38                 picon = picon or type
39                 if picon != self.TYPE_ERROR:
40                         self["ErrorPixmap"].hide()
41                 if picon != self.TYPE_YESNO:
42                         self["QuestionPixmap"].hide()
43                 if picon != self.TYPE_INFO:
44                         self["InfoPixmap"].hide()
45
46                 if type == self.TYPE_YESNO:
47                         if list:
48                                 self.list = list
49                         elif default == True:
50                                 self.list = [ (_("yes"), True), (_("no"), False) ]
51                         else:
52                                 self.list = [ (_("no"), False), (_("yes"), True) ]
53                 else:
54                         self.list = []
55
56                 self["list"] = MenuList(self.list)
57                 if self.list:
58                         self["selectedChoice"].setText(self.list[0][0])
59                 else:
60                         self["list"].hide()
61
62                 if enable_input:
63                         self["actions"] = ActionMap(["MsgBoxActions", "DirectionActions"],
64                                 {
65                                         "cancel": self.cancel,
66                                         "ok": self.ok,
67                                         "alwaysOK": self.alwaysOK,
68                                         "up": self.up,
69                                         "down": self.down,
70                                         "left": self.left,
71                                         "right": self.right,
72                                         "upRepeated": self.up,
73                                         "downRepeated": self.down,
74                                         "leftRepeated": self.left,
75                                         "rightRepeated": self.right
76                                 }, -1)
77
78                 self.onLayoutFinish.append(self.layoutFinished)
79
80         def layoutFinished(self):
81                 self.setTitle(_(self.title))
82
83         def initTimeout(self, timeout):
84                 self.timeout = timeout
85                 if timeout > 0:
86                         self.timer = eTimer()
87                         self.timer.callback.append(self.timerTick)
88                         self.onExecBegin.append(self.startTimer)
89                         self.origTitle = None
90                         if self.execing:
91                                 self.timerTick()
92                         else:
93                                 self.onShown.append(self.__onShown)
94                         self.timerRunning = True
95                 else:
96                         self.timerRunning = False
97
98         def __onShown(self):
99                 self.onShown.remove(self.__onShown)
100                 self.timerTick()
101
102         def startTimer(self):
103                 self.timer.start(1000)
104
105         def stopTimer(self):
106                 if self.timerRunning:
107                         del self.timer
108                         self.onExecBegin.remove(self.startTimer)
109                         self.setTitle(self.origTitle)
110                         self.timerRunning = False
111
112         def timerTick(self):
113                 if self.execing:
114                         self.timeout -= 1
115                         if self.origTitle is None:
116                                 self.origTitle = self.instance.getTitle()
117                         self.setTitle(self.origTitle + " (" + str(self.timeout) + ")")
118                         if self.timeout == 0:
119                                 self.timer.stop()
120                                 self.timerRunning = False
121                                 self.timeoutCallback()
122
123         def timeoutCallback(self):
124                 print "Timeout!"
125                 if self.timeout_default is not None:
126                         self.close(self.timeout_default)
127                 else:
128                         self.ok()
129
130         def cancel(self):
131                 self.close(False)
132
133         def ok(self):
134                 if self.list:
135                         self.close(self["list"].getCurrent()[1])
136                 else:
137                         self.close(True)
138
139         def alwaysOK(self):
140                 self.close(True)
141
142         def up(self):
143                 self.move(self["list"].instance.moveUp)
144
145         def down(self):
146                 self.move(self["list"].instance.moveDown)
147
148         def left(self):
149                 self.move(self["list"].instance.pageUp)
150
151         def right(self):
152                 self.move(self["list"].instance.pageDown)
153
154         def move(self, direction):
155                 if self.close_on_any_key:
156                         self.close(True)
157                 self["list"].instance.moveSelection(direction)
158                 if self.list:
159                         self["selectedChoice"].setText(self["list"].getCurrent()[0])
160                 self.stopTimer()
161
162         def __repr__(self):
163                 return str(type(self)) + "(" + self.text + ")"