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