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