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