]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | # 11/22/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
2 | # | |
3 | # o Updated for wx namespace | |
4 | # | |
8bf5d46e | 5 | |
8fa876ca RD |
6 | import sys |
7 | import wx | |
c368d904 RD |
8 | |
9 | py2 = sys.version[0] == '2' | |
8bf5d46e | 10 | |
8bf5d46e | 11 | try: |
c368d904 RD |
12 | if py2: |
13 | from xml.parsers import expat | |
14 | parsermodule = expat | |
15 | else: | |
16 | from xml.parsers import pyexpat | |
17 | parsermodule = pyexpat | |
1e4a197e | 18 | haveXML = True |
8bf5d46e | 19 | except ImportError: |
1e4a197e | 20 | haveXML = False |
8bf5d46e RD |
21 | |
22 | #---------------------------------------------------------------------- | |
23 | ||
24 | if not haveXML: | |
25 | def runTest(frame, nb, log): | |
8fa876ca RD |
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 | ||
8bf5d46e RD |
32 | dlg.ShowModal() |
33 | dlg.Destroy() | |
34 | ||
35 | else: | |
36 | ||
8fa876ca | 37 | class XMLTree(wx.TreeCtrl): |
8bf5d46e | 38 | def __init__(self, parent, ID): |
8fa876ca | 39 | wx.TreeCtrl.__init__(self, parent, ID) |
8bf5d46e RD |
40 | self.nodeStack = [self.AddRoot("Root")] |
41 | ||
edf2f43e | 42 | # Trees need an image list to do DnD... |
8fa876ca | 43 | self.il = wx.ImageList(16,16) |
edf2f43e RD |
44 | self.SetImageList(self.il) |
45 | ||
46 | # event handlers for DnD | |
8fa876ca RD |
47 | self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnBeginDrag) |
48 | self.Bind(wx.EVT_TREE_END_DRAG, self.OnEndDrag) | |
edf2f43e RD |
49 | |
50 | ||
51 | def OnBeginDrag(self, event): | |
52 | item = event.GetItem() | |
8fa876ca | 53 | |
edf2f43e RD |
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 | ||
8bf5d46e RD |
76 | # Define a handler for start element events |
77 | def StartElement(self, name, attrs ): | |
c368d904 RD |
78 | if py2: |
79 | name = name.encode() | |
8fa876ca | 80 | |
9d8bd15f RD |
81 | id = self.AppendItem(self.nodeStack[-1], name) |
82 | self.nodeStack.append(id) | |
8bf5d46e RD |
83 | |
84 | def EndElement(self, name ): | |
85 | self.nodeStack = self.nodeStack[:-1] | |
86 | ||
87 | def CharacterData(self, data ): | |
1e4a197e | 88 | if data.strip(): |
c368d904 RD |
89 | if py2: |
90 | data = data.encode() | |
8fa876ca | 91 | |
8bf5d46e RD |
92 | self.AppendItem(self.nodeStack[-1], data) |
93 | ||
94 | ||
95 | def LoadTree(self, filename): | |
96 | # Create a parser | |
c368d904 | 97 | Parser = parsermodule.ParserCreate() |
8bf5d46e RD |
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 | ||
8bf5d46e RD |
117 | overview = """\ |
118 | """ | |
119 | ||
1fded56b | 120 | |
1fded56b RD |
121 | if __name__ == '__main__': |
122 | import sys,os | |
123 | import run | |
124 | run.main(['', os.path.basename(sys.argv[0])]) |