| 1 | |
| 2 | import sys |
| 3 | import wx |
| 4 | |
| 5 | py2 = sys.version[0] == '2' |
| 6 | |
| 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 = 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 | |
| 28 | dlg.ShowModal() |
| 29 | dlg.Destroy() |
| 30 | |
| 31 | else: |
| 32 | |
| 33 | class XMLTree(wx.TreeCtrl): |
| 34 | def __init__(self, parent, ID): |
| 35 | wx.TreeCtrl.__init__(self, parent, ID) |
| 36 | self._root = self.AddRoot("Root") |
| 37 | self.nodeStack = [self._root] |
| 38 | |
| 39 | # Trees need an image list to do DnD... |
| 40 | self.il = wx.ImageList(16,16) |
| 41 | self.SetImageList(self.il) |
| 42 | |
| 43 | # event handlers for DnD |
| 44 | self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnBeginDrag) |
| 45 | self.Bind(wx.EVT_TREE_END_DRAG, self.OnEndDrag) |
| 46 | |
| 47 | |
| 48 | def OnBeginDrag(self, event): |
| 49 | item = event.GetItem() |
| 50 | |
| 51 | if item != self.GetRootItem(): |
| 52 | self.draggingItem = item |
| 53 | event.Allow() # if DnD of this item is okay Allow it. |
| 54 | |
| 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) |
| 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 | |
| 74 | if self.IsDescendant(itemDst, itemSrc): |
| 75 | print "Can't move item to its descendant" |
| 76 | return |
| 77 | |
| 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 | |
| 86 | # Define a handler for start element events |
| 87 | def StartElement(self, name, attrs ): |
| 88 | if py2: |
| 89 | name = name.encode() |
| 90 | |
| 91 | id = self.AppendItem(self.nodeStack[-1], name) |
| 92 | self.nodeStack.append(id) |
| 93 | |
| 94 | def EndElement(self, name ): |
| 95 | self.nodeStack = self.nodeStack[:-1] |
| 96 | |
| 97 | def CharacterData(self, data ): |
| 98 | if data.strip(): |
| 99 | if py2: |
| 100 | data = data.encode() |
| 101 | |
| 102 | self.AppendItem(self.nodeStack[-1], data) |
| 103 | |
| 104 | |
| 105 | def LoadTree(self, filename): |
| 106 | # Create a parser |
| 107 | Parser = parsermodule.ParserCreate() |
| 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 | |
| 127 | overview = """\ |
| 128 | """ |
| 129 | |
| 130 | |
| 131 | if __name__ == '__main__': |
| 132 | import sys,os |
| 133 | import run |
| 134 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |