]>
Commit | Line | Data |
---|---|---|
cf694132 RD |
1 | |
2 | from wxPython.wx import * | |
1e4a197e | 3 | import images |
8bf5d46e RD |
4 | import string |
5 | ||
cf694132 RD |
6 | #--------------------------------------------------------------------------- |
7 | ||
f6bcfd97 | 8 | class MyTreeCtrl(wxTreeCtrl): |
3b36695d | 9 | def __init__(self, parent, id, pos, size, style, log): |
f6bcfd97 | 10 | wxTreeCtrl.__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 | ||
cf694132 RD |
23 | class TestTreeCtrlPanel(wxPanel): |
24 | def __init__(self, parent, log): | |
c368d904 RD |
25 | # Use the WANTS_CHARS style so the panel doesn't eat the Return key. |
26 | wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS) | |
f6bcfd97 | 27 | EVT_SIZE(self, self.OnSize) |
cf694132 RD |
28 | |
29 | self.log = log | |
1e4a197e | 30 | tID = wxNewId() |
cf694132 | 31 | |
f6bcfd97 | 32 | self.tree = MyTreeCtrl(self, tID, wxDefaultPosition, wxDefaultSize, |
1e4a197e RD |
33 | wxTR_HAS_BUTTONS |
34 | | wxTR_EDIT_LABELS | |
35 | #| wxTR_MULTIPLE | |
63b6646e | 36 | #| wxTR_HIDE_ROOT |
3b36695d | 37 | , self.log) |
8bf5d46e | 38 | |
1e4a197e RD |
39 | isz = (16,16) |
40 | il = wxImageList(isz[0], isz[1]) | |
41 | fldridx = il.Add(wxArtProvider_GetBitmap(wxART_FOLDER, wxART_OTHER, isz)) | |
42 | fldropenidx = il.Add(wxArtProvider_GetBitmap(wxART_FILE_OPEN, wxART_OTHER, isz)) | |
43 | fileidx = il.Add(wxArtProvider_GetBitmap(wxART_REPORT_VIEW, wxART_OTHER, isz)) | |
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) |
1e4a197e RD |
56 | self.tree.SetItemImage(self.root, fldridx, wxTreeItemIcon_Normal) |
57 | self.tree.SetItemImage(self.root, fldropenidx, wxTreeItemIcon_Expanded) | |
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) |
1e4a197e RD |
63 | self.tree.SetItemImage(child, fldridx, wxTreeItemIcon_Normal) |
64 | self.tree.SetItemImage(child, fldropenidx, wxTreeItemIcon_Expanded) | |
cf694132 RD |
65 | for y in range(5): |
66 | last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y))) | |
f6bcfd97 | 67 | self.tree.SetPyData(last, None) |
1e4a197e RD |
68 | self.tree.SetItemImage(last, fldridx, wxTreeItemIcon_Normal) |
69 | self.tree.SetItemImage(last, fldropenidx, wxTreeItemIcon_Expanded) | |
cf694132 | 70 | for z in range(5): |
f6bcfd97 BP |
71 | item = self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z)) |
72 | self.tree.SetPyData(item, None) | |
1e4a197e RD |
73 | self.tree.SetItemImage(item, fileidx, wxTreeItemIcon_Normal) |
74 | self.tree.SetItemImage(item, smileidx, wxTreeItemIcon_Selected) | |
cf694132 | 75 | |
8bf5d46e | 76 | self.tree.Expand(self.root) |
cf694132 RD |
77 | EVT_TREE_ITEM_EXPANDED (self, tID, self.OnItemExpanded) |
78 | EVT_TREE_ITEM_COLLAPSED (self, tID, self.OnItemCollapsed) | |
79 | EVT_TREE_SEL_CHANGED (self, tID, self.OnSelChanged) | |
8bf5d46e RD |
80 | EVT_TREE_BEGIN_LABEL_EDIT(self, tID, self.OnBeginEdit) |
81 | EVT_TREE_END_LABEL_EDIT (self, tID, self.OnEndEdit) | |
c368d904 | 82 | EVT_TREE_ITEM_ACTIVATED (self, tID, self.OnActivate) |
cf694132 | 83 | |
bb0054cd RD |
84 | EVT_LEFT_DCLICK(self.tree, self.OnLeftDClick) |
85 | EVT_RIGHT_DOWN(self.tree, self.OnRightClick) | |
86 | EVT_RIGHT_UP(self.tree, self.OnRightUp) | |
87 | ||
f6bcfd97 | 88 | |
3115ef3e | 89 | |
bb0054cd | 90 | def OnRightClick(self, event): |
b1462dfa | 91 | pt = event.GetPosition(); |
f6bcfd97 | 92 | item, flags = self.tree.HitTest(pt) |
bd658b8c RD |
93 | self.log.WriteText("OnRightClick: %s, %s, %s\n" % |
94 | (self.tree.GetItemText(item), type(item), item.__class__)) | |
bb0054cd RD |
95 | self.tree.SelectItem(item) |
96 | ||
f6bcfd97 | 97 | |
bb0054cd | 98 | def OnRightUp(self, event): |
b1462dfa | 99 | pt = event.GetPosition(); |
f6bcfd97 | 100 | item, flags = self.tree.HitTest(pt) |
8bf5d46e RD |
101 | self.log.WriteText("OnRightUp: %s (manually starting label edit)\n" |
102 | % self.tree.GetItemText(item)) | |
103 | self.tree.EditLabel(item) | |
104 | ||
105 | ||
106 | ||
107 | def OnBeginEdit(self, event): | |
108 | self.log.WriteText("OnBeginEdit\n") | |
109 | # show how to prevent edit... | |
110 | if self.tree.GetItemText(event.GetItem()) == "The Root Item": | |
111 | wxBell() | |
112 | self.log.WriteText("You can't edit this one...\n") | |
1e4a197e RD |
113 | |
114 | # Lets just see what's visible of its children | |
115 | cookie = 0 | |
116 | root = event.GetItem() | |
117 | (child, cookie) = self.tree.GetFirstChild(root, cookie) | |
118 | while child.IsOk(): | |
119 | self.log.WriteText("Child [%s] visible = %d" % | |
120 | (self.tree.GetItemText(child), | |
121 | self.tree.IsVisible(child))) | |
122 | (child, cookie) = self.tree.GetNextChild(root, cookie) | |
123 | ||
8bf5d46e RD |
124 | event.Veto() |
125 | ||
1e4a197e | 126 | |
8bf5d46e RD |
127 | def OnEndEdit(self, event): |
128 | self.log.WriteText("OnEndEdit\n") | |
129 | # show how to reject edit, we'll not allow any digits | |
130 | for x in event.GetLabel(): | |
131 | if x in string.digits: | |
132 | self.log.WriteText("You can't enter digits...\n") | |
133 | event.Veto() | |
134 | return | |
135 | ||
136 | ||
bb0054cd | 137 | def OnLeftDClick(self, event): |
b1462dfa | 138 | pt = event.GetPosition(); |
f6bcfd97 | 139 | item, flags = self.tree.HitTest(pt) |
bb0054cd | 140 | self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item)) |
f6bcfd97 BP |
141 | parent = self.tree.GetItemParent(item) |
142 | self.tree.SortChildren(parent) | |
143 | event.Skip() | |
bb0054cd | 144 | |
cf694132 RD |
145 | |
146 | def OnSize(self, event): | |
147 | w,h = self.GetClientSizeTuple() | |
148 | self.tree.SetDimensions(0, 0, w, h) | |
149 | ||
150 | ||
151 | def OnItemExpanded(self, event): | |
152 | item = event.GetItem() | |
153 | self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item)) | |
154 | ||
155 | def OnItemCollapsed(self, event): | |
156 | item = event.GetItem() | |
157 | self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item)) | |
158 | ||
159 | def OnSelChanged(self, event): | |
8bf5d46e RD |
160 | self.item = event.GetItem() |
161 | self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item)) | |
c368d904 RD |
162 | if wxPlatform == '__WXMSW__': |
163 | self.log.WriteText("BoundingRect: %s\n" % | |
1e4a197e | 164 | self.tree.GetBoundingRect(self.item, True)) |
c368d904 RD |
165 | #items = self.tree.GetSelections() |
166 | #print map(self.tree.GetItemText, items) | |
167 | event.Skip() | |
168 | ||
164b735b | 169 | |
1fded56b | 170 | def OnActivate(self, event): |
c368d904 | 171 | self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item)) |
cf694132 | 172 | |
f6bcfd97 | 173 | |
cf694132 RD |
174 | #--------------------------------------------------------------------------- |
175 | ||
176 | def runTest(frame, nb, log): | |
177 | win = TestTreeCtrlPanel(nb, log) | |
178 | return win | |
179 | ||
180 | #--------------------------------------------------------------------------- | |
181 | ||
182 | ||
183 | ||
184 | ||
185 | ||
3115ef3e RD |
186 | overview = """\ |
187 | A tree control presents information as a hierarchy, with items that may be expanded to show further items. Items in a tree control are referenced by wxTreeItemId handles. | |
cf694132 | 188 | |
3115ef3e | 189 | """ |
cf694132 RD |
190 | |
191 | ||
192 | ||
3115ef3e RD |
193 | if __name__ == '__main__': |
194 | import sys,os | |
195 | import run | |
196 | run.main(['', os.path.basename(sys.argv[0])]) | |
cf694132 | 197 |