moving the skin out of the source into data/skin.xml
[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         skinfile = file('/usr/share/enigma2/skin.xml', 'r')
20         dom = xml.dom.minidom.parseString(skinfile.read())
21         skinfile.close()
22 except:
23         print "Error opening skin.xml"
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 == "pixmap":
72                         ptr = gPixmapPtr()
73                         if loadPNG(ptr, value):
74                                 raise "loading PNG failed!"
75                         x = ptr
76                         ptr = ptr.__deref__()
77                         desktop.makeCompatiblePixmap(ptr)
78                         guiObject.setPixmap(ptr)
79                         # guiObject.setPixmapFromFile(value)
80                 elif attrib == "valign":
81                         try:
82                                 guiObject.setVAlign(
83                                         { "top": guiObject.alignTop,
84                                                 "center": guiObject.alignCenter,
85                                                 "bottom": guiObject.alignBottom
86                                         }[value])
87                         except KeyError:
88                                 print "valign must be either top, center or bottom!"
89                 elif attrib == "halign":
90                         try:
91                                 guiObject.setHAlign(
92                                         { "left": guiObject.alignLeft,
93                                                 "center": guiObject.alignCenter,
94                                                 "right": guiObject.alignRight,
95                                                 "block": guiObject.alignBlock
96                                         }[value])
97                         except KeyError:
98                                 print "halign must be either left, center, right or block!"
99                 elif attrib == "flags":
100                         flags = value.split(',')
101                         for f in flags:
102                                 try:
103                                         fv = eWindow.__dict__[f]
104                                         guiObject.setFlag(fv)
105                                 except KeyError:
106                                         print "illegal flag %s!" % f
107                 elif attrib == "backgroundColor":
108                         guiObject.setBackgroundColor(parseColor(value))
109                 elif attrib == "foregroundColor":
110                         guiObject.setForegroundColor(parseColor(value))
111                 elif attrib != 'name':
112                         print "unsupported attribute " + attrib + "=" + value
113         except int:
114 # AttributeError:
115                 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
116
117 def applyAllAttributes(guiObject, desktop, attributes):
118         for (attrib, value) in attributes:
119                 applySingleAttribute(guiObject, desktop, attrib, value)
120
121 def loadSkin(desktop):
122         print "loading skin..."
123         
124         def getPNG(x):
125                 g = gPixmapPtr()
126                 loadPNG(g, x)
127                 g = g.grabRef()
128                 return g
129         
130         skin = dom.childNodes[0]
131         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
132         
133         for c in elementsWithTag(skin.childNodes, "colors"):
134                 for color in elementsWithTag(c.childNodes, "color"):
135                         name = str(color.getAttribute("name"))
136                         color = str(color.getAttribute("value"))
137                         
138                         if not len(color):
139                                 raise ("need color and name, got %s %s" % (name, color))
140                                 
141                         colorNames[name] = parseColor(color)
142         
143         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
144                 style = eWindowStyleSkinned()
145                 
146                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
147                         bsName = str(borderset.getAttribute("name"))
148                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
149                                 bpName = str(pixmap.getAttribute("pos"))
150                                 filename = str(pixmap.getAttribute("filename"))
151                                 
152                                 png = getPNG(filename)
153                                 
154                                 # adapt palette
155                                 desktop.makeCompatiblePixmap(png)
156                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png)
157
158                 for color in elementsWithTag(windowstyle.childNodes, "color"):
159                         type = str(color.getAttribute("name"))
160                         color = parseColor(color.getAttribute("color"))
161                         
162                         try:
163                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
164                         except:
165                                 raise ("Unknown color %s" % (type))
166                         
167                 x = eWindowStyleManagerPtr()
168                 eWindowStyleManager.getInstance(x)
169                 x.setStyle(style)
170
171 def readSkin(screen, skin, name, desktop):
172         myscreen = None
173         
174         # first, find the corresponding screen element
175         skin = dom.childNodes[0]
176         
177         for x in elementsWithTag(skin.childNodes, "screen"):
178                 if x.getAttribute('name') == name:
179                         myscreen = x
180         del skin
181         
182         assert myscreen != None, "no skin for screen '" + name + "' found!"
183
184         screen.skinAttributes = [ ]
185         collectAttributes(screen.skinAttributes, myscreen)
186         
187         screen.additionalWidgets = [ ]
188         
189         # now walk all widgets
190         for widget in elementsWithTag(myscreen.childNodes, "widget"):
191                 wname = widget.getAttribute('name')
192                 if wname == None:
193                         print "widget has no name!"
194                         continue
195                 
196                 # get corresponding gui object
197                 try:
198                         attributes = screen[wname].skinAttributes = [ ]
199                 except:
200                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
201                 
202                 collectAttributes(attributes, widget)
203
204         # now walk additional objects
205         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
206                 if widget.tagName == "applet":
207                         codeText = mergeText(widget.childNodes).strip()
208                         type = widget.getAttribute('type')
209
210                         code = compile(codeText, "skin applet", "exec")
211                         
212                         if type == "onLayoutFinish":
213                                 screen.onLayoutFinish.append(code)
214                         else:
215                                 raise str("applet type '%s' unknown!" % type)
216                         
217                         continue
218                 
219                 class additionalWidget:
220                         pass
221                 
222                 w = additionalWidget()
223                 
224                 if widget.tagName == "eLabel":
225                         w.widget = eLabel
226                 elif widget.tagName == "ePixmap":
227                         w.widget = ePixmap
228                 else:
229                         raise str("unsupported stuff : %s" % widget.tagName)
230                 
231                 w.skinAttributes = [ ]
232                 collectAttributes(w.skinAttributes, widget)
233                 
234                 # applyAttributes(guiObject, widget, desktop)
235                 # guiObject.thisown = 0
236                 print screen.additionalWidgets
237                 screen.additionalWidgets.append(w)