]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxTreeCtrl.py
use Windows standard icons instead of our own (patch 443856)
[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 8class 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
23class 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
RD
33 wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS# | wxTR_MULTIPLE
34 , self.log)
8bf5d46e 35
9416aa89 36
96bfd053 37 #import images
a1df7a95 38 #il = wxImageList(16, 16)
96bfd053
RD
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())
f17fee68 44
a1df7a95
RD
45 #self.tree.SetImageList(il)
46 #self.il = il
9b3d3bc4 47
f6bcfd97
BP
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
8bf5d46e 52 self.root = self.tree.AddRoot("The Root Item")
f6bcfd97 53 self.tree.SetPyData(self.root, None)
a1df7a95 54 #self.tree.SetItemImage(self.root, idx1)
9b3d3bc4 55
cf694132 56 for x in range(15):
8bf5d46e 57 child = self.tree.AppendItem(self.root, "Item %d" % x)
f6bcfd97 58 self.tree.SetPyData(child, None)
a1df7a95
RD
59 #self.tree.SetItemImage(child, idx2)
60 #self.tree.SetItemSelectedImage(child, idx3)
cf694132
RD
61 for y in range(5):
62 last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y)))
f6bcfd97 63 self.tree.SetPyData(last, None)
a1df7a95
RD
64 #self.tree.SetItemImage(last, idx4)
65 #self.tree.SetItemSelectedImage(last, idx5)
cf694132 66 for z in range(5):
f6bcfd97
BP
67 item = self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z))
68 self.tree.SetPyData(item, None)
cf694132 69
8bf5d46e 70 self.tree.Expand(self.root)
cf694132
RD
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)
8bf5d46e
RD
74 EVT_TREE_BEGIN_LABEL_EDIT(self, tID, self.OnBeginEdit)
75 EVT_TREE_END_LABEL_EDIT (self, tID, self.OnEndEdit)
c368d904 76 EVT_TREE_ITEM_ACTIVATED (self, tID, self.OnActivate)
cf694132 77
bb0054cd
RD
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
f6bcfd97 82
bb0054cd 83 def OnRightClick(self, event):
b1462dfa 84 pt = event.GetPosition();
f6bcfd97
BP
85 item, flags = self.tree.HitTest(pt)
86 self.log.WriteText("OnRightClick: %s, %s\n" % (self.tree.GetItemText(item),
87 type(item)))
bb0054cd
RD
88 self.tree.SelectItem(item)
89
f6bcfd97
BP
90
91
bb0054cd 92 def OnRightUp(self, event):
b1462dfa 93 pt = event.GetPosition();
f6bcfd97 94 item, flags = self.tree.HitTest(pt)
8bf5d46e
RD
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
bb0054cd 119 def OnLeftDClick(self, event):
b1462dfa 120 pt = event.GetPosition();
f6bcfd97 121 item, flags = self.tree.HitTest(pt)
bb0054cd 122 self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item))
f6bcfd97
BP
123 parent = self.tree.GetItemParent(item)
124 self.tree.SortChildren(parent)
125 event.Skip()
bb0054cd 126
cf694132
RD
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):
8bf5d46e
RD
142 self.item = event.GetItem()
143 self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item))
c368d904
RD
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
164b735b 151
c368d904
RD
152 def OnActivate(self, evt):
153 self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item))
cf694132 154
f6bcfd97 155
cf694132
RD
156#---------------------------------------------------------------------------
157
158def 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
cf694132
RD
176overview = """\
177A 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
cf694132 179"""