Merge branch 'bug_271_ac3pcm_delay_servicemp3' into experimental
[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 from Components.Sources.StaticText import StaticText
6
7 class ChoiceBox(Screen):
8         def __init__(self, session, title = "", list = [], keys = None, selection = 0, skin_name = []):
9                 Screen.__init__(self, session)
10
11                 if isinstance(skin_name, str):
12                         skin_name = [skin_name]
13                 self.skinName = skin_name + ["ChoiceBox"] 
14
15                 self["text"] = Label(title)
16                 self.list = []
17                 self.summarylist = []
18                 if keys is None:
19                         self.__keys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "red", "green", "yellow", "blue" ] + (len(list) - 10) * [""]
20                 else:
21                         self.__keys = keys + (len(list) - len(keys)) * [""]
22                         
23                 self.keymap = {}
24                 pos = 0
25                 for x in list:
26                         strpos = str(self.__keys[pos])
27                         self.list.append(ChoiceEntryComponent(key = strpos, text = x))
28                         if self.__keys[pos] != "":
29                                 self.keymap[self.__keys[pos]] = list[pos]
30                         self.summarylist.append((self.__keys[pos],x[0]))
31                         pos += 1
32                 self["list"] = ChoiceList(list = self.list, selection = selection)
33                 self["summary_list"] = StaticText()
34                 self.updateSummary()
35                                 
36                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "ColorActions", "DirectionActions"], 
37                 {
38                         "ok": self.go,
39                         "back": self.cancel,
40                         "1": self.keyNumberGlobal,
41                         "2": self.keyNumberGlobal,
42                         "3": self.keyNumberGlobal,
43                         "4": self.keyNumberGlobal,
44                         "5": self.keyNumberGlobal,
45                         "6": self.keyNumberGlobal,
46                         "7": self.keyNumberGlobal,
47                         "8": self.keyNumberGlobal,
48                         "9": self.keyNumberGlobal,
49                         "0": self.keyNumberGlobal,
50                         "red": self.keyRed,
51                         "green": self.keyGreen,
52                         "yellow": self.keyYellow,
53                         "blue": self.keyBlue,
54                         "up": self.up,
55                         "down": self.down
56                 }, -1)
57                 
58         def keyLeft(self):
59                 pass
60         
61         def keyRight(self):
62                 pass
63         
64         def up(self):
65                 if len(self["list"].list) > 0:
66                         while 1:
67                                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
68                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
69                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == 0:
70                                         break
71
72         def down(self):
73                 if len(self["list"].list) > 0:
74                         while 1:
75                                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
76                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
77                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == len(self["list"].list) - 1:
78                                         break
79
80         # runs a number shortcut
81         def keyNumberGlobal(self, number):
82                 self.goKey(str(number))
83
84         # runs the current selected entry
85         def go(self):
86                 cursel = self["list"].l.getCurrentSelection()
87                 if cursel:
88                         self.goEntry(cursel[0])
89                 else:
90                         self.cancel()
91
92         # runs a specific entry
93         def goEntry(self, entry):
94                 if len(entry) > 2 and isinstance(entry[1], str) and entry[1] == "CALLFUNC":
95                         # CALLFUNC wants to have the current selection as argument
96                         arg = self["list"].l.getCurrentSelection()[0]
97                         entry[2](arg)
98                 else:
99                         self.close(entry)
100
101         # lookups a key in the keymap, then runs it
102         def goKey(self, key):
103                 if self.keymap.has_key(key):
104                         entry = self.keymap[key]
105                         self.goEntry(entry)
106
107         # runs a color shortcut
108         def keyRed(self):
109                 self.goKey("red")
110
111         def keyGreen(self):
112                 self.goKey("green")
113
114         def keyYellow(self):
115                 self.goKey("yellow")
116
117         def keyBlue(self):
118                 self.goKey("blue")
119
120         def updateSummary(self, curpos=0):
121                 pos = 0
122                 summarytext = ""
123                 for entry in self.summarylist:
124                         if pos > curpos-2 and pos < curpos+5:
125                                 if pos == curpos:
126                                         summarytext += ">"
127                                 else:
128                                         summarytext += entry[0]
129                                 summarytext += ' ' + entry[1] + '\n'
130                         pos += 1
131                 self["summary_list"].setText(summarytext)
132
133         def cancel(self):
134                 self.close(None)