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