Let ChoiceBox font and calculated size be set from the aliasses.
[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 import enigma
7
8 class ChoiceBox(Screen):
9         def __init__(self, session, title = "", list = [], keys = None, selection = 0, skin_name = []):
10                 Screen.__init__(self, session)
11
12                 if isinstance(skin_name, str):
13                         skin_name = [skin_name]
14                 self.skinName = skin_name + ["ChoiceBox"] 
15
16                 self["text"] = Label(title)
17                 self.list = []
18                 self.summarylist = []
19                 if keys is None:
20                         self.__keys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "red", "green", "yellow", "blue" ] + (len(list) - 10) * [""]
21                 else:
22                         self.__keys = keys + (len(list) - len(keys)) * [""]
23                         
24                 self.keymap = {}
25                 pos = 0
26                 for x in list:
27                         strpos = str(self.__keys[pos])
28                         self.list.append(ChoiceEntryComponent(key = strpos, text = x))
29                         if self.__keys[pos] != "":
30                                 self.keymap[self.__keys[pos]] = list[pos]
31                         self.summarylist.append((self.__keys[pos],x[0]))
32                         pos += 1
33                 self["list"] = ChoiceList(list = self.list, selection = selection)
34                 self["summary_list"] = StaticText()
35                 self["summary_selection"] = StaticText()
36                 self.updateSummary(selection)
37                                 
38                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "ColorActions", "DirectionActions"], 
39                 {
40                         "ok": self.go,
41                         "back": self.cancel,
42                         "1": self.keyNumberGlobal,
43                         "2": self.keyNumberGlobal,
44                         "3": self.keyNumberGlobal,
45                         "4": self.keyNumberGlobal,
46                         "5": self.keyNumberGlobal,
47                         "6": self.keyNumberGlobal,
48                         "7": self.keyNumberGlobal,
49                         "8": self.keyNumberGlobal,
50                         "9": self.keyNumberGlobal,
51                         "0": self.keyNumberGlobal,
52                         "red": self.keyRed,
53                         "green": self.keyGreen,
54                         "yellow": self.keyYellow,
55                         "blue": self.keyBlue,
56                         "up": self.up,
57                         "down": self.down
58                 }, -1)
59
60         def autoResize(self):
61                 orgwidth = self.instance.size().width()
62                 orgpos = self.instance.position()
63                 textsize = self["text"].getSize()
64                 count = len(self.list)
65                 if count > 10:
66                         count = 10
67                 offset = 25 * count
68                 wsizex = textsize[0] + 60
69                 wsizey = textsize[1] + offset
70                 if (520 > wsizex):
71                         wsizex = 520
72                 wsize = (wsizex, wsizey)
73                 # resize
74                 self.instance.resize(enigma.eSize(*wsize))
75                 # resize label
76                 self["text"].instance.resize(enigma.eSize(*textsize))
77                 # move list
78                 listsize = (wsizex, 25 * count)
79                 self["list"].instance.move(enigma.ePoint(0, textsize[1]))
80                 self["list"].instance.resize(enigma.eSize(*listsize))
81                 # center window
82                 newwidth = wsize[0]
83                 self.instance.move(enigma.ePoint((720-wsizex)/2, (576-wsizey)/(count > 7 and 2 or 3)))
84
85         def keyLeft(self):
86                 pass
87         
88         def keyRight(self):
89                 pass
90         
91         def up(self):
92                 if len(self["list"].list) > 0:
93                         while 1:
94                                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
95                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
96                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == 0:
97                                         break
98
99         def down(self):
100                 if len(self["list"].list) > 0:
101                         while 1:
102                                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
103                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
104                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == len(self["list"].list) - 1:
105                                         break
106
107         # runs a number shortcut
108         def keyNumberGlobal(self, number):
109                 self.goKey(str(number))
110
111         # runs the current selected entry
112         def go(self):
113                 cursel = self["list"].l.getCurrentSelection()
114                 if cursel:
115                         self.goEntry(cursel[0])
116                 else:
117                         self.cancel()
118
119         # runs a specific entry
120         def goEntry(self, entry):
121                 if len(entry) > 2 and isinstance(entry[1], str) and entry[1] == "CALLFUNC":
122                         # CALLFUNC wants to have the current selection as argument
123                         arg = self["list"].l.getCurrentSelection()[0]
124                         entry[2](arg)
125                 else:
126                         self.close(entry)
127
128         # lookups a key in the keymap, then runs it
129         def goKey(self, key):
130                 if self.keymap.has_key(key):
131                         entry = self.keymap[key]
132                         self.goEntry(entry)
133
134         # runs a color shortcut
135         def keyRed(self):
136                 self.goKey("red")
137
138         def keyGreen(self):
139                 self.goKey("green")
140
141         def keyYellow(self):
142                 self.goKey("yellow")
143
144         def keyBlue(self):
145                 self.goKey("blue")
146
147         def updateSummary(self, curpos=0):
148                 pos = 0
149                 summarytext = ""
150                 for entry in self.summarylist:
151                         if pos > curpos-2 and pos < curpos+5:
152                                 if pos == curpos:
153                                         summarytext += ">"
154                                         self["summary_selection"].setText(entry[1])
155                                 else:
156                                         summarytext += entry[0]
157                                 summarytext += ' ' + entry[1] + '\n'
158                         pos += 1
159                 self["summary_list"].setText(summarytext)
160
161         def cancel(self):
162                 self.close(None)