]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/TreeCtrl.py
style required on wxGTK
[wxWidgets.git] / wxPython / demo / TreeCtrl.py
1
2 import string
3 import wx
4 import images
5
6 #---------------------------------------------------------------------------
7
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)
11 self.log = log
12
13 def OnCompareItems(self, item1, item2):
14 t1 = self.GetItemText(item1)
15 t2 = self.GetItemText(item2)
16 self.log.WriteText('compare: ' + t1 + ' <> ' + t2 + '\n')
17 if t1 < t2: return -1
18 if t1 == t2: return 0
19 return 1
20
21 #---------------------------------------------------------------------------
22
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)
28
29 self.log = log
30 tID = wx.NewId()
31
32 self.tree = MyTreeCtrl(self, tID, wx.DefaultPosition, wx.DefaultSize,
33 wx.TR_HAS_BUTTONS
34 | wx.TR_EDIT_LABELS
35 #| wx.TR_MULTIPLE
36 #| wx.TR_HIDE_ROOT
37 , self.log)
38
39 isz = (16,16)
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_NORMAL_FILE, wx.ART_OTHER, isz))
44 smileidx = il.Add(images.getSmilesBitmap())
45
46 self.tree.SetImageList(il)
47 self.il = il
48
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
52 # the item data.
53
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)
58
59
60 for x in range(15):
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)
65
66 for y in range(5):
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)
71
72 for z in range(5):
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)
77
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)
85
86 self.tree.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick)
87 self.tree.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
88 self.tree.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
89
90
91 def OnRightDown(self, event):
92 pt = event.GetPosition();
93 item, flags = self.tree.HitTest(pt)
94 if item:
95 self.log.WriteText("OnRightClick: %s, %s, %s\n" %
96 (self.tree.GetItemText(item), type(item), item.__class__))
97 self.tree.SelectItem(item)
98
99
100 def OnRightUp(self, event):
101 pt = event.GetPosition();
102 item, flags = self.tree.HitTest(pt)
103 if item:
104 self.log.WriteText("OnRightUp: %s (manually starting label edit)\n"
105 % self.tree.GetItemText(item))
106 self.tree.EditLabel(item)
107
108
109
110 def OnBeginEdit(self, event):
111 self.log.WriteText("OnBeginEdit\n")
112 # show how to prevent edit...
113 item = event.GetItem()
114 if item and self.tree.GetItemText(item) == "The Root Item":
115 wx.Bell()
116 self.log.WriteText("You can't edit this one...\n")
117
118 # Lets just see what's visible of its children
119 cookie = 0
120 root = event.GetItem()
121 (child, cookie) = self.tree.GetFirstChild(root)
122
123 while child.IsOk():
124 self.log.WriteText("Child [%s] visible = %d" %
125 (self.tree.GetItemText(child),
126 self.tree.IsVisible(child)))
127 (child, cookie) = self.tree.GetNextChild(root, cookie)
128
129 event.Veto()
130
131
132 def OnEndEdit(self, event):
133 self.log.WriteText("OnEndEdit: %s %s\n" %
134 (event.IsEditCancelled(), event.GetLabel()) )
135 # show how to reject edit, we'll not allow any digits
136 for x in event.GetLabel():
137 if x in string.digits:
138 self.log.WriteText("You can't enter digits...\n")
139 event.Veto()
140 return
141
142
143 def OnLeftDClick(self, event):
144 pt = event.GetPosition();
145 item, flags = self.tree.HitTest(pt)
146 if item:
147 self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item))
148 parent = self.tree.GetItemParent(item)
149 if parent.IsOk():
150 self.tree.SortChildren(parent)
151 event.Skip()
152
153
154 def OnSize(self, event):
155 w,h = self.GetClientSizeTuple()
156 self.tree.SetDimensions(0, 0, w, h)
157
158
159 def OnItemExpanded(self, event):
160 item = event.GetItem()
161 if item:
162 self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
163
164 def OnItemCollapsed(self, event):
165 item = event.GetItem()
166 if item:
167 self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
168
169 def OnSelChanged(self, event):
170 self.item = event.GetItem()
171 if self.item:
172 self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item))
173 if wx.Platform == '__WXMSW__':
174 self.log.WriteText("BoundingRect: %s\n" %
175 self.tree.GetBoundingRect(self.item, True))
176 #items = self.tree.GetSelections()
177 #print map(self.tree.GetItemText, items)
178 event.Skip()
179
180
181 def OnActivate(self, event):
182 if self.item:
183 self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item))
184
185
186 #---------------------------------------------------------------------------
187
188 def runTest(frame, nb, log):
189 win = TestTreeCtrlPanel(nb, log)
190 return win
191
192 #---------------------------------------------------------------------------
193
194
195
196
197
198 overview = """\
199 A TreeCtrl presents information as a hierarchy, with items that may be
200 expanded to show further items. Items in a tree control are referenced by
201 wx.TreeItemId handles.
202
203 """
204
205
206
207 if __name__ == '__main__':
208 import sys,os
209 import run
210 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
211