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