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