]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxTreeCtrl.py
Applied patch [ 827011 ] Event-based processing of item tooltips in wxTreeCtrl
[wxWidgets.git] / wxPython / demo / wxTreeCtrl.py
CommitLineData
8fa876ca
RD
1# 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2#
3# o Updated for wx namespace
4#
cf694132 5
8fa876ca
RD
6import string
7import wx
8import images
8bf5d46e 9
cf694132
RD
10#---------------------------------------------------------------------------
11
8fa876ca 12class MyTreeCtrl(wx.TreeCtrl):
3b36695d 13 def __init__(self, parent, id, pos, size, style, log):
8fa876ca 14 wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
3b36695d 15 self.log = log
f6bcfd97
BP
16
17 def OnCompareItems(self, item1, item2):
18 t1 = self.GetItemText(item1)
19 t2 = self.GetItemText(item2)
9416aa89 20 self.log.WriteText('compare: ' + t1 + ' <> ' + t2 + '\n')
f6bcfd97
BP
21 if t1 < t2: return -1
22 if t1 == t2: return 0
23 return 1
24
25#---------------------------------------------------------------------------
26
8fa876ca 27class TestTreeCtrlPanel(wx.Panel):
cf694132 28 def __init__(self, parent, log):
c368d904 29 # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
8fa876ca
RD
30 wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
31 self.Bind(wx.EVT_SIZE, self.OnSize)
cf694132
RD
32
33 self.log = log
8fa876ca 34 tID = wx.NewId()
cf694132 35
8fa876ca
RD
36 self.tree = MyTreeCtrl(self, tID, wx.DefaultPosition, wx.DefaultSize,
37 wx.TR_HAS_BUTTONS
38 | wx.TR_EDIT_LABELS
39 #| wx.TR_MULTIPLE
40 #| wx.TR_HIDE_ROOT
3b36695d 41 , self.log)
8bf5d46e 42
1e4a197e 43 isz = (16,16)
8fa876ca
RD
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))
1e4a197e 48 smileidx = il.Add(images.getSmilesBitmap())
9416aa89 49
1e4a197e
RD
50 self.tree.SetImageList(il)
51 self.il = il
9b3d3bc4 52
f6bcfd97
BP
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
1e4a197e
RD
55 # we don't need any real data, so we'll just use None below for
56 # the item data.
f6bcfd97 57
8bf5d46e 58 self.root = self.tree.AddRoot("The Root Item")
f6bcfd97 59 self.tree.SetPyData(self.root, None)
8fa876ca
RD
60 self.tree.SetItemImage(self.root, fldridx, wx.TreeItemIcon_Normal)
61 self.tree.SetItemImage(self.root, fldropenidx, wx.TreeItemIcon_Expanded)
1e4a197e 62
9b3d3bc4 63
cf694132 64 for x in range(15):
8bf5d46e 65 child = self.tree.AppendItem(self.root, "Item %d" % x)
f6bcfd97 66 self.tree.SetPyData(child, None)
8fa876ca
RD
67 self.tree.SetItemImage(child, fldridx, wx.TreeItemIcon_Normal)
68 self.tree.SetItemImage(child, fldropenidx, wx.TreeItemIcon_Expanded)
69
cf694132
RD
70 for y in range(5):
71 last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y)))
f6bcfd97 72 self.tree.SetPyData(last, None)
8fa876ca
RD
73 self.tree.SetItemImage(last, fldridx, wx.TreeItemIcon_Normal)
74 self.tree.SetItemImage(last, fldropenidx, wx.TreeItemIcon_Expanded)
75
cf694132 76 for z in range(5):
f6bcfd97
BP
77 item = self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z))
78 self.tree.SetPyData(item, None)
8fa876ca
RD
79 self.tree.SetItemImage(item, fileidx, wx.TreeItemIcon_Normal)
80 self.tree.SetItemImage(item, smileidx, wx.TreeItemIcon_Selected)
cf694132 81
8bf5d46e 82 self.tree.Expand(self.root)
8fa876ca
RD
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)
cf694132 89
8fa876ca
RD
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)
bb0054cd 93
f6bcfd97 94
3115ef3e 95
bb0054cd 96 def OnRightClick(self, event):
b1462dfa 97 pt = event.GetPosition();
f6bcfd97 98 item, flags = self.tree.HitTest(pt)
bd658b8c
RD
99 self.log.WriteText("OnRightClick: %s, %s, %s\n" %
100 (self.tree.GetItemText(item), type(item), item.__class__))
bb0054cd
RD
101 self.tree.SelectItem(item)
102
f6bcfd97 103
bb0054cd 104 def OnRightUp(self, event):
b1462dfa 105 pt = event.GetPosition();
f6bcfd97 106 item, flags = self.tree.HitTest(pt)
8bf5d46e
RD
107 self.log.WriteText("OnRightUp: %s (manually starting label edit)\n"
108 % self.tree.GetItemText(item))
109 self.tree.EditLabel(item)
110
111
112
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":
8fa876ca 117 wx.Bell()
8bf5d46e 118 self.log.WriteText("You can't edit this one...\n")
1e4a197e
RD
119
120 # Lets just see what's visible of its children
121 cookie = 0
122 root = event.GetItem()
123 (child, cookie) = self.tree.GetFirstChild(root, cookie)
8fa876ca 124
1e4a197e
RD
125 while child.IsOk():
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)
130
8bf5d46e
RD
131 event.Veto()
132
1e4a197e 133
8bf5d46e
RD
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")
140 event.Veto()
141 return
142
143
bb0054cd 144 def OnLeftDClick(self, event):
b1462dfa 145 pt = event.GetPosition();
f6bcfd97 146 item, flags = self.tree.HitTest(pt)
bb0054cd 147 self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item))
f6bcfd97
BP
148 parent = self.tree.GetItemParent(item)
149 self.tree.SortChildren(parent)
150 event.Skip()
bb0054cd 151
cf694132
RD
152
153 def OnSize(self, event):
154 w,h = self.GetClientSizeTuple()
155 self.tree.SetDimensions(0, 0, w, h)
156
157
158 def OnItemExpanded(self, event):
159 item = event.GetItem()
160 self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
161
162 def OnItemCollapsed(self, event):
163 item = event.GetItem()
164 self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
165
166 def OnSelChanged(self, event):
8bf5d46e
RD
167 self.item = event.GetItem()
168 self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item))
8fa876ca 169 if wx.Platform == '__WXMSW__':
c368d904 170 self.log.WriteText("BoundingRect: %s\n" %
1e4a197e 171 self.tree.GetBoundingRect(self.item, True))
c368d904
RD
172 #items = self.tree.GetSelections()
173 #print map(self.tree.GetItemText, items)
174 event.Skip()
175
164b735b 176
1fded56b 177 def OnActivate(self, event):
c368d904 178 self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item))
cf694132 179
f6bcfd97 180
cf694132
RD
181#---------------------------------------------------------------------------
182
183def runTest(frame, nb, log):
184 win = TestTreeCtrlPanel(nb, log)
185 return win
186
187#---------------------------------------------------------------------------
188
189
190
191
192
3115ef3e 193overview = """\
8fa876ca
RD
194A tree control presents information as a hierarchy, with items that may be
195expanded to show further items. Items in a tree control are referenced by
196wxTreeItemId handles.
cf694132 197
3115ef3e 198"""
cf694132
RD
199
200
201
3115ef3e
RD
202if __name__ == '__main__':
203 import sys,os
204 import run
205 run.main(['', os.path.basename(sys.argv[0])])
cf694132 206