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