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