fix ugly bug in satellite / lnb / tuner config
[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 from Tools.Directories import resolveFilename, SCOPE_SKIN, SCOPE_SKIN_IMAGE
18
19 # read the skin
20 dom_skin = xml.dom.minidom.parse(resolveFilename(SCOPE_SKIN, 'skin.xml'))
21 dom_skin_default = xml.dom.minidom.parse(resolveFilename(SCOPE_SKIN, 'skin_default.xml'))
22
23 def parsePosition(str):
24         x, y = str.split(',')
25         return ePoint(int(x), int(y))
26
27 def parseSize(str):
28         x, y = str.split(',')
29         return eSize(int(x), int(y))
30
31 def parseFont(str):
32         name, size = str.split(';')
33         return gFont(name, int(size))
34
35 def parseColor(str):
36         if str[0] != '#':
37                 try:
38                         return colorNames[str]
39                 except:
40                         raise ("color '%s' must be #aarrggbb or valid named color" % (str))
41         return gRGB(int(str[1:], 0x10))
42
43 def collectAttributes(skinAttributes, node):
44         # walk all attributes
45         for p in range(node.attributes.length):
46                 a = node.attributes.item(p)
47                 
48                 # convert to string (was: unicode)
49                 attrib = str(a.name)
50                 # TODO: proper UTF8 translation?! (for value)
51                 # TODO: localization? as in e1?
52                 value = str(a.value)
53                 
54                 skinAttributes.append((attrib, value))
55
56 def applySingleAttribute(guiObject, desktop, attrib, value):            
57         # and set attributes
58         try:
59                 if attrib == 'position':
60                         guiObject.move(parsePosition(value))
61                 elif attrib == 'size':
62                         guiObject.resize(parseSize(value))
63                 elif attrib == 'title':
64                         guiObject.setTitle(_(value))
65                 elif attrib == 'text':
66                         guiObject.setText(value)
67                 elif attrib == 'font':
68                         guiObject.setFont(parseFont(value))
69                 elif attrib == 'zPosition':
70                         guiObject.setZPosition(int(value))
71                 elif attrib == "pixmap":
72                         ptr = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, value))
73                         # that __deref__ still scares me!
74                         desktop.makeCompatiblePixmap(ptr.__deref__())
75                         guiObject.setPixmap(ptr.__deref__())
76                         # guiObject.setPixmapFromFile(value)
77                 elif attrib == "alphatest": # used by ePixmap
78                         guiObject.setAlphatest(
79                                 { "on": True,
80                                   "off": False
81                                 }[value])
82                 elif attrib == "orientation": # used by eSlider
83                         try:
84                                 guiObject.setOrientation(
85                                         { "orVertical": guiObject.orVertical,
86                                                 "orHorizontal": guiObject.orHorizontal
87                                         }[value])
88                         except KeyError:
89                                 print "oprientation must be either orVertical or orHorizontal!"
90                 elif attrib == "valign":
91                         try:
92                                 guiObject.setVAlign(
93                                         { "top": guiObject.alignTop,
94                                                 "center": guiObject.alignCenter,
95                                                 "bottom": guiObject.alignBottom
96                                         }[value])
97                         except KeyError:
98                                 print "valign must be either top, center or bottom!"
99                 elif attrib == "halign":
100                         try:
101                                 guiObject.setHAlign(
102                                         { "left": guiObject.alignLeft,
103                                                 "center": guiObject.alignCenter,
104                                                 "right": guiObject.alignRight,
105                                                 "block": guiObject.alignBlock
106                                         }[value])
107                         except KeyError:
108                                 print "halign must be either left, center, right or block!"
109                 elif attrib == "flags":
110                         flags = value.split(',')
111                         for f in flags:
112                                 try:
113                                         fv = eWindow.__dict__[f]
114                                         guiObject.setFlag(fv)
115                                 except KeyError:
116                                         print "illegal flag %s!" % f
117                 elif attrib == "backgroundColor":
118                         guiObject.setBackgroundColor(parseColor(value))
119                 elif attrib == "foregroundColor":
120                         guiObject.setForegroundColor(parseColor(value))
121                 elif attrib == "selectionDisabled":
122                         guiObject.setSelectionEnable(0)
123                 elif attrib == "transparent":
124                         guiObject.setTransparent(int(value))
125                 elif attrib == "borderColor":
126                         guiObject.setBorderColor(parseColor(value))
127                 elif attrib == "borderWidth":
128                         guiObject.setBorderWidth(int(value))
129                 elif attrib == "scrollbarMode":
130                         guiObject.setScrollbarMode(
131                                 { "showOnDemand": guiObject.showOnDemand,
132                                         "showAlways": guiObject.showAlways,
133                                         "showNever": guiObject.showNever
134                                 }[value])
135                 elif attrib == "enableWrapAround":
136                         guiObject.setWrapAround(True)
137                 elif attrib == "pointer":
138                         (name, pos) = value.split(':')
139                         pos = parsePosition(pos)
140                         ptr = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, name))
141                         desktop.makeCompatiblePixmap(ptr.__deref__())
142                         guiObject.setPointer(ptr.__deref__(), pos)
143                 elif attrib != 'name':
144                         print "unsupported attribute " + attrib + "=" + value
145         except int:
146 # AttributeError:
147                 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
148
149 def applyAllAttributes(guiObject, desktop, attributes):
150         for (attrib, value) in attributes:
151                 applySingleAttribute(guiObject, desktop, attrib, value)
152
153 def loadSkin(desktop):
154         print "loading skin..."
155         
156         skin = dom_skin.childNodes[0]
157         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
158         
159         for c in elementsWithTag(skin.childNodes, "colors"):
160                 for color in elementsWithTag(c.childNodes, "color"):
161                         name = str(color.getAttribute("name"))
162                         color = str(color.getAttribute("value"))
163                         
164                         if not len(color):
165                                 raise ("need color and name, got %s %s" % (name, color))
166                                 
167                         colorNames[name] = parseColor(color)
168         
169         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
170                 style = eWindowStyleSkinned()
171                 
172                 style.setTitleFont(gFont("Regular", 20));
173                 style.setTitleOffset(eSize(20, 5));
174                 
175                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
176                         bsName = str(borderset.getAttribute("name"))
177                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
178                                 bpName = str(pixmap.getAttribute("pos"))
179                                 filename = str(pixmap.getAttribute("filename"))
180                                 
181                                 png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, filename))
182                                 
183                                 # adapt palette
184                                 desktop.makeCompatiblePixmap(png.__deref__())
185                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png.__deref__())
186
187                 for color in elementsWithTag(windowstyle.childNodes, "color"):
188                         type = str(color.getAttribute("name"))
189                         color = parseColor(color.getAttribute("color"))
190                         
191                         try:
192                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
193                         except:
194                                 raise ("Unknown color %s" % (type))
195                         
196                 x = eWindowStyleManagerPtr()
197                 eWindowStyleManager.getInstance(x)
198                 x.setStyle(style)
199
200 def readSkin(screen, skin, name, desktop):
201         myscreen = None
202         
203         # first, find the corresponding screen element
204         skin = dom_skin.childNodes[0] 
205         skin_default = dom_skin_default.childNodes[0]
206         
207         for x in elementsWithTag(skin.childNodes, "screen"):
208                 if x.getAttribute('name') == name:
209                         myscreen = x
210                         break
211         
212         # if not found, check default skin      
213         if myscreen is None:
214                 for x in elementsWithTag(skin_default.childNodes, "screen"):
215                         if x.getAttribute('name') == name:
216                                 myscreen = x
217                                 break
218
219         del skin, skin_default
220         
221         # otherwise try embedded skin
222         myscreen = myscreen or getattr(screen, "parsedSkin", None)
223         
224         # try uncompiled embedded skin
225         if myscreen is None and getattr(screen, "skin", None):
226                 myscreen = screen.parsedSkin = xml.dom.minidom.parseString(screen.skin).childNodes[0]
227         
228         assert myscreen is not None, "no skin for screen '" + name + "' found!"
229
230         screen.skinAttributes = [ ]
231         collectAttributes(screen.skinAttributes, myscreen)
232         
233         screen.additionalWidgets = [ ]
234         
235         # now walk all widgets
236         for widget in elementsWithTag(myscreen.childNodes, "widget"):
237                 wname = widget.getAttribute('name')
238                 if wname == None:
239                         print "widget has no name!"
240                         continue
241                 
242                 # get corresponding gui object
243                 try:
244                         attributes = screen[wname].skinAttributes = [ ]
245                 except:
246                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
247                 
248                 collectAttributes(attributes, widget)
249
250         # now walk additional objects
251         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
252                 if widget.tagName == "applet":
253                         codeText = mergeText(widget.childNodes).strip()
254                         type = widget.getAttribute('type')
255
256                         code = compile(codeText, "skin applet", "exec")
257                         
258                         if type == "onLayoutFinish":
259                                 screen.onLayoutFinish.append(code)
260                         else:
261                                 raise str("applet type '%s' unknown!" % type)
262                         
263                         continue
264                 
265                 class additionalWidget:
266                         pass
267                 
268                 w = additionalWidget()
269                 
270                 if widget.tagName == "eLabel":
271                         w.widget = eLabel
272                 elif widget.tagName == "ePixmap":
273                         w.widget = ePixmap
274                 else:
275                         raise str("unsupported stuff : %s" % widget.tagName)
276                 
277                 w.skinAttributes = [ ]
278                 collectAttributes(w.skinAttributes, widget)
279                 
280                 # applyAttributes(guiObject, widget, desktop)
281                 # guiObject.thisown = 0
282                 screen.additionalWidgets.append(w)