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