8015621f7e1609437d997cc12cbe060cf043514e
[enigma2.git] / components.py
1 from enigma import *
2 import time
3 import sys
4
5 # some helper classes first:
6 class HTMLComponent:
7         def produceHTML(self):
8                 return ""
9                 
10 class HTMLSkin:
11         order = ()
12
13         def __init__(self, order):
14                 self.order = order
15
16         def produceHTML(self):
17                 res = "<html>\n"
18                 for name in self.order:
19                         res += self[name].produceHTML()
20                 res += "</html>\n";
21                 return res
22
23 class GUISkin:
24         def __init__(self):
25                 self.data = { }
26         
27         def createGUIScreen(self, parent):
28                 for (name, val) in self.items():
29                         self.data[name] = { }
30                         val.GUIcreate(self.data[name], parent, None)
31         
32         def deleteGUIScreen(self):
33                 for (name, val) in self.items():
34                         w = self.data[name]["instance"]
35                         val.GUIdelete(self.data[name])
36                         del self.data[name]
37                         
38                         # note: you'll probably run into this assert. if this happens, don't panic!
39                         # yes, it's evil. I told you that programming in python is just fun, and 
40                         # suddently, you have to care about things you don't even know.
41                         #
42                         # but calm down, the solution is easy, at least on paper:
43                         #
44                         # Each Component, which is a GUIComponent, owns references to each
45                         # instantiated eWidget (namely in screen.data[name]["instance"], in case
46                         # you care.)
47                         # on deleteGUIscreen, all eWidget *must* (!) be deleted (otherwise,
48                         # well, problems appear. I don't want to go into details too much,
49                         # but this would be a memory leak anyway.)
50                         # The assert beyond checks for that. It asserts that the corresponding
51                         # eWidget is about to be removed (i.e., that the refcount becomes 0 after
52                         # running deleteGUIscreen).
53                         # (You might wonder why the refcount is checked for 2 and not for 1 or 0 -
54                         # one reference is still hold by the local variable 'w', another one is
55                         # hold be the function argument to sys.getrefcount itself. So only if it's
56                         # 2 at this point, the object will be destroyed after leaving deleteGUIscreen.)
57                         #
58                         # Now, how to fix this problem? You're holding a reference somewhere. (References
59                         # can only be hold from Python, as eWidget itself isn't related to the c++
60                         # way of having refcounted objects. So it must be in python.)
61                         #
62                         # It could be possible that you're calling deleteGUIscreen trough a call of
63                         # a PSignal. For example, you could try to call screen.doClose() in response
64                         # to a Button::click. This will fail. (It wouldn't work anyway, as you would
65                         # remove a dialog while running it. It never worked - enigma1 just set a 
66                         # per-mainloop variable on eWidget::close() to leave the exec()...)
67                         # That's why Session supports delayed closes. Just call Session.close() and
68                         # it will work.
69                         #
70                         # Another reason is that you just stored the data["instance"] somewhere. or
71                         # added it into a notifier list and didn't removed it.
72                         #
73                         # If you can't help yourself, just ask me. I'll be glad to help you out.
74                         # Sorry for not keeping this code foolproof. I really wanted to archive
75                         # that, but here I failed miserably. All I could do was to add this assert.
76                         assert sys.getrefcount(w) == 2, "too many refs hold to " + str(w)
77         
78         def close(self):
79                 self.deleteGUIScreen()
80                 del self.data
81
82 # note: components can be used in multiple screens, so we have kind of
83 # two contexts: first the per-component one (self), then the per-screen (i.e.:
84 # per eWidget one), called "priv". In "priv", for example, the instance
85 # of the eWidget is stored.
86
87
88 # GUI components have a "notifier list" of associated eWidgets to one component
89 # (as said - one component instance can be used at multiple screens)
90 class GUIComponent:
91         """ GUI component """
92
93         def __init__(self):
94                 self.notifier = [ ]
95         
96         def GUIcreate(self, priv, parent, skindata):
97                 i = self.GUIcreateInstance(self, parent, skindata)
98                 priv["instance"] = i
99                 self.notifier.append(i)
100                 try:
101                         self.notifierAdded(i)
102                 except:
103                         pass
104         
105         # GUIdelete must delete *all* references to the current component!
106         def GUIdelete(self, priv):
107                 g = priv["instance"]
108                 self.notifier.remove(g)
109                 self.GUIdeleteInstance(g)
110                 del priv["instance"]
111
112         def GUIdeleteInstance(self, priv):
113                 pass
114
115 class VariableText:
116         """VariableText can be used for components which have a variable text, based on any widget with setText call"""
117         
118         def __init__(self):
119                 self.message = ""
120         
121         def notifierAdded(self, notifier):
122                 notifier.setText(self.message)
123
124         def setText(self, text):
125                 if self.message != text:
126                         self.message = text
127                         for x in self.notifier:
128                                 x.setText(self.message)
129
130         def getText(self):
131                 return self.message
132
133 class VariableValue:
134         """VariableValue can be used for components which have a variable value (like eSlider), based on any widget with setValue call"""
135         
136         def __init__(self):
137                 self.value = 0
138         
139         def notifierAdded(self, notifier):
140                 notifier.setValue(self.value)
141
142         def setValue(self, value):
143                 if self.value != value:
144                         self.value = value
145                         for x in self.notifier:
146                                 x.setValue(self.value)
147
148         def getValue(self):
149                 return self.value
150
151 # now some "real" components:
152
153 class Clock(HTMLComponent, GUIComponent, VariableText):
154         def __init__(self):
155                 VariableText.__init__(self)
156                 GUIComponent.__init__(self)
157                 self.doClock()
158                 
159                 self.clockTimer = eTimer()
160                 self.clockTimer.timeout.get().append(self.doClock)
161                 self.clockTimer.start(1000)
162
163 # "funktionalitaet"     
164         def doClock(self):
165                 self.setText("clock: " + time.asctime())
166
167 # realisierung als GUI
168         def GUIcreateInstance(self, priv, parent, skindata):
169                 g = eLabel(parent)
170                 return g
171
172 # ...und als HTML:
173         def produceHTML(self):
174                 return self.getText()
175
176 class Button(HTMLComponent, GUIComponent, VariableText):
177         def __init__(self, text=""):
178                 GUIComponent.__init__(self)
179                 VariableText.__init__(self)
180                 self.setText(text)
181                 self.onClick = [ ]
182         
183         def push(self):
184                 for x in self.onClick:
185                         x()
186                 return 0
187         
188 # html: 
189         def produceHTML(self):
190                 return "<input type=\"submit\" text=\"" + self.getText() + "\">\n"
191
192 # GUI:
193         def GUIcreateInstance(self, priv, parent, skindata):
194                 g = eButton(parent)
195                 g.selected.get().append(self.push)
196                 return g
197         
198         def GUIdeleteInstance(self, g):
199                 g.selected.get().remove(self.push)
200
201 class Header(HTMLComponent, GUIComponent, VariableText):
202
203         def __init__(self, message):
204                 GUIComponent.__init__(self)
205                 VariableText.__init__(self)
206                 self.setText(message)
207         
208         def produceHTML(self):
209                 return "<h2>" + self.getText() + "</h2>\n"
210
211         def GUIcreateInstance(self, priv, parent, skindata):
212                 g = eLabel(parent)
213                 g.setText(self.message)
214                 return g
215
216 class VolumeBar(HTMLComponent, GUIComponent, VariableValue):
217         
218         def __init__(self):
219                 GUIComponent.__init__(self)
220                 VariableValue.__init__(self)
221
222         def GUIcreateInstance(self, priv, parent, skindata):
223                 g = eSlider(parent)
224                 g.setRange(0, 100)
225                 return g
226
227
228 class MenuList(HTMLComponent, GUIComponent):
229         def __init__(self, list):
230                 GUIComponent.__init__(self)
231                 self.l = eListboxPythonStringContent()
232                 self.l.setList(list)
233         
234         def getCurrent(self):
235                 return self.l.getCurrentSelection()
236         
237         def GUIcreateInstance(self, priv, parent, skindata):
238                 g = eListbox(parent)
239                 g.setContent(self.l)
240                 return g
241         
242         def GUIdeleteInstance(self, g):
243                 g.setContent(None)
244
245 class ServiceList(HTMLComponent, GUIComponent):
246         def __init__(self):
247                 GUIComponent.__init__(self)
248                 self.l = eListboxServiceContent()
249
250         def GUIcreateInstance(self, priv, parent, skindata):
251                 g = eListbox(parent)
252                 g.setContent(self.l)
253                 return g
254         
255         def GUIdeleteInstance(self, g):
256                 g.setContent(None)
257
258         def setRoot(self, root):
259                 self.l.setRoot(root)