- add info-bg.png
[enigma2.git] / skin.py
1 from enigma import *
2 import xml.dom.minidom
3 from xml.dom import EMPTY_NAMESPACE
4
5
6 colorNames = dict()
7
8 def dump(x, i=0):
9         print " " * i + str(x)
10         try:
11                 for n in x.childNodes:
12                         dump(n, i + 1)
13         except:
14                 None
15
16 dom = xml.dom.minidom.parseString(
17         """<skin>
18         
19                 <colors>
20                         <color name="white" value="#ffffff" />
21                         <color name="black" value="#000000" />
22                         <color name="dark"  value="#294a6b" />
23                 </colors>
24                 <windowstyle type="skinned">
25                         <color name="Background" color="#4075a7" />
26                         <color name="LabelForeground" color="#ffffff" />
27                         <color name="ListboxBackground" color="#4075a7" />
28                         <color name="ListboxForeground" color="#ffffff" />
29                         <color name="ListboxSelectedBackground" color="#80ff80" />
30                         <color name="ListboxSelectedForeground" color="#ffffff" />
31                         <color name="ListboxMarkedBackground" color="#ff0000" />
32                         <color name="ListboxMarkedForeground" color="#ffffff" />
33                         <borderset name="bsWindow">
34                                 <pixmap pos="bpTopLeft"     filename="data/b_w_tl.png" />
35                                 <pixmap pos="bpTop"         filename="data/b_w_t.png"  />
36                                 <pixmap pos="bpTopRight"    filename="data/b_w_tr.png" />
37                                 <pixmap pos="bpLeft"        filename="data/b_w_l.png"  />
38                                 <pixmap pos="bpRight"       filename="data/b_w_r.png"  />
39                                 <pixmap pos="bpBottomLeft"  filename="data/b_w_bl.png" />
40                                 <pixmap pos="bpBottom"      filename="data/b_w_b.png"  />
41                                 <pixmap pos="bpBottomRight" filename="data/b_w_br.png" />
42                         </borderset>
43                 </windowstyle>
44                 <screen name="mainMenu" position="300,100" size="300,300" title="real main menu">
45                         <widget name="okbutton" position="10,190" size="280,50" font="Arial;20" valign="center" halign="center" />
46                         <widget name="title" position="10,10" size="280,20" />
47                         <widget name="menu" position="10,30" size="280,140" />
48                 </screen>
49                 <screen name="clockDisplay" position="300,100" size="300,300">
50                         <widget name="okbutton" position="10,10" size="280,40" />
51                         <widget name="title" position="10,120" size="280,50" />
52                         <widget name="theClock" position="10,60" size="280,50" />
53                 </screen>
54                 <screen name="infoBar" position="0,380" size="720,151" title="InfoBar" flags="wfNoBorder">
55                         <ePixmap position="0,0" size="720,151" pixmap="data/info-bg.png" />
56                         
57                         <widget name="ServiceName" position="69,30" size="427,26" valign="center" font="Arial;32" backgroundColor="#101258" />
58                         <widget name="CurrentTime" position="575,10" size="66,30" backgroundColor="dark" font="Arial;16" />
59                         <widget name="Event_Now" position="273,68" size="282,30" font="Arial;29" backgroundColor="dark" />
60                         <widget name="Event_Next" position="273,98" size="282,30" font="Arial;29" backgroundColor="dark" />
61                         <widget name="Event_Now_Duration" position="555,68" size="70,26" font="Arial;26" backgroundColor="dark" />
62                         <widget name="Event_Next_Duration" position="555,98" size="70,26" font="Arial;26" backgroundColor="dark" />
63 <!--                    <eLabel position="70,0" size="300,30" text=".oO skin Oo." font="Arial;20" /> -->
64                 </screen>
65                 <screen name="channelSelection" position="100,80" size="500,240" title="Channel Selection">
66                         <widget name="list" position="20,50" size="300,150" />
67                         <widget name="okbutton" position="340,50" size="140,30" />
68                 </screen>
69                 <screen name="serviceScan" position="150,100" size="300,200" title="Service Scan">
70                         <widget name="scan_progress" position="10,10" size="280,50" />
71                         <widget name="scan_state" position="10,60" size="280,30" />
72                         <widget name="okbutton" position="10,100" size="280,40" />
73                 </screen>
74         </skin>""")
75
76 # filters all elements of childNode with the specified function
77 # example: nodes = elementsWithTag(childNodes, lambda x: x == "bla")
78 def elementsWithTag(el, tag):
79
80         # fiiixme! (works but isn't nice)
81         if tag.__class__ == "".__class__:
82                 str = tag
83                 tag = lambda x: x == str
84                 
85         for x in el:
86                 if x.nodeType != xml.dom.minidom.Element.nodeType:
87                         continue
88                 if tag(x.tagName):
89                         yield x
90
91 def parsePosition(str):
92         x, y = str.split(',')
93         return ePoint(int(x), int(y))
94
95 def parseSize(str):
96         x, y = str.split(',')
97         return eSize(int(x), int(y))
98
99 def parseFont(str):
100         name, size = str.split(';')
101         return gFont(name, int(size))
102
103 def parseColor(str):
104         if str[0] != '#':
105                 try:
106                         return colorNames[str]
107                 except:
108                         raise ("color '%s' must be #aarrggbb or valid named color" % (str))
109         return gRGB(int(str[1:], 0x10))
110
111 def applyAttributes(guiObject, node, desktop):
112         # walk all attributes
113         for p in range(node.attributes.length):
114                 a = node.attributes.item(p)
115                 
116                 # convert to string (was: unicode)
117                 attrib = str(a.name)
118                 # TODO: proper UTF8 translation?! (for value)
119                 # TODO: localization? as in e1?
120                 value = str(a.value)
121                 
122                 # and set attributes
123                 try:
124                         if attrib == 'position':
125                                 guiObject.move(parsePosition(value))
126                         elif attrib == 'size':
127                                 guiObject.resize(parseSize(value))
128                         elif attrib == 'title':
129                                 guiObject.setTitle(value)
130                         elif attrib == 'text':
131                                 guiObject.setText(value)
132                         elif attrib == 'font':
133                                 guiObject.setFont(parseFont(value))
134                         elif attrib == "pixmap":
135                                 ptr = gPixmapPtr()
136                                 if loadPNG(ptr, value):
137                                         raise "loading PNG failed!"
138                                 x = ptr
139                                 ptr = ptr.__deref__()
140                                 print desktop
141                                 desktop.makeCompatiblePixmap(ptr)
142                                 guiObject.setPixmap(ptr)
143 #                               guiObject.setPixmapFromFile(value)
144                         elif attrib == "valign":
145                                 try:
146                                         guiObject.setVAlign(
147                                                 { "top": guiObject.alignTop,
148                                                         "center": guiObject.alignCenter,
149                                                         "bottom": guiObject.alignBottom
150                                                 }[value])
151                                 except KeyError:
152                                         print "valign must be either top, center or bottom!"
153                         elif attrib == "halign":
154                                 try:
155                                         guiObject.setHAlign(
156                                                 { "left": guiObject.alignLeft,
157                                                         "center": guiObject.alignCenter,
158                                                         "right": guiObject.alignRight,
159                                                         "block": guiObject.alignBlock
160                                                 }[value])
161                                 except KeyError:
162                                         print "halign must be either left, center, right or block!"
163                         elif attrib == "flags":
164                                 flags = value.split(',')
165                                 for f in flags:
166                                         try:
167                                                 fv = eWindow.__dict__[f]
168                                                 guiObject.setFlag(fv)
169                                         except KeyError:
170                                                 print "illegal flag %s!" % f
171                         elif attrib == "backgroundColor":
172                                 guiObject.setBackgroundColor(parseColor(value))
173                         elif attrib != 'name':
174                                 print "unsupported attribute " + attrib + "=" + value
175                 except AttributeError:
176                         print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
177
178 def loadSkin():
179         print "loading skin..."
180         
181         def getPNG(x):
182                 g = gPixmapPtr()
183                 loadPNG(g, x)
184                 g = g.grabRef()
185                 return g
186         
187         skin = dom.childNodes[0]
188         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
189         
190         for c in elementsWithTag(skin.childNodes, "colors"):
191                 for color in elementsWithTag(c.childNodes, "color"):
192                         name = str(color.getAttribute("name"))
193                         color = str(color.getAttribute("value"))
194                         
195                         if not len(color):
196                                 raise ("need color and name, got %s %s" % (name, color))
197                                 
198                         colorNames[name] = parseColor(color)
199         
200         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
201                 style = eWindowStyleSkinned()
202                 
203                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
204                         bsName = str(borderset.getAttribute("name"))
205                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
206                                 bpName = str(pixmap.getAttribute("pos"))
207                                 filename = str(pixmap.getAttribute("filename"))
208                                 
209                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], getPNG(filename))
210
211                 for color in elementsWithTag(windowstyle.childNodes, "color"):
212                         type = str(color.getAttribute("name"))
213                         color = parseColor(color.getAttribute("color"))
214                         
215                         try:
216                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
217                         except:
218                                 raise ("Unknown color %s" % (type))
219                         
220                 x = eWindowStyleManagerPtr()
221                 eWindowStyleManager.getInstance(x)
222                 x.setStyle(style)
223
224 def applyGUIskin(screen, skin, name, desktop):
225         myscreen = None
226         
227         # first, find the corresponding screen element
228         skin = dom.childNodes[0]
229         
230         for x in elementsWithTag(skin.childNodes, "screen"):
231                 if x.getAttribute('name') == name:
232                         myscreen = x
233         del skin
234         
235         assert myscreen != None, "no skin for screen '" + name + "' found!"
236         
237         applyAttributes(screen.instance, myscreen, desktop)
238         
239         # now walk all widgets
240         for widget in elementsWithTag(myscreen.childNodes, "widget"):
241                 wname = widget.getAttribute('name')
242                 if wname == None:
243                         print "widget has no name!"
244                         continue
245                 
246                 # get corresponding gui object
247                 try:
248                         guiObject = screen[wname].instance
249                 except:
250                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
251                 
252                 applyAttributes(guiObject, widget, desktop)
253
254         # now walk additional objects
255         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
256                 if widget.tagName == "eLabel":
257                         guiObject = eLabel(screen.instance)
258                 elif widget.tagName == "ePixmap":
259                         guiObject = ePixmap(screen.instance)
260                 else:
261                         raise str("unsupported stuff : %s" % widget.tagName)
262                 
263                 applyAttributes(guiObject, widget, desktop      )
264                 guiObject.thisown = 0