]>
Commit | Line | Data |
---|---|---|
cf694132 | 1 | |
8fa876ca RD |
2 | import string |
3 | import wx | |
4 | import images | |
8bf5d46e | 5 | |
cf694132 RD |
6 | #--------------------------------------------------------------------------- |
7 | ||
8fa876ca | 8 | class MyTreeCtrl(wx.TreeCtrl): |
3b36695d | 9 | def __init__(self, parent, id, pos, size, style, log): |
8fa876ca | 10 | wx.TreeCtrl.__init__(self, parent, id, pos, size, style) |
3b36695d | 11 | self.log = log |
f6bcfd97 BP |
12 | |
13 | def OnCompareItems(self, item1, item2): | |
14 | t1 = self.GetItemText(item1) | |
15 | t2 = self.GetItemText(item2) | |
9416aa89 | 16 | self.log.WriteText('compare: ' + t1 + ' <> ' + t2 + '\n') |
f6bcfd97 BP |
17 | if t1 < t2: return -1 |
18 | if t1 == t2: return 0 | |
19 | return 1 | |
20 | ||
21 | #--------------------------------------------------------------------------- | |
22 | ||
8fa876ca | 23 | class TestTreeCtrlPanel(wx.Panel): |
cf694132 | 24 | def __init__(self, parent, log): |
c368d904 | 25 | # Use the WANTS_CHARS style so the panel doesn't eat the Return key. |
8fa876ca RD |
26 | wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS) |
27 | self.Bind(wx.EVT_SIZE, self.OnSize) | |
cf694132 RD |
28 | |
29 | self.log = log | |
8fa876ca | 30 | tID = wx.NewId() |
cf694132 | 31 | |
8fa876ca RD |
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 | |
3b36695d | 37 | , self.log) |
8bf5d46e | 38 | |
1e4a197e | 39 | isz = (16,16) |
8fa876ca RD |
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)) | |
1e4a197e | 44 | smileidx = il.Add(images.getSmilesBitmap()) |
9416aa89 | 45 | |
1e4a197e RD |
46 | self.tree.SetImageList(il) |
47 | self.il = il | |
9b3d3bc4 | 48 | |
f6bcfd97 BP |
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 | |
1e4a197e RD |
51 | # we don't need any real data, so we'll just use None below for |
52 | # the item data. | |
f6bcfd97 | 53 | |
8bf5d46e | 54 | self.root = self.tree.AddRoot("The Root Item") |
f6bcfd97 | 55 | self.tree.SetPyData(self.root, None) |
8fa876ca RD |
56 | self.tree.SetItemImage(self.root, fldridx, wx.TreeItemIcon_Normal) |
57 | self.tree.SetItemImage(self.root, fldropenidx, wx.TreeItemIcon_Expanded) | |
1e4a197e | 58 | |
9b3d3bc4 | 59 | |
cf694132 | 60 | for x in range(15): |
8bf5d46e | 61 | child = self.tree.AppendItem(self.root, "Item %d" % x) |
f6bcfd97 | 62 | self.tree.SetPyData(child, None) |
8fa876ca RD |
63 | self.tree.SetItemImage(child, fldridx, wx.TreeItemIcon_Normal) |
64 | self.tree.SetItemImage(child, fldropenidx, wx.TreeItemIcon_Expanded) | |
65 | ||
cf694132 RD |
66 | for y in range(5): |
67 | last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y))) | |
f6bcfd97 | 68 | self.tree.SetPyData(last, None) |
8fa876ca RD |
69 | self.tree.SetItemImage(last, fldridx, wx.TreeItemIcon_Normal) |
70 | self.tree.SetItemImage(last, fldropenidx, wx.TreeItemIcon_Expanded) | |
71 | ||
cf694132 | 72 | for z in range(5): |
f6bcfd97 BP |
73 | item = self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z)) |
74 | self.tree.SetPyData(item, None) | |
8fa876ca RD |
75 | self.tree.SetItemImage(item, fileidx, wx.TreeItemIcon_Normal) |
76 | self.tree.SetItemImage(item, smileidx, wx.TreeItemIcon_Selected) | |
cf694132 | 77 | |
8bf5d46e | 78 | self.tree.Expand(self.root) |
8fa876ca RD |
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) | |
cf694132 | 85 | |
8fa876ca RD |
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) | |
bb0054cd | 89 | |
f6bcfd97 | 90 | |
3115ef3e | 91 | |
bb0054cd | 92 | def OnRightClick(self, event): |
b1462dfa | 93 | pt = event.GetPosition(); |
f6bcfd97 | 94 | item, flags = self.tree.HitTest(pt) |
bd658b8c RD |
95 | self.log.WriteText("OnRightClick: %s, %s, %s\n" % |
96 | (self.tree.GetItemText(item), type(item), item.__class__)) | |
bb0054cd RD |
97 | self.tree.SelectItem(item) |
98 | ||
f6bcfd97 | 99 | |
bb0054cd | 100 | def OnRightUp(self, event): |
b1462dfa | 101 | pt = event.GetPosition(); |
f6bcfd97 | 102 | item, flags = self.tree.HitTest(pt) |
8bf5d46e RD |
103 | self.log.WriteText("OnRightUp: %s (manually starting label edit)\n" |
104 | % self.tree.GetItemText(item)) | |
105 | self.tree.EditLabel(item) | |
106 | ||
107 | ||
108 | ||
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": | |
8fa876ca | 113 | wx.Bell() |
8bf5d46e | 114 | self.log.WriteText("You can't edit this one...\n") |
1e4a197e RD |
115 | |
116 | # Lets just see what's visible of its children | |
117 | cookie = 0 | |
118 | root = event.GetItem() | |
58be4fb1 | 119 | (child, cookie) = self.tree.GetFirstChild(root) |
8fa876ca | 120 | |
1e4a197e RD |
121 | while child.IsOk(): |
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) | |
126 | ||
8bf5d46e RD |
127 | event.Veto() |
128 | ||
1e4a197e | 129 | |
8bf5d46e | 130 | def OnEndEdit(self, event): |
ce6780cf RD |
131 | self.log.WriteText("OnEndEdit: %s %s\n" % |
132 | (event.IsEditCancelled(), event.GetLabel()) ) | |
8bf5d46e RD |
133 | # show how to reject edit, we'll not allow any digits |
134 | for x in event.GetLabel(): | |
135 | if x in string.digits: | |
136 | self.log.WriteText("You can't enter digits...\n") | |
137 | event.Veto() | |
138 | return | |
139 | ||
140 | ||
bb0054cd | 141 | def OnLeftDClick(self, event): |
b1462dfa | 142 | pt = event.GetPosition(); |
f6bcfd97 | 143 | item, flags = self.tree.HitTest(pt) |
bb0054cd | 144 | self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item)) |
f6bcfd97 BP |
145 | parent = self.tree.GetItemParent(item) |
146 | self.tree.SortChildren(parent) | |
147 | event.Skip() | |
bb0054cd | 148 | |
cf694132 RD |
149 | |
150 | def OnSize(self, event): | |
151 | w,h = self.GetClientSizeTuple() | |
152 | self.tree.SetDimensions(0, 0, w, h) | |
153 | ||
154 | ||
155 | def OnItemExpanded(self, event): | |
156 | item = event.GetItem() | |
157 | self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item)) | |
158 | ||
159 | def OnItemCollapsed(self, event): | |
160 | item = event.GetItem() | |
161 | self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item)) | |
162 | ||
163 | def OnSelChanged(self, event): | |
8bf5d46e RD |
164 | self.item = event.GetItem() |
165 | self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item)) | |
8fa876ca | 166 | if wx.Platform == '__WXMSW__': |
c368d904 | 167 | self.log.WriteText("BoundingRect: %s\n" % |
1e4a197e | 168 | self.tree.GetBoundingRect(self.item, True)) |
c368d904 RD |
169 | #items = self.tree.GetSelections() |
170 | #print map(self.tree.GetItemText, items) | |
171 | event.Skip() | |
172 | ||
164b735b | 173 | |
1fded56b | 174 | def OnActivate(self, event): |
c368d904 | 175 | self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item)) |
cf694132 | 176 | |
f6bcfd97 | 177 | |
cf694132 RD |
178 | #--------------------------------------------------------------------------- |
179 | ||
180 | def runTest(frame, nb, log): | |
181 | win = TestTreeCtrlPanel(nb, log) | |
182 | return win | |
183 | ||
184 | #--------------------------------------------------------------------------- | |
185 | ||
186 | ||
187 | ||
188 | ||
189 | ||
3115ef3e | 190 | overview = """\ |
95bfd958 | 191 | A TreeCtrl presents information as a hierarchy, with items that may be |
8fa876ca | 192 | expanded to show further items. Items in a tree control are referenced by |
95bfd958 | 193 | wx.TreeItemId handles. |
cf694132 | 194 | |
3115ef3e | 195 | """ |
cf694132 RD |
196 | |
197 | ||
198 | ||
3115ef3e RD |
199 | if __name__ == '__main__': |
200 | import sys,os | |
201 | import run | |
8eca4fef | 202 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
cf694132 | 203 |