3 from xml.dom import EMPTY_NAMESPACE
5 from Tools.XMLTools import elementsWithTag, mergeText
10 print " " * i + str(x)
12 for n in x.childNodes:
17 from Tools.Directories import resolveFilename, SCOPE_SKIN, SCOPE_SKIN_IMAGE
20 dom_skin = xml.dom.minidom.parse(resolveFilename(SCOPE_SKIN, 'skin.xml'))
21 dom_skin_default = xml.dom.minidom.parse(resolveFilename(SCOPE_SKIN, 'skin_default.xml'))
23 def parsePosition(str):
25 return ePoint(int(x), int(y))
29 return eSize(int(x), int(y))
32 name, size = str.split(';')
33 return gFont(name, int(size))
38 return colorNames[str]
40 raise ("color '%s' must be #aarrggbb or valid named color" % (str))
41 return gRGB(int(str[1:], 0x10))
43 def collectAttributes(skinAttributes, node, skin_path_prefix=None):
45 for p in range(node.attributes.length):
46 a = node.attributes.item(p)
48 # convert to string (was: unicode)
50 # TODO: localization? as in e1?
51 value = a.value.encode("utf-8")
53 if skin_path_prefix and attrib in ["pixmap", "pointer"] and len(value) and value[0:2] == "~/":
54 value = skin_path_prefix + value[1:]
56 skinAttributes.append((attrib, value))
61 raise "pixmap file %s not found!" % (path)
64 def applySingleAttribute(guiObject, desktop, attrib, value):
67 if attrib == 'position':
68 guiObject.move(parsePosition(value))
69 elif attrib == 'size':
70 guiObject.resize(parseSize(value))
71 elif attrib == 'title':
72 guiObject.setTitle(_(value))
73 elif attrib == 'text':
74 guiObject.setText(value)
75 elif attrib == 'font':
76 guiObject.setFont(parseFont(value))
77 elif attrib == 'zPosition':
78 guiObject.setZPosition(int(value))
79 elif attrib == "pixmap":
80 ptr = loadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, value))
81 # that __deref__ still scares me!
82 desktop.makeCompatiblePixmap(ptr.__deref__())
83 guiObject.setPixmap(ptr.__deref__())
84 # guiObject.setPixmapFromFile(value)
85 elif attrib == "alphatest": # used by ePixmap
86 guiObject.setAlphatest(
90 elif attrib == "orientation": # used by eSlider
92 guiObject.setOrientation(
93 { "orVertical": guiObject.orVertical,
94 "orHorizontal": guiObject.orHorizontal
97 print "oprientation must be either orVertical or orHorizontal!"
98 elif attrib == "valign":
101 { "top": guiObject.alignTop,
102 "center": guiObject.alignCenter,
103 "bottom": guiObject.alignBottom
106 print "valign must be either top, center or bottom!"
107 elif attrib == "halign":
110 { "left": guiObject.alignLeft,
111 "center": guiObject.alignCenter,
112 "right": guiObject.alignRight,
113 "block": guiObject.alignBlock
116 print "halign must be either left, center, right or block!"
117 elif attrib == "flags":
118 flags = value.split(',')
121 fv = eWindow.__dict__[f]
122 guiObject.setFlag(fv)
124 print "illegal flag %s!" % f
125 elif attrib == "backgroundColor":
126 guiObject.setBackgroundColor(parseColor(value))
127 elif attrib == "foregroundColor":
128 guiObject.setForegroundColor(parseColor(value))
129 elif attrib == "selectionDisabled":
130 guiObject.setSelectionEnable(0)
131 elif attrib == "transparent":
132 guiObject.setTransparent(int(value))
133 elif attrib == "borderColor":
134 guiObject.setBorderColor(parseColor(value))
135 elif attrib == "borderWidth":
136 guiObject.setBorderWidth(int(value))
137 elif attrib == "scrollbarMode":
138 guiObject.setScrollbarMode(
139 { "showOnDemand": guiObject.showOnDemand,
140 "showAlways": guiObject.showAlways,
141 "showNever": guiObject.showNever
143 elif attrib == "enableWrapAround":
144 guiObject.setWrapAround(True)
145 elif attrib == "pointer":
146 (name, pos) = value.split(':')
147 pos = parsePosition(pos)
148 ptr = loadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, name))
149 desktop.makeCompatiblePixmap(ptr.__deref__())
150 guiObject.setPointer(ptr.__deref__(), pos)
151 elif attrib != 'name':
152 print "unsupported attribute " + attrib + "=" + value
155 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
157 def applyAllAttributes(guiObject, desktop, attributes):
158 for (attrib, value) in attributes:
159 applySingleAttribute(guiObject, desktop, attrib, value)
161 def loadSkin(desktop):
162 print "loading skin..."
164 skin = dom_skin.childNodes[0]
165 assert skin.tagName == "skin", "root element in skin must be 'skin'!"
167 for c in elementsWithTag(skin.childNodes, "colors"):
168 for color in elementsWithTag(c.childNodes, "color"):
169 name = str(color.getAttribute("name"))
170 color = str(color.getAttribute("value"))
173 raise ("need color and name, got %s %s" % (name, color))
175 colorNames[name] = parseColor(color)
177 for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
178 style = eWindowStyleSkinned()
180 style.setTitleFont(gFont("Regular", 20));
181 style.setTitleOffset(eSize(20, 5));
183 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
184 bsName = str(borderset.getAttribute("name"))
185 for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
186 bpName = str(pixmap.getAttribute("pos"))
187 filename = str(pixmap.getAttribute("filename"))
189 png = loadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, filename))
192 desktop.makeCompatiblePixmap(png.__deref__())
193 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png.__deref__())
195 for color in elementsWithTag(windowstyle.childNodes, "color"):
196 type = str(color.getAttribute("name"))
197 color = parseColor(color.getAttribute("color"))
200 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
202 raise ("Unknown color %s" % (type))
204 x = eWindowStyleManagerPtr()
205 eWindowStyleManager.getInstance(x)
208 def readSkin(screen, skin, name, desktop):
211 # first, find the corresponding screen element
212 skin = dom_skin.childNodes[0]
213 skin_default = dom_skin_default.childNodes[0]
215 for x in elementsWithTag(skin.childNodes, "screen"):
216 if x.getAttribute('name') == name:
220 # if not found, check default skin
222 for x in elementsWithTag(skin_default.childNodes, "screen"):
223 if x.getAttribute('name') == name:
227 del skin, skin_default
229 # otherwise try embedded skin
230 myscreen = myscreen or getattr(screen, "parsedSkin", None)
232 # try uncompiled embedded skin
233 if myscreen is None and getattr(screen, "skin", None):
234 myscreen = screen.parsedSkin = xml.dom.minidom.parseString(screen.skin).childNodes[0]
236 assert myscreen is not None, "no skin for screen '" + name + "' found!"
238 screen.skinAttributes = [ ]
240 skin_path_prefix = getattr(screen, "skin_path", None)
242 collectAttributes(screen.skinAttributes, myscreen, skin_path_prefix)
244 screen.additionalWidgets = [ ]
246 # now walk all widgets
247 for widget in elementsWithTag(myscreen.childNodes, "widget"):
248 wname = widget.getAttribute('name')
250 print "widget has no name!"
253 # get corresponding gui object
255 attributes = screen[wname].skinAttributes = [ ]
257 raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
259 collectAttributes(attributes, widget, skin_path_prefix)
261 # now walk additional objects
262 for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
263 if widget.tagName == "applet":
264 codeText = mergeText(widget.childNodes).strip()
265 type = widget.getAttribute('type')
267 code = compile(codeText, "skin applet", "exec")
269 if type == "onLayoutFinish":
270 screen.onLayoutFinish.append(code)
272 raise str("applet type '%s' unknown!" % type)
276 class additionalWidget:
279 w = additionalWidget()
281 if widget.tagName == "eLabel":
283 elif widget.tagName == "ePixmap":
286 raise str("unsupported stuff : %s" % widget.tagName)
288 w.skinAttributes = [ ]
289 collectAttributes(w.skinAttributes, widget, skin_path_prefix)
291 # applyAttributes(guiObject, widget, desktop)
292 # guiObject.thisown = 0
293 screen.additionalWidgets.append(w)