]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/wxTreeCtrl.py
minor additions and bugfixes
[wxWidgets.git] / utils / wxPython / demo / wxTreeCtrl.py
1
2 from wxPython.wx import *
3
4 import string
5
6 #---------------------------------------------------------------------------
7
8 class TestTreeCtrlPanel(wxPanel):
9 def __init__(self, parent, log):
10 wxPanel.__init__(self, parent, -1)
11
12 self.log = log
13 tID = NewId()
14
15 self.tree = wxTreeCtrl(self, tID, wxDefaultPosition, wxDefaultSize,
16 wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS) #| wxTR_MULTIPLE)
17
18 self.root = self.tree.AddRoot("The Root Item")
19 for x in range(15):
20 child = self.tree.AppendItem(self.root, "Item %d" % x)
21 for y in range(5):
22 last = self.tree.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y)))
23 for z in range(5):
24 self.tree.AppendItem(last, "item %d-%s-%d" % (x, chr(ord("a")+y), z))
25
26 self.tree.Expand(self.root)
27 EVT_TREE_ITEM_EXPANDED (self, tID, self.OnItemExpanded)
28 EVT_TREE_ITEM_COLLAPSED (self, tID, self.OnItemCollapsed)
29 EVT_TREE_SEL_CHANGED (self, tID, self.OnSelChanged)
30 EVT_TREE_BEGIN_LABEL_EDIT(self, tID, self.OnBeginEdit)
31 EVT_TREE_END_LABEL_EDIT (self, tID, self.OnEndEdit)
32
33 EVT_LEFT_DCLICK(self.tree, self.OnLeftDClick)
34 EVT_RIGHT_DOWN(self.tree, self.OnRightClick)
35 EVT_RIGHT_UP(self.tree, self.OnRightUp)
36
37 def OnRightClick(self, event):
38 (x,y) = event.Position();
39 item = self.tree.HitTest(wxPoint(x,y))
40 self.log.WriteText("OnRightClick: %s\n" % self.tree.GetItemText(item))
41 self.tree.SelectItem(item)
42
43 def OnRightUp(self, event):
44 (x,y) = event.Position();
45 item = self.tree.HitTest(wxPoint(x,y))
46 self.log.WriteText("OnRightUp: %s (manually starting label edit)\n"
47 % self.tree.GetItemText(item))
48 self.tree.EditLabel(item)
49
50
51
52 def OnBeginEdit(self, event):
53 self.log.WriteText("OnBeginEdit\n")
54 # show how to prevent edit...
55 if self.tree.GetItemText(event.GetItem()) == "The Root Item":
56 wxBell()
57 self.log.WriteText("You can't edit this one...\n")
58 event.Veto()
59
60 def OnEndEdit(self, event):
61 self.log.WriteText("OnEndEdit\n")
62 # show how to reject edit, we'll not allow any digits
63 for x in event.GetLabel():
64 if x in string.digits:
65 self.log.WriteText("You can't enter digits...\n")
66 event.Veto()
67 return
68
69
70
71 def OnLeftDClick(self, event):
72 (x,y) = event.Position();
73 item = self.tree.HitTest(wxPoint(x,y))
74 self.log.WriteText("OnLeftDClick: %s\n" % self.tree.GetItemText(item))
75
76
77 def OnSize(self, event):
78 w,h = self.GetClientSizeTuple()
79 self.tree.SetDimensions(0, 0, w, h)
80
81
82 def OnItemExpanded(self, event):
83 item = event.GetItem()
84 self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
85
86 def OnItemCollapsed(self, event):
87 item = event.GetItem()
88 self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
89
90 def OnSelChanged(self, event):
91 self.item = event.GetItem()
92 self.log.WriteText("OnSelChanged: %s\n" % self.tree.GetItemText(self.item))
93
94 #---------------------------------------------------------------------------
95
96 def runTest(frame, nb, log):
97 win = TestTreeCtrlPanel(nb, log)
98 return win
99
100 #---------------------------------------------------------------------------
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 overview = """\
120 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.
121
122 wxTreeCtrl()
123 -------------------------
124
125 Default constructor.
126
127 wxTreeCtrl(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")
128
129 Constructor, creating and showing a tree control.
130
131 Parameters
132 -------------------
133
134 parent = Parent window. Must not be NULL.
135
136 id = Window identifier. A value of -1 indicates a default value.
137
138 pos = Window position.
139
140 size = Window size. If the default size (-1, -1) is specified then the window is sized appropriately.
141
142 style = Window style. See wxTreeCtrl.
143
144 validator = Window validator.
145
146 name = Window name.
147 """