update python
[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 session.close() 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                 if self.notifierAdded:
101                         self.notifierAdded(i)
102         
103         # GUIdelete must delete *all* references to the current component!
104         def GUIdelete(self, priv):
105                 g = priv["instance"]
106                 self.notifier.remove(g)
107                 self.GUIdeleteInstance(g)
108                 del priv["instance"]
109
110         def GUIdeleteInstance(self, priv):
111                 pass
112
113 class VariableText:
114         """VariableText can be used for components which have a variable text, based on any widget with setText call"""
115         
116         def __init__(self):
117                 self.message = ""
118         
119         def notifierAdded(self, notifier):
120                 notifier.setText(self.message)
121
122         def setText(self, text):
123                 if self.message != text:
124                         self.message = text
125                         for x in self.notifier:
126                                 x.setText(self.message)
127
128         def getText(self):
129                 return self.message
130
131 class VariableValue:
132         """VariableValue can be used for components which have a variable value (like eSlider), based on any widget with setValue call"""
133         
134         def __init__(self):
135                 self.value = 0
136         
137         def notifierAdded(self, notifier):
138                 notifier.setValue(self.value)
139
140         def setValue(self, value):
141                 if self.value != value:
142                         self.value = value
143                         for x in self.notifier:
144                                 x.setValue(self.value)
145
146         def getValue(self):
147                 return self.value
148
149 # now some "real" components:
150
151 class Clock(HTMLComponent, GUIComponent, VariableText):
152         def __init__(self):
153                 VariableText.__init__(self)
154                 GUIComponent.__init__(self)
155                 self.doClock()
156                 
157                 self.clockTimer = eTimer()
158                 self.clockTimer.timeout.get().append(self.doClock)
159                 self.clockTimer.start(1000)
160
161 # "funktionalitaet"     
162         def doClock(self):
163                 self.setText("clock: " + time.asctime())
164
165 # realisierung als GUI
166         def GUIcreateInstance(self, priv, parent, skindata):
167                 g = eLabel(parent)
168                 return g
169
170 # ...und als HTML:
171         def produceHTML(self):
172                 return self.getText()
173
174 class Button(HTMLComponent, GUIComponent, VariableText):
175         def __init__(self, text=""):
176                 GUIComponent.__init__(self)
177                 VariableText.__init__(self)
178                 self.setText(text)
179                 self.onClick = [ ]
180         
181         def push(self):
182                 for x in self.onClick:
183                         x()
184                 return 0
185         
186 # html: 
187         def produceHTML(self):
188                 return "<input type=\"submit\" text=\"" + self.getText() + "\">\n"
189
190 # GUI:
191         def GUIcreateInstance(self, priv, parent, skindata):
192                 g = eButton(parent)
193                 g.selected.get().append(self.push)
194                 return g
195         
196         def GUIdeleteInstance(self, g):
197                 g.selected.get().remove(self.push)
198
199 class Header(HTMLComponent, GUIComponent, VariableText):
200
201         def __init__(self, message):
202                 GUIComponent.__init__(self)
203                 VariableText.__init__(self)
204                 self.setText(message)
205         
206         def produceHTML(self):
207                 return "<h2>" + self.getText() + "</h2>\n"
208
209         def GUIcreateInstance(self, priv, parent, skindata):
210                 g = eLabel(parent)
211                 g.setText(self.message)
212                 return g
213
214 class VolumeBar(HTMLComponent, GUIComponent, VariableValue):
215         
216         def __init__(self):
217                 GUIComponent.__init__(self)
218                 VariableValue.__init__(self)
219
220         def GUIcreateInstance(self, priv, parent, skindata):
221                 g = eSlider(parent)
222                 g.setRange(0, 100)
223                 return g