3 from xml.dom import EMPTY_NAMESPACE
4 from Tools.Import import my_import
7 from Components.config import ConfigSubsection, configElement, configText, config
9 from Tools.XMLTools import elementsWithTag, mergeText
14 print " " * i + str(x)
16 for n in x.childNodes:
21 from Tools.Directories import resolveFilename, SCOPE_SKIN, SCOPE_SKIN_IMAGE, SCOPE_FONTS
23 class SkinError(Exception):
24 def __init__(self, message):
25 self.message = message
34 filename = resolveFilename(SCOPE_SKIN, name)
35 path = os.path.dirname(filename) + "/"
36 dom_skins.append((path, xml.dom.minidom.parse(filename)))
38 # we do our best to always select the "right" value
39 # skins are loaded in order of priority: skin with
40 # highest priority is loaded last, usually the user-provided
43 # currently, loadSingleSkinData (colors, bordersets etc.)
44 # are applied one-after-each, in order of ascending priority.
45 # the dom_skin will keep all screens in descending priority,
46 # so the first screen found will be used.
48 # example: loadSkin("nemesis_greenline/skin.xml")
49 config.skin = ConfigSubsection()
50 config.skin.primary_skin = configElement("config.skin.primary_skin", configText, "skin.xml", 0)
53 loadSkin(config.skin.primary_skin.value)
54 except SkinError, err:
55 print "SKIN ERROR:", err
56 print "defaulting to standard skin..."
58 loadSkin('skin_default.xml')
60 def parsePosition(str):
62 return ePoint(int(x), int(y))
66 return eSize(int(x), int(y))
69 name, size = str.split(';')
70 return gFont(name, int(size))
75 return colorNames[str]
77 raise ("color '%s' must be #aarrggbb or valid named color" % (str))
78 return gRGB(int(str[1:], 0x10))
80 def collectAttributes(skinAttributes, node, skin_path_prefix=None, ignore=[]):
82 for p in range(node.attributes.length):
83 a = node.attributes.item(p)
85 # convert to string (was: unicode)
87 # TODO: localization? as in e1?
88 value = a.value.encode("utf-8")
90 if attrib in ["pixmap", "pointer"]:
91 value = resolveFilename(SCOPE_SKIN_IMAGE, value, path_prefix=skin_path_prefix)
93 if attrib not in ignore:
94 skinAttributes.append((attrib, value))
99 raise "pixmap file %s not found!" % (path)
102 def applySingleAttribute(guiObject, desktop, attrib, value):
105 if attrib == 'position':
106 guiObject.move(parsePosition(value))
107 elif attrib == 'size':
108 guiObject.resize(parseSize(value))
109 elif attrib == 'title':
110 guiObject.setTitle(_(value))
111 elif attrib == 'text':
112 guiObject.setText(value)
113 elif attrib == 'font':
114 guiObject.setFont(parseFont(value))
115 elif attrib == 'zPosition':
116 guiObject.setZPosition(int(value))
117 elif attrib == "pixmap":
118 ptr = loadPixmap(value) # this should already have been filename-resolved.
119 # that __deref__ still scares me!
120 desktop.makeCompatiblePixmap(ptr.__deref__())
121 guiObject.setPixmap(ptr.__deref__())
122 # guiObject.setPixmapFromFile(value)
123 elif attrib == "alphatest": # used by ePixmap
124 guiObject.setAlphatest(
128 elif attrib == "orientation": # used by eSlider
130 guiObject.setOrientation(
131 { "orVertical": guiObject.orVertical,
132 "orHorizontal": guiObject.orHorizontal
135 print "oprientation must be either orVertical or orHorizontal!"
136 elif attrib == "valign":
139 { "top": guiObject.alignTop,
140 "center": guiObject.alignCenter,
141 "bottom": guiObject.alignBottom
144 print "valign must be either top, center or bottom!"
145 elif attrib == "halign":
148 { "left": guiObject.alignLeft,
149 "center": guiObject.alignCenter,
150 "right": guiObject.alignRight,
151 "block": guiObject.alignBlock
154 print "halign must be either left, center, right or block!"
155 elif attrib == "flags":
156 flags = value.split(',')
159 fv = eWindow.__dict__[f]
160 guiObject.setFlag(fv)
162 print "illegal flag %s!" % f
163 elif attrib == "backgroundColor":
164 guiObject.setBackgroundColor(parseColor(value))
165 elif attrib == "foregroundColor":
166 guiObject.setForegroundColor(parseColor(value))
167 elif attrib == "shadowColor":
168 guiObject.setShadowColor(parseColor(value))
169 elif attrib == "selectionDisabled":
170 guiObject.setSelectionEnable(0)
171 elif attrib == "transparent":
172 guiObject.setTransparent(int(value))
173 elif attrib == "borderColor":
174 guiObject.setBorderColor(parseColor(value))
175 elif attrib == "borderWidth":
176 guiObject.setBorderWidth(int(value))
177 elif attrib == "scrollbarMode":
178 guiObject.setScrollbarMode(
179 { "showOnDemand": guiObject.showOnDemand,
180 "showAlways": guiObject.showAlways,
181 "showNever": guiObject.showNever
183 elif attrib == "enableWrapAround":
184 guiObject.setWrapAround(True)
185 elif attrib == "pointer":
186 (name, pos) = value.split(':')
187 pos = parsePosition(pos)
188 ptr = loadPixmap(name)
189 desktop.makeCompatiblePixmap(ptr.__deref__())
190 guiObject.setPointer(ptr.__deref__(), pos)
191 elif attrib == 'shadowOffset':
192 guiObject.setShadowOffset(parsePosition(value))
194 raise "unsupported attribute " + attrib + "=" + value
197 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
199 def applyAllAttributes(guiObject, desktop, attributes):
200 for (attrib, value) in attributes:
201 applySingleAttribute(guiObject, desktop, attrib, value)
203 def loadSingleSkinData(desktop, dom_skin, path_prefix):
204 """loads skin data like colors, windowstyle etc."""
206 skin = dom_skin.childNodes[0]
207 assert skin.tagName == "skin", "root element in skin must be 'skin'!"
209 for c in elementsWithTag(skin.childNodes, "colors"):
210 for color in elementsWithTag(c.childNodes, "color"):
211 name = str(color.getAttribute("name"))
212 color = str(color.getAttribute("value"))
215 raise ("need color and name, got %s %s" % (name, color))
217 colorNames[name] = parseColor(color)
219 for c in elementsWithTag(skin.childNodes, "fonts"):
220 for font in elementsWithTag(c.childNodes, "font"):
221 filename = str(font.getAttribute("filename") or "<NONAME>")
222 name = str(font.getAttribute("name") or "Regular")
223 scale = int(font.getAttribute("scale") or "100")
224 is_replacement = font.getAttribute("replacement") != ""
225 addFont(resolveFilename(SCOPE_FONTS, filename, path_prefix=path_prefix), name, scale, is_replacement)
227 for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
228 style = eWindowStyleSkinned()
231 font = gFont("Regular", 20)
232 offset = eSize(20, 5)
234 for title in elementsWithTag(windowstyle.childNodes, "title"):
235 offset = parseSize(title.getAttribute("offset"))
236 font = parseFont(str(title.getAttribute("font")))
238 style.setTitleFont(font);
239 style.setTitleOffset(offset)
241 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
242 bsName = str(borderset.getAttribute("name"))
243 for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
244 bpName = str(pixmap.getAttribute("pos"))
245 filename = str(pixmap.getAttribute("filename"))
247 png = loadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, filename, path_prefix=path_prefix))
250 desktop.makeCompatiblePixmap(png.__deref__())
251 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png.__deref__())
253 for color in elementsWithTag(windowstyle.childNodes, "color"):
254 type = str(color.getAttribute("name"))
255 color = parseColor(color.getAttribute("color"))
258 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
260 raise ("Unknown color %s" % (type))
262 x = eWindowStyleManagerPtr()
263 eWindowStyleManager.getInstance(x)
266 def loadSkinData(desktop):
269 for (path, dom_skin) in skins:
270 loadSingleSkinData(desktop, dom_skin, path)
272 def lookupScreen(name):
273 for (path, dom_skin) in dom_skins:
274 # first, find the corresponding screen element
275 skin = dom_skin.childNodes[0]
276 for x in elementsWithTag(skin.childNodes, "screen"):
277 if x.getAttribute('name') == name:
281 def readSkin(screen, skin, name, desktop):
282 myscreen, path = lookupScreen(name)
284 # otherwise try embedded skin
285 myscreen = myscreen or getattr(screen, "parsedSkin", None)
287 # try uncompiled embedded skin
288 if myscreen is None and getattr(screen, "skin", None):
289 myscreen = screen.parsedSkin = xml.dom.minidom.parseString(screen.skin).childNodes[0]
291 assert myscreen is not None, "no skin for screen '" + name + "' found!"
293 screen.skinAttributes = [ ]
295 skin_path_prefix = getattr(screen, "skin_path", path)
297 collectAttributes(screen.skinAttributes, myscreen, skin_path_prefix, ignore=["name"])
299 screen.additionalWidgets = [ ]
300 screen.renderer = [ ]
302 # now walk all widgets
303 for widget in elementsWithTag(myscreen.childNodes, "widget"):
304 # ok, we either have 1:1-mapped widgets ('old style'), or 1:n-mapped
305 # widgets (source->renderer).
307 wname = widget.getAttribute('name')
308 wsource = widget.getAttribute('source')
310 if wname is None and wsource is None:
311 print "widget has no name and no source!"
315 # get corresponding 'gui' object
317 attributes = screen[wname].skinAttributes = [ ]
319 raise SkinError("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
321 # assert screen[wname] is not Source
323 # and collect attributes for this
324 collectAttributes(attributes, widget, skin_path_prefix, ignore=['name'])
326 # get corresponding source
327 source = screen.get(wsource)
329 raise SkinError("source '" + wsource + "' was not found in screen '" + name + "'!")
331 wrender = widget.getAttribute('render')
334 raise SkinError("you must define a renderer with render= for source '%s'" % (wsource))
336 for converter in elementsWithTag(widget.childNodes, "convert"):
337 ctype = converter.getAttribute('type')
338 assert ctype, "'convert'-tag needs a 'type'-attribute"
339 converter_class = my_import('.'.join(["Components", "Converter", ctype])).__dict__.get(ctype)
340 parms = mergeText(converter.childNodes).strip()
341 c = converter_class(parms)
346 renderer_class = my_import('.'.join(["Components", "Renderer", wrender])).__dict__.get(wrender)
348 renderer = renderer_class() # instantiate renderer
350 renderer.connect(source) # connect to source
351 attributes = renderer.skinAttributes = [ ]
352 collectAttributes(attributes, widget, skin_path_prefix, ignore=['render', 'source'])
354 screen.renderer.append(renderer)
356 # now walk additional objects
357 for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
358 if widget.tagName == "applet":
359 codeText = mergeText(widget.childNodes).strip()
360 type = widget.getAttribute('type')
362 code = compile(codeText, "skin applet", "exec")
364 if type == "onLayoutFinish":
365 screen.onLayoutFinish.append(code)
367 raise SkinError("applet type '%s' unknown!" % type)
371 class additionalWidget:
374 w = additionalWidget()
376 if widget.tagName == "eLabel":
378 elif widget.tagName == "ePixmap":
381 raise SkinError("unsupported stuff : %s" % widget.tagName)
383 w.skinAttributes = [ ]
384 collectAttributes(w.skinAttributes, widget, skin_path_prefix, ignore=['name'])
386 # applyAttributes(guiObject, widget, desktop)
387 # guiObject.thisown = 0
388 screen.additionalWidgets.append(w)