]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/XMLtreeview.py
Don't use deprecated wx.MaskColour
[wxWidgets.git] / wxPython / demo / XMLtreeview.py
CommitLineData
8bf5d46e 1
8fa876ca
RD
2import sys
3import wx
c368d904
RD
4
5py2 = sys.version[0] == '2'
8bf5d46e 6
8bf5d46e 7try:
c368d904
RD
8 if py2:
9 from xml.parsers import expat
10 parsermodule = expat
11 else:
12 from xml.parsers import pyexpat
13 parsermodule = pyexpat
1e4a197e 14 haveXML = True
8bf5d46e 15except ImportError:
1e4a197e 16 haveXML = False
8bf5d46e
RD
17
18#----------------------------------------------------------------------
19
20if not haveXML:
21 def runTest(frame, nb, log):
8fa876ca
RD
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
26 )
27
8bf5d46e
RD
28 dlg.ShowModal()
29 dlg.Destroy()
30
31else:
32
8fa876ca 33 class XMLTree(wx.TreeCtrl):
8bf5d46e 34 def __init__(self, parent, ID):
8fa876ca 35 wx.TreeCtrl.__init__(self, parent, ID)
8bf5d46e
RD
36 self.nodeStack = [self.AddRoot("Root")]
37
edf2f43e 38 # Trees need an image list to do DnD...
8fa876ca 39 self.il = wx.ImageList(16,16)
edf2f43e
RD
40 self.SetImageList(self.il)
41
42 # event handlers for DnD
8fa876ca
RD
43 self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnBeginDrag)
44 self.Bind(wx.EVT_TREE_END_DRAG, self.OnEndDrag)
edf2f43e
RD
45
46
47 def OnBeginDrag(self, event):
48 item = event.GetItem()
8fa876ca 49
edf2f43e
RD
50 if item != self.GetRootItem():
51 self.draggingItem = item
52 event.Allow() # if DnD of this item is okay Allow it.
53
54
55 def OnEndDrag(self, evt):
56 itemSrc = self.draggingItem
57 itemDst = evt.GetItem()
58 self.draggingItem = None
59
60 if not itemDst.IsOk():
61 print "Can't drag to here..."
62 return
63
64 # For this simple example just take the text of the source item
65 # and append it to the destination item. In real life you would
66 # possibly want to copy subtrees...
67 text = self.GetItemText(itemSrc)
68 self.AppendItem(itemDst, text)
69 self.Delete(itemSrc)
70
71
8bf5d46e
RD
72 # Define a handler for start element events
73 def StartElement(self, name, attrs ):
c368d904
RD
74 if py2:
75 name = name.encode()
8fa876ca 76
9d8bd15f
RD
77 id = self.AppendItem(self.nodeStack[-1], name)
78 self.nodeStack.append(id)
8bf5d46e
RD
79
80 def EndElement(self, name ):
81 self.nodeStack = self.nodeStack[:-1]
82
83 def CharacterData(self, data ):
1e4a197e 84 if data.strip():
c368d904
RD
85 if py2:
86 data = data.encode()
8fa876ca 87
8bf5d46e
RD
88 self.AppendItem(self.nodeStack[-1], data)
89
90
91 def LoadTree(self, filename):
92 # Create a parser
c368d904 93 Parser = parsermodule.ParserCreate()
8bf5d46e
RD
94
95 # Tell the parser what the start element handler is
96 Parser.StartElementHandler = self.StartElement
97 Parser.EndElementHandler = self.EndElement
98 Parser.CharacterDataHandler = self.CharacterData
99
100 # Parse the XML File
101 ParserStatus = Parser.Parse(open(filename,'r').read(), 1)
102
103
104 def runTest(frame, nb, log):
105 win = XMLTree(nb, -1)
106 win.LoadTree("paper.xml")
107 return win
108
109#----------------------------------------------------------------------
110
111
112
8bf5d46e
RD
113overview = """\
114"""
115
1fded56b 116
1fded56b
RD
117if __name__ == '__main__':
118 import sys,os
119 import run
8eca4fef 120 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])