do some voodoo on the config list to store the connectedTo values properly (reset...
[enigma2.git] / lib / python / Screens / ChoiceBox.py
index 9711da2dc5680670ed0a422ec923d00554b28f76..0436f66d0152993fff3076e0614b07bf27f86f8b 100644 (file)
@@ -1,14 +1,8 @@
-from enigma import *
 from Screens.Screen import Screen
-from Screens.MessageBox import MessageBox
 from Components.ActionMap import NumberActionMap
 from Components.Label import Label
-from Components.MenuList import MenuList
-from Components.GUIComponent import *
 from Components.ChoiceList import ChoiceEntryComponent, ChoiceList
 
-import os
-
 class ChoiceBox(Screen):
        def __init__(self, session, title = "", list = [], keys = None, selection = 0):
                Screen.__init__(self, session)
@@ -16,17 +10,17 @@ class ChoiceBox(Screen):
                self["text"] = Label(title)
                self.list = []
                if keys is None:
-                       self.keys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "red", "green", "yellow", "blue" ] + (len(list) - 10) * [""]
+                       self.__keys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "red", "green", "yellow", "blue" ] + (len(list) - 10) * [""]
                else:
-                       self.keys = keys
+                       self.__keys = keys + (len(list) - len(keys)) * [""]
                        
                self.keymap = {}
                pos = 0
                for x in list:
-                       strpos = str(self.keys[pos])
+                       strpos = str(self.__keys[pos])
                        self.list.append(ChoiceEntryComponent(key = strpos, text = x))
-                       if self.keys[pos] != "":
-                               self.keymap[self.keys[pos]] = list[pos]
+                       if self.__keys[pos] != "":
+                               self.keymap[self.__keys[pos]] = list[pos]
                        pos += 1
                self["list"] = ChoiceList(list = self.list, selection = selection)
                                
@@ -59,42 +53,58 @@ class ChoiceBox(Screen):
                pass
        
        def up(self):
-               while 1:
-                       self["list"].instance.moveSelection(self["list"].instance.moveUp)
-                       if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == 0:
-                               break
-               
-       def down(self):
-               while 1:
-                       self["list"].instance.moveSelection(self["list"].instance.moveDown)
-                       if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == len(self["list"].list) - 1:
-                               break
+               if len(self["list"].list) > 0:
+                       while 1:
+                               self["list"].instance.moveSelection(self["list"].instance.moveUp)
+                               if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == 0:
+                                       break
 
+       def down(self):
+               if len(self["list"].list) > 0:
+                       while 1:
+                               self["list"].instance.moveSelection(self["list"].instance.moveDown)
+                               if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == len(self["list"].list) - 1:
+                                       break
 
+       # runs a number shortcut
        def keyNumberGlobal(self, number):
-               print "pressed", number
-               if self.keymap.has_key(str(number)):
-                       self.close(self.keymap[str(number)])
-               
+               self.goKey(str(number))
+
+       # runs the current selected entry
        def go(self):
-               self.close(self["list"].l.getCurrentSelection()[0])
-               #self.close(self["input"].getText())
+               cursel = self["list"].l.getCurrentSelection()
+               if cursel:
+                       self.goEntry(cursel[0])
+               else:
+                       self.cancel()
+
+       # runs a specific entry
+       def goEntry(self, entry):
+               if len(entry) > 2 and isinstance(entry[1], str) and entry[1] == "CALLFUNC":
+                       # CALLFUNC wants to have the current selection as argument
+                       arg = self["list"].l.getCurrentSelection()[0]
+                       entry[2](arg)
+               else:
+                       self.close(entry)
+
+       # lookups a key in the keymap, then runs it
+       def goKey(self, key):
+               if self.keymap.has_key(key):
+                       entry = self.keymap[key]
+                       self.goEntry(entry)
 
+       # runs a color shortcut
        def keyRed(self):
-               if self.keymap.has_key("red"):
-                       self.close(self.keymap["red"])
+               self.goKey("red")
 
        def keyGreen(self):
-               if self.keymap.has_key("green"):
-                       self.close(self.keymap["green"])
-       
+               self.goKey("green")
+
        def keyYellow(self):
-               if self.keymap.has_key("yellow"):
-                       self.close(self.keymap["yellow"])
-                       
+               self.goKey("yellow")
+
        def keyBlue(self):
-               if self.keymap.has_key("blue"):
-                       self.close(self.keymap["blue"])
-                       
+               self.goKey("blue")
+
        def cancel(self):
-               self.close(None)
\ No newline at end of file
+               self.close(None)