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