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