]>
Commit | Line | Data |
---|---|---|
cf694132 RD |
1 | |
2 | from wxPython.wx import * | |
3 | ||
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 | |
30 | tID = NewId() | |
31 | ||
f6bcfd97 | 32 | self.tree = MyTreeCtrl(self, tID, wxDefaultPosition, wxDefaultSize, |
3b36695d | 33 | wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS# | wxTR_MULTIPLE |
63b6646e | 34 | #| wxTR_HIDE_ROOT |
3b36695d | 35 | , self.log) |
8bf5d46e | 36 | |
9416aa89 | 37 | |
ecc08ead RD |
38 | ##import images |
39 | ##il = wxImageList(16, 16) | |
40 | ##idx1 = il.Add(images.getSmilesBitmap()) | |
41 | ##idx2 = il.Add(images.getOpenBitmap()) | |
42 | ##idx3 = il.Add(images.getNewBitmap()) | |
43 | ##idx4 = il.Add(images.getCopyBitmap()) | |
44 | ##idx5 = il.Add(images.getPasteBitmap()) | |
f17fee68 | 45 | |
ecc08ead 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 | |
51 | # we don't need any real data, so we'll just use None. | |
52 | ||
8bf5d46e | 53 | self.root = self.tree.AddRoot("The Root Item") |
f6bcfd97 | 54 | self.tree.SetPyData(self.root, None) |
ecc08ead | 55 | ##self.tree.SetItemImage(self.root, idx1) |
9b3d3bc4 | 56 | |
cf694132 | 57 | for x in range(15): |
8bf5d46e | 58 | child = self.tree.AppendItem(self.root, "Item %d" % x) |
f6bcfd97 | 59 | self.tree.SetPyData(child, None) |
ecc08ead RD |
60 | ##self.tree.SetItemImage(child, idx2, wxTreeItemIcon_Expanded) |
61 | ##self.tree.SetItemSelectedImage(child, idx3) | |
cf694132 RD |
62 | for y in range(5): |
63 | last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y))) | |
f6bcfd97 | 64 | self.tree.SetPyData(last, None) |
ecc08ead RD |
65 | ##self.tree.SetItemImage(last, idx4) |
66 | ##self.tree.SetItemSelectedImage(last, idx5) | |
cf694132 | 67 | for z in range(5): |
f6bcfd97 BP |
68 | item = self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z)) |
69 | self.tree.SetPyData(item, None) | |
cf694132 | 70 | |
8bf5d46e | 71 | self.tree.Expand(self.root) |
cf694132 RD |
72 | EVT_TREE_ITEM_EXPANDED (self, tID, self.OnItemExpanded) |
73 | EVT_TREE_ITEM_COLLAPSED (self, tID, self.OnItemCollapsed) | |
74 | EVT_TREE_SEL_CHANGED (self, tID, self.OnSelChanged) | |
8bf5d46e RD |
75 | EVT_TREE_BEGIN_LABEL_EDIT(self, tID, self.OnBeginEdit) |
76 | EVT_TREE_END_LABEL_EDIT (self, tID, self.OnEndEdit) | |
c368d904 | 77 | EVT_TREE_ITEM_ACTIVATED (self, tID, self.OnActivate) |
cf694132 | 78 | |
bb0054cd RD |
79 | EVT_LEFT_DCLICK(self.tree, self.OnLeftDClick) |
80 | EVT_RIGHT_DOWN(self.tree, self.OnRightClick) | |
81 | EVT_RIGHT_UP(self.tree, self.OnRightUp) | |
82 | ||
f6bcfd97 | 83 | |
3115ef3e | 84 | |
bb0054cd | 85 | def OnRightClick(self, event): |
b1462dfa | 86 | pt = event.GetPosition(); |
f6bcfd97 | 87 | item, flags = self.tree.HitTest(pt) |
bd658b8c RD |
88 | self.log.WriteText("OnRightClick: %s, %s, %s\n" % |
89 | (self.tree.GetItemText(item), type(item), item.__class__)) | |
bb0054cd RD |
90 | self.tree.SelectItem(item) |
91 | ||
f6bcfd97 | 92 | |
bb0054cd | 93 | def OnRightUp(self, event): |
b1462dfa | 94 | pt = event.GetPosition(); |
f6bcfd97 | 95 | item, flags = self.tree.HitTest(pt) |
8bf5d46e RD |
96 | self.log.WriteText("OnRightUp: %s (manually starting label edit)\n" |
97 | % self.tree.GetItemText(item)) | |
98 | self.tree.EditLabel(item) | |
99 | ||
100 | ||
101 | ||
102 | def OnBeginEdit(self, event): | |
103 | self.log.WriteText("OnBeginEdit\n") | |
104 | # show how to prevent edit... | |
105 | if self.tree.GetItemText(event.GetItem()) == "The Root Item": | |
106 | wxBell() | |
107 | self.log.WriteText("You can't edit this one...\n") | |
108 | event.Veto() | |
109 | ||
110 | def OnEndEdit(self, event): | |
111 | self.log.WriteText("OnEndEdit\n") | |
112 | # show how to reject edit, we'll not allow any digits | |
113 | for x in event.GetLabel(): | |
114 | if x in string.digits: | |
115 | self.log.WriteText("You can't enter digits...\n") | |
116 | event.Veto() | |
117 | return | |
118 | ||
119 | ||
bb0054cd | 120 | def OnLeftDClick(self, event): |
b1462dfa | 121 | pt = event.GetPosition(); |
f6bcfd97 | 122 | item, flags = self.tree.HitTest(pt) |
bb0054cd | 123 | self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item)) |
f6bcfd97 BP |
124 | parent = self.tree.GetItemParent(item) |
125 | self.tree.SortChildren(parent) | |
126 | event.Skip() | |
bb0054cd | 127 | |
cf694132 RD |
128 | |
129 | def OnSize(self, event): | |
130 | w,h = self.GetClientSizeTuple() | |
131 | self.tree.SetDimensions(0, 0, w, h) | |
132 | ||
133 | ||
134 | def OnItemExpanded(self, event): | |
135 | item = event.GetItem() | |
136 | self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item)) | |
137 | ||
138 | def OnItemCollapsed(self, event): | |
139 | item = event.GetItem() | |
140 | self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item)) | |
141 | ||
142 | def OnSelChanged(self, event): | |
8bf5d46e RD |
143 | self.item = event.GetItem() |
144 | self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item)) | |
c368d904 RD |
145 | if wxPlatform == '__WXMSW__': |
146 | self.log.WriteText("BoundingRect: %s\n" % | |
147 | self.tree.GetBoundingRect(self.item, true)) | |
148 | #items = self.tree.GetSelections() | |
149 | #print map(self.tree.GetItemText, items) | |
150 | event.Skip() | |
151 | ||
164b735b | 152 | |
c368d904 RD |
153 | def OnActivate(self, evt): |
154 | self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item)) | |
cf694132 | 155 | |
f6bcfd97 | 156 | |
cf694132 RD |
157 | #--------------------------------------------------------------------------- |
158 | ||
159 | def runTest(frame, nb, log): | |
160 | win = TestTreeCtrlPanel(nb, log) | |
161 | return win | |
162 | ||
163 | #--------------------------------------------------------------------------- | |
164 | ||
165 | ||
166 | ||
167 | ||
168 | ||
3115ef3e RD |
169 | overview = """\ |
170 | 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 | 171 | |
3115ef3e | 172 | """ |
cf694132 RD |
173 | |
174 | ||
175 | ||
3115ef3e RD |
176 | if __name__ == '__main__': |
177 | import sys,os | |
178 | import run | |
179 | run.main(['', os.path.basename(sys.argv[0])]) | |
cf694132 | 180 |