6 #---------------------------------------------------------------------------
8 class MyTreeCtrl(wx
.TreeCtrl
):
9 def __init__(self
, parent
, id, pos
, size
, style
, log
):
10 wx
.TreeCtrl
.__init
__(self
, parent
, id, pos
, size
, style
)
13 def OnCompareItems(self
, item1
, item2
):
14 t1
= self
.GetItemText(item1
)
15 t2
= self
.GetItemText(item2
)
16 self
.log
.WriteText('compare: ' + t1
+ ' <> ' + t2
+ '\n')
21 #---------------------------------------------------------------------------
23 class TestTreeCtrlPanel(wx
.Panel
):
24 def __init__(self
, parent
, log
):
25 # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
26 wx
.Panel
.__init
__(self
, parent
, -1, style
=wx
.WANTS_CHARS
)
27 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
32 self
.tree
= MyTreeCtrl(self
, tID
, wx
.DefaultPosition
, wx
.DefaultSize
,
40 il
= wx
.ImageList(isz
[0], isz
[1])
41 fldridx
= il
.Add(wx
.ArtProvider_GetBitmap(wx
.ART_FOLDER
, wx
.ART_OTHER
, isz
))
42 fldropenidx
= il
.Add(wx
.ArtProvider_GetBitmap(wx
.ART_FILE_OPEN
, wx
.ART_OTHER
, isz
))
43 fileidx
= il
.Add(wx
.ArtProvider_GetBitmap(wx
.ART_REPORT_VIEW
, wx
.ART_OTHER
, isz
))
44 smileidx
= il
.Add(images
.getSmilesBitmap())
46 self
.tree
.SetImageList(il
)
49 # NOTE: For some reason tree items have to have a data object in
50 # order to be sorted. Since our compare just uses the labels
51 # we don't need any real data, so we'll just use None below for
54 self
.root
= self
.tree
.AddRoot("The Root Item")
55 self
.tree
.SetPyData(self
.root
, None)
56 self
.tree
.SetItemImage(self
.root
, fldridx
, wx
.TreeItemIcon_Normal
)
57 self
.tree
.SetItemImage(self
.root
, fldropenidx
, wx
.TreeItemIcon_Expanded
)
61 child
= self
.tree
.AppendItem(self
.root
, "Item %d" % x
)
62 self
.tree
.SetPyData(child
, None)
63 self
.tree
.SetItemImage(child
, fldridx
, wx
.TreeItemIcon_Normal
)
64 self
.tree
.SetItemImage(child
, fldropenidx
, wx
.TreeItemIcon_Expanded
)
67 last
= self
.tree
.AppendItem(child
, "item %d-%s" % (x
, chr(ord("a")+y
)))
68 self
.tree
.SetPyData(last
, None)
69 self
.tree
.SetItemImage(last
, fldridx
, wx
.TreeItemIcon_Normal
)
70 self
.tree
.SetItemImage(last
, fldropenidx
, wx
.TreeItemIcon_Expanded
)
73 item
= self
.tree
.AppendItem(last
, "item %d-%s-%d" % (x
, chr(ord("a")+y
), z
))
74 self
.tree
.SetPyData(item
, None)
75 self
.tree
.SetItemImage(item
, fileidx
, wx
.TreeItemIcon_Normal
)
76 self
.tree
.SetItemImage(item
, smileidx
, wx
.TreeItemIcon_Selected
)
78 self
.tree
.Expand(self
.root
)
79 self
.Bind(wx
.EVT_TREE_ITEM_EXPANDED
, self
.OnItemExpanded
, self
.tree
)
80 self
.Bind(wx
.EVT_TREE_ITEM_COLLAPSED
, self
.OnItemCollapsed
, self
.tree
)
81 self
.Bind(wx
.EVT_TREE_SEL_CHANGED
, self
.OnSelChanged
, self
.tree
)
82 self
.Bind(wx
.EVT_TREE_BEGIN_LABEL_EDIT
, self
.OnBeginEdit
, self
.tree
)
83 self
.Bind(wx
.EVT_TREE_END_LABEL_EDIT
, self
.OnEndEdit
, self
.tree
)
84 self
.Bind(wx
.EVT_TREE_ITEM_ACTIVATED
, self
.OnActivate
, self
.tree
)
86 self
.tree
.Bind(wx
.EVT_LEFT_DCLICK
, self
.OnLeftDClick
)
87 self
.tree
.Bind(wx
.EVT_RIGHT_DOWN
, self
.OnRightClick
)
88 self
.tree
.Bind(wx
.EVT_RIGHT_UP
, self
.OnRightUp
)
92 def OnRightClick(self
, event
):
93 pt
= event
.GetPosition();
94 item
, flags
= self
.tree
.HitTest(pt
)
95 self
.log
.WriteText("OnRightClick: %s, %s, %s\n" %
96 (self
.tree
.GetItemText(item
), type(item
), item
.__class
__))
97 self
.tree
.SelectItem(item
)
100 def OnRightUp(self
, event
):
101 pt
= event
.GetPosition();
102 item
, flags
= self
.tree
.HitTest(pt
)
103 self
.log
.WriteText("OnRightUp: %s (manually starting label edit)\n"
104 % self
.tree
.GetItemText(item
))
105 self
.tree
.EditLabel(item
)
109 def OnBeginEdit(self
, event
):
110 self
.log
.WriteText("OnBeginEdit\n")
111 # show how to prevent edit...
112 if self
.tree
.GetItemText(event
.GetItem()) == "The Root Item":
114 self
.log
.WriteText("You can't edit this one...\n")
116 # Lets just see what's visible of its children
118 root
= event
.GetItem()
119 (child
, cookie
) = self
.tree
.GetFirstChild(root
)
122 self
.log
.WriteText("Child [%s] visible = %d" %
123 (self
.tree
.GetItemText(child
),
124 self
.tree
.IsVisible(child
)))
125 (child
, cookie
) = self
.tree
.GetNextChild(root
, cookie
)
130 def OnEndEdit(self
, event
):
131 self
.log
.WriteText("OnEndEdit\n")
132 # show how to reject edit, we'll not allow any digits
133 for x
in event
.GetLabel():
134 if x
in string
.digits
:
135 self
.log
.WriteText("You can't enter digits...\n")
140 def OnLeftDClick(self
, event
):
141 pt
= event
.GetPosition();
142 item
, flags
= self
.tree
.HitTest(pt
)
143 self
.log
.WriteText("OnLeftDClick: %s\n" % self
.tree
.GetItemText(item
))
144 parent
= self
.tree
.GetItemParent(item
)
145 self
.tree
.SortChildren(parent
)
149 def OnSize(self
, event
):
150 w
,h
= self
.GetClientSizeTuple()
151 self
.tree
.SetDimensions(0, 0, w
, h
)
154 def OnItemExpanded(self
, event
):
155 item
= event
.GetItem()
156 self
.log
.WriteText("OnItemExpanded: %s\n" % self
.tree
.GetItemText(item
))
158 def OnItemCollapsed(self
, event
):
159 item
= event
.GetItem()
160 self
.log
.WriteText("OnItemCollapsed: %s\n" % self
.tree
.GetItemText(item
))
162 def OnSelChanged(self
, event
):
163 self
.item
= event
.GetItem()
164 self
.log
.WriteText("OnSelChanged: %s\n" % self
.tree
.GetItemText(self
.item
))
165 if wx
.Platform
== '__WXMSW__':
166 self
.log
.WriteText("BoundingRect: %s\n" %
167 self
.tree
.GetBoundingRect(self
.item
, True))
168 #items = self.tree.GetSelections()
169 #print map(self.tree.GetItemText, items)
173 def OnActivate(self
, event
):
174 self
.log
.WriteText("OnActivate: %s\n" % self
.tree
.GetItemText(self
.item
))
177 #---------------------------------------------------------------------------
179 def runTest(frame
, nb
, log
):
180 win
= TestTreeCtrlPanel(nb
, log
)
183 #---------------------------------------------------------------------------
190 A TreeCtrl presents information as a hierarchy, with items that may be
191 expanded to show further items. Items in a tree control are referenced by
192 wx.TreeItemId handles.
198 if __name__
== '__main__':
201 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])