]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: treetest.cpp | |
3 | // Purpose: wxTreeCtrl sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
28 | // under Windows the icons are in the .rc file | |
29 | #ifndef __WXMSW__ | |
30 | #include "icon1.xpm" | |
31 | #include "icon2.xpm" | |
32 | #include "mondrian.xpm" | |
33 | #endif | |
34 | ||
35 | #include "wx/log.h" | |
36 | ||
37 | #include "wx/imaglist.h" | |
38 | #include "wx/treectrl.h" | |
39 | ||
40 | #include "treetest.h" | |
41 | ||
42 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
43 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) | |
44 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
45 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) | |
46 | END_EVENT_TABLE() | |
47 | ||
48 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
49 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) | |
50 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
51 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
52 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
53 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
54 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) | |
55 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) | |
56 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
57 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
58 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
59 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
60 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
61 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
62 | EVT_CHAR(MyTreeCtrl::OnKeyDown) | |
63 | END_EVENT_TABLE() | |
64 | ||
65 | IMPLEMENT_APP(MyApp) | |
66 | ||
67 | // `Main program' equivalent, creating windows and returning main app frame | |
68 | bool MyApp::OnInit() | |
69 | { | |
70 | // Create the main frame window | |
71 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340); | |
72 | ||
73 | // Show the frame | |
74 | frame->Show(TRUE); | |
75 | SetTopWindow(frame); | |
76 | ||
77 | return TRUE; | |
78 | } | |
79 | ||
80 | ||
81 | // My frame constructor | |
82 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
83 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)) | |
84 | { | |
85 | // This reduces flicker effects - even better would be to define | |
86 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
87 | // show or hidden, the frame is sent a background erase event. | |
88 | SetBackgroundColour(wxColour(255, 255, 255)); | |
89 | ||
90 | // Give it an icon | |
91 | SetIcon(wxICON(mondrian)); | |
92 | ||
93 | // Make a menubar | |
94 | wxMenu *file_menu = new wxMenu; | |
95 | ||
96 | file_menu->Append(TreeTest_Dump, "&Dump tree items"); | |
97 | file_menu->AppendSeparator(); | |
98 | file_menu->Append(TreeTest_About, "&About..."); | |
99 | file_menu->AppendSeparator(); | |
100 | file_menu->Append(TreeTest_Quit, "E&xit"); | |
101 | ||
102 | wxMenuBar *menu_bar = new wxMenuBar; | |
103 | menu_bar->Append(file_menu, "&File"); | |
104 | SetMenuBar(menu_bar); | |
105 | ||
106 | // Make a panel with a message | |
107 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
108 | wxDefaultPosition, wxDefaultSize, | |
109 | wxTR_HAS_BUTTONS | wxSUNKEN_BORDER); | |
110 | wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "", | |
111 | wxDefaultPosition, wxDefaultSize, | |
112 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
113 | ||
114 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
115 | c->top.SameAs(this, wxTop); | |
116 | c->left.SameAs(this, wxLeft); | |
117 | c->right.SameAs(this, wxRight); | |
118 | c->height.PercentOf(this, wxHeight, 66); | |
119 | m_treeCtrl->SetConstraints(c); | |
120 | ||
121 | c = new wxLayoutConstraints; | |
122 | c->top.Below(m_treeCtrl); | |
123 | c->left.SameAs(this, wxLeft); | |
124 | c->right.SameAs(this, wxRight); | |
125 | c->bottom.SameAs(this, wxBottom); | |
126 | textCtrl->SetConstraints(c); | |
127 | SetAutoLayout(TRUE); | |
128 | ||
129 | // create a status bar with 3 panes | |
130 | CreateStatusBar(3); | |
131 | SetStatusText("", 0); | |
132 | ||
133 | // set our text control as the log target | |
134 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl); | |
135 | delete wxLog::SetActiveTarget(logWindow); | |
136 | } | |
137 | ||
138 | MyFrame::~MyFrame() | |
139 | { | |
140 | delete wxLog::SetActiveTarget(NULL); | |
141 | } | |
142 | ||
143 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
144 | { | |
145 | Close(TRUE); | |
146 | } | |
147 | ||
148 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
149 | { | |
150 | wxMessageDialog dialog(this, | |
151 | "Tree test sample\n" | |
152 | "Julian Smart (c) 1997", | |
153 | "About tree test", wxOK); | |
154 | ||
155 | dialog.ShowModal(); | |
156 | } | |
157 | ||
158 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) | |
159 | { | |
160 | wxTreeItemId root=m_treeCtrl->GetSelection(); | |
161 | m_treeCtrl->GetItemsRecursively(root, -1); | |
162 | } | |
163 | ||
164 | // MyTreeCtrl implementation | |
165 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, | |
166 | const wxPoint& pos, const wxSize& size, | |
167 | long style) | |
168 | : wxTreeCtrl(parent, id, pos, size, style) | |
169 | { | |
170 | // Make an image list containing small icons | |
171 | m_imageListNormal = new wxImageList(16, 16, TRUE); | |
172 | ||
173 | // should correspond to TreeCtrlIcon_xxx enum | |
174 | m_imageListNormal->Add(wxICON(icon2)); | |
175 | m_imageListNormal->Add(wxICON(icon1)); | |
176 | ||
177 | SetImageList(m_imageListNormal); | |
178 | ||
179 | // Add some items to the tree | |
180 | AddTestItemsToTree(3, 2); | |
181 | } | |
182 | ||
183 | MyTreeCtrl::~MyTreeCtrl() | |
184 | { | |
185 | delete m_imageListNormal; | |
186 | } | |
187 | ||
188 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, | |
189 | size_t numChildren, | |
190 | size_t depth, | |
191 | size_t folder) | |
192 | { | |
193 | if ( depth > 0 ) | |
194 | { | |
195 | wxString str; | |
196 | for ( size_t n = 0; n < numChildren; n++ ) | |
197 | { | |
198 | // at depth 1 elements won't have any more children | |
199 | if (depth == 1) | |
200 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
201 | else | |
202 | str.Printf("%s child %d","Folder", n + 1); | |
203 | ||
204 | int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; | |
205 | wxTreeItemId id = AppendItem(idParent, str, image, image, | |
206 | new MyTreeItemData(str)); | |
207 | AddItemsRecursively(id, numChildren, depth - 1,n+1); | |
208 | } | |
209 | } | |
210 | //else: done! | |
211 | } | |
212 | ||
213 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, | |
214 | size_t depth) | |
215 | { | |
216 | wxTreeItemId rootId = AddRoot("Root", | |
217 | TreeCtrlIcon_Folder, TreeCtrlIcon_Folder, | |
218 | new MyTreeItemData("Root item")); | |
219 | ||
220 | AddItemsRecursively(rootId, numChildren, depth,0); | |
221 | } | |
222 | ||
223 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
224 | { | |
225 | wxTreeItemId id; | |
226 | ||
227 | if( cookie == -1 ) | |
228 | id = GetFirstChild(idParent, cookie); | |
229 | else | |
230 | id = GetNextChild(idParent, cookie); | |
231 | ||
232 | if(id <= 0) | |
233 | return; | |
234 | ||
235 | wxString text=GetItemText(id); | |
236 | wxLogMessage(text); | |
237 | ||
238 | if (ItemHasChildren(id)) | |
239 | GetItemsRecursively(id,-1); | |
240 | ||
241 | GetItemsRecursively(idParent, cookie); | |
242 | } | |
243 | ||
244 | ||
245 | // avoid repetition | |
246 | #define TREE_EVENT_HANDLER(name) \ | |
247 | void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \ | |
248 | { \ | |
249 | wxLogMessage(#name); \ | |
250 | } | |
251 | ||
252 | TREE_EVENT_HANDLER(OnBeginDrag) | |
253 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
254 | TREE_EVENT_HANDLER(OnBeginLabelEdit) | |
255 | TREE_EVENT_HANDLER(OnEndLabelEdit) | |
256 | TREE_EVENT_HANDLER(OnDeleteItem) | |
257 | TREE_EVENT_HANDLER(OnGetInfo) | |
258 | TREE_EVENT_HANDLER(OnSetInfo) | |
259 | TREE_EVENT_HANDLER(OnItemExpanded) | |
260 | TREE_EVENT_HANDLER(OnItemExpanding) | |
261 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
262 | TREE_EVENT_HANDLER(OnSelChanged) | |
263 | TREE_EVENT_HANDLER(OnSelChanging) | |
264 | ||
265 | #undef TREE_EVENT_HANDLER | |
266 | ||
267 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) | |
268 | { | |
269 | wxLogMessage("OnItemCollapsing"); | |
270 | ||
271 | // for testing, prevent the user from collapsing the root item | |
272 | wxTreeItemId itemId = event.GetItem(); | |
273 | if ( !GetParent(itemId).IsOk() ) | |
274 | { | |
275 | wxMessageBox("You can't collapse the root item."); | |
276 | ||
277 | event.Veto(); | |
278 | } | |
279 | } | |
280 | ||
281 | void MyTreeCtrl::OnKeyDown(wxTreeEvent& WXUNUSED(event)) | |
282 | { | |
283 | // show some info about this item | |
284 | wxTreeItemId itemId = GetSelection(); | |
285 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
286 | ||
287 | if ( item != NULL ) | |
288 | { | |
289 | item->ShowInfo(this); | |
290 | } | |
291 | ||
292 | wxLogMessage("OnKeyDown"); | |
293 | } | |
294 | ||
295 | static inline const char *Bool2String(bool b) | |
296 | { | |
297 | return b ? "" : "not "; | |
298 | } | |
299 | ||
300 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) | |
301 | { | |
302 | wxLogMessage("Item '%s': %sselected, %sexpanded, " | |
303 | "%u children (%u immediately under this item).", | |
304 | m_desc.c_str(), | |
305 | Bool2String(tree->IsSelected(GetId())), | |
306 | Bool2String(tree->IsExpanded(GetId())), | |
307 | tree->GetChildrenCount(GetId()), | |
308 | tree->GetChildrenCount(GetId(), FALSE)); | |
309 | } |