]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/XMLtreeview.py
xcode 1.2 native targets
[wxWidgets.git] / wxPython / demo / XMLtreeview.py
1
2 import sys
3 import wx
4
5 py2 = sys.version[0] == '2'
6
7 try:
8 if py2:
9 from xml.parsers import expat
10 parsermodule = expat
11 else:
12 from xml.parsers import pyexpat
13 parsermodule = pyexpat
14 haveXML = True
15 except ImportError:
16 haveXML = False
17
18 #----------------------------------------------------------------------
19
20 if not haveXML:
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
26 )
27
28 dlg.ShowModal()
29 dlg.Destroy()
30
31 else:
32
33 class XMLTree(wx.TreeCtrl):
34 def __init__(self, parent, ID):
35 wx.TreeCtrl.__init__(self, parent, ID)
36 self.nodeStack = [self.AddRoot("Root")]
37
38 # Trees need an image list to do DnD...
39 self.il = wx.ImageList(16,16)
40 self.SetImageList(self.il)
41
42 # event handlers for DnD
43 self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnBeginDrag)
44 self.Bind(wx.EVT_TREE_END_DRAG, self.OnEndDrag)
45
46
47 def OnBeginDrag(self, event):
48 item = event.GetItem()
49
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
72 # Define a handler for start element events
73 def StartElement(self, name, attrs ):
74 if py2:
75 name = name.encode()
76
77 id = self.AppendItem(self.nodeStack[-1], name)
78 self.nodeStack.append(id)
79
80 def EndElement(self, name ):
81 self.nodeStack = self.nodeStack[:-1]
82
83 def CharacterData(self, data ):
84 if data.strip():
85 if py2:
86 data = data.encode()
87
88 self.AppendItem(self.nodeStack[-1], data)
89
90
91 def LoadTree(self, filename):
92 # Create a parser
93 Parser = parsermodule.ParserCreate()
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
113 overview = """\
114 """
115
116
117 if __name__ == '__main__':
118 import sys,os
119 import run
120 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])