]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tests/testTree.py
forgot to commit - no changes
[wxWidgets.git] / wxPython / tests / testTree.py
1 #!/usr/bin/env python
2
3 from wxPython import wx
4 import sys, os
5 from stat import *
6
7 GlobalObjList = []
8
9 class Obj:
10 def __init__(self, obj):
11 self.obj = obj
12 # Uncomment next line to eliminate crash.
13 # GlobalObjList.append(self)
14
15 def Name(self):
16 head, tail = os.path.split(self.obj)
17 if tail:
18 return tail
19 else:
20 return head
21
22 def HasChildren(self):
23 return os.path.isdir(self.obj)
24
25 def Children(self):
26 objList = os.listdir(self.obj)
27 objList.sort()
28 objList = map(lambda obj,parent=self.obj: os.path.join(parent,obj),
29 objList)
30 objectList = map(Obj, objList)
31 return objectList
32
33 def __str__(self):
34 return self.obj
35
36 def __repr__(self):
37 return self.obj
38
39 def __del__(self):
40 print 'del', self.obj
41
42
43 #----------------------------------------------------------------------
44
45 class pyTree(wx.wxTreeCtrl):
46
47 def __init__(self, parent, id, obj):
48 wx.wxTreeCtrl.__init__(self, parent, id)
49 self.root = self.AddRoot(obj.Name(), -1, -1, wx.wxTreeItemData(''))
50 self.SetPyData(self.root, obj)
51 if obj.HasChildren():
52 self.SetItemHasChildren(self.root, wx.TRUE)
53 wx.EVT_TREE_ITEM_EXPANDING(self, self.GetId(), self.OnItemExpanding)
54 wx.EVT_TREE_ITEM_COLLAPSED(self, self.GetId(), self.OnItemCollapsed)
55 wx.EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged)
56 self.output = None
57
58 def SetOutput(self, output):
59 self.output = output
60
61 def OnItemExpanding(self,event):
62 item = event.GetItem()
63 obj = self.GetPyData(item)
64 children = obj.Children()
65 for child in children:
66 new_item = self.AppendItem(item, child.Name(), -1, -1,
67 wx.wxTreeItemData(''))
68 self.SetPyData(new_item, child)
69 if child.HasChildren():
70 self.SetItemHasChildren(new_item, wx.TRUE)
71
72 def OnItemCollapsed(self, event):
73 item = event.GetItem()
74 self.DeleteChildren(item)
75
76 def OnSelChanged(self, event):
77 if not self.output:
78 return
79 obj = self.GetPyData( event.GetItem() )
80 apply(self.output, (`obj`,))
81
82
83
84 #----------------------------------------------------------------------
85 if __name__ == '__main__':
86
87 class MyFrame(wx.wxFrame):
88
89 def __init__(self):
90 wx.wxFrame.__init__(self, wx.NULL, -1, 'PyTreeItemData Test',
91 wx.wxDefaultPosition, wx.wxSize(600,500))
92 split = wx.wxSplitterWindow(self, -1)
93
94 if sys.platform == 'win32':
95 tree = pyTree(split, -1, Obj('C:\\'))
96 else:
97 tree = pyTree(split, -1, Obj('/'))
98
99 text = wx.wxTextCtrl(split, -1, '', wx.wxDefaultPosition,
100 wx.wxDefaultSize, wx.wxTE_MULTILINE)
101 split.SplitVertically(tree, text, 200)
102 tree.SetOutput(text.SetValue)
103 tree.SelectItem(tree.root)
104
105 class MyApp(wx.wxApp):
106
107 def OnInit(self):
108 frame = MyFrame()
109 frame.Show(wx.TRUE)
110 self.SetTopWindow(frame)
111 return wx.TRUE
112
113 app = MyApp(0)
114 app.MainLoop()
115
116