]>
Commit | Line | Data |
---|---|---|
be05b434 RD |
1 | import wx |
2 | import data | |
3 | ||
4 | class TestFrame(wx.Frame): | |
5 | def __init__(self): | |
6 | wx.Frame.__init__(self, None, title="virtual tree with icons", size=(400,500)) | |
7 | il = wx.ImageList(16,16) | |
8 | self.fldridx = il.Add( | |
9 | wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, (16,16))) | |
10 | self.fldropenidx = il.Add( | |
11 | wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, (16,16))) | |
12 | self.fileidx = il.Add( | |
13 | wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, (16,16))) | |
14 | self.tree = wx.TreeCtrl(self) | |
15 | self.tree.AssignImageList(il) | |
16 | root = self.tree.AddRoot("wx.Object") | |
17 | self.tree.SetItemImage(root, self.fldridx, | |
18 | wx.TreeItemIcon_Normal) | |
19 | self.tree.SetItemImage(root, self.fldropenidx, | |
20 | wx.TreeItemIcon_Expanded) | |
21 | ||
22 | # Instead of adding nodes for the whole tree, just attach some | |
23 | # data to the root node so that it can find and add its child | |
24 | # nodes when it is expanded, and mark it as having children so | |
25 | # it will be expandable. | |
26 | self.tree.SetItemPyData(root, data.tree) | |
27 | self.tree.SetItemHasChildren(root, True) | |
28 | ||
29 | # Bind some interesting events | |
30 | self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnItemExpanded, self.tree) | |
31 | self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnItemCollapsed, self.tree) | |
32 | self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree) | |
33 | self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated, self.tree) | |
34 | ||
35 | self.Bind(wx.EVT_TREE_ITEM_EXPANDING, self.OnItemExpanding, self.tree) | |
36 | self.tree.Expand(root) | |
37 | ||
38 | ||
39 | def AddTreeNodes(self, parentItem): | |
40 | """ | |
41 | Add nodes for just the children of the parentItem | |
42 | """ | |
43 | items = self.tree.GetItemPyData(parentItem) | |
44 | for item in items: | |
45 | if type(item) == str: | |
46 | # a leaf node | |
47 | newItem = self.tree.AppendItem(parentItem, item) | |
48 | self.tree.SetItemImage(newItem, self.fileidx, | |
49 | wx.TreeItemIcon_Normal) | |
50 | else: | |
51 | # this item has children | |
52 | newItem = self.tree.AppendItem(parentItem, item[0]) | |
53 | self.tree.SetItemImage(newItem, self.fldridx, | |
54 | wx.TreeItemIcon_Normal) | |
55 | self.tree.SetItemImage(newItem, self.fldropenidx, | |
56 | wx.TreeItemIcon_Expanded) | |
57 | self.tree.SetItemPyData(newItem, item[1]) | |
58 | self.tree.SetItemHasChildren(newItem, True) | |
59 | ||
60 | ||
61 | ||
62 | def GetItemText(self, item): | |
63 | if item: | |
64 | return self.tree.GetItemText(item) | |
65 | else: | |
66 | return "" | |
67 | ||
68 | def OnItemExpanded(self, evt): | |
69 | print "OnItemExpanded: ", self.GetItemText(evt.GetItem()) | |
70 | ||
71 | def OnItemExpanding(self, evt): | |
72 | # When the item is about to be expanded add the first level of child nodes | |
73 | print "OnItemExpanding:", self.GetItemText(evt.GetItem()) | |
74 | self.AddTreeNodes(evt.GetItem()) | |
75 | ||
76 | def OnItemCollapsed(self, evt): | |
77 | print "OnItemCollapsed:", self.GetItemText(evt.GetItem()) | |
78 | # And remove them when collapsed as we don't need them any longer | |
79 | self.tree.DeleteChildren(evt.GetItem()) | |
80 | ||
81 | def OnSelChanged(self, evt): | |
82 | print "OnSelChanged: ", self.GetItemText(evt.GetItem()) | |
83 | ||
84 | def OnActivated(self, evt): | |
85 | print "OnActivated: ", self.GetItemText(evt.GetItem()) | |
86 | ||
87 | ||
88 | app = wx.PySimpleApp(redirect=True) | |
89 | frame = TestFrame() | |
90 | frame.Show() | |
91 | app.MainLoop() | |
92 |