]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/XMLtreeview.py
Track and translate typenames where possible
[wxWidgets.git] / wxPython / demo / XMLtreeview.py
1 # 11/22/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 import sys
7 import wx
8
9 py2 = sys.version[0] == '2'
10
11 try:
12 if py2:
13 from xml.parsers import expat
14 parsermodule = expat
15 else:
16 from xml.parsers import pyexpat
17 parsermodule = pyexpat
18 haveXML = True
19 except ImportError:
20 haveXML = False
21
22 #----------------------------------------------------------------------
23
24 if not haveXML:
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
30 )
31
32 dlg.ShowModal()
33 dlg.Destroy()
34
35 else:
36
37 class XMLTree(wx.TreeCtrl):
38 def __init__(self, parent, ID):
39 wx.TreeCtrl.__init__(self, parent, ID)
40 self.nodeStack = [self.AddRoot("Root")]
41
42 # Trees need an image list to do DnD...
43 self.il = wx.ImageList(16,16)
44 self.SetImageList(self.il)
45
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)
49
50
51 def OnBeginDrag(self, event):
52 item = event.GetItem()
53
54 if item != self.GetRootItem():
55 self.draggingItem = item
56 event.Allow() # if DnD of this item is okay Allow it.
57
58
59 def OnEndDrag(self, evt):
60 itemSrc = self.draggingItem
61 itemDst = evt.GetItem()
62 self.draggingItem = None
63
64 if not itemDst.IsOk():
65 print "Can't drag to here..."
66 return
67
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)
73 self.Delete(itemSrc)
74
75
76 # Define a handler for start element events
77 def StartElement(self, name, attrs ):
78 if py2:
79 name = name.encode()
80
81 id = self.AppendItem(self.nodeStack[-1], name)
82 self.nodeStack.append(id)
83
84 def EndElement(self, name ):
85 self.nodeStack = self.nodeStack[:-1]
86
87 def CharacterData(self, data ):
88 if data.strip():
89 if py2:
90 data = data.encode()
91
92 self.AppendItem(self.nodeStack[-1], data)
93
94
95 def LoadTree(self, filename):
96 # Create a parser
97 Parser = parsermodule.ParserCreate()
98
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
103
104 # Parse the XML File
105 ParserStatus = Parser.Parse(open(filename,'r').read(), 1)
106
107
108 def runTest(frame, nb, log):
109 win = XMLTree(nb, -1)
110 win.LoadTree("paper.xml")
111 return win
112
113 #----------------------------------------------------------------------
114
115
116
117 overview = """\
118 """
119
120
121 if __name__ == '__main__':
122 import sys,os
123 import run
124 run.main(['', os.path.basename(sys.argv[0])])