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