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