nicer code
[enigma2.git] / lib / python / Screens / ChoiceBox.py
1 from Screens.Screen import Screen
2 from Screens.MessageBox import MessageBox
3 from Components.ActionMap import NumberActionMap
4 from Components.Label import Label
5 from Components.MenuList import MenuList
6 from Components.ChoiceList import ChoiceEntryComponent, ChoiceList
7
8 class ChoiceBox(Screen):
9         def __init__(self, session, title = "", list = [], keys = None, selection = 0):
10                 Screen.__init__(self, session)
11
12                 self["text"] = Label(title)
13                 self.list = []
14                 if keys is None:
15                         self.__keys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "red", "green", "yellow", "blue" ] + (len(list) - 10) * [""]
16                 else:
17                         self.__keys = keys + (len(list) - len(keys)) * [""]
18                         
19                 self.keymap = {}
20                 pos = 0
21                 for x in list:
22                         strpos = str(self.__keys[pos])
23                         self.list.append(ChoiceEntryComponent(key = strpos, text = x))
24                         if self.__keys[pos] != "":
25                                 self.keymap[self.__keys[pos]] = list[pos]
26                         pos += 1
27                 self["list"] = ChoiceList(list = self.list, selection = selection)
28                                 
29                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "ColorActions", "DirectionActions"], 
30                 {
31                         "ok": self.go,
32                         "back": self.cancel,
33                         "1": self.keyNumberGlobal,
34                         "2": self.keyNumberGlobal,
35                         "3": self.keyNumberGlobal,
36                         "4": self.keyNumberGlobal,
37                         "5": self.keyNumberGlobal,
38                         "6": self.keyNumberGlobal,
39                         "7": self.keyNumberGlobal,
40                         "8": self.keyNumberGlobal,
41                         "9": self.keyNumberGlobal,
42                         "0": self.keyNumberGlobal,
43                         "red": self.keyRed,
44                         "green": self.keyGreen,
45                         "yellow": self.keyYellow,
46                         "blue": self.keyBlue,
47                         "up": self.up,
48                         "down": self.down
49                 }, -1)
50                 
51         def keyLeft(self):
52                 pass
53         
54         def keyRight(self):
55                 pass
56         
57         def up(self):
58                 if len(self["list"].list) > 0:
59                         while 1:
60                                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
61                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == 0:
62                                         break
63
64         def down(self):
65                 if len(self["list"].list) > 0:
66                         while 1:
67                                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
68                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == len(self["list"].list) - 1:
69                                         break
70
71         # runs a number shortcut
72         def keyNumberGlobal(self, number):
73                 self.goKey(str(number))
74
75         # runs the current selected entry
76         def go(self):
77                 self.goEntry(self["list"].l.getCurrentSelection()[0])
78
79         # runs a specific entry
80         def goEntry(self, entry):
81                 if len(entry) > 2 and isinstance(entry[1], str) and entry[1] == "CALLFUNC":
82                         # CALLFUNC wants to have the current selection as argument
83                         arg = self["list"].l.getCurrentSelection()[0]
84                         entry[2](arg)
85                 else:
86                         self.close(entry)
87
88         # lookups a key in the keymap, then runs it
89         def goKey(self, key):
90                 if self.keymap.has_key(key):
91                         entry = self.keymap[key]
92                         self.goEntry(entry)
93
94         # runs a color shortcut
95         def keyRed(self):
96                 self.goKey("red")
97
98         def keyGreen(self):
99                 self.goKey("green")
100
101         def keyYellow(self):
102                 self.goKey("yellow")
103
104         def keyBlue(self):
105                 self.goKey("blue")
106
107         def cancel(self):
108                 self.close(None)