3 from xml.dom import EMPTY_NAMESPACE
4 from Tools.Import import my_import
7 from Tools.XMLTools import elementsWithTag, mergeText
12 print " " * i + str(x)
14 for n in x.childNodes:
19 from Tools.Directories import resolveFilename, SCOPE_SKIN, SCOPE_SKIN_IMAGE, SCOPE_FONTS
25 filename = resolveFilename(SCOPE_SKIN, name)
26 path = os.path.dirname(filename) + "/"
27 dom_skins.append((path, xml.dom.minidom.parse(filename)))
29 # we do our best to always select the "right" value
30 # skins are loaded in order of priority: skin with
31 # highest priority is loaded last, usually the user-provided
34 # currently, loadSingleSkinData (colors, bordersets etc.)
35 # are applied one-after-each, in order of ascending priority.
36 # the dom_skin will keep all screens in descending priority,
37 # so the first screen found will be used.
39 # example: loadSkin("nemesis_greenline/skin.xml")
41 loadSkin('skin_default.xml')
43 def parsePosition(str):
45 return ePoint(int(x), int(y))
49 return eSize(int(x), int(y))
52 name, size = str.split(';')
53 return gFont(name, int(size))
58 return colorNames[str]
60 raise ("color '%s' must be #aarrggbb or valid named color" % (str))
61 return gRGB(int(str[1:], 0x10))
63 def collectAttributes(skinAttributes, node, skin_path_prefix=None, ignore=[]):
65 for p in range(node.attributes.length):
66 a = node.attributes.item(p)
68 # convert to string (was: unicode)
70 # TODO: localization? as in e1?
71 value = a.value.encode("utf-8")
73 if attrib in ["pixmap", "pointer"]:
74 value = resolveFilename(SCOPE_SKIN_IMAGE, value, path_prefix=skin_path_prefix)
76 if attrib not in ignore:
77 skinAttributes.append((attrib, value))
82 raise "pixmap file %s not found!" % (path)
85 def applySingleAttribute(guiObject, desktop, attrib, value):
88 if attrib == 'position':
89 guiObject.move(parsePosition(value))
90 elif attrib == 'size':
91 guiObject.resize(parseSize(value))
92 elif attrib == 'title':
93 guiObject.setTitle(_(value))
94 elif attrib == 'text':
95 guiObject.setText(value)
96 elif attrib == 'font':
97 guiObject.setFont(parseFont(value))
98 elif attrib == 'zPosition':
99 guiObject.setZPosition(int(value))
100 elif attrib == "pixmap":
101 ptr = loadPixmap(value) # this should already have been filename-resolved.
102 # that __deref__ still scares me!
103 desktop.makeCompatiblePixmap(ptr.__deref__())
104 guiObject.setPixmap(ptr.__deref__())
105 # guiObject.setPixmapFromFile(value)
106 elif attrib == "alphatest": # used by ePixmap
107 guiObject.setAlphatest(
111 elif attrib == "orientation": # used by eSlider
113 guiObject.setOrientation(
114 { "orVertical": guiObject.orVertical,
115 "orHorizontal": guiObject.orHorizontal
118 print "oprientation must be either orVertical or orHorizontal!"
119 elif attrib == "valign":
122 { "top": guiObject.alignTop,
123 "center": guiObject.alignCenter,
124 "bottom": guiObject.alignBottom
127 print "valign must be either top, center or bottom!"
128 elif attrib == "halign":
131 { "left": guiObject.alignLeft,
132 "center": guiObject.alignCenter,
133 "right": guiObject.alignRight,
134 "block": guiObject.alignBlock
137 print "halign must be either left, center, right or block!"
138 elif attrib == "flags":
139 flags = value.split(',')
142 fv = eWindow.__dict__[f]
143 guiObject.setFlag(fv)
145 print "illegal flag %s!" % f
146 elif attrib == "backgroundColor":
147 guiObject.setBackgroundColor(parseColor(value))
148 elif attrib == "foregroundColor":
149 guiObject.setForegroundColor(parseColor(value))
150 elif attrib == "shadowColor":
151 guiObject.setShadowColor(parseColor(value))
152 elif attrib == "selectionDisabled":
153 guiObject.setSelectionEnable(0)
154 elif attrib == "transparent":
155 guiObject.setTransparent(int(value))
156 elif attrib == "borderColor":
157 guiObject.setBorderColor(parseColor(value))
158 elif attrib == "borderWidth":
159 guiObject.setBorderWidth(int(value))
160 elif attrib == "scrollbarMode":
161 guiObject.setScrollbarMode(
162 { "showOnDemand": guiObject.showOnDemand,
163 "showAlways": guiObject.showAlways,
164 "showNever": guiObject.showNever
166 elif attrib == "enableWrapAround":
167 guiObject.setWrapAround(True)
168 elif attrib == "pointer":
169 (name, pos) = value.split(':')
170 pos = parsePosition(pos)
171 ptr = loadPixmap(name)
172 desktop.makeCompatiblePixmap(ptr.__deref__())
173 guiObject.setPointer(ptr.__deref__(), pos)
174 elif attrib == 'shadowOffset':
175 guiObject.setShadowOffset(parsePosition(value))
177 print "unsupported attribute " + attrib + "=" + value
180 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
182 def applyAllAttributes(guiObject, desktop, attributes):
183 for (attrib, value) in attributes:
184 applySingleAttribute(guiObject, desktop, attrib, value)
186 def loadSingleSkinData(desktop, dom_skin, path_prefix):
187 """loads skin data like colors, windowstyle etc."""
189 skin = dom_skin.childNodes[0]
190 assert skin.tagName == "skin", "root element in skin must be 'skin'!"
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"))
198 raise ("need color and name, got %s %s" % (name, color))
200 colorNames[name] = parseColor(color)
202 for c in elementsWithTag(skin.childNodes, "fonts"):
203 for font in elementsWithTag(c.childNodes, "font"):
204 filename = str(font.getAttribute("filename") or "<NONAME>")
205 name = str(font.getAttribute("name") or "Regular")
206 scale = int(font.getAttribute("scale") or "100")
207 is_replacement = font.getAttribute("replacement") != ""
208 addFont(resolveFilename(SCOPE_FONTS, filename, path_prefix=path_prefix), name, scale, is_replacement)
210 for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
211 style = eWindowStyleSkinned()
214 font = gFont("Regular", 20)
215 offset = eSize(20, 5)
217 for title in elementsWithTag(windowstyle.childNodes, "title"):
218 offset = parseSize(title.getAttribute("offset"))
219 font = parseFont(str(title.getAttribute("font")))
221 style.setTitleFont(font);
222 style.setTitleOffset(offset)
224 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
225 bsName = str(borderset.getAttribute("name"))
226 for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
227 bpName = str(pixmap.getAttribute("pos"))
228 filename = str(pixmap.getAttribute("filename"))
230 png = loadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, filename, path_prefix=path_prefix))
233 desktop.makeCompatiblePixmap(png.__deref__())
234 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png.__deref__())
236 for color in elementsWithTag(windowstyle.childNodes, "color"):
237 type = str(color.getAttribute("name"))
238 color = parseColor(color.getAttribute("color"))
241 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
243 raise ("Unknown color %s" % (type))
245 x = eWindowStyleManagerPtr()
246 eWindowStyleManager.getInstance(x)
249 def loadSkinData(desktop):
252 for (path, dom_skin) in skins:
253 loadSingleSkinData(desktop, dom_skin, path)
255 def lookupScreen(name):
256 for (path, dom_skin) in dom_skins:
257 # first, find the corresponding screen element
258 skin = dom_skin.childNodes[0]
259 for x in elementsWithTag(skin.childNodes, "screen"):
260 if x.getAttribute('name') == name:
264 def readSkin(screen, skin, name, desktop):
265 myscreen, path = lookupScreen(name)
267 # otherwise try embedded skin
268 myscreen = myscreen or getattr(screen, "parsedSkin", None)
270 # try uncompiled embedded skin
271 if myscreen is None and getattr(screen, "skin", None):
272 myscreen = screen.parsedSkin = xml.dom.minidom.parseString(screen.skin).childNodes[0]
274 assert myscreen is not None, "no skin for screen '" + name + "' found!"
276 screen.skinAttributes = [ ]
278 skin_path_prefix = getattr(screen, "skin_path", path)
280 collectAttributes(screen.skinAttributes, myscreen, skin_path_prefix, ignore=["name"])
282 screen.additionalWidgets = [ ]
283 screen.renderer = [ ]
285 # now walk all widgets
286 for widget in elementsWithTag(myscreen.childNodes, "widget"):
287 # ok, we either have 1:1-mapped widgets ('old style'), or 1:n-mapped
288 # widgets (source->renderer).
290 wname = widget.getAttribute('name')
291 wsource = widget.getAttribute('source')
293 if wname is None and wsource is None:
294 print "widget has no name and no source!"
298 # get corresponding 'gui' object
300 attributes = screen[wname].skinAttributes = [ ]
302 raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
304 # assert screen[wname] is not Source
306 # and collect attributes for this
307 collectAttributes(attributes, widget, skin_path_prefix, ignore=['name'])
309 # get corresponding source
310 source = screen.get(wsource)
312 raise str("source '" + wsource + "' was not found in screen '" + name + "'!")
314 wrender = widget.getAttribute('render')
316 for converter in elementsWithTag(widget.childNodes, "convert"):
317 ctype = converter.getAttribute('type')
319 converter_class = my_import('.'.join(["Components", "Converter", ctype])).__dict__.get(ctype)
320 parms = mergeText(converter.childNodes).strip()
321 c = converter_class(parms)
326 renderer_class = my_import('.'.join(["Components", "Renderer", wrender])).__dict__.get(wrender)
328 renderer = renderer_class() # instantiate renderer
330 renderer.connect(source) # connect to source
331 attributes = renderer.skinAttributes = [ ]
332 collectAttributes(attributes, widget, skin_path_prefix, ignore=['render', 'source'])
334 screen.renderer.append(renderer)
336 # now walk additional objects
337 for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
338 if widget.tagName == "applet":
339 codeText = mergeText(widget.childNodes).strip()
340 type = widget.getAttribute('type')
342 code = compile(codeText, "skin applet", "exec")
344 if type == "onLayoutFinish":
345 screen.onLayoutFinish.append(code)
347 raise str("applet type '%s' unknown!" % type)
351 class additionalWidget:
354 w = additionalWidget()
356 if widget.tagName == "eLabel":
358 elif widget.tagName == "ePixmap":
361 raise str("unsupported stuff : %s" % widget.tagName)
363 w.skinAttributes = [ ]
364 collectAttributes(w.skinAttributes, widget, skin_path_prefix)
366 # applyAttributes(guiObject, widget, desktop)
367 # guiObject.thisown = 0
368 screen.additionalWidgets.append(w)