add more useful __repr__
[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):
16                 self.type = type
17                 Screen.__init__(self, session)
18                 
19                 self["text"] = Label(text)
20                 
21                 self.text = text
22                 
23                 self["ErrorPixmap"] = Pixmap()
24                 self["QuestionPixmap"] = Pixmap()
25                 self["InfoPixmap"] = Pixmap()
26                 self.timerRunning = False
27                 if timeout > 0:
28                         self.timer = eTimer()
29                         self.timer.timeout.get().append(self.timerTick)
30                         self.timer.start(1000)
31                         self.origTitle = None
32                         self.onShown.append(self.timerTick)
33                         self.timerRunning = True
34                 self.timeout = timeout
35                 
36                 self.list = []
37                 if type != self.TYPE_ERROR:
38                         self["ErrorPixmap"].hide()
39                 if type != self.TYPE_YESNO:
40                         self["QuestionPixmap"].hide()
41                 if type != self.TYPE_INFO:
42                         self["InfoPixmap"].hide()
43                         
44                 if type == self.TYPE_YESNO:
45                         self.list = [ (_("yes"), 0), (_("no"), 1) ]
46
47
48                 self["list"] = MenuList(self.list)
49                 
50                 self["actions"] = ActionMap(["MsgBoxActions", "DirectionActions"], 
51                         {
52                                 "cancel": self.cancel,
53                                 "ok": self.ok,
54                                 "alwaysOK": self.alwaysOK,
55                                 "up": self.up,
56                                 "down": self.down,
57                                 "left": self.left,
58                                 "right": self.right,
59                                 "upRepeated": self.up,
60                                 "downRepeated": self.down,
61                                 "leftRepeated": self.left,
62                                 "rightRepeated": self.right
63                         }, -1)
64                         
65         
66         def timerTick(self):
67                 self.timeout -= 1
68                 if self.origTitle is None:
69                         self.origTitle = self.instance.getTitle()
70                 self.setTitle(self.origTitle + " (" + str(self.timeout) + ")")
71                 if self.timeout == 0:
72                         self.timer.stop()
73                         self.timerRunning = False
74                         self.timeoutCallback()
75                         
76         def timeoutCallback(self):
77                 print "Timeout!"
78                 self.ok()
79         
80         def cancel(self):
81                 self.close(False)
82         
83         def ok(self):
84                 if self.type == self.TYPE_YESNO:
85                         self.close(self["list"].getCurrent()[1] == 0)
86                 else:
87                         self.close(True)
88
89         def alwaysOK(self):
90                 self.close(True)
91
92         def up(self):
93                 self.move(self["list"].instance.moveUp)
94                 
95         def down(self):
96                 self.move(self["list"].instance.moveDown)
97
98         def left(self):
99                 self.move(self["list"].instance.pageUp)
100                 
101
102                 
103         def right(self):
104                 self.move(self["list"].instance.pageDown)
105
106         def move(self, direction):
107                 self["list"].instance.moveSelection(direction)
108                 if self.timerRunning:
109                         self.timer.stop()
110                         self.setTitle(self.origTitle)
111                         self.timerRunning = False
112
113         def __repr__(self):
114                 return str(type(self)) + "(" + self.text + ")"