1 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
10 #---------------------------------------------------------------------------
12 class MyTreeCtrl(wx
.TreeCtrl
):
13 def __init__(self
, parent
, id, pos
, size
, style
, log
):
14 wx
.TreeCtrl
.__init
__(self
, parent
, id, pos
, size
, style
)
17 def OnCompareItems(self
, item1
, item2
):
18 t1
= self
.GetItemText(item1
)
19 t2
= self
.GetItemText(item2
)
20 self
.log
.WriteText('compare: ' + t1
+ ' <> ' + t2
+ '\n')
25 #---------------------------------------------------------------------------
27 class TestTreeCtrlPanel(wx
.Panel
):
28 def __init__(self
, parent
, log
):
29 # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
30 wx
.Panel
.__init
__(self
, parent
, -1, style
=wx
.WANTS_CHARS
)
31 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
36 self
.tree
= MyTreeCtrl(self
, tID
, wx
.DefaultPosition
, wx
.DefaultSize
,
44 il
= wx
.ImageList(isz
[0], isz
[1])
45 fldridx
= il
.Add(wx
.ArtProvider_GetBitmap(wx
.ART_FOLDER
, wx
.ART_OTHER
, isz
))
46 fldropenidx
= il
.Add(wx
.ArtProvider_GetBitmap(wx
.ART_FILE_OPEN
, wx
.ART_OTHER
, isz
))
47 fileidx
= il
.Add(wx
.ArtProvider_GetBitmap(wx
.ART_REPORT_VIEW
, wx
.ART_OTHER
, isz
))
48 smileidx
= il
.Add(images
.getSmilesBitmap())
50 self
.tree
.SetImageList(il
)
53 # NOTE: For some reason tree items have to have a data object in
54 # order to be sorted. Since our compare just uses the labels
55 # we don't need any real data, so we'll just use None below for
58 self
.root
= self
.tree
.AddRoot("The Root Item")
59 self
.tree
.SetPyData(self
.root
, None)
60 self
.tree
.SetItemImage(self
.root
, fldridx
, wx
.TreeItemIcon_Normal
)
61 self
.tree
.SetItemImage(self
.root
, fldropenidx
, wx
.TreeItemIcon_Expanded
)
65 child
= self
.tree
.AppendItem(self
.root
, "Item %d" % x
)
66 self
.tree
.SetPyData(child
, None)
67 self
.tree
.SetItemImage(child
, fldridx
, wx
.TreeItemIcon_Normal
)
68 self
.tree
.SetItemImage(child
, fldropenidx
, wx
.TreeItemIcon_Expanded
)
71 last
= self
.tree
.AppendItem(child
, "item %d-%s" % (x
, chr(ord("a")+y
)))
72 self
.tree
.SetPyData(last
, None)
73 self
.tree
.SetItemImage(last
, fldridx
, wx
.TreeItemIcon_Normal
)
74 self
.tree
.SetItemImage(last
, fldropenidx
, wx
.TreeItemIcon_Expanded
)
77 item
= self
.tree
.AppendItem(last
, "item %d-%s-%d" % (x
, chr(ord("a")+y
), z
))
78 self
.tree
.SetPyData(item
, None)
79 self
.tree
.SetItemImage(item
, fileidx
, wx
.TreeItemIcon_Normal
)
80 self
.tree
.SetItemImage(item
, smileidx
, wx
.TreeItemIcon_Selected
)
82 self
.tree
.Expand(self
.root
)
83 self
.Bind(wx
.EVT_TREE_ITEM_EXPANDED
, self
.OnItemExpanded
, self
.tree
)
84 self
.Bind(wx
.EVT_TREE_ITEM_COLLAPSED
, self
.OnItemCollapsed
, self
.tree
)
85 self
.Bind(wx
.EVT_TREE_SEL_CHANGED
, self
.OnSelChanged
, self
.tree
)
86 self
.Bind(wx
.EVT_TREE_BEGIN_LABEL_EDIT
, self
.OnBeginEdit
, self
.tree
)
87 self
.Bind(wx
.EVT_TREE_END_LABEL_EDIT
, self
.OnEndEdit
, self
.tree
)
88 self
.Bind(wx
.EVT_TREE_ITEM_ACTIVATED
, self
.OnActivate
, self
.tree
)
90 self
.tree
.Bind(wx
.EVT_LEFT_DCLICK
, self
.OnLeftDClick
)
91 self
.tree
.Bind(wx
.EVT_RIGHT_DOWN
, self
.OnRightClick
)
92 self
.tree
.Bind(wx
.EVT_RIGHT_UP
, self
.OnRightUp
)
96 def OnRightClick(self
, event
):
97 pt
= event
.GetPosition();
98 item
, flags
= self
.tree
.HitTest(pt
)
99 self
.log
.WriteText("OnRightClick: %s, %s, %s\n" %
100 (self
.tree
.GetItemText(item
), type(item
), item
.__class
__))
101 self
.tree
.SelectItem(item
)
104 def OnRightUp(self
, event
):
105 pt
= event
.GetPosition();
106 item
, flags
= self
.tree
.HitTest(pt
)
107 self
.log
.WriteText("OnRightUp: %s (manually starting label edit)\n"
108 % self
.tree
.GetItemText(item
))
109 self
.tree
.EditLabel(item
)
113 def OnBeginEdit(self
, event
):
114 self
.log
.WriteText("OnBeginEdit\n")
115 # show how to prevent edit...
116 if self
.tree
.GetItemText(event
.GetItem()) == "The Root Item":
118 self
.log
.WriteText("You can't edit this one...\n")
120 # Lets just see what's visible of its children
122 root
= event
.GetItem()
123 (child
, cookie
) = self
.tree
.GetFirstChild(root
, cookie
)
126 self
.log
.WriteText("Child [%s] visible = %d" %
127 (self
.tree
.GetItemText(child
),
128 self
.tree
.IsVisible(child
)))
129 (child
, cookie
) = self
.tree
.GetNextChild(root
, cookie
)
134 def OnEndEdit(self
, event
):
135 self
.log
.WriteText("OnEndEdit\n")
136 # show how to reject edit, we'll not allow any digits
137 for x
in event
.GetLabel():
138 if x
in string
.digits
:
139 self
.log
.WriteText("You can't enter digits...\n")
144 def OnLeftDClick(self
, event
):
145 pt
= event
.GetPosition();
146 item
, flags
= self
.tree
.HitTest(pt
)
147 self
.log
.WriteText("OnLeftDClick: %s\n" % self
.tree
.GetItemText(item
))
148 parent
= self
.tree
.GetItemParent(item
)
149 self
.tree
.SortChildren(parent
)
153 def OnSize(self
, event
):
154 w
,h
= self
.GetClientSizeTuple()
155 self
.tree
.SetDimensions(0, 0, w
, h
)
158 def OnItemExpanded(self
, event
):
159 item
= event
.GetItem()
160 self
.log
.WriteText("OnItemExpanded: %s\n" % self
.tree
.GetItemText(item
))
162 def OnItemCollapsed(self
, event
):
163 item
= event
.GetItem()
164 self
.log
.WriteText("OnItemCollapsed: %s\n" % self
.tree
.GetItemText(item
))
166 def OnSelChanged(self
, event
):
167 self
.item
= event
.GetItem()
168 self
.log
.WriteText("OnSelChanged: %s\n" % self
.tree
.GetItemText(self
.item
))
169 if wx
.Platform
== '__WXMSW__':
170 self
.log
.WriteText("BoundingRect: %s\n" %
171 self
.tree
.GetBoundingRect(self
.item
, True))
172 #items = self.tree.GetSelections()
173 #print map(self.tree.GetItemText, items)
177 def OnActivate(self
, event
):
178 self
.log
.WriteText("OnActivate: %s\n" % self
.tree
.GetItemText(self
.item
))
181 #---------------------------------------------------------------------------
183 def runTest(frame
, nb
, log
):
184 win
= TestTreeCtrlPanel(nb
, log
)
187 #---------------------------------------------------------------------------
194 A tree control presents information as a hierarchy, with items that may be
195 expanded to show further items. Items in a tree control are referenced by
196 wxTreeItemId handles.
202 if __name__
== '__main__':
205 run
.main(['', os
.path
.basename(sys
.argv
[0])])