- fix some colors
[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
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 dom = xml.dom.minidom.parseString(
18         """<skin>
19         
20                 <colors>
21                         <color name="white"     value="#ffffff" />
22                         <color name="black"     value="#000000" />
23                         <color name="dark"      value="#294a6b" />
24                         
25                         <color name="red"               value="#ff0000" />
26                         <color name="green"     value="#00ff00" />
27                         <color name="blue"              value="#0000ff" />
28                         <color name="yellow"    value="#c0c000" />
29                 </colors>
30                 <windowstyle type="skinned">
31                         <color name="Background" color="#4075a7" />
32                         <color name="LabelForeground" color="#ffffff" />
33                         <color name="ListboxBackground" color="#4075a7" />
34                         <color name="ListboxForeground" color="#ffffff" />
35                         <color name="ListboxSelectedBackground" color="#404080" />
36                         <color name="ListboxSelectedForeground" color="#ffffff" />
37                         <color name="ListboxMarkedBackground" color="#ff0000" />
38                         <color name="ListboxMarkedForeground" color="#ffffff" />
39                         <borderset name="bsWindow">
40                                 <pixmap pos="bpTopLeft"     filename="data/b_w_tl.png" />
41                                 <pixmap pos="bpTop"         filename="data/b_w_t.png"  />
42                                 <pixmap pos="bpTopRight"    filename="data/b_w_tr.png" />
43                                 <pixmap pos="bpLeft"        filename="data/b_w_l.png"  />
44                                 <pixmap pos="bpRight"       filename="data/b_w_r.png"  />
45                                 <pixmap pos="bpBottomLeft"  filename="data/b_w_bl.png" />
46                                 <pixmap pos="bpBottom"      filename="data/b_w_b.png"  />
47                                 <pixmap pos="bpBottomRight" filename="data/b_w_br.png" />
48                         </borderset>
49                 </windowstyle>
50                 <screen name="Menu" position="300,100" size="300,300" title="real main menu">
51 <!--                    <widget name="okbutton" position="10,190" size="280,50" font="Arial;20" valign="center" halign="center" />-->
52                         <widget name="title" position="10,10" size="280,20" />
53                         <widget name="menu" position="10,30" size="280,200" />
54                 </screen>
55                 <screen name="ScartLoopThrough" position="0,0" size="720,576">
56                 </screen>
57                 <screen name="ConfigMenu" position="300,100" size="300,300" title="real main menu">
58                         <widget name="txt_var_1" position="20,20" size="100,20" />
59                         <widget name="btn_var_1" position="110,20" size="200,20" />
60                         <widget name="txt_var_2" position="20,60" size="100,20" />
61                         <widget name="btn_var_2" position="110,60" size="200,20" />
62                 </screen>
63                 <screen name="configOSD" position="140,125" size="460,350" title="OSD Settings">
64                         <widget name="okbutton" position="20,245" size="205,40" />
65                         <widget name="txt_alpha" position="20,20" size="110,20" />
66                         <widget name="sld_alpha" position="150,20" size="290,20" />
67                         <widget name="txt_brightness" position="20,60" size="120,20" />
68                         <widget name="sld_brightness" position="150,20" size="290,20" />
69                         <widget name="txt_gamma" position="20,100" size="120,20" />
70                         <widget name="sld_gamma" position="150,100" size="290,20" />
71                 </screen>
72                 <screen name="configTest" position="300,100" size="300,300" title="config menu">
73                         <widget name="config" position="10,30" size="280,140" />
74                 </screen>
75                 <screen name="TimerEditList" position="160,100" size="420,430" title="Timer Editor">
76                         <widget name="timerlist" position="10,30" size="400,300" />
77                 </screen>
78                 <screen name="clockDisplay" position="300,100" size="300,300">
79                         <widget name="okbutton" position="10,10" size="280,40" />
80                         <widget name="title" position="10,120" size="280,50" />
81                         <widget name="theClock" position="10,60" size="280,50" />
82                 </screen>
83                 <screen name="InfoBar" position="0,380" size="720,151" title="InfoBar" flags="wfNoBorder">
84                         <ePixmap position="0,0" size="720,151" pixmap="data/info-bg.png" />
85                         
86                         <widget name="ServiceName" position="69,30" size="427,26" valign="center" font="Arial;32" backgroundColor="#101258" />
87                         <widget name="CurrentTime" position="575,10" size="66,30" backgroundColor="dark" font="Arial;16" />
88                         <widget name="Event_Now" position="273,68" size="282,30" font="Arial;29" backgroundColor="dark" />
89                         <widget name="Event_Next" position="273,98" size="282,30" font="Arial;29" backgroundColor="dark" />
90                         <widget name="Event_Now_Duration" position="555,68" size="70,26" font="Arial;26" backgroundColor="dark" />
91                         <widget name="Event_Next_Duration" position="555,98" size="70,26" font="Arial;26" backgroundColor="dark" />
92 <!--                    <eLabel position="70,0" size="300,30" text=".oO skin Oo." font="Arial;20" /> -->
93                 </screen>
94                 <screen name="ChannelSelection" position="90,100" size="560,420" title="Channel Selection">
95                         <widget name="list" position="0,50" size="560,340" />
96 <!--                    <widget name="okbutton" position="340,50" size="140,30" />-->
97                         <widget name="key_red" position="0,0" size="140,40" backgroundColor="red" />
98                         <widget name="key_green" position="140,0" size="140,40" backgroundColor="green" />
99                         <widget name="key_yellow" position="280,0" size="140,40" backgroundColor="yellow" />
100                         <widget name="key_blue" position="420,0" size="140,40" backgroundColor="blue" />
101                 </screen>
102                 <screen name="ServiceScan" position="150,100" size="300,200" title="Service Scan">
103                         <widget name="scan_progress" position="10,10" size="280,50" />
104                         <widget name="scan_state" position="10,60" size="280,30" />
105                 </screen>
106                 <screen name="TimerEdit" position="70,100" size="590,335" title="Timer Edit">
107                         <widget name="description" position="10,10" size="580,40" font="Arial;25"/>
108                         <widget name="lbegin" position="405,102" size="103,30" font="Arial;25" />
109                         <widget name="lend" position="405,158" size="103,30" font="Arial;25" />
110                         <widget name="begin" position="508,105" size="72,35" font="Arial;25" />
111                         <widget name="end" position="508,150" size="72,35" font="Arial;25" />
112                         <widget name="apply" position="10,240" size="250,35" />
113                 </screen>
114         </skin>""")
115
116 def parsePosition(str):
117         x, y = str.split(',')
118         return ePoint(int(x), int(y))
119
120 def parseSize(str):
121         x, y = str.split(',')
122         return eSize(int(x), int(y))
123
124 def parseFont(str):
125         name, size = str.split(';')
126         return gFont(name, int(size))
127
128 def parseColor(str):
129         if str[0] != '#':
130                 try:
131                         return colorNames[str]
132                 except:
133                         raise ("color '%s' must be #aarrggbb or valid named color" % (str))
134         return gRGB(int(str[1:], 0x10))
135
136 def applyAttributes(guiObject, node, desktop):
137         # walk all attributes
138         for p in range(node.attributes.length):
139                 a = node.attributes.item(p)
140                 
141                 # convert to string (was: unicode)
142                 attrib = str(a.name)
143                 # TODO: proper UTF8 translation?! (for value)
144                 # TODO: localization? as in e1?
145                 value = str(a.value)
146                 
147                 # and set attributes
148                 try:
149                         if attrib == 'position':
150                                 guiObject.move(parsePosition(value))
151                         elif attrib == 'size':
152                                 guiObject.resize(parseSize(value))
153                         elif attrib == 'title':
154                                 guiObject.setTitle(value)
155                         elif attrib == 'text':
156                                 guiObject.setText(value)
157                         elif attrib == 'font':
158                                 guiObject.setFont(parseFont(value))
159                         elif attrib == "pixmap":
160                                 ptr = gPixmapPtr()
161                                 if loadPNG(ptr, value):
162                                         raise "loading PNG failed!"
163                                 x = ptr
164                                 ptr = ptr.__deref__()
165                                 print desktop
166                                 desktop.makeCompatiblePixmap(ptr)
167                                 guiObject.setPixmap(ptr)
168 #                               guiObject.setPixmapFromFile(value)
169                         elif attrib == "valign":
170                                 try:
171                                         guiObject.setVAlign(
172                                                 { "top": guiObject.alignTop,
173                                                         "center": guiObject.alignCenter,
174                                                         "bottom": guiObject.alignBottom
175                                                 }[value])
176                                 except KeyError:
177                                         print "valign must be either top, center or bottom!"
178                         elif attrib == "halign":
179                                 try:
180                                         guiObject.setHAlign(
181                                                 { "left": guiObject.alignLeft,
182                                                         "center": guiObject.alignCenter,
183                                                         "right": guiObject.alignRight,
184                                                         "block": guiObject.alignBlock
185                                                 }[value])
186                                 except KeyError:
187                                         print "halign must be either left, center, right or block!"
188                         elif attrib == "flags":
189                                 flags = value.split(',')
190                                 for f in flags:
191                                         try:
192                                                 fv = eWindow.__dict__[f]
193                                                 guiObject.setFlag(fv)
194                                         except KeyError:
195                                                 print "illegal flag %s!" % f
196                         elif attrib == "backgroundColor":
197                                 guiObject.setBackgroundColor(parseColor(value))
198                         elif attrib != 'name':
199                                 print "unsupported attribute " + attrib + "=" + value
200                 except AttributeError:
201                         print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
202
203 def loadSkin():
204         print "loading skin..."
205         
206         def getPNG(x):
207                 g = gPixmapPtr()
208                 loadPNG(g, x)
209                 g = g.grabRef()
210                 return g
211         
212         skin = dom.childNodes[0]
213         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
214         
215         for c in elementsWithTag(skin.childNodes, "colors"):
216                 for color in elementsWithTag(c.childNodes, "color"):
217                         name = str(color.getAttribute("name"))
218                         color = str(color.getAttribute("value"))
219                         
220                         if not len(color):
221                                 raise ("need color and name, got %s %s" % (name, color))
222                                 
223                         colorNames[name] = parseColor(color)
224         
225         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
226                 style = eWindowStyleSkinned()
227                 
228                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
229                         bsName = str(borderset.getAttribute("name"))
230                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
231                                 bpName = str(pixmap.getAttribute("pos"))
232                                 filename = str(pixmap.getAttribute("filename"))
233                                 
234                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], getPNG(filename))
235
236                 for color in elementsWithTag(windowstyle.childNodes, "color"):
237                         type = str(color.getAttribute("name"))
238                         color = parseColor(color.getAttribute("color"))
239                         
240                         try:
241                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
242                         except:
243                                 raise ("Unknown color %s" % (type))
244                         
245                 x = eWindowStyleManagerPtr()
246                 eWindowStyleManager.getInstance(x)
247                 x.setStyle(style)
248
249 def applyGUIskin(screen, skin, name, desktop):
250         myscreen = None
251         
252         # first, find the corresponding screen element
253         skin = dom.childNodes[0]
254         
255         for x in elementsWithTag(skin.childNodes, "screen"):
256                 if x.getAttribute('name') == name:
257                         myscreen = x
258         del skin
259         
260         assert myscreen != None, "no skin for screen '" + name + "' found!"
261         
262         applyAttributes(screen.instance, myscreen, desktop)
263         
264         # now walk all widgets
265         for widget in elementsWithTag(myscreen.childNodes, "widget"):
266                 wname = widget.getAttribute('name')
267                 if wname == None:
268                         print "widget has no name!"
269                         continue
270                 
271                 # get corresponding gui object
272                 try:
273                         guiObject = screen[wname].instance
274                 except:
275                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
276                 
277                 applyAttributes(guiObject, widget, desktop)
278
279         # now walk additional objects
280         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
281                 if widget.tagName == "eLabel":
282                         guiObject = eLabel(screen.instance)
283                 elif widget.tagName == "ePixmap":
284                         guiObject = ePixmap(screen.instance)
285                 else:
286                         raise str("unsupported stuff : %s" % widget.tagName)
287                 
288                 applyAttributes(guiObject, widget, desktop      )
289                 guiObject.thisown = 0