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