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