f8123e5a1b72925f6d121faa96cb9978536aa884
[openblackhole/openblackhole-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["summary_selection"] = StaticText()
35                 self.updateSummary(selection)
36                                 
37                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "ColorActions", "DirectionActions"], 
38                 {
39                         "ok": self.go,
40                         "back": self.cancel,
41                         "1": self.keyNumberGlobal,
42                         "2": self.keyNumberGlobal,
43                         "3": self.keyNumberGlobal,
44                         "4": self.keyNumberGlobal,
45                         "5": self.keyNumberGlobal,
46                         "6": self.keyNumberGlobal,
47                         "7": self.keyNumberGlobal,
48                         "8": self.keyNumberGlobal,
49                         "9": self.keyNumberGlobal,
50                         "0": self.keyNumberGlobal,
51                         "red": self.keyRed,
52                         "green": self.keyGreen,
53                         "yellow": self.keyYellow,
54                         "blue": self.keyBlue,
55                         "up": self.up,
56                         "down": self.down
57                 }, -1)
58                 
59         def keyLeft(self):
60                 pass
61         
62         def keyRight(self):
63                 pass
64         
65         def up(self):
66                 if len(self["list"].list) > 0:
67                         while 1:
68                                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
69                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
70                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == 0:
71                                         break
72
73         def down(self):
74                 if len(self["list"].list) > 0:
75                         while 1:
76                                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
77                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
78                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == len(self["list"].list) - 1:
79                                         break
80
81         # runs a number shortcut
82         def keyNumberGlobal(self, number):
83                 self.goKey(str(number))
84
85         # runs the current selected entry
86         def go(self):
87                 cursel = self["list"].l.getCurrentSelection()
88                 if cursel:
89                         self.goEntry(cursel[0])
90                 else:
91                         self.cancel()
92
93         # runs a specific entry
94         def goEntry(self, entry):
95                 if len(entry) > 2 and isinstance(entry[1], str) and entry[1] == "CALLFUNC":
96                         # CALLFUNC wants to have the current selection as argument
97                         arg = self["list"].l.getCurrentSelection()[0]
98                         entry[2](arg)
99                 else:
100                         self.close(entry)
101
102         # lookups a key in the keymap, then runs it
103         def goKey(self, key):
104                 if self.keymap.has_key(key):
105                         entry = self.keymap[key]
106                         self.goEntry(entry)
107
108         # runs a color shortcut
109         def keyRed(self):
110                 self.goKey("red")
111
112         def keyGreen(self):
113                 self.goKey("green")
114
115         def keyYellow(self):
116                 self.goKey("yellow")
117
118         def keyBlue(self):
119                 self.goKey("blue")
120
121         def updateSummary(self, curpos=0):
122                 pos = 0
123                 summarytext = ""
124                 for entry in self.summarylist:
125                         if pos > curpos-2 and pos < curpos+5:
126                                 if pos == curpos:
127                                         summarytext += ">"
128                                         self["summary_selection"].setText(entry[1])
129                                 else:
130                                         summarytext += entry[0]
131                                 summarytext += ' ' + entry[1] + '\n'
132                         pos += 1
133                 self["summary_list"].setText(summarytext)
134
135         def cancel(self):
136                 self.close(None)