Merge commit 'origin/bug_112_enable_extensions_manager' into experimental
[enigma2.git] / lib / python / Components / Sources / List.py
1 from Source import Source
2 from Components.Element import cached
3
4 class List(Source, object):
5         """The datasource of a listbox. Currently, the format depends on the used converter. So
6 if you put a simple string list in here, you need to use a StringList converter, if you are
7 using a "multi content list styled"-list, you need to use the StaticMultiList converter, and
8 setup the "fonts". 
9
10 This has been done so another converter could convert the list to a different format, for example
11 to generate HTML."""
12         def __init__(self, list = [ ], enableWrapAround = False, item_height = 25, fonts = [ ]):
13                 Source.__init__(self)
14                 self.__list = list
15                 self.onSelectionChanged = [ ]
16                 self.item_height = item_height
17                 self.fonts = fonts
18                 self.disable_callbacks = False
19                 self.enableWrapAround = enableWrapAround
20                 self.__style = "default" # style might be an optional string which can be used to define different visualisations in the skin
21
22         def setList(self, list):
23                 self.__list = list
24                 self.changed((self.CHANGED_ALL,))
25
26         list = property(lambda self: self.__list, setList)
27
28         def entry_changed(self, index):
29                 if not self.disable_callbacks:
30                         self.downstream_elements.entry_changed(index)
31
32         def modifyEntry(self, index, data):
33                 self.__list[index] = data
34                 self.entry_changed(index)
35
36         def count(self):
37                 return len(self.__list)
38
39         def selectionChanged(self, index):
40                 if self.disable_callbacks:
41                         return
42
43                 # update all non-master targets
44                 for x in self.downstream_elements:
45                         if x is not self.master:
46                                 x.index = index
47
48                 for x in self.onSelectionChanged:
49                         x()
50
51         @cached
52         def getCurrent(self):
53                 return self.master is not None and self.master.current
54
55         current = property(getCurrent)
56
57         def setIndex(self, index):
58                 if self.master is not None:
59                         self.master.index = index
60                         self.selectionChanged(index)
61
62         @cached
63         def getIndex(self):
64                 if self.master is not None:
65                         return self.master.index
66                 else:
67                         return None
68
69         setCurrentIndex = setIndex
70
71         index = property(getIndex, setIndex)
72         
73         def selectNext(self):
74                 if self.getIndex() + 1 >= self.count():
75                         if self.enableWrapAround:
76                                 self.index = 0
77                 else:
78                         self.index += 1
79                 self.setIndex(self.index)
80
81         def selectPrevious(self):
82                 if self.getIndex() - 1 < 0:
83                         if self.enableWrapAround:
84                                 self.index = self.count() - 1
85                 else:
86                         self.index -= 1
87                 self.setIndex(self.index)
88
89         @cached
90         def getStyle(self):
91                 return self.__style
92
93         def setStyle(self, style):
94                 self.__style = style
95                 self.changed((self.CHANGED_SPECIFIC, "style"))
96
97         style = property(getStyle, setStyle)
98
99         def updateList(self, list):
100                 """Changes the list without changing the selection or emitting changed Events"""
101                 assert len(list) == len(self.__list)
102                 old_index = self.index
103                 self.disable_callbacks = True
104                 self.list = list
105                 self.index = old_index
106                 self.disable_callbacks = False