no more crash when press ok in a empty choicebox
[enigma2.git] / lib / python / Screens / ChoiceBox.py
index cc6afb1c33ee972afd2c5330d8bdc43621d70516..b1d74ad54db88960ed29025e1834fb5aacbd56f3 100644 (file)
@@ -1,25 +1,35 @@
-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 *
-
-import os
+from Components.ChoiceList import ChoiceEntryComponent, ChoiceList
 
 class ChoiceBox(Screen):
-       def __init__(self, session, title = "", list = []):
+       def __init__(self, session, title = "", list = [], keys = None, selection = 0):
                Screen.__init__(self, session)
 
                self["text"] = Label(title)
-               self.list = list
-               self["list"] = MenuList(list)
+               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) * [""]
+               else:
+                       self.__keys = keys + (len(list) - len(keys)) * [""]
+                       
+               self.keymap = {}
+               pos = 0
+               for x in list:
+                       strpos = str(self.__keys[pos])
+                       self.list.append(ChoiceEntryComponent(key = strpos, text = x))
+                       if self.__keys[pos] != "":
+                               self.keymap[self.__keys[pos]] = list[pos]
+                       pos += 1
+               self["list"] = ChoiceList(list = self.list, selection = selection)
                                
-               self["actions"] = NumberActionMap(["WizardActions", "InputActions"], 
+               self["actions"] = NumberActionMap(["WizardActions", "InputActions", "ColorActions", "DirectionActions"], 
                {
                        "ok": self.go,
-                       "back": self.close,
+                       "back": self.cancel,
                        "1": self.keyNumberGlobal,
                        "2": self.keyNumberGlobal,
                        "3": self.keyNumberGlobal,
@@ -29,7 +39,13 @@ class ChoiceBox(Screen):
                        "7": self.keyNumberGlobal,
                        "8": self.keyNumberGlobal,
                        "9": self.keyNumberGlobal,
-                       "0": self.keyNumberGlobal
+                       "0": self.keyNumberGlobal,
+                       "red": self.keyRed,
+                       "green": self.keyGreen,
+                       "yellow": self.keyYellow,
+                       "blue": self.keyBlue,
+                       "up": self.up,
+                       "down": self.down
                }, -1)
                
        def keyLeft(self):
@@ -38,13 +54,59 @@ class ChoiceBox(Screen):
        def keyRight(self):
                pass
        
+       def up(self):
+               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
-               #self["input"].number(number)
-               
+               self.goKey(str(number))
+
+       # runs the current selected entry
        def go(self):
-               self.close(self["list"].l.getCurrentSelection())
-               #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):
+               self.goKey("red")
+
+       def keyGreen(self):
+               self.goKey("green")
+
+       def keyYellow(self):
+               self.goKey("yellow")
+
+       def keyBlue(self):
+               self.goKey("blue")
+
        def cancel(self):
-               self.close(None)
\ No newline at end of file
+               self.close(None)