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
23 dom_skins.append(xml.dom.minidom.parse(resolveFilename(SCOPE_SKIN, name)))
26 loadSkin('skin_default.xml')
28 def parsePosition(str):
30 return ePoint(int(x), int(y))
34 return eSize(int(x), int(y))
37 name, size = str.split(';')
38 return gFont(name, int(size))
43 return colorNames[str]
45 raise ("color '%s' must be #aarrggbb or valid named color" % (str))
46 return gRGB(int(str[1:], 0x10))
48 def collectAttributes(skinAttributes, node, skin_path_prefix=None):
50 for p in range(node.attributes.length):
51 a = node.attributes.item(p)
53 # convert to string (was: unicode)
55 # TODO: localization? as in e1?
56 value = a.value.encode("utf-8")
58 if skin_path_prefix and attrib in ["pixmap", "pointer"] and len(value) and value[0:2] == "~/":
59 value = skin_path_prefix + value[1:]
61 skinAttributes.append((attrib, value))
66 raise "pixmap file %s not found!" % (path)
69 def applySingleAttribute(guiObject, desktop, attrib, value):
72 if attrib == 'position':
73 guiObject.move(parsePosition(value))
74 elif attrib == 'size':
75 guiObject.resize(parseSize(value))
76 elif attrib == 'title':
77 guiObject.setTitle(_(value))
78 elif attrib == 'text':
79 guiObject.setText(value)
80 elif attrib == 'font':
81 guiObject.setFont(parseFont(value))
82 elif attrib == 'zPosition':
83 guiObject.setZPosition(int(value))
84 elif attrib == "pixmap":
85 ptr = loadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, value))
86 # that __deref__ still scares me!
87 desktop.makeCompatiblePixmap(ptr.__deref__())
88 guiObject.setPixmap(ptr.__deref__())
89 # guiObject.setPixmapFromFile(value)
90 elif attrib == "alphatest": # used by ePixmap
91 guiObject.setAlphatest(
95 elif attrib == "orientation": # used by eSlider
97 guiObject.setOrientation(
98 { "orVertical": guiObject.orVertical,
99 "orHorizontal": guiObject.orHorizontal
102 print "oprientation must be either orVertical or orHorizontal!"
103 elif attrib == "valign":
106 { "top": guiObject.alignTop,
107 "center": guiObject.alignCenter,
108 "bottom": guiObject.alignBottom
111 print "valign must be either top, center or bottom!"
112 elif attrib == "halign":
115 { "left": guiObject.alignLeft,
116 "center": guiObject.alignCenter,
117 "right": guiObject.alignRight,
118 "block": guiObject.alignBlock
121 print "halign must be either left, center, right or block!"
122 elif attrib == "flags":
123 flags = value.split(',')
126 fv = eWindow.__dict__[f]
127 guiObject.setFlag(fv)
129 print "illegal flag %s!" % f
130 elif attrib == "backgroundColor":
131 guiObject.setBackgroundColor(parseColor(value))
132 elif attrib == "foregroundColor":
133 guiObject.setForegroundColor(parseColor(value))
134 elif attrib == "selectionDisabled":
135 guiObject.setSelectionEnable(0)
136 elif attrib == "transparent":
137 guiObject.setTransparent(int(value))
138 elif attrib == "borderColor":
139 guiObject.setBorderColor(parseColor(value))
140 elif attrib == "borderWidth":
141 guiObject.setBorderWidth(int(value))
142 elif attrib == "scrollbarMode":
143 guiObject.setScrollbarMode(
144 { "showOnDemand": guiObject.showOnDemand,
145 "showAlways": guiObject.showAlways,
146 "showNever": guiObject.showNever
148 elif attrib == "enableWrapAround":
149 guiObject.setWrapAround(True)
150 elif attrib == "pointer":
151 (name, pos) = value.split(':')
152 pos = parsePosition(pos)
153 ptr = loadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, name))
154 desktop.makeCompatiblePixmap(ptr.__deref__())
155 guiObject.setPointer(ptr.__deref__(), pos)
156 elif attrib != 'name':
157 print "unsupported attribute " + attrib + "=" + value
160 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
162 def applyAllAttributes(guiObject, desktop, attributes):
163 for (attrib, value) in attributes:
164 applySingleAttribute(guiObject, desktop, attrib, value)
166 def loadSingleSkinData(desktop, dom_skin):
167 """loads skin data like colors, windowstyle etc."""
169 skin = dom_skin.childNodes[0]
170 assert skin.tagName == "skin", "root element in skin must be 'skin'!"
172 for c in elementsWithTag(skin.childNodes, "colors"):
173 for color in elementsWithTag(c.childNodes, "color"):
174 name = str(color.getAttribute("name"))
175 color = str(color.getAttribute("value"))
178 raise ("need color and name, got %s %s" % (name, color))
180 colorNames[name] = parseColor(color)
182 for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
183 style = eWindowStyleSkinned()
185 style.setTitleFont(gFont("Regular", 20));
186 style.setTitleOffset(eSize(20, 5));
188 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
189 bsName = str(borderset.getAttribute("name"))
190 for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
191 bpName = str(pixmap.getAttribute("pos"))
192 filename = str(pixmap.getAttribute("filename"))
194 png = loadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, filename))
197 desktop.makeCompatiblePixmap(png.__deref__())
198 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png.__deref__())
200 for color in elementsWithTag(windowstyle.childNodes, "color"):
201 type = str(color.getAttribute("name"))
202 color = parseColor(color.getAttribute("color"))
205 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
207 raise ("Unknown color %s" % (type))
209 x = eWindowStyleManagerPtr()
210 eWindowStyleManager.getInstance(x)
213 def loadSkinData(desktop):
214 for dom_skin in dom_skins:
215 loadSingleSkinData(desktop, dom_skin)
217 def lookupScreen(name):
218 for dom_skin in dom_skins:
219 # first, find the corresponding screen element
220 skin = dom_skin.childNodes[0]
221 for x in elementsWithTag(skin.childNodes, "screen"):
222 if x.getAttribute('name') == name:
226 def readSkin(screen, skin, name, desktop):
227 myscreen = lookupScreen(name)
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)