]>
Commit | Line | Data |
---|---|---|
8bf5d46e | 1 | |
8fa876ca RD |
2 | import sys |
3 | import wx | |
c368d904 | 4 | |
c4ef95da | 5 | from xml.parsers import expat |
8bf5d46e RD |
6 | |
7 | #---------------------------------------------------------------------- | |
8 | ||
c4ef95da RD |
9 | class XMLTree(wx.TreeCtrl): |
10 | def __init__(self, parent, ID): | |
11 | wx.TreeCtrl.__init__(self, parent, ID) | |
12 | self._root = self.AddRoot("Root") | |
13 | self.nodeStack = [self._root] | |
14 | ||
15 | # Trees need an image list to do DnD... | |
16 | self.il = wx.ImageList(16,16) | |
17 | self.SetImageList(self.il) | |
18 | ||
19 | # event handlers for DnD | |
20 | self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnBeginDrag) | |
21 | self.Bind(wx.EVT_TREE_END_DRAG, self.OnEndDrag) | |
22 | ||
23 | ||
24 | def OnBeginDrag(self, event): | |
25 | item = event.GetItem() | |
26 | ||
27 | if item != self.GetRootItem(): | |
28 | self.draggingItem = item | |
29 | event.Allow() # if DnD of this item is okay Allow it. | |
30 | ||
31 | def IsDescendant(self, firstItem, secondItem): | |
32 | "Recursive check if firstItem is a descendant of a secondItem." | |
33 | if firstItem == self._root: | |
34 | return False | |
35 | parentItem = self.GetItemParent(firstItem) | |
36 | if parentItem == secondItem: | |
37 | return True | |
38 | else: | |
39 | return self.IsDescendant(parentItem, secondItem) | |
8fa876ca | 40 | |
c4ef95da RD |
41 | def OnEndDrag(self, evt): |
42 | itemSrc = self.draggingItem | |
43 | itemDst = evt.GetItem() | |
44 | self.draggingItem = None | |
8bf5d46e | 45 | |
c4ef95da RD |
46 | if not itemDst.IsOk(): |
47 | print "Can't drag to here..." | |
48 | return | |
8bf5d46e | 49 | |
c4ef95da RD |
50 | if self.IsDescendant(itemDst, itemSrc): |
51 | print "Can't move item to its descendant" | |
52 | return | |
53 | ||
54 | # For this simple example just take the text of the source item | |
55 | # and append it to the destination item. In real life you would | |
56 | # possibly want to copy subtrees... | |
57 | text = self.GetItemText(itemSrc) | |
58 | self.AppendItem(itemDst, text) | |
59 | self.Delete(itemSrc) | |
60 | ||
61 | ||
62 | # Define a handler for start element events | |
63 | def StartElement(self, name, attrs ): | |
69b6d63d | 64 | name = name.encode() |
c4ef95da RD |
65 | |
66 | id = self.AppendItem(self.nodeStack[-1], name) | |
67 | self.nodeStack.append(id) | |
68 | ||
69 | def EndElement(self, name ): | |
70 | self.nodeStack = self.nodeStack[:-1] | |
71 | ||
72 | def CharacterData(self, data ): | |
73 | if data.strip(): | |
69b6d63d | 74 | data = data.encode() |
8fa876ca | 75 | |
c4ef95da | 76 | self.AppendItem(self.nodeStack[-1], data) |
8bf5d46e RD |
77 | |
78 | ||
c4ef95da RD |
79 | def LoadTree(self, filename): |
80 | # Create a parser | |
81 | Parser = expat.ParserCreate() | |
8bf5d46e | 82 | |
c4ef95da RD |
83 | # Tell the parser what the start element handler is |
84 | Parser.StartElementHandler = self.StartElement | |
85 | Parser.EndElementHandler = self.EndElement | |
86 | Parser.CharacterDataHandler = self.CharacterData | |
8bf5d46e | 87 | |
c4ef95da RD |
88 | # Parse the XML File |
89 | ParserStatus = Parser.Parse(open(filename,'r').read(), 1) | |
8bf5d46e RD |
90 | |
91 | ||
c4ef95da RD |
92 | def runTest(frame, nb, log): |
93 | win = XMLTree(nb, -1) | |
94 | win.LoadTree("paper.xml") | |
95 | return win | |
8bf5d46e RD |
96 | |
97 | #---------------------------------------------------------------------- | |
98 | ||
99 | ||
100 | ||
8bf5d46e RD |
101 | overview = """\ |
102 | """ | |
103 | ||
1fded56b | 104 | |
1fded56b RD |
105 | if __name__ == '__main__': |
106 | import sys,os | |
107 | import run | |
8eca4fef | 108 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |