first step for skin support
[enigma2.git] / components.py
1 from enigma import *
2 import time
3
4 # some helper classes first:
5 class HTMLComponent:
6         def produceHTML(self):
7                 return ""
8                 
9 class HTMLSkin:
10         order = ()
11
12         def __init__(self, order):
13                 self.order = order
14
15         def produceHTML(self):
16                 res = "<html>\n"
17                 for name in self.order:
18                         res += self[name].produceHTML()
19                 res += "</html>\n";
20                 return res
21
22 class GUISkin:
23         data = { }
24         def createGUIScreen(self, parent):
25                 for (name, val) in self.items():
26                         self.data[name] = { }
27                         val.GUIcreate(self.data[name], parent, None)
28
29 class GUIComponent:
30         """ GUI component """
31
32         def __init__(self):
33                 self.notifier = [ ]
34         
35         def GUIcreate(self, priv, parent, skindata):
36                 i = self.GUIcreateInstance(self, parent, skindata)
37                 priv["instance"] = i
38                 self.notifier.append(i)
39                 if self.notifierAdded:
40                         self.notifierAdded(i)
41
42 class VariableText:
43         """VariableText can be used for components which have a variable text, based on any widget with setText call"""
44         
45         def __init__(self):
46                 self.message = ""
47         
48         def notifierAdded(self, notifier):
49                 notifier.setText(self.message)
50
51         def setText(self, text):
52                 if self.message != text:
53                         self.message = text
54                         for x in self.notifier:
55                                 x.setText(self.message)
56
57         def getText(self):
58                 return self.message
59
60 class VariableValue:
61         """VariableValue can be used for components which have a variable value (like eSlider), based on any widget with setValue call"""
62         
63         def __init__(self):
64                 self.value = 0
65         
66         def notifierAdded(self, notifier):
67                 notifier.setValue(self.value)
68
69         def setValue(self, value):
70                 if self.value != value:
71                         self.value = value
72                         for x in self.notifier:
73                                 x.setValue(self.value)
74
75         def getValue(self):
76                 return self.value
77
78 # now some "real" components:
79
80 class Clock(HTMLComponent, GUIComponent, VariableText):
81         def __init__(self):
82                 VariableText.__init__(self)
83                 GUIComponent.__init__(self)
84                 self.doClock()
85
86 # "funktionalitaet"     
87         def doClock(self):
88                 self.setText("clock: " + time.asctime())
89
90 # realisierung als GUI
91         def GUIcreateInstance(self, priv, parent, skindata):
92                 g = eLabel(parent)
93                 return g
94
95 # ...und als HTML:
96         def produceHTML(self):
97                 return self.getText()
98
99 class Button(HTMLComponent, GUIComponent, VariableText):
100         onClick = {}
101         
102         def __init__(self, text=""):
103                 GUIComponent.__init__(self)
104                 VariableText.__init__(self)
105                 self.setText(text)
106         
107         def click(self):
108                 for x in self.onClick:
109                         x()
110
111 # html: 
112         def produceHTML(self):
113                 return "<input type=\"submit\" text=\"" + self.getText() + "\">\n"
114
115 # GUI:
116         def GUIcreateInstance(self, priv, parent, skindata):
117                 g = eButton(parent)
118 #               g.clicked = [ self.click ]
119                 return g
120
121 class Header(HTMLComponent, GUIComponent, VariableText):
122
123         def __init__(self, message):
124                 GUIComponent.__init__(self)
125                 VariableText.__init__(self)
126                 self.setText(message)
127         
128         def produceHTML(self):
129                 return "<h2>" + self.getText() + "</h2>\n"
130
131         def GUIcreateInstance(self, priv, parent, skindata):
132                 g = eLabel(parent)
133                 g.setText(self.message)
134                 return g
135
136 class VolumeBar(HTMLComponent, GUIComponent, VariableValue):
137         
138         def __init__(self):
139                 GUIComponent.__init__(self)
140                 VariableValue.__init__(self)
141
142         def GUIcreateInstance(self, priv, parent, skindata):
143                 g = eSlider(parent)
144                 g.setRange(0, 100)
145                 return g
146