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
28 filename = resolveFilename(SCOPE_SKIN, name)
29 path = os.path.dirname(filename) + "/"
30 dom_skins.append((path, xml.dom.minidom.parse(filename)))
32 # we do our best to always select the "right" value
33 # skins are loaded in order of priority: skin with
34 # highest priority is loaded last, usually the user-provided
37 # currently, loadSingleSkinData (colors, bordersets etc.)
38 # are applied one-after-each, in order of ascending priority.
39 # the dom_skin will keep all screens in descending priority,
40 # so the first screen found will be used.
42 # example: loadSkin("nemesis_greenline/skin.xml")
44 loadSkin('skin_default.xml')
46 def parsePosition(str):
48 return ePoint(int(x), int(y))
52 return eSize(int(x), int(y))
55 name, size = str.split(';')
56 return gFont(name, int(size))
61 return colorNames[str]
63 raise ("color '%s' must be #aarrggbb or valid named color" % (str))
64 return gRGB(int(str[1:], 0x10))
66 def collectAttributes(skinAttributes, node, skin_path_prefix=None, ignore=[]):
68 for p in range(node.attributes.length):
69 a = node.attributes.item(p)
71 # convert to string (was: unicode)
73 # TODO: localization? as in e1?
74 value = a.value.encode("utf-8")
76 if attrib in ["pixmap", "pointer"]:
77 value = resolveFilename(SCOPE_SKIN_IMAGE, value, path_prefix=skin_path_prefix)
79 if attrib not in ignore:
80 skinAttributes.append((attrib, value))
85 raise "pixmap file %s not found!" % (path)
88 def applySingleAttribute(guiObject, desktop, attrib, value):
91 if attrib == 'position':
92 guiObject.move(parsePosition(value))
93 elif attrib == 'size':
94 guiObject.resize(parseSize(value))
95 elif attrib == 'title':
96 guiObject.setTitle(_(value))
97 elif attrib == 'text':
98 guiObject.setText(value)
99 elif attrib == 'font':
100 guiObject.setFont(parseFont(value))
101 elif attrib == 'zPosition':
102 guiObject.setZPosition(int(value))
103 elif attrib == "pixmap":
104 ptr = loadPixmap(value) # this should already have been filename-resolved.
105 # that __deref__ still scares me!
106 desktop.makeCompatiblePixmap(ptr.__deref__())
107 guiObject.setPixmap(ptr.__deref__())
108 # guiObject.setPixmapFromFile(value)
109 elif attrib == "alphatest": # used by ePixmap
110 guiObject.setAlphatest(
114 elif attrib == "orientation": # used by eSlider
116 guiObject.setOrientation(
117 { "orVertical": guiObject.orVertical,
118 "orHorizontal": guiObject.orHorizontal
121 print "oprientation must be either orVertical or orHorizontal!"
122 elif attrib == "valign":
125 { "top": guiObject.alignTop,
126 "center": guiObject.alignCenter,
127 "bottom": guiObject.alignBottom
130 print "valign must be either top, center or bottom!"
131 elif attrib == "halign":
134 { "left": guiObject.alignLeft,
135 "center": guiObject.alignCenter,
136 "right": guiObject.alignRight,
137 "block": guiObject.alignBlock
140 print "halign must be either left, center, right or block!"
141 elif attrib == "flags":
142 flags = value.split(',')
145 fv = eWindow.__dict__[f]
146 guiObject.setFlag(fv)
148 print "illegal flag %s!" % f
149 elif attrib == "backgroundColor":
150 guiObject.setBackgroundColor(parseColor(value))
151 elif attrib == "foregroundColor":
152 guiObject.setForegroundColor(parseColor(value))
153 elif attrib == "shadowColor":
154 guiObject.setShadowColor(parseColor(value))
155 elif attrib == "selectionDisabled":
156 guiObject.setSelectionEnable(0)
157 elif attrib == "transparent":
158 guiObject.setTransparent(int(value))
159 elif attrib == "borderColor":
160 guiObject.setBorderColor(parseColor(value))
161 elif attrib == "borderWidth":
162 guiObject.setBorderWidth(int(value))
163 elif attrib == "scrollbarMode":
164 guiObject.setScrollbarMode(
165 { "showOnDemand": guiObject.showOnDemand,
166 "showAlways": guiObject.showAlways,
167 "showNever": guiObject.showNever
169 elif attrib == "enableWrapAround":
170 guiObject.setWrapAround(True)
171 elif attrib == "pointer":
172 (name, pos) = value.split(':')
173 pos = parsePosition(pos)
174 ptr = loadPixmap(name)
175 desktop.makeCompatiblePixmap(ptr.__deref__())
176 guiObject.setPointer(ptr.__deref__(), pos)
177 elif attrib == 'shadowOffset':
178 guiObject.setShadowOffset(parsePosition(value))
180 print "unsupported attribute " + attrib + "=" + value
183 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
185 def applyAllAttributes(guiObject, desktop, attributes):
186 for (attrib, value) in attributes:
187 applySingleAttribute(guiObject, desktop, attrib, value)
189 def loadSingleSkinData(desktop, dom_skin, path_prefix):
190 """loads skin data like colors, windowstyle etc."""
192 skin = dom_skin.childNodes[0]
193 assert skin.tagName == "skin", "root element in skin must be 'skin'!"
195 for c in elementsWithTag(skin.childNodes, "colors"):
196 for color in elementsWithTag(c.childNodes, "color"):
197 name = str(color.getAttribute("name"))
198 color = str(color.getAttribute("value"))
201 raise ("need color and name, got %s %s" % (name, color))
203 colorNames[name] = parseColor(color)
205 for c in elementsWithTag(skin.childNodes, "fonts"):
206 for font in elementsWithTag(c.childNodes, "font"):
207 filename = str(font.getAttribute("filename") or "<NONAME>")
208 name = str(font.getAttribute("name") or "Regular")
209 scale = int(font.getAttribute("scale") or "100")
210 is_replacement = font.getAttribute("replacement") != ""
211 addFont(resolveFilename(SCOPE_FONTS, filename, path_prefix=path_prefix), name, scale, is_replacement)
213 for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
214 style = eWindowStyleSkinned()
217 font = gFont("Regular", 20)
218 offset = eSize(20, 5)
220 for title in elementsWithTag(windowstyle.childNodes, "title"):
221 offset = parseSize(title.getAttribute("offset"))
222 font = parseFont(str(title.getAttribute("font")))
224 style.setTitleFont(font);
225 style.setTitleOffset(offset)
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"))
233 png = loadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, filename, path_prefix=path_prefix))
236 desktop.makeCompatiblePixmap(png.__deref__())
237 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png.__deref__())
239 for color in elementsWithTag(windowstyle.childNodes, "color"):
240 type = str(color.getAttribute("name"))
241 color = parseColor(color.getAttribute("color"))
244 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
246 raise ("Unknown color %s" % (type))
248 x = eWindowStyleManagerPtr()
249 eWindowStyleManager.getInstance(x)
252 def loadSkinData(desktop):
255 for (path, dom_skin) in skins:
256 loadSingleSkinData(desktop, dom_skin, path)
258 def lookupScreen(name):
259 for (path, dom_skin) in dom_skins:
260 # first, find the corresponding screen element
261 skin = dom_skin.childNodes[0]
262 for x in elementsWithTag(skin.childNodes, "screen"):
263 if x.getAttribute('name') == name:
267 def readSkin(screen, skin, name, desktop):
268 myscreen, path = lookupScreen(name)
270 # otherwise try embedded skin
271 myscreen = myscreen or getattr(screen, "parsedSkin", None)
273 # try uncompiled embedded skin
274 if myscreen is None and getattr(screen, "skin", None):
275 myscreen = screen.parsedSkin = xml.dom.minidom.parseString(screen.skin).childNodes[0]
277 assert myscreen is not None, "no skin for screen '" + name + "' found!"
279 screen.skinAttributes = [ ]
281 skin_path_prefix = getattr(screen, "skin_path", path)
283 collectAttributes(screen.skinAttributes, myscreen, skin_path_prefix, ignore=["name"])
285 screen.additionalWidgets = [ ]
286 screen.renderer = [ ]
288 # now walk all widgets
289 for widget in elementsWithTag(myscreen.childNodes, "widget"):
290 # ok, we either have 1:1-mapped widgets ('old style'), or 1:n-mapped
291 # widgets (source->renderer).
293 wname = widget.getAttribute('name')
294 wsource = widget.getAttribute('source')
296 if wname is None and wsource is None:
297 print "widget has no name and no source!"
301 # get corresponding 'gui' object
303 attributes = screen[wname].skinAttributes = [ ]
305 raise SkinError("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
307 # assert screen[wname] is not Source
309 # and collect attributes for this
310 collectAttributes(attributes, widget, skin_path_prefix, ignore=['name'])
312 # get corresponding source
313 source = screen.get(wsource)
315 raise SkinError("source '" + wsource + "' was not found in screen '" + name + "'!")
317 wrender = widget.getAttribute('render')
320 raise SkinError("you must define a renderer with render= for source '%s'" % (wsource))
322 for converter in elementsWithTag(widget.childNodes, "convert"):
323 ctype = converter.getAttribute('type')
325 converter_class = my_import('.'.join(["Components", "Converter", ctype])).__dict__.get(ctype)
326 parms = mergeText(converter.childNodes).strip()
327 c = converter_class(parms)
332 renderer_class = my_import('.'.join(["Components", "Renderer", wrender])).__dict__.get(wrender)
334 renderer = renderer_class() # instantiate renderer
336 renderer.connect(source) # connect to source
337 attributes = renderer.skinAttributes = [ ]
338 collectAttributes(attributes, widget, skin_path_prefix, ignore=['render', 'source'])
340 screen.renderer.append(renderer)
342 # now walk additional objects
343 for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
344 if widget.tagName == "applet":
345 codeText = mergeText(widget.childNodes).strip()
346 type = widget.getAttribute('type')
348 code = compile(codeText, "skin applet", "exec")
350 if type == "onLayoutFinish":
351 screen.onLayoutFinish.append(code)
353 raise SkinError("applet type '%s' unknown!" % type)
357 class additionalWidget:
360 w = additionalWidget()
362 if widget.tagName == "eLabel":
364 elif widget.tagName == "ePixmap":
367 raise SkinError("unsupported stuff : %s" % widget.tagName)
369 w.skinAttributes = [ ]
370 collectAttributes(w.skinAttributes, widget, skin_path_prefix)
372 # applyAttributes(guiObject, widget, desktop)
373 # guiObject.thisown = 0
374 screen.additionalWidgets.append(w)