2 from wxPython
.wx
import *
6 #---------------------------------------------------------------------------
8 class MyTreeCtrl(wxTreeCtrl
):
9 def __init__(self
, parent
, id, pos
, size
, style
, log
):
10 wxTreeCtrl
.__init
__(self
, parent
, id, pos
, size
, style
)
13 def OnCompareItems(self
, item1
, item2
):
14 t1
= self
.GetItemText(item1
)
15 t2
= self
.GetItemText(item2
)
16 self
.log
.WriteText('compare: ' + t1
+ ' <> ' + t2
+ '\n')
21 #---------------------------------------------------------------------------
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
)
32 self
.tree
= MyTreeCtrl(self
, tID
, wxDefaultPosition
, wxDefaultSize
,
33 wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS
# | wxTR_MULTIPLE
38 ##il = wxImageList(16, 16)
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())
45 ##self.tree.SetImageList(il)
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.
52 self
.root
= self
.tree
.AddRoot("The Root Item")
53 self
.tree
.SetPyData(self
.root
, None)
54 ##self.tree.SetItemImage(self.root, idx1)
57 child
= self
.tree
.AppendItem(self
.root
, "Item %d" % x
)
58 self
.tree
.SetPyData(child
, None)
59 ##self.tree.SetItemImage(child, idx2, wxTreeItemIcon_Expanded)
60 ##self.tree.SetItemSelectedImage(child, idx3)
62 last
= self
.tree
.AppendItem(child
, "item %d-%s" % (x
, chr(ord("a")+y
)))
63 self
.tree
.SetPyData(last
, None)
64 ##self.tree.SetItemImage(last, idx4)
65 ##self.tree.SetItemSelectedImage(last, idx5)
67 item
= self
.tree
.AppendItem(last
, "item %d-%s-%d" % (x
, chr(ord("a")+y
), z
))
68 self
.tree
.SetPyData(item
, None)
70 self
.tree
.Expand(self
.root
)
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
)
74 EVT_TREE_BEGIN_LABEL_EDIT(self
, tID
, self
.OnBeginEdit
)
75 EVT_TREE_END_LABEL_EDIT (self
, tID
, self
.OnEndEdit
)
76 EVT_TREE_ITEM_ACTIVATED (self
, tID
, self
.OnActivate
)
78 EVT_LEFT_DCLICK(self
.tree
, self
.OnLeftDClick
)
79 EVT_RIGHT_DOWN(self
.tree
, self
.OnRightClick
)
80 EVT_RIGHT_UP(self
.tree
, self
.OnRightUp
)
83 def OnRightClick(self
, event
):
84 pt
= event
.GetPosition();
85 item
, flags
= self
.tree
.HitTest(pt
)
86 self
.log
.WriteText("OnRightClick: %s, %s\n" % (self
.tree
.GetItemText(item
),
88 self
.tree
.SelectItem(item
)
92 def OnRightUp(self
, event
):
93 pt
= event
.GetPosition();
94 item
, flags
= self
.tree
.HitTest(pt
)
95 self
.log
.WriteText("OnRightUp: %s (manually starting label edit)\n"
96 % self
.tree
.GetItemText(item
))
97 self
.tree
.EditLabel(item
)
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":
106 self
.log
.WriteText("You can't edit this one...\n")
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")
119 def OnLeftDClick(self
, event
):
120 pt
= event
.GetPosition();
121 item
, flags
= self
.tree
.HitTest(pt
)
122 self
.log
.WriteText("OnLeftDClick: %s\n" % self
.tree
.GetItemText(item
))
123 parent
= self
.tree
.GetItemParent(item
)
124 self
.tree
.SortChildren(parent
)
128 def OnSize(self
, event
):
129 w
,h
= self
.GetClientSizeTuple()
130 self
.tree
.SetDimensions(0, 0, w
, h
)
133 def OnItemExpanded(self
, event
):
134 item
= event
.GetItem()
135 self
.log
.WriteText("OnItemExpanded: %s\n" % self
.tree
.GetItemText(item
))
137 def OnItemCollapsed(self
, event
):
138 item
= event
.GetItem()
139 self
.log
.WriteText("OnItemCollapsed: %s\n" % self
.tree
.GetItemText(item
))
141 def OnSelChanged(self
, event
):
142 self
.item
= event
.GetItem()
143 self
.log
.WriteText("OnSelChanged: %s\n" % self
.tree
.GetItemText(self
.item
))
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)
152 def OnActivate(self
, evt
):
153 self
.log
.WriteText("OnActivate: %s\n" % self
.tree
.GetItemText(self
.item
))
156 #---------------------------------------------------------------------------
158 def runTest(frame
, nb
, log
):
159 win
= TestTreeCtrlPanel(nb
, log
)
162 #---------------------------------------------------------------------------
177 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.