]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/XMLtreeview.py
5 from xml
.parsers
import expat
7 #----------------------------------------------------------------------
9 class XMLTree(wx
.TreeCtrl
):
10 def __init__(self
, parent
, ID
):
11 wx
.TreeCtrl
.__init
__(self
, parent
, ID
)
12 self
._root
= self
.AddRoot("Root")
13 self
.nodeStack
= [self
._root
]
15 # Trees need an image list to do DnD...
16 self
.il
= wx
.ImageList(16,16)
17 self
.SetImageList(self
.il
)
19 # event handlers for DnD
20 self
.Bind(wx
.EVT_TREE_BEGIN_DRAG
, self
.OnBeginDrag
)
21 self
.Bind(wx
.EVT_TREE_END_DRAG
, self
.OnEndDrag
)
24 def OnBeginDrag(self
, event
):
25 item
= event
.GetItem()
27 if item
!= self
.GetRootItem():
28 self
.draggingItem
= item
29 event
.Allow() # if DnD of this item is okay Allow it.
31 def IsDescendant(self
, firstItem
, secondItem
):
32 "Recursive check if firstItem is a descendant of a secondItem."
33 if firstItem
== self
._root
:
35 parentItem
= self
.GetItemParent(firstItem
)
36 if parentItem
== secondItem
:
39 return self
.IsDescendant(parentItem
, secondItem
)
41 def OnEndDrag(self
, evt
):
42 itemSrc
= self
.draggingItem
43 itemDst
= evt
.GetItem()
44 self
.draggingItem
= None
46 if not itemDst
.IsOk():
47 print "Can't drag to here..."
50 if self
.IsDescendant(itemDst
, itemSrc
):
51 print "Can't move item to its descendant"
54 # For this simple example just take the text of the source item
55 # and append it to the destination item. In real life you would
56 # possibly want to copy subtrees...
57 text
= self
.GetItemText(itemSrc
)
58 self
.AppendItem(itemDst
, text
)
62 # Define a handler for start element events
63 def StartElement(self
, name
, attrs
):
67 id = self
.AppendItem(self
.nodeStack
[-1], name
)
68 self
.nodeStack
.append(id)
70 def EndElement(self
, name
):
71 self
.nodeStack
= self
.nodeStack
[:-1]
73 def CharacterData(self
, data
):
78 self
.AppendItem(self
.nodeStack
[-1], data
)
81 def LoadTree(self
, filename
):
83 Parser
= expat
.ParserCreate()
85 # Tell the parser what the start element handler is
86 Parser
.StartElementHandler
= self
.StartElement
87 Parser
.EndElementHandler
= self
.EndElement
88 Parser
.CharacterDataHandler
= self
.CharacterData
91 ParserStatus
= Parser
.Parse(open(filename
,'r').read(), 1)
94 def runTest(frame
, nb
, log
):
96 win
.LoadTree("paper.xml")
99 #----------------------------------------------------------------------
107 if __name__
== '__main__':
110 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])