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