]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTreeCtrl.py
Added scripts to make Tex2RTF distribution on Unix
[wxWidgets.git] / wxPython / demo / wxTreeCtrl.py
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 #| wxTR_HIDE_ROOT
35 , self.log)
36
37
38 ##import images
39 ##il = wxImageList(16, 16)
40 ##idx1 = il.Add(images.getSmilesBitmap())
41 ##idx2 = il.Add(images.getOpenBitmap())
42 ##idx3 = il.Add(images.getNewBitmap())
43 ##idx4 = il.Add(images.getCopyBitmap())
44 ##idx5 = il.Add(images.getPasteBitmap())
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.
52
53 self.root = self.tree.AddRoot("The Root Item")
54 self.tree.SetPyData(self.root, None)
55 ##self.tree.SetItemImage(self.root, idx1)
56
57 for x in range(15):
58 child = self.tree.AppendItem(self.root, "Item %d" % x)
59 self.tree.SetPyData(child, None)
60 ##self.tree.SetItemImage(child, idx2, wxTreeItemIcon_Expanded)
61 ##self.tree.SetItemSelectedImage(child, idx3)
62 for y in range(5):
63 last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y)))
64 self.tree.SetPyData(last, None)
65 ##self.tree.SetItemImage(last, idx4)
66 ##self.tree.SetItemSelectedImage(last, idx5)
67 for z in range(5):
68 item = self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z))
69 self.tree.SetPyData(item, None)
70
71 self.tree.Expand(self.root)
72 EVT_TREE_ITEM_EXPANDED (self, tID, self.OnItemExpanded)
73 EVT_TREE_ITEM_COLLAPSED (self, tID, self.OnItemCollapsed)
74 EVT_TREE_SEL_CHANGED (self, tID, self.OnSelChanged)
75 EVT_TREE_BEGIN_LABEL_EDIT(self, tID, self.OnBeginEdit)
76 EVT_TREE_END_LABEL_EDIT (self, tID, self.OnEndEdit)
77 EVT_TREE_ITEM_ACTIVATED (self, tID, self.OnActivate)
78
79 EVT_LEFT_DCLICK(self.tree, self.OnLeftDClick)
80 EVT_RIGHT_DOWN(self.tree, self.OnRightClick)
81 EVT_RIGHT_UP(self.tree, self.OnRightUp)
82
83
84
85 def OnRightClick(self, event):
86 pt = event.GetPosition();
87 item, flags = self.tree.HitTest(pt)
88 self.log.WriteText("OnRightClick: %s, %s, %s\n" %
89 (self.tree.GetItemText(item), type(item), item.__class__))
90 self.tree.SelectItem(item)
91
92
93 def OnRightUp(self, event):
94 pt = event.GetPosition();
95 item, flags = self.tree.HitTest(pt)
96 self.log.WriteText("OnRightUp: %s (manually starting label edit)\n"
97 % self.tree.GetItemText(item))
98 self.tree.EditLabel(item)
99
100
101
102 def OnBeginEdit(self, event):
103 self.log.WriteText("OnBeginEdit\n")
104 # show how to prevent edit...
105 if self.tree.GetItemText(event.GetItem()) == "The Root Item":
106 wxBell()
107 self.log.WriteText("You can't edit this one...\n")
108 event.Veto()
109
110 def OnEndEdit(self, event):
111 self.log.WriteText("OnEndEdit\n")
112 # show how to reject edit, we'll not allow any digits
113 for x in event.GetLabel():
114 if x in string.digits:
115 self.log.WriteText("You can't enter digits...\n")
116 event.Veto()
117 return
118
119
120 def OnLeftDClick(self, event):
121 pt = event.GetPosition();
122 item, flags = self.tree.HitTest(pt)
123 self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item))
124 parent = self.tree.GetItemParent(item)
125 self.tree.SortChildren(parent)
126 event.Skip()
127
128
129 def OnSize(self, event):
130 w,h = self.GetClientSizeTuple()
131 self.tree.SetDimensions(0, 0, w, h)
132
133
134 def OnItemExpanded(self, event):
135 item = event.GetItem()
136 self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
137
138 def OnItemCollapsed(self, event):
139 item = event.GetItem()
140 self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
141
142 def OnSelChanged(self, event):
143 self.item = event.GetItem()
144 self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item))
145 if wxPlatform == '__WXMSW__':
146 self.log.WriteText("BoundingRect: %s\n" %
147 self.tree.GetBoundingRect(self.item, true))
148 #items = self.tree.GetSelections()
149 #print map(self.tree.GetItemText, items)
150 event.Skip()
151
152
153 def OnActivate(self, evt):
154 self.log.WriteText("OnActivate: %s\n" % self.tree.GetItemText(self.item))
155
156
157 #---------------------------------------------------------------------------
158
159 def runTest(frame, nb, log):
160 win = TestTreeCtrlPanel(nb, log)
161 return win
162
163 #---------------------------------------------------------------------------
164
165
166
167
168
169 overview = """\
170 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.
171
172 """
173
174
175
176 if __name__ == '__main__':
177 import sys,os
178 import run
179 run.main(['', os.path.basename(sys.argv[0])])
180