HELP_BUTTON = wxNewId()
CONTEXT_HELP_BUTTON = wxNewId()
+ REF = wxNewId()
+
LAST = wxNewId()
class XML_Tree(wxTreeCtrl):
def __init__(self, parent, id):
- wxTreeCtrl.__init__(self, parent, id, style = wxTR_HAS_BUTTONS)
+ wxTreeCtrl.__init__(self, parent, id, style = wxTR_HAS_BUTTONS | wxTR_MULTIPLE)
self.SetBackgroundColour(wxColour(224, 248, 224))
# Register events
EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged)
EVT_ENTER_WINDOW(self, g.tools.OnMouse)
EVT_LEAVE_WINDOW(self, g.tools.OnMouse)
- def Unselect(self):
- self.selection = None
- wxTreeCtrl.Unselect(self)
- g.tools.UpdateUI()
-
def ExpandAll(self, item):
if self.ItemHasChildren(item):
self.Expand(item)
self.Unselect()
# Add tree item for given parent item if node is DOM element node with
- # 'object' tag. xxxParent is parent xxx object
+ # object/object_ref tag. xxxParent is parent xxx object
def AddNode(self, itemParent, xxxParent, node):
# Set item data to current node
try:
item = self.AppendItem(itemParent, treeObj.treeName(),
image=treeObj.treeImage(),
data=wxTreeItemData(xxx))
+ # Different color for references
+ if treeObj.ref:
+ self.SetItemTextColour(item, 'DarkGreen')
# Try to find children objects
if treeObj.hasChildren:
nodes = treeObj.element.childNodes[:]
parent.element.appendChild(elem)
newItem = self.AppendItem(itemParent, xxx.treeName(), image=xxx.treeImage(),
data=wxTreeItemData(xxx))
+ # Different color for references
+ if xxx.treeObject().ref: self.SetItemTextColour(newItem, 'DarkGreen')
# Add children items
if xxx.hasChildren:
treeObj = xxx.treeObject()
return parentWin.GetSizer()
elif isinstance(xxx.parent, xxxToolBar):
# How to get tool from toolbar?
- return parentWin.GetChildren()[0]
+ return parentWin
elif isinstance(xxx.parent, xxxStdDialogButtonSizer):
# This sizer returns non-existing children
for ch in parentWin.GetChildren():
pos = g.testWinPos
# Save in memory FS
memFile = MemoryFile('xxx.xrc')
- # Create partial XML file - faster for big files
-
- dom = MyDocument()
- mainNode = dom.createElement('resource')
- dom.appendChild(mainNode)
-
- # Remove temporarily from old parent
+ # Create memory XML file
elem = xxx.element
# Change window id to _XRCED_T_W. This gives some name for
# unnamed windows, and for named gives the possibility to
elem.setAttribute('class', 'wxPanel')
parent = elem.parentNode
next = elem.nextSibling
- parent.replaceChild(self.dummyNode, elem)
- # Append to new DOM, write it
- mainNode.appendChild(elem)
- dom.writexml(memFile, encoding=self.rootObj.params['encoding'].value())
+ encd = self.rootObj.params['encoding'].value()
+ if not encd: encd = None
+ self.dom.writexml(open('ttt.xrc','w'), encoding=encd)
+ self.dom.writexml(memFile, encoding=encd)
# Put back in place
- mainNode.removeChild(elem)
- dom.unlink()
- parent.replaceChild(elem, self.dummyNode)
# Remove temporary name or restore changed
if not xxx.name:
elem.removeAttribute('name')
needInsert = self.NeedInsert(item)
if item == self.root or needInsert and self.GetItemParent(item) == self.root:
SetMenu(m, pullDownMenu.topLevel)
+ m.AppendSeparator()
+ m.Append(ID_NEW.REF, 'reference...', 'Create object_ref node')
else:
xxx = self.GetPyData(item).treeObject()
# Check parent for possible child nodes if inserting sibling
m.Enable(m.FindItem('sizer'), False)
elif not (xxx.isSizer or xxx.parent and xxx.parent.isSizer):
m.Enable(ID_NEW.SPACER, False)
+ m.AppendSeparator()
+ m.Append(ID_NEW.REF, 'reference...', 'Create object_ref node')
# Select correct label for create menu
if not needInsert:
if self.shift:
menu.AppendMenu(id, 'Replace With', m)
if not m.GetMenuItemCount(): menu.Enable(id, False)
menu.Append(pullDownMenu.ID_SUBCLASS, 'Subclass...',
- 'Set subclass property')
+ 'Set "subclass" property')
menu.AppendSeparator()
# Not using standart IDs because we don't want to show shortcuts
menu.Append(wxID_CUT, 'Cut', 'Cut to the clipboard')
if isinstance(xxx, xxxBoxSizer):
self.SetItemImage(item, xxx.treeImage())
# Set global modified state
- g.frame.modified = True
+ g.frame.SetModified()
#image = -1
# Construct a new xxx object from DOM element
# parent is parent xxx object (or None if none), element is DOM element object
- def __init__(self, parent, element):
+ def __init__(self, parent, element, refElem=None):
self.parent = parent
self.element = element
+ self.refElem = refElem
self.undo = None
- # Get attributes
- self.className = element.getAttribute('class')
+ # Reference are dereferenced
+ if element.tagName == 'object_ref':
+ # Find original object
+ self.ref = element.getAttribute('ref')
+ if refElem:
+ self.className = self.refElem.getAttribute('class')
+ else:
+ self.className = 'xxxUnknown'
+ self.required = []
+ else:
+ # Get attributes
+ self.ref = None
+ self.className = element.getAttribute('class')
self.subclass = element.getAttribute('subclass')
if self.hasName: self.name = element.getAttribute('name')
# Set parameters (text element children)
for node in nodes:
if node.nodeType == minidom.Node.ELEMENT_NODE:
tag = node.tagName
- if tag == 'object':
+ if tag in ['object', 'object_ref']:
continue # do nothing for object children here
- if tag not in self.allParams and tag not in self.styles:
+ elif tag not in self.allParams and tag not in self.styles:
print 'WARNING: unknown parameter for %s: %s' % \
(self.className, tag)
elif tag in self.specials:
return className
# Class name or subclass
def panelName(self):
- if self.subclass: return self.subclass + '(' + self.className + ')'
- else: return self.className
+ if self.subclass: name = self.subclass + '(' + self.className + ')'
+ name = self.className
+ if self.ref: name = 'ref: ' + self.ref + ', ' + name
+ return name
# Sets name of tree object
def setTreeName(self, name):
if self.hasChild: obj = self.child
obj.name = name
obj.element.setAttribute('name', name)
+# Imitation of FindResource/DoFindResource from xmlres.cpp
+def DoFindResource(parent, name, classname, recursive):
+ for n in parent.childNodes:
+ if n.nodeType == minidom.Node.ELEMENT_NODE and \
+ n.tagName in ['object', 'object_ref'] and \
+ n.getAttribute('name') == name:
+ cls = n.getAttribute('class')
+ if not classname or cls == classname: return n
+ if not cls or n.tagName == 'object_ref':
+ refName = n.getAttribute('ref')
+ if not refName: continue
+ refNode = FindResource(refName)
+ if refName and refNode.getAttribute('class') == classname:
+ return n
+ if recursive:
+ for n in parent.childNodes:
+ if n.nodeType == minidom.Node.ELEMENT_NODE and \
+ n.tagName in ['object', 'object_ref']:
+ found = DoFindResource(n, name, classname, True)
+ if found: return found
+def FindResource(name, classname='', recursive=True):
+ found = DoFindResource(g.tree.mainNode, name, classname, recursive)
+ if found: return found
+ wxLogError('XRC resource "%s" not found!' % name)
+
+
################################################################################
# This is a little special: it is both xxxObject and xxxNode
class xxxChildContainer(xxxObject):
hasName = hasStyle = False
hasChild = True
- def __init__(self, parent, element):
- xxxObject.__init__(self, parent, element)
+ def __init__(self, parent, element, refElem=None):
+ xxxObject.__init__(self, parent, element, refElem)
# Must have one child with 'object' tag, but we don't check it
nodes = element.childNodes[:] # create copy
for node in nodes:
if node.nodeType == minidom.Node.ELEMENT_NODE:
- if node.tagName == 'object':
+ if node.tagName in ['object', 'object_ref']:
# Create new xxx object for child node
self.child = MakeXXXFromDOM(self, node)
self.child.parent = parent
allParams = ['option', 'flag', 'border', 'minsize', 'ratio']
paramDict = {'option': ParamInt, 'minsize': ParamPosSize, 'ratio': ParamPosSize}
#default = {'cellspan': '1,1'}
- def __init__(self, parent, element):
+ def __init__(self, parent, element, refElem=None):
# For GridBag sizer items, extra parameters added
if isinstance(parent, xxxGridBagSizer):
self.allParams = self.allParams + ['cellpos', 'cellspan']
- xxxChildContainer.__init__(self, parent, element)
+ xxxChildContainer.__init__(self, parent, element, refElem)
# Remove pos parameter - not needed for sizeritems
if 'pos' in self.child.allParams:
self.child.allParams = self.child.allParams[:]
class xxxSizerItemButton(xxxSizerItem):
allParams = []
paramDict = {}
- def __init__(self, parent, element):
- xxxChildContainer.__init__(self, parent, element)
+ def __init__(self, parent, element, refElem=None):
+ xxxChildContainer.__init__(self, parent, element, refElem=None)
# Remove pos parameter - not needed for sizeritems
if 'pos' in self.child.allParams:
self.child.allParams = self.child.allParams[:]
allParams = ['label', 'selected']
paramDict = {'selected': ParamBool}
required = ['label']
- def __init__(self, parent, element):
- xxxChildContainer.__init__(self, parent, element)
+ def __init__(self, parent, element, refElem=None):
+ xxxChildContainer.__init__(self, parent, element, refElem)
# pos and size dont matter for notebookpages
if 'pos' in self.child.allParams:
self.child.allParams = self.child.allParams[:]
# Test for object elements
def IsObject(node):
- return node.nodeType == minidom.Node.ELEMENT_NODE and node.tagName == 'object'
+ return node.nodeType == minidom.Node.ELEMENT_NODE and node.tagName in ['object', 'object_ref']
# Make XXX object from some DOM object, selecting correct class
def MakeXXXFromDOM(parent, element):
+ if element.tagName == 'object_ref':
+ ref = element.getAttribute('ref')
+ refElem = FindResource(ref)
+ if refElem: cls = refElem.getAttribute('class')
+ else: return xxxUnknown(parent, element)
+ else:
+ refElem = None
+ cls = element.getAttribute('class')
try:
- klass = xxxDict[element.getAttribute('class')]
+ klass = xxxDict[cls]
except KeyError:
# If we encounter a weird class, use unknown template
print 'WARNING: unsupported class:', element.getAttribute('class')
klass = xxxUnknown
- return klass(parent, element)
+ return klass(parent, element, refElem)
# Make empty DOM element
def MakeEmptyDOM(className):
# Now just make object
return MakeXXXFromDOM(parent, elem)
+# Make empty DOM element for reference
+def MakeEmptyRefDOM(ref):
+ elem = g.tree.dom.createElement('object_ref')
+ elem.setAttribute('ref', ref)
+ return elem
+
+# Make empty XXX object
+def MakeEmptyRefXXX(parent, ref):
+ # Make corresponding DOM object first
+ elem = MakeEmptyRefDOM(ref)
+ # If parent is a sizer, we should create sizeritem object, except for spacers
+ if parent:
+ if parent.isSizer:
+ sizerItemElem = MakeEmptyDOM(parent.itemTag)
+ sizerItemElem.appendChild(elem)
+ elem = sizerItemElem
+ elif isinstance(parent, xxxNotebook):
+ pageElem = MakeEmptyDOM('notebookpage')
+ pageElem.appendChild(elem)
+ elem = pageElem
+ # Now just make object
+ return MakeXXXFromDOM(parent, elem)
+