added some screens / widgets
[openblackhole/openblackhole-enigma2.git] / skin.py
1 from enigma import *
2 import xml.dom.minidom
3 from xml.dom import EMPTY_NAMESPACE
4
5
6 colorNames = dict()
7
8 def dump(x, i=0):
9         print " " * i + str(x)
10         try:
11                 for n in x.childNodes:
12                         dump(n, i + 1)
13         except:
14                 None
15
16 dom = xml.dom.minidom.parseString(
17         """<skin>
18         
19                 <colors>
20                         <color name="white"     value="#ffffff" />
21                         <color name="black"     value="#000000" />
22                         <color name="dark"      value="#294a6b" />
23                         
24                         <color name="red"               value="#ff0000" />
25                         <color name="green"     value="#00ff00" />
26                         <color name="blue"              value="#0000ff" />
27                         <color name="yellow"    value="#c0c000" />
28                 </colors>
29                 <windowstyle type="skinned">
30                         <color name="Background" color="#4075a7" />
31                         <color name="LabelForeground" color="#ffffff" />
32                         <color name="ListboxBackground" color="#4075a7" />
33                         <color name="ListboxForeground" color="#ffffff" />
34                         <color name="ListboxSelectedBackground" color="#80ff80" />
35                         <color name="ListboxSelectedForeground" color="#ffffff" />
36                         <color name="ListboxMarkedBackground" color="#ff0000" />
37                         <color name="ListboxMarkedForeground" color="#ffffff" />
38                         <borderset name="bsWindow">
39                                 <pixmap pos="bpTopLeft"     filename="data/b_w_tl.png" />
40                                 <pixmap pos="bpTop"         filename="data/b_w_t.png"  />
41                                 <pixmap pos="bpTopRight"    filename="data/b_w_tr.png" />
42                                 <pixmap pos="bpLeft"        filename="data/b_w_l.png"  />
43                                 <pixmap pos="bpRight"       filename="data/b_w_r.png"  />
44                                 <pixmap pos="bpBottomLeft"  filename="data/b_w_bl.png" />
45                                 <pixmap pos="bpBottom"      filename="data/b_w_b.png"  />
46                                 <pixmap pos="bpBottomRight" filename="data/b_w_br.png" />
47                         </borderset>
48                 </windowstyle>
49                 <screen name="Menu" position="300,100" size="300,300" title="real main menu">
50 <!--                    <widget name="okbutton" position="10,190" size="280,50" font="Arial;20" valign="center" halign="center" />-->
51                         <widget name="title" position="10,10" size="280,20" />
52                         <widget name="menu" position="10,30" size="280,200" />
53                 </screen>
54                 <screen name="ScartLoopThrough" position="0,0" size="720,576">
55                 </screen>
56                 <screen name="ConfigMenu" position="300,100" size="300,300" title="real main menu">
57                         <widget name="txt_var_1" position="20,20" size="100,20" />
58                         <widget name="btn_var_1" position="110,20" size="200,20" />
59                         <widget name="txt_var_2" position="20,60" size="100,20" />
60                         <widget name="btn_var_2" position="110,60" size="200,20" />
61                 </screen>
62                 <screen name="configOSD" position="140,125" size="460,350" title="OSD Settings">
63                         <widget name="okbutton" position="20,245" size="205,40" />
64                         <widget name="txt_alpha" position="20,20" size="110,20" />
65                         <widget name="sld_alpha" position="150,20" size="290,20" />
66                         <widget name="txt_brightness" position="20,60" size="120,20" />
67                         <widget name="sld_brightness" position="150,20" size="290,20" />
68                         <widget name="txt_gamma" position="20,100" size="120,20" />
69                         <widget name="sld_gamma" position="150,100" size="290,20" />
70                 </screen>
71                 <screen name="configTest" position="300,100" size="300,300" title="config menu">
72                         <widget name="config" position="10,30" size="280,140" />
73                 </screen>
74                 <screen name="clockDisplay" position="300,100" size="300,300">
75                         <widget name="okbutton" position="10,10" size="280,40" />
76                         <widget name="title" position="10,120" size="280,50" />
77                         <widget name="theClock" position="10,60" size="280,50" />
78                 </screen>
79                 <screen name="infoBar" position="0,380" size="720,151" title="InfoBar" flags="wfNoBorder">
80                         <ePixmap position="0,0" size="720,151" pixmap="data/info-bg.png" />
81                         
82                         <widget name="ServiceName" position="69,30" size="427,26" valign="center" font="Arial;32" backgroundColor="#101258" />
83                         <widget name="CurrentTime" position="575,10" size="66,30" backgroundColor="dark" font="Arial;16" />
84                         <widget name="Event_Now" position="273,68" size="282,30" font="Arial;29" backgroundColor="dark" />
85                         <widget name="Event_Next" position="273,98" size="282,30" font="Arial;29" backgroundColor="dark" />
86                         <widget name="Event_Now_Duration" position="555,68" size="70,26" font="Arial;26" backgroundColor="dark" />
87                         <widget name="Event_Next_Duration" position="555,98" size="70,26" font="Arial;26" backgroundColor="dark" />
88 <!--                    <eLabel position="70,0" size="300,30" text=".oO skin Oo." font="Arial;20" /> -->
89                 </screen>
90                 <screen name="channelSelection" position="90,100" size="560,420" title="Channel Selection">
91                         <widget name="list" position="0,50" size="560,340" />
92 <!--                    <widget name="okbutton" position="340,50" size="140,30" />-->
93                         <widget name="key_red" position="0,0" size="140,40" backgroundColor="red" />
94                         <widget name="key_green" position="140,0" size="140,40" backgroundColor="green" />
95                         <widget name="key_yellow" position="280,0" size="140,40" backgroundColor="yellow" />
96                         <widget name="key_blue" position="420,0" size="140,40" backgroundColor="blue" />
97                 </screen>
98                 <screen name="serviceScan" position="150,100" size="300,200" title="Service Scan">
99                         <widget name="scan_progress" position="10,10" size="280,50" />
100                         <widget name="scan_state" position="10,60" size="280,30" />
101                 </screen>
102         </skin>""")
103
104 # filters all elements of childNode with the specified function
105 # example: nodes = elementsWithTag(childNodes, lambda x: x == "bla")
106 def elementsWithTag(el, tag):
107
108         # fiiixme! (works but isn't nice)
109         if tag.__class__ == "".__class__:
110                 str = tag
111                 tag = lambda x: x == str
112                 
113         for x in el:
114                 if x.nodeType != xml.dom.minidom.Element.nodeType:
115                         continue
116                 if tag(x.tagName):
117                         yield x
118
119 def parsePosition(str):
120         x, y = str.split(',')
121         return ePoint(int(x), int(y))
122
123 def parseSize(str):
124         x, y = str.split(',')
125         return eSize(int(x), int(y))
126
127 def parseFont(str):
128         name, size = str.split(';')
129         return gFont(name, int(size))
130
131 def parseColor(str):
132         if str[0] != '#':
133                 try:
134                         return colorNames[str]
135                 except:
136                         raise ("color '%s' must be #aarrggbb or valid named color" % (str))
137         return gRGB(int(str[1:], 0x10))
138
139 def applyAttributes(guiObject, node, desktop):
140         # walk all attributes
141         for p in range(node.attributes.length):
142                 a = node.attributes.item(p)
143                 
144                 # convert to string (was: unicode)
145                 attrib = str(a.name)
146                 # TODO: proper UTF8 translation?! (for value)
147                 # TODO: localization? as in e1?
148                 value = str(a.value)
149                 
150                 # and set attributes
151                 try:
152                         if attrib == 'position':
153                                 guiObject.move(parsePosition(value))
154                         elif attrib == 'size':
155                                 guiObject.resize(parseSize(value))
156                         elif attrib == 'title':
157                                 guiObject.setTitle(value)
158                         elif attrib == 'text':
159                                 guiObject.setText(value)
160                         elif attrib == 'font':
161                                 guiObject.setFont(parseFont(value))
162                         elif attrib == "pixmap":
163                                 ptr = gPixmapPtr()
164                                 if loadPNG(ptr, value):
165                                         raise "loading PNG failed!"
166                                 x = ptr
167                                 ptr = ptr.__deref__()
168                                 print desktop
169                                 desktop.makeCompatiblePixmap(ptr)
170                                 guiObject.setPixmap(ptr)
171 #                               guiObject.setPixmapFromFile(value)
172                         elif attrib == "valign":
173                                 try:
174                                         guiObject.setVAlign(
175                                                 { "top": guiObject.alignTop,
176                                                         "center": guiObject.alignCenter,
177                                                         "bottom": guiObject.alignBottom
178                                                 }[value])
179                                 except KeyError:
180                                         print "valign must be either top, center or bottom!"
181                         elif attrib == "halign":
182                                 try:
183                                         guiObject.setHAlign(
184                                                 { "left": guiObject.alignLeft,
185                                                         "center": guiObject.alignCenter,
186                                                         "right": guiObject.alignRight,
187                                                         "block": guiObject.alignBlock
188                                                 }[value])
189                                 except KeyError:
190                                         print "halign must be either left, center, right or block!"
191                         elif attrib == "flags":
192                                 flags = value.split(',')
193                                 for f in flags:
194                                         try:
195                                                 fv = eWindow.__dict__[f]
196                                                 guiObject.setFlag(fv)
197                                         except KeyError:
198                                                 print "illegal flag %s!" % f
199                         elif attrib == "backgroundColor":
200                                 guiObject.setBackgroundColor(parseColor(value))
201                         elif attrib != 'name':
202                                 print "unsupported attribute " + attrib + "=" + value
203                 except AttributeError:
204                         print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
205
206 def loadSkin():
207         print "loading skin..."
208         
209         def getPNG(x):
210                 g = gPixmapPtr()
211                 loadPNG(g, x)
212                 g = g.grabRef()
213                 return g
214         
215         skin = dom.childNodes[0]
216         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
217         
218         for c in elementsWithTag(skin.childNodes, "colors"):
219                 for color in elementsWithTag(c.childNodes, "color"):
220                         name = str(color.getAttribute("name"))
221                         color = str(color.getAttribute("value"))
222                         
223                         if not len(color):
224                                 raise ("need color and name, got %s %s" % (name, color))
225                                 
226                         colorNames[name] = parseColor(color)
227         
228         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
229                 style = eWindowStyleSkinned()
230                 
231                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
232                         bsName = str(borderset.getAttribute("name"))
233                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
234                                 bpName = str(pixmap.getAttribute("pos"))
235                                 filename = str(pixmap.getAttribute("filename"))
236                                 
237                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], getPNG(filename))
238
239                 for color in elementsWithTag(windowstyle.childNodes, "color"):
240                         type = str(color.getAttribute("name"))
241                         color = parseColor(color.getAttribute("color"))
242                         
243                         try:
244                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
245                         except:
246                                 raise ("Unknown color %s" % (type))
247                         
248                 x = eWindowStyleManagerPtr()
249                 eWindowStyleManager.getInstance(x)
250                 x.setStyle(style)
251
252 def applyGUIskin(screen, skin, name, desktop):
253         myscreen = None
254         
255         # first, find the corresponding screen element
256         skin = dom.childNodes[0]
257         
258         for x in elementsWithTag(skin.childNodes, "screen"):
259                 if x.getAttribute('name') == name:
260                         myscreen = x
261         del skin
262         
263         assert myscreen != None, "no skin for screen '" + name + "' found!"
264         
265         applyAttributes(screen.instance, myscreen, desktop)
266         
267         # now walk all widgets
268         for widget in elementsWithTag(myscreen.childNodes, "widget"):
269                 wname = widget.getAttribute('name')
270                 if wname == None:
271                         print "widget has no name!"
272                         continue
273                 
274                 # get corresponding gui object
275                 try:
276                         guiObject = screen[wname].instance
277                 except:
278                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
279                 
280                 applyAttributes(guiObject, widget, desktop)
281
282         # now walk additional objects
283         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
284                 if widget.tagName == "eLabel":
285                         guiObject = eLabel(screen.instance)
286                 elif widget.tagName == "ePixmap":
287                         guiObject = ePixmap(screen.instance)
288                 else:
289                         raise str("unsupported stuff : %s" % widget.tagName)
290                 
291                 applyAttributes(guiObject, widget, desktop      )
292                 guiObject.thisown = 0