- fixed blit in gRC
[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         """
15         <skin>
16                 <screen name="mainMenu" position="300,100" size="300,300" title="real main menu">
17                         <widget name="okbutton" position="10,190" size="280,50" font="Arial:20" valign="center" halign="center" />
18                         <widget name="title" position="10,10" size="280,20" />
19                         <widget name="menu" position="10,30" size="280,140" />
20                 </screen>
21                 <screen name="clockDisplay" position="300,100" size="300,300">
22                         <widget name="okbutton" position="10,10" size="280,40" />
23                         <widget name="title" position="10,120" size="280,50" />
24                         <widget name="theClock" position="10,60" size="280,50" />
25                 </screen>
26                 <screen name="infoBar" position="80,350" size="540,150" title="InfoBar">
27                         <widget name="CurrentTime" position="10,10" size="40,30" />
28                         <widget name="ServiceName" position="50,20" size="200,30" />
29                         <widget name="Event_Now" position="100,40" size="300,30" valign="top" halign="left" />
30                         <widget name="Event_Next" position="100,90" size="300,30" valign="top" halign="left" />
31                         <widget name="Event_Now_Duration" position="440,40" size="80,30" valign="top" halign="left" />
32                         <widget name="Event_Next_Duration" position="440,90" size="80,30" valign="top" halign="left" />
33                         <eLabel position="70,0" size="300,30" text=".oO skin Oo." font="Arial:20" />
34 <!--                    <ePixmap position="70,0" size="300,30" pixmap="info-bg.png" /> -->
35                 </screen>
36                 <screen name="channelSelection" position="100,80" size="500,240" title="Channel Selection">
37                         <widget name="list" position="20,50" size="300,150" />
38                         <widget name="okbutton" position="340,50" size="140,30" />
39                 </screen>
40                 <screen name="serviceScan" position="150,100" size="300,200" title="Service Scan">
41                         <widget name="scan_progress" position="10,10" size="280,50" />
42                         <widget name="scan_state" position="10,60" size="280,30" />
43                         <widget name="okbutton" position="10,100" size="280,40" />
44                 </screen>
45         </skin>
46 """)
47
48 # filters all elements of childNode with the specified function
49 # example: nodes = elementsWithTag(childNodes, lambda x: x == "bla")
50 def elementsWithTag(el, tag):
51         for x in el:
52                 if x.nodeType != xml.dom.minidom.Element.nodeType:
53                         continue
54                 if tag(x.tagName):
55                         yield x
56
57 def parsePosition(str):
58         x, y = str.split(',')
59         return ePoint(int(x), int(y))
60
61 def parseSize(str):
62         x, y = str.split(',')
63         return eSize(int(x), int(y))
64
65 def parseFont(str):
66         name, size = str.split(':')
67         return gFont(name, int(size))
68
69 def applyAttributes(guiObject, node):
70         # walk all attributes
71         for p in range(node.attributes.length):
72                 a = node.attributes.item(p)
73                 
74                 # convert to string (was: unicode)
75                 attrib = str(a.name)
76                 # TODO: proper UTF8 translation?! (for value)
77                 # TODO: localization? as in e1?
78                 value = str(a.value)
79                 
80                 # and set attributes
81                 try:
82                         if attrib == 'position':
83                                 guiObject.move(parsePosition(value))
84                         elif attrib == 'size':
85                                 guiObject.resize(parseSize(value))
86                         elif attrib == 'title':
87                                 guiObject.setTitle(value)
88                         elif attrib == 'text':
89                                 guiObject.setText(value)
90                         elif attrib == 'font':
91                                 guiObject.setFont(parseFont(value))
92                         elif attrib == "pixmap":
93                                 ptr = gPixmapPtr()
94                                 if loadPNG(ptr, value):
95                                         raise "loading PNG failed!"
96                                 guiObject.setPixmap(ptr.__deref__())
97                         elif attrib == "valign":
98                                 try:
99                                         guiObject.setVAlign(
100                                                 { "top": guiObject.alignTop,
101                                                         "center": guiObject.alignCenter,
102                                                         "bottom": guiObject.alignBottom
103                                                 }[value])
104                                 except KeyError:
105                                         print "valign must be either top, center or bottom!"
106                         elif attrib == "halign":
107                                 try:
108                                         guiObject.setHAlign(
109                                                 { "left": guiObject.alignLeft,
110                                                         "center": guiObject.alignCenter,
111                                                         "right": guiObject.alignRight,
112                                                         "block": guiObject.alignBlock
113                                                 }[value])
114                                 except KeyError:
115                                         print "halign must be either left, center, right or block!"
116                         elif attrib != 'name':
117                                 print "unsupported attribute " + attrib + "=" + value
118                 except AttributeError:
119                         print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
120
121 def applyGUIskin(screen, skin, name):
122         myscreen = None
123         
124         # first, find the corresponding screen element
125         skin = dom.childNodes[0]
126         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
127         
128         for x in elementsWithTag(skin.childNodes, lambda x: x == "screen"):
129                 if x.getAttribute('name') == name:
130                         myscreen = x
131         del skin
132         
133         assert myscreen != None, "no skin for screen '" + name + "' found!"
134         
135         applyAttributes(screen.instance, myscreen)
136         
137         # now walk all widgets
138         for widget in elementsWithTag(myscreen.childNodes, lambda x: x == "widget"):
139                 wname = widget.getAttribute('name')
140                 if wname == None:
141                         print "widget has no name!"
142                         continue
143                 
144                 # get corresponding gui object
145                 try:
146                         guiObject = screen[wname].instance
147                 except:
148                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
149                 
150                 applyAttributes(guiObject, widget)
151
152         # now walk additional objects
153         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
154                 if widget.tagName == "eLabel":
155                         guiObject = eLabel(screen.instance)
156                 elif widget.tagName == "ePixmap":
157                         guiObject = ePixmap(screen.instance)
158                 else:
159                         raise str("unsupported stuff : %s" % widget.tagName)
160                 
161                 applyAttributes(guiObject, widget)
162                 guiObject.thisown = 0