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