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