]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTreeCtrl.py
Removed AlignIn after further thought
[wxWidgets.git] / wxPython / demo / wxTreeCtrl.py
1
2 from wxPython.wx import *
3 import images
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 = wxNewId()
31
32 self.tree = MyTreeCtrl(self, tID, wxDefaultPosition, wxDefaultSize,
33 wxTR_HAS_BUTTONS
34 | wxTR_EDIT_LABELS
35 #| wxTR_MULTIPLE
36 #| wxTR_HIDE_ROOT
37 , self.log)
38
39 isz = (16,16)
40 il = wxImageList(isz[0], isz[1])
41 fldridx = il.Add(wxArtProvider_GetBitmap(wxART_FOLDER, wxART_OTHER, isz))
42 fldropenidx = il.Add(wxArtProvider_GetBitmap(wxART_FILE_OPEN, wxART_OTHER, isz))
43 fileidx = il.Add(wxArtProvider_GetBitmap(wxART_REPORT_VIEW, wxART_OTHER, isz))
44 smileidx = il.Add(images.getSmilesBitmap())
45
46 self.tree.SetImageList(il)
47 self.il = il
48
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 below for
52 # the item data.
53
54 self.root = self.tree.AddRoot("The Root Item")
55 self.tree.SetPyData(self.root, None)
56 self.tree.SetItemImage(self.root, fldridx, wxTreeItemIcon_Normal)
57 self.tree.SetItemImage(self.root, fldropenidx, wxTreeItemIcon_Expanded)
58
59
60 for x in range(15):
61 child = self.tree.AppendItem(self.root, "Item %d" % x)
62 self.tree.SetPyData(child, None)
63 self.tree.SetItemImage(child, fldridx, wxTreeItemIcon_Normal)
64 self.tree.SetItemImage(child, fldropenidx, wxTreeItemIcon_Expanded)
65 for y in range(5):
66 last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y)))
67 self.tree.SetPyData(last, None)
68 self.tree.SetItemImage(last, fldridx, wxTreeItemIcon_Normal)
69 self.tree.SetItemImage(last, fldropenidx, wxTreeItemIcon_Expanded)
70 for z in range(5):
71 item = self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z))
72 self.tree.SetPyData(item, None)
73 self.tree.SetItemImage(item, fileidx, wxTreeItemIcon_Normal)
74 self.tree.SetItemImage(item, smileidx, wxTreeItemIcon_Selected)
75
76 self.tree.Expand(self.root)
77 EVT_TREE_ITEM_EXPANDED (self, tID, self.OnItemExpanded)
78 EVT_TREE_ITEM_COLLAPSED (self, tID, self.OnItemCollapsed)
79 EVT_TREE_SEL_CHANGED (self, tID, self.OnSelChanged)
80 EVT_TREE_BEGIN_LABEL_EDIT(self, tID, self.OnBeginEdit)
81 EVT_TREE_END_LABEL_EDIT (self, tID, self.OnEndEdit)
82 EVT_TREE_ITEM_ACTIVATED (self, tID, self.OnActivate)
83
84 EVT_LEFT_DCLICK(self.tree, self.OnLeftDClick)
85 EVT_RIGHT_DOWN(self.tree, self.OnRightClick)
86 EVT_RIGHT_UP(self.tree, self.OnRightUp)
87
88
89
90 def OnRightClick(self, event):
91 pt = event.GetPosition();
92 item, flags = self.tree.HitTest(pt)
93 self.log.WriteText("OnRightClick: %s, %s, %s\n" %
94 (self.tree.GetItemText(item), type(item), item.__class__))
95 self.tree.SelectItem(item)
96
97
98 def OnRightUp(self, event):
99 pt = event.GetPosition();
100 item, flags = self.tree.HitTest(pt)
101 self.log.WriteText("OnRightUp: %s (manually starting label edit)\n"
102 % self.tree.GetItemText(item))
103 self.tree.EditLabel(item)
104
105
106
107 def OnBeginEdit(self, event):
108 self.log.WriteText("OnBeginEdit\n")
109 # show how to prevent edit...
110 if self.tree.GetItemText(event.GetItem()) == "The Root Item":
111 wxBell()
112 self.log.WriteText("You can't edit this one...\n")
113
114 # Lets just see what's visible of its children
115 cookie = 0
116 root = event.GetItem()
117 (child, cookie) = self.tree.GetFirstChild(root, cookie)
118 while child.IsOk():
119 self.log.WriteText("Child [%s] visible = %d" %
120 (self.tree.GetItemText(child),
121 self.tree.IsVisible(child)))
122 (child, cookie) = self.tree.GetNextChild(root, cookie)
123
124 event.Veto()
125
126
127 def OnEndEdit(self, event):
128 self.log.WriteText("OnEndEdit\n")
129 # show how to reject edit, we'll not allow any digits
130 for x in event.GetLabel():
131 if x in string.digits:
132 self.log.WriteText("You can't enter digits...\n")
133 event.Veto()
134 return
135
136
137 def OnLeftDClick(self, event):
138 pt = event.GetPosition();
139 item, flags = self.tree.HitTest(pt)
140 self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item))
141 parent = self.tree.GetItemParent(item)
142 self.tree.SortChildren(parent)
143 event.Skip()
144
145
146 def OnSize(self, event):
147 w,h = self.GetClientSizeTuple()
148 self.tree.SetDimensions(0, 0, w, h)
149
150
151 def OnItemExpanded(self, event):
152 item = event.GetItem()
153 self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
154
155 def OnItemCollapsed(self, event):
156 item = event.GetItem()
157 self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
158
159 def OnSelChanged(self, event):
160 self.item = event.GetItem()
161 self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item))
162 if wxPlatform == '__WXMSW__':
163 self.log.WriteText("BoundingRect: %s\n" %
164 self.tree.GetBoundingRect(self.item, True))
165 #items = self.tree.GetSelections()
166 #print map(self.tree.GetItemText, items)
167 event.Skip()
168
169
170 def OnActivate(self, evt):
171 self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item))
172
173
174 #---------------------------------------------------------------------------
175
176 def runTest(frame, nb, log):
177 win = TestTreeCtrlPanel(nb, log)
178 return win
179
180 #---------------------------------------------------------------------------
181
182
183
184
185
186 overview = """\
187 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.
188
189 """
190
191
192
193 if __name__ == '__main__':
194 import sys,os
195 import run
196 run.main(['', os.path.basename(sys.argv[0])])
197