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