]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/XMLtreeview.py
1 # 11/22/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
9 py2
= sys
.version
[0] == '2'
13 from xml
.parsers
import expat
16 from xml
.parsers
import pyexpat
17 parsermodule
= pyexpat
22 #----------------------------------------------------------------------
25 def runTest(frame
, nb
, log
):
26 dlg
= wx
.MessageDialog(
27 frame
, 'This demo requires the XML package. '
28 'See http://www.python.org/sigs/xml-sig/',
29 'Sorry', wx
.OK | wx
.ICON_INFORMATION
37 class XMLTree(wx
.TreeCtrl
):
38 def __init__(self
, parent
, ID
):
39 wx
.TreeCtrl
.__init
__(self
, parent
, ID
)
40 self
.nodeStack
= [self
.AddRoot("Root")]
42 # Trees need an image list to do DnD...
43 self
.il
= wx
.ImageList(16,16)
44 self
.SetImageList(self
.il
)
46 # event handlers for DnD
47 self
.Bind(wx
.EVT_TREE_BEGIN_DRAG
, self
.OnBeginDrag
)
48 self
.Bind(wx
.EVT_TREE_END_DRAG
, self
.OnEndDrag
)
51 def OnBeginDrag(self
, event
):
52 item
= event
.GetItem()
54 if item
!= self
.GetRootItem():
55 self
.draggingItem
= item
56 event
.Allow() # if DnD of this item is okay Allow it.
59 def OnEndDrag(self
, evt
):
60 itemSrc
= self
.draggingItem
61 itemDst
= evt
.GetItem()
62 self
.draggingItem
= None
64 if not itemDst
.IsOk():
65 print "Can't drag to here..."
68 # For this simple example just take the text of the source item
69 # and append it to the destination item. In real life you would
70 # possibly want to copy subtrees...
71 text
= self
.GetItemText(itemSrc
)
72 self
.AppendItem(itemDst
, text
)
76 # Define a handler for start element events
77 def StartElement(self
, name
, attrs
):
81 id = self
.AppendItem(self
.nodeStack
[-1], name
)
82 self
.nodeStack
.append(id)
84 def EndElement(self
, name
):
85 self
.nodeStack
= self
.nodeStack
[:-1]
87 def CharacterData(self
, data
):
92 self
.AppendItem(self
.nodeStack
[-1], data
)
95 def LoadTree(self
, filename
):
97 Parser
= parsermodule
.ParserCreate()
99 # Tell the parser what the start element handler is
100 Parser
.StartElementHandler
= self
.StartElement
101 Parser
.EndElementHandler
= self
.EndElement
102 Parser
.CharacterDataHandler
= self
.CharacterData
105 ParserStatus
= Parser
.Parse(open(filename
,'r').read(), 1)
108 def runTest(frame
, nb
, log
):
109 win
= XMLTree(nb
, -1)
110 win
.LoadTree("paper.xml")
113 #----------------------------------------------------------------------
121 if __name__
== '__main__':
124 run
.main(['', os
.path
.basename(sys
.argv
[0])])