]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/XMLtreeview.py
5 py2
= sys
.version
[0] == '2'
9 from xml
.parsers
import expat
12 from xml
.parsers
import pyexpat
13 parsermodule
= pyexpat
18 #----------------------------------------------------------------------
21 def runTest(frame
, nb
, log
):
22 dlg
= wx
.MessageDialog(
23 frame
, 'This demo requires the XML package. '
24 'See http://www.python.org/sigs/xml-sig/',
25 'Sorry', wx
.OK | wx
.ICON_INFORMATION
33 class XMLTree(wx
.TreeCtrl
):
34 def __init__(self
, parent
, ID
):
35 wx
.TreeCtrl
.__init
__(self
, parent
, ID
)
36 self
._root
= self
.AddRoot("Root")
37 self
.nodeStack
= [self
._root
]
39 # Trees need an image list to do DnD...
40 self
.il
= wx
.ImageList(16,16)
41 self
.SetImageList(self
.il
)
43 # event handlers for DnD
44 self
.Bind(wx
.EVT_TREE_BEGIN_DRAG
, self
.OnBeginDrag
)
45 self
.Bind(wx
.EVT_TREE_END_DRAG
, self
.OnEndDrag
)
48 def OnBeginDrag(self
, event
):
49 item
= event
.GetItem()
51 if item
!= self
.GetRootItem():
52 self
.draggingItem
= item
53 event
.Allow() # if DnD of this item is okay Allow it.
55 def IsDescendant(self
, firstItem
, secondItem
):
56 "Recursive check if firstItem is a descendant of a secondItem."
57 if firstItem
== self
._root
:
59 parentItem
= self
.GetItemParent(firstItem
)
60 if parentItem
== secondItem
:
63 return self
.IsDescendant(parentItem
, secondItem
)
65 def OnEndDrag(self
, evt
):
66 itemSrc
= self
.draggingItem
67 itemDst
= evt
.GetItem()
68 self
.draggingItem
= None
70 if not itemDst
.IsOk():
71 print "Can't drag to here..."
74 if self
.IsDescendant(itemDst
, itemSrc
):
75 print "Can't move item to its descendant"
78 # For this simple example just take the text of the source item
79 # and append it to the destination item. In real life you would
80 # possibly want to copy subtrees...
81 text
= self
.GetItemText(itemSrc
)
82 self
.AppendItem(itemDst
, text
)
86 # Define a handler for start element events
87 def StartElement(self
, name
, attrs
):
91 id = self
.AppendItem(self
.nodeStack
[-1], name
)
92 self
.nodeStack
.append(id)
94 def EndElement(self
, name
):
95 self
.nodeStack
= self
.nodeStack
[:-1]
97 def CharacterData(self
, data
):
102 self
.AppendItem(self
.nodeStack
[-1], data
)
105 def LoadTree(self
, filename
):
107 Parser
= parsermodule
.ParserCreate()
109 # Tell the parser what the start element handler is
110 Parser
.StartElementHandler
= self
.StartElement
111 Parser
.EndElementHandler
= self
.EndElement
112 Parser
.CharacterDataHandler
= self
.CharacterData
115 ParserStatus
= Parser
.Parse(open(filename
,'r').read(), 1)
118 def runTest(frame
, nb
, log
):
119 win
= XMLTree(nb
, -1)
120 win
.LoadTree("paper.xml")
123 #----------------------------------------------------------------------
131 if __name__
== '__main__':
134 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])