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