added config stuff
[openblackhole/openblackhole-enigma2.git] / skin.py
1 from enigma import *
2 import xml.dom.minidom
3 from xml.dom import EMPTY_NAMESPACE
4
5 from Tools.XMLTools import elementsWithTag, mergeText
6
7 colorNames = dict()
8
9 def dump(x, i=0):
10         print " " * i + str(x)
11         try:
12                 for n in x.childNodes:
13                         dump(n, i + 1)
14         except:
15                 None
16
17 dom = xml.dom.minidom.parseString(
18         """<skin>
19         
20                 <colors>
21                         <color name="white"     value="#ffffff" />
22                         <color name="black"     value="#000000" />
23                         <color name="dark"      value="#20294a6b" />
24                         
25                         <color name="red"               value="#ff0000" />
26                         <color name="green"     value="#00ff00" />
27                         <color name="blue"              value="#0000ff" />
28                         <color name="yellow"    value="#c0c000" />
29                 </colors>
30                 <windowstyle type="skinned">
31                         <color name="Background" color="#4075a7" />
32                         <color name="LabelForeground" color="#ffffff" />
33                         <color name="ListboxBackground" color="#4075a7" />
34                         <color name="ListboxForeground" color="#ffffff" />
35                         <color name="ListboxSelectedBackground" color="#404080" />
36                         <color name="ListboxSelectedForeground" color="#ffffff" />
37                         <color name="ListboxMarkedBackground" color="#ff0000" />
38                         <color name="ListboxMarkedForeground" color="#ffffff" />
39                         <borderset name="bsWindow">
40                                 <pixmap pos="bpTopLeft"     filename="data/b_w_tl.png" />
41                                 <pixmap pos="bpTop"         filename="data/b_w_t.png"  />
42                                 <pixmap pos="bpTopRight"    filename="data/b_w_tr.png" />
43                                 <pixmap pos="bpLeft"        filename="data/b_w_l.png"  />
44                                 <pixmap pos="bpRight"       filename="data/b_w_r.png"  />
45                                 <pixmap pos="bpBottomLeft"  filename="data/b_w_bl.png" />
46                                 <pixmap pos="bpBottom"      filename="data/b_w_b.png"  />
47                                 <pixmap pos="bpBottomRight" filename="data/b_w_br.png" />
48                         </borderset>
49                 </windowstyle> """ """
50                 <screen name="Menu" position="300,100" size="300,300" title="real main menu">
51 <!--                    <widget name="okbutton" position="10,190" size="280,50" font="Arial;20" valign="center" halign="center" />-->
52                         <widget name="title" position="10,10" size="280,20" />
53                         <widget name="menu" position="10,30" size="280,200" />
54                         
55                 </screen>
56                 <screen name="ScartLoopThrough" position="0,0" size="720,576">
57                 </screen>
58                 <screen name="ConfigMenu" position="300,100" size="300,300" title="real main menu">
59                         <widget name="txt_var_1" position="20,20" size="100,20" />
60                         <widget name="btn_var_1" position="110,20" size="200,20" />
61                         <widget name="txt_var_2" position="20,60" size="100,20" />
62                         <widget name="btn_var_2" position="110,60" size="200,20" />
63                 </screen>
64                 <screen name="Setup" position="140,125" size="460,350" title="RCU Setup">
65                         <widget name="config" position="10,30" size="280,140" />
66                 </screen>
67                 <screen name="configOSD" position="140,125" size="460,350" title="OSD Settings">
68                         <widget name="okbutton" position="20,245" size="205,40" />
69                         <widget name="txt_alpha" position="20,20" size="110,20" />
70                         <widget name="sld_alpha" position="150,20" size="290,20" />
71                         <widget name="txt_brightness" position="20,60" size="120,20" />
72                         <widget name="sld_brightness" position="150,20" size="290,20" />
73                         <widget name="txt_gamma" position="20,100" size="120,20" />
74                         <widget name="sld_gamma" position="150,100" size="290,20" />
75                 </screen>
76                 <screen name="configTest" position="300,100" size="300,300" title="config menu">
77                         <widget name="config" position="10,30" size="280,140" />
78                 </screen>
79                 <screen name="TimerEditList" position="160,100" size="420,430" title="Timer Editor">
80                         <widget name="timerlist" position="10,30" size="400,300" />
81                 </screen>
82                 <screen name="clockDisplay" position="300,100" size="300,300">
83                         <widget name="okbutton" position="10,10" size="280,40" />
84                         <widget name="title" position="10,120" size="280,50" />
85                         <widget name="theClock" position="10,60" size="280,50" />
86                 </screen>
87                 <screen name="InfoBar" flags="wfNoBorder" position="0,380" size="720,148" title="InfoBar">
88                         <ePixmap position="0,0" size="720,148" pixmap="data/info-bg.png" />
89                         
90                         <widget name="ServiceName" position="69,25" size="427,26" valign="center" font="Arial;22" backgroundColor="#101258" />
91                         <widget name="CurrentTime" position="575,10" size="80,30" backgroundColor="dark" font="Arial;19" />
92                         <widget name="Volume" position="575,45" size="100,5" backgroundColor="dark" />
93                         <widget name="Event_Now" position="273,68" size="282,30" font="Arial;22" backgroundColor="dark" />
94                         <widget name="Event_Next" position="273,98" size="282,30" font="Arial;22" backgroundColor="dark" />
95                         <widget name="Event_Now_Duration" position="555,68" size="70,26" font="Arial;22" backgroundColor="dark" />
96                         <widget name="Event_Next_Duration" position="555,98" size="70,26" font="Arial;22" backgroundColor="dark" />
97 <!--                    <eLabel position="70,0" size="300,30" text=".oO skin Oo." font="Arial;20" /> -->
98                 </screen>
99                 <screen name="ChannelSelection" position="90,100" size="560,420" title="Channel Selection">
100                         <widget name="list" position="0,50" size="560,340" />
101 <!--                    <widget name="okbutton" position="340,50" size="140,30" />-->
102                         <widget name="key_red" position="0,0" size="140,40" backgroundColor="red" />
103                         <widget name="key_green" position="140,0" size="140,40" backgroundColor="green" />
104                         <widget name="key_yellow" position="280,0" size="140,40" backgroundColor="yellow" />
105                         <widget name="key_blue" position="420,0" size="140,40" backgroundColor="blue" />
106                 </screen>
107                 <screen name="MovieSelection" position="150,100" size="400,420" title="Select-a-movie">
108                         <widget name="list" position="0,50" size="400,300" />
109                 </screen>
110                 <screen name="ServiceScan" position="150,100" size="300,200" title="Service Scan">
111                         <widget name="scan_progress" position="10,10" size="280,50" />
112                         <widget name="scan_state" position="10,60" size="280,30" />
113                 </screen>
114                 <screen name="TimerEdit" position="70,100" size="590,335" title="Timer Edit">
115                         <widget name="description" position="10,10" size="580,40" font="Arial;25" />
116                         <widget name="lbegin" position="405,102" size="103,30" font="Arial;25" foregroundColor="red" />
117                         <widget name="lend" position="405,158" size="103,30" font="Arial;25" foregroundColor="green" />
118                         <widget name="begin" position="508,105" size="72,35" font="Arial;25" />
119                         <widget name="end" position="508,150" size="72,35" font="Arial;25" />
120                         <widget name="apply" position="10,240" size="250,35" />
121                 </screen>
122                 <screen name="MessageBox" position="0,300" size="720,10" title="Message">
123                         <widget name="text" position="0,0" size="500,0" font="Arial;25" />
124                         <applet type="onLayoutFinish">
125 # this should be factored out into some helper code, but currently demonstrated applets.
126 from enigma import eSize, ePoint
127
128 orgwidth = self.instance.size().width()
129 orgpos = self.instance.position()
130 textsize = self["text"].getSize()
131
132 # y size still must be fixed in font stuff...
133 textsize = (textsize[0], textsize[1] + 20)
134 wsize = (textsize[0] + 20, textsize[1] + 20)
135
136 # resize 
137 self.instance.resize(eSize(*wsize))
138
139 # resize label
140 self["text"].instance.resize(eSize(*textsize))
141
142 # center window
143 newwidth = wsize[0]
144 self.instance.move(ePoint(orgpos.x() + (orgwidth - newwidth)/2, orgpos.y()))
145                         </applet>
146                 </screen>
147         </skin>""")
148
149 def parsePosition(str):
150         x, y = str.split(',')
151         return ePoint(int(x), int(y))
152
153 def parseSize(str):
154         x, y = str.split(',')
155         return eSize(int(x), int(y))
156
157 def parseFont(str):
158         name, size = str.split(';')
159         return gFont(name, int(size))
160
161 def parseColor(str):
162         if str[0] != '#':
163                 try:
164                         return colorNames[str]
165                 except:
166                         raise ("color '%s' must be #aarrggbb or valid named color" % (str))
167         return gRGB(int(str[1:], 0x10))
168
169 def collectAttributes(skinAttributes, node):
170         # walk all attributes
171         for p in range(node.attributes.length):
172                 a = node.attributes.item(p)
173                 
174                 # convert to string (was: unicode)
175                 attrib = str(a.name)
176                 # TODO: proper UTF8 translation?! (for value)
177                 # TODO: localization? as in e1?
178                 value = str(a.value)
179                 
180                 skinAttributes.append((attrib, value))
181
182 def applySingleAttribute(guiObject, desktop, attrib, value):            
183         # and set attributes
184         try:
185                 if attrib == 'position':
186                         guiObject.move(parsePosition(value))
187                 elif attrib == 'size':
188                         guiObject.resize(parseSize(value))
189                 elif attrib == 'title':
190                         guiObject.setTitle(value)
191                 elif attrib == 'text':
192                         guiObject.setText(value)
193                 elif attrib == 'font':
194                         guiObject.setFont(parseFont(value))
195                 elif attrib == "pixmap":
196                         ptr = gPixmapPtr()
197                         if loadPNG(ptr, value):
198                                 raise "loading PNG failed!"
199                         x = ptr
200                         ptr = ptr.__deref__()
201                         desktop.makeCompatiblePixmap(ptr)
202                         guiObject.setPixmap(ptr)
203                         # guiObject.setPixmapFromFile(value)
204                 elif attrib == "valign":
205                         try:
206                                 guiObject.setVAlign(
207                                         { "top": guiObject.alignTop,
208                                                 "center": guiObject.alignCenter,
209                                                 "bottom": guiObject.alignBottom
210                                         }[value])
211                         except KeyError:
212                                 print "valign must be either top, center or bottom!"
213                 elif attrib == "halign":
214                         try:
215                                 guiObject.setHAlign(
216                                         { "left": guiObject.alignLeft,
217                                                 "center": guiObject.alignCenter,
218                                                 "right": guiObject.alignRight,
219                                                 "block": guiObject.alignBlock
220                                         }[value])
221                         except KeyError:
222                                 print "halign must be either left, center, right or block!"
223                 elif attrib == "flags":
224                         flags = value.split(',')
225                         for f in flags:
226                                 try:
227                                         fv = eWindow.__dict__[f]
228                                         guiObject.setFlag(fv)
229                                 except KeyError:
230                                         print "illegal flag %s!" % f
231                 elif attrib == "backgroundColor":
232                         guiObject.setBackgroundColor(parseColor(value))
233                 elif attrib == "foregroundColor":
234                         guiObject.setForegroundColor(parseColor(value))
235                 elif attrib != 'name':
236                         print "unsupported attribute " + attrib + "=" + value
237         except int:
238 # AttributeError:
239                 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
240
241 def applyAllAttributes(guiObject, desktop, attributes):
242         for (attrib, value) in attributes:
243                 applySingleAttribute(guiObject, desktop, attrib, value)
244
245 def loadSkin(desktop):
246         print "loading skin..."
247         
248         def getPNG(x):
249                 g = gPixmapPtr()
250                 loadPNG(g, x)
251                 g = g.grabRef()
252                 return g
253         
254         skin = dom.childNodes[0]
255         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
256         
257         for c in elementsWithTag(skin.childNodes, "colors"):
258                 for color in elementsWithTag(c.childNodes, "color"):
259                         name = str(color.getAttribute("name"))
260                         color = str(color.getAttribute("value"))
261                         
262                         if not len(color):
263                                 raise ("need color and name, got %s %s" % (name, color))
264                                 
265                         colorNames[name] = parseColor(color)
266         
267         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
268                 style = eWindowStyleSkinned()
269                 
270                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
271                         bsName = str(borderset.getAttribute("name"))
272                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
273                                 bpName = str(pixmap.getAttribute("pos"))
274                                 filename = str(pixmap.getAttribute("filename"))
275                                 
276                                 png = getPNG(filename)
277                                 
278                                 # adapt palette
279                                 desktop.makeCompatiblePixmap(png)
280                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png)
281
282                 for color in elementsWithTag(windowstyle.childNodes, "color"):
283                         type = str(color.getAttribute("name"))
284                         color = parseColor(color.getAttribute("color"))
285                         
286                         try:
287                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
288                         except:
289                                 raise ("Unknown color %s" % (type))
290                         
291                 x = eWindowStyleManagerPtr()
292                 eWindowStyleManager.getInstance(x)
293                 x.setStyle(style)
294
295 def readSkin(screen, skin, name, desktop):
296         myscreen = None
297         
298         # first, find the corresponding screen element
299         skin = dom.childNodes[0]
300         
301         for x in elementsWithTag(skin.childNodes, "screen"):
302                 if x.getAttribute('name') == name:
303                         myscreen = x
304         del skin
305         
306         assert myscreen != None, "no skin for screen '" + name + "' found!"
307
308         screen.skinAttributes = [ ]
309         collectAttributes(screen.skinAttributes, myscreen)
310         
311         screen.additionalWidgets = [ ]
312         
313         # now walk all widgets
314         for widget in elementsWithTag(myscreen.childNodes, "widget"):
315                 wname = widget.getAttribute('name')
316                 if wname == None:
317                         print "widget has no name!"
318                         continue
319                 
320                 # get corresponding gui object
321                 try:
322                         attributes = screen[wname].skinAttributes = [ ]
323                 except:
324                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
325                 
326                 collectAttributes(attributes, widget)
327
328         # now walk additional objects
329         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
330                 if widget.tagName == "applet":
331                         codeText = mergeText(widget.childNodes).strip()
332                         type = widget.getAttribute('type')
333
334                         code = compile(codeText, "skin applet", "exec")
335                         
336                         if type == "onLayoutFinish":
337                                 screen.onLayoutFinish.append(code)
338                         else:
339                                 raise str("applet type '%s' unknown!" % type)
340                         
341                         continue
342                 
343                 class additionalWidget:
344                         pass
345                 
346                 w = additionalWidget()
347                 
348                 if widget.tagName == "eLabel":
349                         w.widget = eLabel
350                 elif widget.tagName == "ePixmap":
351                         w.widget = ePixmap
352                 else:
353                         raise str("unsupported stuff : %s" % widget.tagName)
354                 
355                 w.skinAttributes = [ ]
356                 collectAttributes(w.skinAttributes, widget)
357                 
358                 # applyAttributes(guiObject, widget, desktop)
359                 # guiObject.thisown = 0
360                 print screen.additionalWidgets
361                 screen.additionalWidgets.append(w)