do voltage and tone control in SEC_SEND_SEQUENCE on old api..sorry for this hack
[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                 </screen>
73         </skin>""")
74
75 # filters all elements of childNode with the specified function
76 # example: nodes = elementsWithTag(childNodes, lambda x: x == "bla")
77 def elementsWithTag(el, tag):
78
79         # fiiixme! (works but isn't nice)
80         if tag.__class__ == "".__class__:
81                 str = tag
82                 tag = lambda x: x == str
83                 
84         for x in el:
85                 if x.nodeType != xml.dom.minidom.Element.nodeType:
86                         continue
87                 if tag(x.tagName):
88                         yield x
89
90 def parsePosition(str):
91         x, y = str.split(',')
92         return ePoint(int(x), int(y))
93
94 def parseSize(str):
95         x, y = str.split(',')
96         return eSize(int(x), int(y))
97
98 def parseFont(str):
99         name, size = str.split(';')
100         return gFont(name, int(size))
101
102 def parseColor(str):
103         if str[0] != '#':
104                 try:
105                         return colorNames[str]
106                 except:
107                         raise ("color '%s' must be #aarrggbb or valid named color" % (str))
108         return gRGB(int(str[1:], 0x10))
109
110 def applyAttributes(guiObject, node, desktop):
111         # walk all attributes
112         for p in range(node.attributes.length):
113                 a = node.attributes.item(p)
114                 
115                 # convert to string (was: unicode)
116                 attrib = str(a.name)
117                 # TODO: proper UTF8 translation?! (for value)
118                 # TODO: localization? as in e1?
119                 value = str(a.value)
120                 
121                 # and set attributes
122                 try:
123                         if attrib == 'position':
124                                 guiObject.move(parsePosition(value))
125                         elif attrib == 'size':
126                                 guiObject.resize(parseSize(value))
127                         elif attrib == 'title':
128                                 guiObject.setTitle(value)
129                         elif attrib == 'text':
130                                 guiObject.setText(value)
131                         elif attrib == 'font':
132                                 guiObject.setFont(parseFont(value))
133                         elif attrib == "pixmap":
134                                 ptr = gPixmapPtr()
135                                 if loadPNG(ptr, value):
136                                         raise "loading PNG failed!"
137                                 x = ptr
138                                 ptr = ptr.__deref__()
139                                 print desktop
140                                 desktop.makeCompatiblePixmap(ptr)
141                                 guiObject.setPixmap(ptr)
142 #                               guiObject.setPixmapFromFile(value)
143                         elif attrib == "valign":
144                                 try:
145                                         guiObject.setVAlign(
146                                                 { "top": guiObject.alignTop,
147                                                         "center": guiObject.alignCenter,
148                                                         "bottom": guiObject.alignBottom
149                                                 }[value])
150                                 except KeyError:
151                                         print "valign must be either top, center or bottom!"
152                         elif attrib == "halign":
153                                 try:
154                                         guiObject.setHAlign(
155                                                 { "left": guiObject.alignLeft,
156                                                         "center": guiObject.alignCenter,
157                                                         "right": guiObject.alignRight,
158                                                         "block": guiObject.alignBlock
159                                                 }[value])
160                                 except KeyError:
161                                         print "halign must be either left, center, right or block!"
162                         elif attrib == "flags":
163                                 flags = value.split(',')
164                                 for f in flags:
165                                         try:
166                                                 fv = eWindow.__dict__[f]
167                                                 guiObject.setFlag(fv)
168                                         except KeyError:
169                                                 print "illegal flag %s!" % f
170                         elif attrib == "backgroundColor":
171                                 guiObject.setBackgroundColor(parseColor(value))
172                         elif attrib != 'name':
173                                 print "unsupported attribute " + attrib + "=" + value
174                 except AttributeError:
175                         print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
176
177 def loadSkin():
178         print "loading skin..."
179         
180         def getPNG(x):
181                 g = gPixmapPtr()
182                 loadPNG(g, x)
183                 g = g.grabRef()
184                 return g
185         
186         skin = dom.childNodes[0]
187         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
188         
189         for c in elementsWithTag(skin.childNodes, "colors"):
190                 for color in elementsWithTag(c.childNodes, "color"):
191                         name = str(color.getAttribute("name"))
192                         color = str(color.getAttribute("value"))
193                         
194                         if not len(color):
195                                 raise ("need color and name, got %s %s" % (name, color))
196                                 
197                         colorNames[name] = parseColor(color)
198         
199         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
200                 style = eWindowStyleSkinned()
201                 
202                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
203                         bsName = str(borderset.getAttribute("name"))
204                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
205                                 bpName = str(pixmap.getAttribute("pos"))
206                                 filename = str(pixmap.getAttribute("filename"))
207                                 
208                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], getPNG(filename))
209
210                 for color in elementsWithTag(windowstyle.childNodes, "color"):
211                         type = str(color.getAttribute("name"))
212                         color = parseColor(color.getAttribute("color"))
213                         
214                         try:
215                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
216                         except:
217                                 raise ("Unknown color %s" % (type))
218                         
219                 x = eWindowStyleManagerPtr()
220                 eWindowStyleManager.getInstance(x)
221                 x.setStyle(style)
222
223 def applyGUIskin(screen, skin, name, desktop):
224         myscreen = None
225         
226         # first, find the corresponding screen element
227         skin = dom.childNodes[0]
228         
229         for x in elementsWithTag(skin.childNodes, "screen"):
230                 if x.getAttribute('name') == name:
231                         myscreen = x
232         del skin
233         
234         assert myscreen != None, "no skin for screen '" + name + "' found!"
235         
236         applyAttributes(screen.instance, myscreen, desktop)
237         
238         # now walk all widgets
239         for widget in elementsWithTag(myscreen.childNodes, "widget"):
240                 wname = widget.getAttribute('name')
241                 if wname == None:
242                         print "widget has no name!"
243                         continue
244                 
245                 # get corresponding gui object
246                 try:
247                         guiObject = screen[wname].instance
248                 except:
249                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
250                 
251                 applyAttributes(guiObject, widget, desktop)
252
253         # now walk additional objects
254         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
255                 if widget.tagName == "eLabel":
256                         guiObject = eLabel(screen.instance)
257                 elif widget.tagName == "ePixmap":
258                         guiObject = ePixmap(screen.instance)
259                 else:
260                         raise str("unsupported stuff : %s" % widget.tagName)
261                 
262                 applyAttributes(guiObject, widget, desktop      )
263                 guiObject.thisown = 0