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