]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTreeCtrl.py
A little tweak to the usage text
[wxWidgets.git] / wxPython / demo / wxTreeCtrl.py
1 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 import string
7 import wx
8 import images
9
10 #---------------------------------------------------------------------------
11
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)
15 self.log = log
16
17 def OnCompareItems(self, item1, item2):
18 t1 = self.GetItemText(item1)
19 t2 = self.GetItemText(item2)
20 self.log.WriteText('compare: ' + t1 + ' <> ' + t2 + '\n')
21 if t1 < t2: return -1
22 if t1 == t2: return 0
23 return 1
24
25 #---------------------------------------------------------------------------
26
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)
32
33 self.log = log
34 tID = wx.NewId()
35
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
41 , self.log)
42
43 isz = (16,16)
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())
49
50 self.tree.SetImageList(il)
51 self.il = il
52
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
56 # the item data.
57
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)
62
63
64 for x in range(15):
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)
69
70 for y in range(5):
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)
75
76 for z in range(5):
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)
81
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)
89
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)
93
94
95
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)
102
103
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)
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":
117 wx.Bell()
118 self.log.WriteText("You can't edit this one...\n")
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)
124
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
131 event.Veto()
132
133
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
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)
150 event.Skip()
151
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):
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)
174 event.Skip()
175
176
177 def OnActivate(self, event):
178 self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item))
179
180
181 #---------------------------------------------------------------------------
182
183 def runTest(frame, nb, log):
184 win = TestTreeCtrlPanel(nb, log)
185 return win
186
187 #---------------------------------------------------------------------------
188
189
190
191
192
193 overview = """\
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.
197
198 """
199
200
201
202 if __name__ == '__main__':
203 import sys,os
204 import run
205 run.main(['', os.path.basename(sys.argv[0])])
206