remove VolumeControl from infobar
[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 != 'name':
137                         print "unsupported attribute " + attrib + "=" + value
138         except int:
139 # AttributeError:
140                 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
141
142 def applyAllAttributes(guiObject, desktop, attributes):
143         for (attrib, value) in attributes:
144                 applySingleAttribute(guiObject, desktop, attrib, value)
145
146 def loadSkin(desktop):
147         print "loading skin..."
148         
149         skin = dom.childNodes[0]
150         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
151         
152         for c in elementsWithTag(skin.childNodes, "colors"):
153                 for color in elementsWithTag(c.childNodes, "color"):
154                         name = str(color.getAttribute("name"))
155                         color = str(color.getAttribute("value"))
156                         
157                         if not len(color):
158                                 raise ("need color and name, got %s %s" % (name, color))
159                                 
160                         colorNames[name] = parseColor(color)
161         
162         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
163                 style = eWindowStyleSkinned()
164                 
165                 style.setTitleFont(gFont("Regular", 20));
166                 style.setTitleOffset(eSize(20, 5));
167                 
168                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
169                         bsName = str(borderset.getAttribute("name"))
170                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
171                                 bpName = str(pixmap.getAttribute("pos"))
172                                 filename = str(pixmap.getAttribute("filename"))
173                                 
174                                 png = loadPNG(filename)
175                                 
176                                 # adapt palette
177                                 desktop.makeCompatiblePixmap(png.__deref__())
178                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png.__deref__())
179
180                 for color in elementsWithTag(windowstyle.childNodes, "color"):
181                         type = str(color.getAttribute("name"))
182                         color = parseColor(color.getAttribute("color"))
183                         
184                         try:
185                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
186                         except:
187                                 raise ("Unknown color %s" % (type))
188                         
189                 x = eWindowStyleManagerPtr()
190                 eWindowStyleManager.getInstance(x)
191                 x.setStyle(style)
192
193 def readSkin(screen, skin, name, desktop):
194         myscreen = None
195         
196         # first, find the corresponding screen element
197         skin = dom.childNodes[0]
198         
199         for x in elementsWithTag(skin.childNodes, "screen"):
200                 if x.getAttribute('name') == name:
201                         myscreen = x
202         del skin
203         
204         if myscreen is None:
205                 # try embedded skin
206                 print screen.__dict__
207                 if "parsedSkin" in screen.__dict__:
208                         myscreen = screen.parsedSkin
209                 elif "skin" in screen.__dict__:
210                         myscreen = screen.parsedSkin = xml.dom.minidom.parseString(screen.skin).childNodes[0]
211         
212         assert myscreen is not None, "no skin for screen '" + name + "' found!"
213
214         screen.skinAttributes = [ ]
215         collectAttributes(screen.skinAttributes, myscreen)
216         
217         screen.additionalWidgets = [ ]
218         
219         # now walk all widgets
220         for widget in elementsWithTag(myscreen.childNodes, "widget"):
221                 wname = widget.getAttribute('name')
222                 if wname == None:
223                         print "widget has no name!"
224                         continue
225                 
226                 # get corresponding gui object
227                 try:
228                         attributes = screen[wname].skinAttributes = [ ]
229                 except:
230                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
231                 
232                 collectAttributes(attributes, widget)
233
234         # now walk additional objects
235         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
236                 if widget.tagName == "applet":
237                         codeText = mergeText(widget.childNodes).strip()
238                         type = widget.getAttribute('type')
239
240                         code = compile(codeText, "skin applet", "exec")
241                         
242                         if type == "onLayoutFinish":
243                                 screen.onLayoutFinish.append(code)
244                         else:
245                                 raise str("applet type '%s' unknown!" % type)
246                         
247                         continue
248                 
249                 class additionalWidget:
250                         pass
251                 
252                 w = additionalWidget()
253                 
254                 if widget.tagName == "eLabel":
255                         w.widget = eLabel
256                 elif widget.tagName == "ePixmap":
257                         w.widget = ePixmap
258                 else:
259                         raise str("unsupported stuff : %s" % widget.tagName)
260                 
261                 w.skinAttributes = [ ]
262                 collectAttributes(w.skinAttributes, widget)
263                 
264                 # applyAttributes(guiObject, widget, desktop)
265                 # guiObject.thisown = 0
266                 screen.additionalWidgets.append(w)