]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/XMLtreeview.py
Removed AlignIn after further thought
[wxWidgets.git] / wxPython / demo / XMLtreeview.py
1
2 import sys
3
4 py2 = sys.version[0] == '2'
5
6 from wxPython.wx import *
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 = wxMessageDialog(frame, 'This demo requires the XML package. '
23 'See http://www.python.org/sigs/xml-sig/',
24 'Sorry', wxOK | wxICON_INFORMATION)
25 dlg.ShowModal()
26 dlg.Destroy()
27
28 else:
29
30 class XMLTree(wxTreeCtrl):
31 def __init__(self, parent, ID):
32 wxTreeCtrl.__init__(self, parent, ID)
33 self.nodeStack = [self.AddRoot("Root")]
34
35 # Trees need an image list to do DnD...
36 self.il = wxImageList(16,16)
37 self.SetImageList(self.il)
38
39 # event handlers for DnD
40 EVT_TREE_BEGIN_DRAG(self, ID, self.OnBeginDrag)
41 EVT_TREE_END_DRAG(self, ID, self.OnEndDrag)
42
43
44 def OnBeginDrag(self, event):
45 item = event.GetItem()
46 if item != self.GetRootItem():
47 self.draggingItem = item
48 event.Allow() # if DnD of this item is okay Allow it.
49
50
51 def OnEndDrag(self, evt):
52 itemSrc = self.draggingItem
53 itemDst = evt.GetItem()
54 self.draggingItem = None
55
56 if not itemDst.IsOk():
57 print "Can't drag to here..."
58 return
59
60 # For this simple example just take the text of the source item
61 # and append it to the destination item. In real life you would
62 # possibly want to copy subtrees...
63 text = self.GetItemText(itemSrc)
64 self.AppendItem(itemDst, text)
65 self.Delete(itemSrc)
66
67
68 # Define a handler for start element events
69 def StartElement(self, name, attrs ):
70 if py2:
71 name = name.encode()
72 id = self.AppendItem(self.nodeStack[-1], name)
73 self.nodeStack.append(id)
74
75 def EndElement(self, name ):
76 self.nodeStack = self.nodeStack[:-1]
77
78 def CharacterData(self, data ):
79 if data.strip():
80 if py2:
81 data = data.encode()
82 self.AppendItem(self.nodeStack[-1], data)
83
84
85 def LoadTree(self, filename):
86 # Create a parser
87 Parser = parsermodule.ParserCreate()
88
89 # Tell the parser what the start element handler is
90 Parser.StartElementHandler = self.StartElement
91 Parser.EndElementHandler = self.EndElement
92 Parser.CharacterDataHandler = self.CharacterData
93
94 # Parse the XML File
95 ParserStatus = Parser.Parse(open(filename,'r').read(), 1)
96
97
98 def runTest(frame, nb, log):
99 win = XMLTree(nb, -1)
100 win.LoadTree("paper.xml")
101 return win
102
103 #----------------------------------------------------------------------
104
105
106
107
108
109
110 overview = """\
111 """
112