new service info screen (prepared to show transponder info... not functional yet)
[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                         # that __deref__ still scares me!
76                         desktop.makeCompatiblePixmap(ptr.__deref__())
77                         guiObject.setPixmap(ptr.__deref__())
78                         # guiObject.setPixmapFromFile(value)
79                 elif attrib == "alphatest": # used by ePixmap
80                         guiObject.setAlphatest(
81                                 { "on": True,
82                                   "off": False
83                                 }[value])
84                 elif attrib == "orientation": # used by eSlider
85                         try:
86                                 guiObject.setOrientation(
87                                         { "orVertical": guiObject.orVertical,
88                                                 "orHorizontal": guiObject.orHorizontal
89                                         }[value])
90                         except KeyError:
91                                 print "oprientation must be either orVertical or orHorizontal!"
92                 elif attrib == "valign":
93                         try:
94                                 guiObject.setVAlign(
95                                         { "top": guiObject.alignTop,
96                                                 "center": guiObject.alignCenter,
97                                                 "bottom": guiObject.alignBottom
98                                         }[value])
99                         except KeyError:
100                                 print "valign must be either top, center or bottom!"
101                 elif attrib == "halign":
102                         try:
103                                 guiObject.setHAlign(
104                                         { "left": guiObject.alignLeft,
105                                                 "center": guiObject.alignCenter,
106                                                 "right": guiObject.alignRight,
107                                                 "block": guiObject.alignBlock
108                                         }[value])
109                         except KeyError:
110                                 print "halign must be either left, center, right or block!"
111                 elif attrib == "flags":
112                         flags = value.split(',')
113                         for f in flags:
114                                 try:
115                                         fv = eWindow.__dict__[f]
116                                         guiObject.setFlag(fv)
117                                 except KeyError:
118                                         print "illegal flag %s!" % f
119                 elif attrib == "backgroundColor":
120                         guiObject.setBackgroundColor(parseColor(value))
121                 elif attrib == "foregroundColor":
122                         guiObject.setForegroundColor(parseColor(value))
123                 elif attrib == "selectionDisabled":
124                         guiObject.setSelectionEnable(0)
125                 elif attrib == "transparent":
126                         guiObject.setTransparent(int(value))
127                 elif attrib == "borderColor":
128                         guiObject.setBorderColor(parseColor(value))
129                 elif attrib == "borderWidth":
130                         guiObject.setBorderWidth(int(value))
131                 elif attrib == "scrollbarMode":
132                         guiObject.setScrollbarMode(
133                                 { "showOnDemand": guiObject.showOnDemand,
134                                         "showAlways": guiObject.showAlways,
135                                         "showNever": guiObject.showNever
136                                 }[value])
137                 elif attrib == "enableWrapAround":
138                         guiObject.setWrapAround(True)
139                 elif attrib == "pointer":
140                         (name, pos) = value.split(':')
141                         pos = parsePosition(pos)
142                         ptr = loadPNG(name)
143                         desktop.makeCompatiblePixmap(ptr.__deref__())
144                         guiObject.setPointer(ptr.__deref__(), pos)
145                 elif attrib != 'name':
146                         print "unsupported attribute " + attrib + "=" + value
147         except int:
148 # AttributeError:
149                 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
150
151 def applyAllAttributes(guiObject, desktop, attributes):
152         for (attrib, value) in attributes:
153                 applySingleAttribute(guiObject, desktop, attrib, value)
154
155 def loadSkin(desktop):
156         print "loading skin..."
157         
158         skin = dom.childNodes[0]
159         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
160         
161         for c in elementsWithTag(skin.childNodes, "colors"):
162                 for color in elementsWithTag(c.childNodes, "color"):
163                         name = str(color.getAttribute("name"))
164                         color = str(color.getAttribute("value"))
165                         
166                         if not len(color):
167                                 raise ("need color and name, got %s %s" % (name, color))
168                                 
169                         colorNames[name] = parseColor(color)
170         
171         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
172                 style = eWindowStyleSkinned()
173                 
174                 style.setTitleFont(gFont("Regular", 20));
175                 style.setTitleOffset(eSize(20, 5));
176                 
177                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
178                         bsName = str(borderset.getAttribute("name"))
179                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
180                                 bpName = str(pixmap.getAttribute("pos"))
181                                 filename = str(pixmap.getAttribute("filename"))
182                                 
183                                 png = loadPNG(filename)
184                                 
185                                 # adapt palette
186                                 desktop.makeCompatiblePixmap(png.__deref__())
187                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png.__deref__())
188
189                 for color in elementsWithTag(windowstyle.childNodes, "color"):
190                         type = str(color.getAttribute("name"))
191                         color = parseColor(color.getAttribute("color"))
192                         
193                         try:
194                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
195                         except:
196                                 raise ("Unknown color %s" % (type))
197                         
198                 x = eWindowStyleManagerPtr()
199                 eWindowStyleManager.getInstance(x)
200                 x.setStyle(style)
201
202 def readSkin(screen, skin, name, desktop):
203         myscreen = None
204         
205         # first, find the corresponding screen element
206         skin = dom.childNodes[0]
207         
208         for x in elementsWithTag(skin.childNodes, "screen"):
209                 if x.getAttribute('name') == name:
210                         myscreen = x
211         del skin
212         
213         # try embedded skin
214         myscreen = myscreen or getattr(screen, "parsedSkin", None)
215         
216         # try uncompiled embedded skin
217         if myscreen is None and getattr(screen, "skin", None):
218                 myscreen = screen.parsedSkin = xml.dom.minidom.parseString(screen.skin).childNodes[0]
219         
220         assert myscreen is not None, "no skin for screen '" + name + "' found!"
221
222         screen.skinAttributes = [ ]
223         collectAttributes(screen.skinAttributes, myscreen)
224         
225         screen.additionalWidgets = [ ]
226         
227         # now walk all widgets
228         for widget in elementsWithTag(myscreen.childNodes, "widget"):
229                 wname = widget.getAttribute('name')
230                 if wname == None:
231                         print "widget has no name!"
232                         continue
233                 
234                 # get corresponding gui object
235                 try:
236                         attributes = screen[wname].skinAttributes = [ ]
237                 except:
238                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
239                 
240                 collectAttributes(attributes, widget)
241
242         # now walk additional objects
243         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
244                 if widget.tagName == "applet":
245                         codeText = mergeText(widget.childNodes).strip()
246                         type = widget.getAttribute('type')
247
248                         code = compile(codeText, "skin applet", "exec")
249                         
250                         if type == "onLayoutFinish":
251                                 screen.onLayoutFinish.append(code)
252                         else:
253                                 raise str("applet type '%s' unknown!" % type)
254                         
255                         continue
256                 
257                 class additionalWidget:
258                         pass
259                 
260                 w = additionalWidget()
261                 
262                 if widget.tagName == "eLabel":
263                         w.widget = eLabel
264                 elif widget.tagName == "ePixmap":
265                         w.widget = ePixmap
266                 else:
267                         raise str("unsupported stuff : %s" % widget.tagName)
268                 
269                 w.skinAttributes = [ ]
270                 collectAttributes(w.skinAttributes, widget)
271                 
272                 # applyAttributes(guiObject, widget, desktop)
273                 # guiObject.thisown = 0
274                 screen.additionalWidgets.append(w)