]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/XMLtreeview.py
regenerated after adding DEBUG_ options
[wxWidgets.git] / wxPython / demo / XMLtreeview.py
CommitLineData
8bf5d46e 1
1e4a197e 2import sys
c368d904
RD
3
4py2 = sys.version[0] == '2'
8bf5d46e
RD
5
6from wxPython.wx import *
7try:
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 15except ImportError:
1e4a197e 16 haveXML = False
8bf5d46e
RD
17
18#----------------------------------------------------------------------
19
20if not haveXML:
21 def runTest(frame, nb, log):
c368d904
RD
22 dlg = wxMessageDialog(frame, 'This demo requires the XML package. '
23 'See http://www.python.org/sigs/xml-sig/',
8bf5d46e
RD
24 'Sorry', wxOK | wxICON_INFORMATION)
25 dlg.ShowModal()
26 dlg.Destroy()
27
28else:
29
30 class XMLTree(wxTreeCtrl):
31 def __init__(self, parent, ID):
32 wxTreeCtrl.__init__(self, parent, ID)
33 self.nodeStack = [self.AddRoot("Root")]
34
edf2f43e
RD
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
8bf5d46e
RD
68 # Define a handler for start element events
69 def StartElement(self, name, attrs ):
c368d904
RD
70 if py2:
71 name = name.encode()
9d8bd15f
RD
72 id = self.AppendItem(self.nodeStack[-1], name)
73 self.nodeStack.append(id)
8bf5d46e
RD
74
75 def EndElement(self, name ):
76 self.nodeStack = self.nodeStack[:-1]
77
78 def CharacterData(self, data ):
1e4a197e 79 if data.strip():
c368d904
RD
80 if py2:
81 data = data.encode()
8bf5d46e
RD
82 self.AppendItem(self.nodeStack[-1], data)
83
84
85 def LoadTree(self, filename):
86 # Create a parser
c368d904 87 Parser = parsermodule.ParserCreate()
8bf5d46e
RD
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
110overview = """\
111"""
112
1fded56b
RD
113
114
115
116
117if __name__ == '__main__':
118 import sys,os
119 import run
120 run.main(['', os.path.basename(sys.argv[0])])