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