- merge cluts for borders, too
[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                                 desktop.makeCompatiblePixmap(ptr)
166                                 guiObject.setPixmap(ptr)
167 #                               guiObject.setPixmapFromFile(value)
168                         elif attrib == "valign":
169                                 try:
170                                         guiObject.setVAlign(
171                                                 { "top": guiObject.alignTop,
172                                                         "center": guiObject.alignCenter,
173                                                         "bottom": guiObject.alignBottom
174                                                 }[value])
175                                 except KeyError:
176                                         print "valign must be either top, center or bottom!"
177                         elif attrib == "halign":
178                                 try:
179                                         guiObject.setHAlign(
180                                                 { "left": guiObject.alignLeft,
181                                                         "center": guiObject.alignCenter,
182                                                         "right": guiObject.alignRight,
183                                                         "block": guiObject.alignBlock
184                                                 }[value])
185                                 except KeyError:
186                                         print "halign must be either left, center, right or block!"
187                         elif attrib == "flags":
188                                 flags = value.split(',')
189                                 for f in flags:
190                                         try:
191                                                 fv = eWindow.__dict__[f]
192                                                 guiObject.setFlag(fv)
193                                         except KeyError:
194                                                 print "illegal flag %s!" % f
195                         elif attrib == "backgroundColor":
196                                 guiObject.setBackgroundColor(parseColor(value))
197                         elif attrib != 'name':
198                                 print "unsupported attribute " + attrib + "=" + value
199                 except AttributeError:
200                         print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
201
202 def loadSkin(desktop):
203         print "loading skin..."
204         
205         def getPNG(x):
206                 g = gPixmapPtr()
207                 loadPNG(g, x)
208                 g = g.grabRef()
209                 return g
210         
211         skin = dom.childNodes[0]
212         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
213         
214         for c in elementsWithTag(skin.childNodes, "colors"):
215                 for color in elementsWithTag(c.childNodes, "color"):
216                         name = str(color.getAttribute("name"))
217                         color = str(color.getAttribute("value"))
218                         
219                         if not len(color):
220                                 raise ("need color and name, got %s %s" % (name, color))
221                                 
222                         colorNames[name] = parseColor(color)
223         
224         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
225                 style = eWindowStyleSkinned()
226                 
227                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
228                         bsName = str(borderset.getAttribute("name"))
229                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
230                                 bpName = str(pixmap.getAttribute("pos"))
231                                 filename = str(pixmap.getAttribute("filename"))
232                                 
233                                 png = getPNG(filename)
234                                 
235                                 # adapt palette
236                                 desktop.makeCompatiblePixmap(png)
237                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png)
238
239                 for color in elementsWithTag(windowstyle.childNodes, "color"):
240                         type = str(color.getAttribute("name"))
241                         color = parseColor(color.getAttribute("color"))
242                         
243                         try:
244                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
245                         except:
246                                 raise ("Unknown color %s" % (type))
247                         
248                 x = eWindowStyleManagerPtr()
249                 eWindowStyleManager.getInstance(x)
250                 x.setStyle(style)
251
252 def applyGUIskin(screen, skin, name, desktop):
253         myscreen = None
254         
255         # first, find the corresponding screen element
256         skin = dom.childNodes[0]
257         
258         for x in elementsWithTag(skin.childNodes, "screen"):
259                 if x.getAttribute('name') == name:
260                         myscreen = x
261         del skin
262         
263         assert myscreen != None, "no skin for screen '" + name + "' found!"
264         
265         applyAttributes(screen.instance, myscreen, desktop)
266         
267         # now walk all widgets
268         for widget in elementsWithTag(myscreen.childNodes, "widget"):
269                 wname = widget.getAttribute('name')
270                 if wname == None:
271                         print "widget has no name!"
272                         continue
273                 
274                 # get corresponding gui object
275                 try:
276                         guiObject = screen[wname].instance
277                 except:
278                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
279                 
280                 applyAttributes(guiObject, widget, desktop)
281
282         # now walk additional objects
283         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
284                 if widget.tagName == "eLabel":
285                         guiObject = eLabel(screen.instance)
286                 elif widget.tagName == "ePixmap":
287                         guiObject = ePixmap(screen.instance)
288                 else:
289                         raise str("unsupported stuff : %s" % widget.tagName)
290                 
291                 applyAttributes(guiObject, widget, desktop      )
292                 guiObject.thisown = 0