]>
Commit | Line | Data |
---|---|---|
cf694132 RD |
1 | |
2 | from wxPython.wx import * | |
3 | ||
8bf5d46e RD |
4 | import string |
5 | ||
cf694132 RD |
6 | #--------------------------------------------------------------------------- |
7 | ||
f6bcfd97 BP |
8 | class MyTreeCtrl(wxTreeCtrl): |
9 | def __init__(self, parent, id, pos, size, style): | |
10 | wxTreeCtrl.__init__(self, parent, id, pos, size, style) | |
11 | ||
12 | ||
13 | def OnCompareItems(self, item1, item2): | |
14 | t1 = self.GetItemText(item1) | |
15 | t2 = self.GetItemText(item2) | |
16 | if t1 < t2: return -1 | |
17 | if t1 == t2: return 0 | |
18 | return 1 | |
19 | ||
20 | #--------------------------------------------------------------------------- | |
21 | ||
cf694132 RD |
22 | class TestTreeCtrlPanel(wxPanel): |
23 | def __init__(self, parent, log): | |
24 | wxPanel.__init__(self, parent, -1) | |
f6bcfd97 | 25 | EVT_SIZE(self, self.OnSize) |
cf694132 RD |
26 | |
27 | self.log = log | |
28 | tID = NewId() | |
29 | ||
f6bcfd97 | 30 | self.tree = MyTreeCtrl(self, tID, wxDefaultPosition, wxDefaultSize, |
b1462dfa | 31 | wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS)# | wxTR_MULTIPLE) |
8bf5d46e | 32 | |
a1df7a95 RD |
33 | #il = wxImageList(16, 16) |
34 | #idx1 = il.Add(wxBitmap('bitmaps/smiles.bmp', wxBITMAP_TYPE_BMP)) | |
35 | #idx2 = il.Add(wxBitmap('bitmaps/open.bmp', wxBITMAP_TYPE_BMP)) | |
36 | #idx3 = il.Add(wxBitmap('bitmaps/new.bmp', wxBITMAP_TYPE_BMP)) | |
37 | #idx4 = il.Add(wxBitmap('bitmaps/copy.bmp', wxBITMAP_TYPE_BMP)) | |
38 | #idx5 = il.Add(wxBitmap('bitmaps/paste.bmp', wxBITMAP_TYPE_BMP)) | |
f17fee68 | 39 | |
a1df7a95 RD |
40 | #self.tree.SetImageList(il) |
41 | #self.il = il | |
9b3d3bc4 | 42 | |
f6bcfd97 BP |
43 | # NOTE: For some reason tree items have to have a data object in |
44 | # order to be sorted. Since our compare just uses the labels | |
45 | # we don't need any real data, so we'll just use None. | |
46 | ||
8bf5d46e | 47 | self.root = self.tree.AddRoot("The Root Item") |
f6bcfd97 | 48 | self.tree.SetPyData(self.root, None) |
a1df7a95 | 49 | #self.tree.SetItemImage(self.root, idx1) |
9b3d3bc4 | 50 | |
cf694132 | 51 | for x in range(15): |
8bf5d46e | 52 | child = self.tree.AppendItem(self.root, "Item %d" % x) |
f6bcfd97 | 53 | self.tree.SetPyData(child, None) |
a1df7a95 RD |
54 | #self.tree.SetItemImage(child, idx2) |
55 | #self.tree.SetItemSelectedImage(child, idx3) | |
cf694132 RD |
56 | for y in range(5): |
57 | last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y))) | |
f6bcfd97 | 58 | self.tree.SetPyData(last, None) |
a1df7a95 RD |
59 | #self.tree.SetItemImage(last, idx4) |
60 | #self.tree.SetItemSelectedImage(last, idx5) | |
cf694132 | 61 | for z in range(5): |
f6bcfd97 BP |
62 | item = self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z)) |
63 | self.tree.SetPyData(item, None) | |
cf694132 | 64 | |
8bf5d46e | 65 | self.tree.Expand(self.root) |
cf694132 RD |
66 | EVT_TREE_ITEM_EXPANDED (self, tID, self.OnItemExpanded) |
67 | EVT_TREE_ITEM_COLLAPSED (self, tID, self.OnItemCollapsed) | |
68 | EVT_TREE_SEL_CHANGED (self, tID, self.OnSelChanged) | |
8bf5d46e RD |
69 | EVT_TREE_BEGIN_LABEL_EDIT(self, tID, self.OnBeginEdit) |
70 | EVT_TREE_END_LABEL_EDIT (self, tID, self.OnEndEdit) | |
cf694132 | 71 | |
bb0054cd RD |
72 | EVT_LEFT_DCLICK(self.tree, self.OnLeftDClick) |
73 | EVT_RIGHT_DOWN(self.tree, self.OnRightClick) | |
74 | EVT_RIGHT_UP(self.tree, self.OnRightUp) | |
75 | ||
f6bcfd97 | 76 | |
bb0054cd | 77 | def OnRightClick(self, event): |
b1462dfa | 78 | pt = event.GetPosition(); |
f6bcfd97 BP |
79 | item, flags = self.tree.HitTest(pt) |
80 | self.log.WriteText("OnRightClick: %s, %s\n" % (self.tree.GetItemText(item), | |
81 | type(item))) | |
bb0054cd RD |
82 | self.tree.SelectItem(item) |
83 | ||
f6bcfd97 BP |
84 | |
85 | ||
bb0054cd | 86 | def OnRightUp(self, event): |
b1462dfa | 87 | pt = event.GetPosition(); |
f6bcfd97 | 88 | item, flags = self.tree.HitTest(pt) |
8bf5d46e RD |
89 | self.log.WriteText("OnRightUp: %s (manually starting label edit)\n" |
90 | % self.tree.GetItemText(item)) | |
91 | self.tree.EditLabel(item) | |
92 | ||
93 | ||
94 | ||
95 | def OnBeginEdit(self, event): | |
96 | self.log.WriteText("OnBeginEdit\n") | |
97 | # show how to prevent edit... | |
98 | if self.tree.GetItemText(event.GetItem()) == "The Root Item": | |
99 | wxBell() | |
100 | self.log.WriteText("You can't edit this one...\n") | |
101 | event.Veto() | |
102 | ||
103 | def OnEndEdit(self, event): | |
104 | self.log.WriteText("OnEndEdit\n") | |
105 | # show how to reject edit, we'll not allow any digits | |
106 | for x in event.GetLabel(): | |
107 | if x in string.digits: | |
108 | self.log.WriteText("You can't enter digits...\n") | |
109 | event.Veto() | |
110 | return | |
111 | ||
112 | ||
bb0054cd | 113 | def OnLeftDClick(self, event): |
b1462dfa | 114 | pt = event.GetPosition(); |
f6bcfd97 | 115 | item, flags = self.tree.HitTest(pt) |
bb0054cd | 116 | self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item)) |
f6bcfd97 BP |
117 | parent = self.tree.GetItemParent(item) |
118 | self.tree.SortChildren(parent) | |
119 | event.Skip() | |
bb0054cd | 120 | |
cf694132 RD |
121 | |
122 | def OnSize(self, event): | |
123 | w,h = self.GetClientSizeTuple() | |
124 | self.tree.SetDimensions(0, 0, w, h) | |
125 | ||
126 | ||
127 | def OnItemExpanded(self, event): | |
128 | item = event.GetItem() | |
129 | self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item)) | |
130 | ||
131 | def OnItemCollapsed(self, event): | |
132 | item = event.GetItem() | |
133 | self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item)) | |
134 | ||
135 | def OnSelChanged(self, event): | |
8bf5d46e RD |
136 | self.item = event.GetItem() |
137 | self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item)) | |
164b735b RD |
138 | self.log.WriteText("BoundingRect: %s\n" % |
139 | self.tree.GetBoundingRect(self.item, true)) | |
140 | ||
cf694132 | 141 | |
f6bcfd97 | 142 | |
cf694132 RD |
143 | #--------------------------------------------------------------------------- |
144 | ||
145 | def runTest(frame, nb, log): | |
146 | win = TestTreeCtrlPanel(nb, log) | |
147 | return win | |
148 | ||
149 | #--------------------------------------------------------------------------- | |
150 | ||
151 | ||
152 | ||
153 | ||
154 | ||
155 | ||
156 | ||
157 | ||
158 | ||
159 | ||
160 | ||
161 | ||
162 | ||
163 | ||
164 | ||
165 | ||
166 | ||
167 | ||
168 | overview = """\ | |
169 | 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. | |
170 | ||
171 | wxTreeCtrl() | |
172 | ------------------------- | |
173 | ||
174 | Default constructor. | |
175 | ||
176 | wxTreeCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTR_HAS_BUTTONS, const wxValidator& validator = wxDefaultValidator, const wxString& name = "listCtrl") | |
177 | ||
178 | Constructor, creating and showing a tree control. | |
179 | ||
180 | Parameters | |
181 | ------------------- | |
182 | ||
183 | parent = Parent window. Must not be NULL. | |
184 | ||
185 | id = Window identifier. A value of -1 indicates a default value. | |
186 | ||
187 | pos = Window position. | |
188 | ||
189 | size = Window size. If the default size (-1, -1) is specified then the window is sized appropriately. | |
190 | ||
191 | style = Window style. See wxTreeCtrl. | |
192 | ||
193 | validator = Window validator. | |
194 | ||
195 | name = Window name. | |
196 | """ |