]>
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 | // verify that the item is ok and insult the user if it is not | |
43 | #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ | |
44 | wxMessageBox("Please select some item first!", \ | |
45 | "Tree sample error", \ | |
46 | wxOK | wxICON_EXCLAMATION, \ | |
47 | this); \ | |
48 | return; \ | |
49 | } | |
50 | ||
51 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
52 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) | |
53 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
54 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) | |
55 | EVT_MENU(TreeTest_Rename, MyFrame::OnRename) | |
56 | EVT_MENU(TreeTest_Sort, MyFrame::OnSort) | |
57 | EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev) | |
58 | EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold) | |
59 | EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold) | |
60 | EVT_MENU(TreeTest_Delete, MyFrame::OnDelete) | |
61 | EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren) | |
62 | EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll) | |
63 | EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate) | |
64 | END_EVENT_TABLE() | |
65 | ||
66 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
67 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) | |
68 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
69 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
70 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
71 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
72 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) | |
73 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) | |
74 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
75 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
76 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
77 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
78 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
79 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
80 | EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) | |
81 | EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) | |
82 | END_EVENT_TABLE() | |
83 | ||
84 | IMPLEMENT_APP(MyApp) | |
85 | ||
86 | bool MyApp::OnInit() | |
87 | { | |
88 | // Create the main frame window | |
89 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340); | |
90 | ||
91 | // Show the frame | |
92 | frame->Show(TRUE); | |
93 | SetTopWindow(frame); | |
94 | ||
95 | return TRUE; | |
96 | } | |
97 | ||
98 | ||
99 | // My frame constructor | |
100 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
101 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)) | |
102 | { | |
103 | // This reduces flicker effects - even better would be to define | |
104 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
105 | // show or hidden, the frame is sent a background erase event. | |
106 | SetBackgroundColour(wxColour(255, 255, 255)); | |
107 | ||
108 | // Give it an icon | |
109 | SetIcon(wxICON(mondrian)); | |
110 | ||
111 | // Make a menubar | |
112 | wxMenu *file_menu = new wxMenu, | |
113 | *tree_menu = new wxMenu; | |
114 | ||
115 | file_menu->Append(TreeTest_About, "&About..."); | |
116 | file_menu->AppendSeparator(); | |
117 | file_menu->Append(TreeTest_Quit, "E&xit"); | |
118 | ||
119 | tree_menu->Append(TreeTest_Dump, "D&ump tree items"); | |
120 | tree_menu->Append(TreeTest_Recreate, "&Recreate the tree"); | |
121 | tree_menu->AppendSeparator(); | |
122 | tree_menu->Append(TreeTest_Delete, "&Delete this item"); | |
123 | tree_menu->Append(TreeTest_DeleteChildren, "Delete &children"); | |
124 | tree_menu->Append(TreeTest_DeleteAll, "Delete &all items"); | |
125 | tree_menu->AppendSeparator(); | |
126 | tree_menu->Append(TreeTest_Sort, "Sort children of current item"); | |
127 | tree_menu->Append(TreeTest_SortRev, "Sort in reversed order"); | |
128 | tree_menu->Append(TreeTest_Rename, "Rename item..."); | |
129 | tree_menu->AppendSeparator(); | |
130 | tree_menu->Append(TreeTest_Bold, "Make item &bold"); | |
131 | tree_menu->Append(TreeTest_UnBold, "Make item ¬ bold"); | |
132 | ||
133 | wxMenuBar *menu_bar = new wxMenuBar; | |
134 | menu_bar->Append(file_menu, "&File"); | |
135 | menu_bar->Append(tree_menu, "&Tree"); | |
136 | SetMenuBar(menu_bar); | |
137 | ||
138 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
139 | wxDefaultPosition, wxDefaultSize, | |
140 | wxTR_HAS_BUTTONS | wxSUNKEN_BORDER); | |
141 | wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "", | |
142 | wxDefaultPosition, wxDefaultSize, | |
143 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
144 | ||
145 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
146 | c->top.SameAs(this, wxTop); | |
147 | c->left.SameAs(this, wxLeft); | |
148 | c->right.SameAs(this, wxRight); | |
149 | c->height.PercentOf(this, wxHeight, 66); | |
150 | m_treeCtrl->SetConstraints(c); | |
151 | ||
152 | c = new wxLayoutConstraints; | |
153 | c->top.Below(m_treeCtrl); | |
154 | c->left.SameAs(this, wxLeft); | |
155 | c->right.SameAs(this, wxRight); | |
156 | c->bottom.SameAs(this, wxBottom); | |
157 | textCtrl->SetConstraints(c); | |
158 | SetAutoLayout(TRUE); | |
159 | ||
160 | // create a status bar with 3 panes | |
161 | CreateStatusBar(3); | |
162 | SetStatusText("", 0); | |
163 | ||
164 | #ifdef __WXMOTIF__ | |
165 | // For some reason, we get a memcpy crash in wxLogStream::DoLogStream | |
166 | // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc? | |
167 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
168 | #else | |
169 | // set our text control as the log target | |
170 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl); | |
171 | delete wxLog::SetActiveTarget(logWindow); | |
172 | #endif | |
173 | } | |
174 | ||
175 | MyFrame::~MyFrame() | |
176 | { | |
177 | delete wxLog::SetActiveTarget(NULL); | |
178 | } | |
179 | ||
180 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
181 | { | |
182 | Close(TRUE); | |
183 | } | |
184 | ||
185 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
186 | { | |
187 | wxMessageBox("Tree test sample\n" | |
188 | "Julian Smart (c) 1997,\n" | |
189 | "Vadim Zeitlin (c) 1998", | |
190 | "About tree test", | |
191 | wxOK | wxICON_INFORMATION, this); | |
192 | } | |
193 | ||
194 | void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) | |
195 | { | |
196 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
197 | ||
198 | CHECK_ITEM( item ); | |
199 | ||
200 | static wxString s_text; | |
201 | s_text = wxGetTextFromUser("New name: ", "Tree sample question", | |
202 | s_text, this); | |
203 | if ( !s_text.IsEmpty() ) | |
204 | { | |
205 | m_treeCtrl->SetItemText(item, s_text); | |
206 | } | |
207 | } | |
208 | ||
209 | void MyFrame::DoSort(bool reverse) | |
210 | { | |
211 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
212 | ||
213 | CHECK_ITEM( item ); | |
214 | ||
215 | m_treeCtrl->DoSortChildren(item, reverse); | |
216 | } | |
217 | ||
218 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) | |
219 | { | |
220 | wxTreeItemId root = m_treeCtrl->GetSelection(); | |
221 | ||
222 | CHECK_ITEM( root ); | |
223 | ||
224 | m_treeCtrl->GetItemsRecursively(root, -1); | |
225 | } | |
226 | ||
227 | void MyFrame::DoSetBold(bool bold) | |
228 | { | |
229 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
230 | ||
231 | CHECK_ITEM( item ); | |
232 | ||
233 | m_treeCtrl->SetItemBold(item, bold); | |
234 | } | |
235 | ||
236 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
237 | { | |
238 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
239 | ||
240 | CHECK_ITEM( item ); | |
241 | ||
242 | m_treeCtrl->Delete(item); | |
243 | } | |
244 | ||
245 | void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event)) | |
246 | { | |
247 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
248 | ||
249 | CHECK_ITEM( item ); | |
250 | ||
251 | m_treeCtrl->DeleteChildren(item); | |
252 | } | |
253 | ||
254 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) | |
255 | { | |
256 | m_treeCtrl->DeleteAllItems(); | |
257 | } | |
258 | ||
259 | void MyFrame::OnRecreate(wxCommandEvent& event) | |
260 | { | |
261 | OnDeleteAll(event); | |
262 | m_treeCtrl->AddTestItemsToTree(3, 2); | |
263 | } | |
264 | ||
265 | // MyTreeCtrl implementation | |
266 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl) | |
267 | ||
268 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, | |
269 | const wxPoint& pos, const wxSize& size, | |
270 | long style) | |
271 | : wxTreeCtrl(parent, id, pos, size, style) | |
272 | { | |
273 | m_reverseSort = FALSE; | |
274 | ||
275 | // Make an image list containing small icons | |
276 | m_imageListNormal = new wxImageList(16, 16, TRUE); | |
277 | ||
278 | // should correspond to TreeCtrlIcon_xxx enum | |
279 | #if defined(__WXMSW__) && defined(__WIN16__) | |
280 | // This is required in 16-bit Windows mode only because we can't load a specific (16x16) | |
281 | // icon image, so it comes out stretched | |
282 | m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE)); | |
283 | m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE)); | |
284 | #else | |
285 | m_imageListNormal->Add(wxICON(icon1)); | |
286 | m_imageListNormal->Add(wxICON(icon2)); | |
287 | #endif | |
288 | ||
289 | SetImageList(m_imageListNormal); | |
290 | ||
291 | // Add some items to the tree | |
292 | AddTestItemsToTree(3, 2); | |
293 | } | |
294 | ||
295 | MyTreeCtrl::~MyTreeCtrl() | |
296 | { | |
297 | delete m_imageListNormal; | |
298 | } | |
299 | ||
300 | int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, | |
301 | const wxTreeItemId& item2) | |
302 | { | |
303 | if ( m_reverseSort ) | |
304 | { | |
305 | // just exchange 1st and 2nd items | |
306 | return wxTreeCtrl::OnCompareItems(item2, item1); | |
307 | } | |
308 | else | |
309 | { | |
310 | return wxTreeCtrl::OnCompareItems(item1, item2); | |
311 | } | |
312 | } | |
313 | ||
314 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, | |
315 | size_t numChildren, | |
316 | size_t depth, | |
317 | size_t folder) | |
318 | { | |
319 | if ( depth > 0 ) | |
320 | { | |
321 | wxString str; | |
322 | for ( size_t n = 0; n < numChildren; n++ ) | |
323 | { | |
324 | // at depth 1 elements won't have any more children | |
325 | if (depth == 1) | |
326 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
327 | else | |
328 | str.Printf("%s child %d","Folder", n + 1); | |
329 | ||
330 | int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; | |
331 | wxTreeItemId id = AppendItem(idParent, str, image, image, | |
332 | new MyTreeItemData(str)); | |
333 | AddItemsRecursively(id, numChildren, depth - 1, n + 1); | |
334 | } | |
335 | } | |
336 | //else: done! | |
337 | } | |
338 | ||
339 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, | |
340 | size_t depth) | |
341 | { | |
342 | wxTreeItemId rootId = AddRoot("Root", | |
343 | TreeCtrlIcon_Folder, TreeCtrlIcon_Folder, | |
344 | new MyTreeItemData("Root item")); | |
345 | ||
346 | AddItemsRecursively(rootId, numChildren, depth, 0); | |
347 | } | |
348 | ||
349 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
350 | { | |
351 | wxTreeItemId id; | |
352 | ||
353 | if( cookie == -1 ) | |
354 | id = GetFirstChild(idParent, cookie); | |
355 | else | |
356 | id = GetNextChild(idParent, cookie); | |
357 | ||
358 | if(id <= 0) | |
359 | return; | |
360 | ||
361 | wxString text=GetItemText(id); | |
362 | wxLogMessage(text); | |
363 | ||
364 | if (ItemHasChildren(id)) | |
365 | GetItemsRecursively(id,-1); | |
366 | ||
367 | GetItemsRecursively(idParent, cookie); | |
368 | } | |
369 | ||
370 | ||
371 | // avoid repetition | |
372 | #define TREE_EVENT_HANDLER(name) \ | |
373 | void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \ | |
374 | { \ | |
375 | wxLogMessage(#name); \ | |
376 | } | |
377 | ||
378 | TREE_EVENT_HANDLER(OnBeginDrag) | |
379 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
380 | TREE_EVENT_HANDLER(OnBeginLabelEdit) | |
381 | TREE_EVENT_HANDLER(OnEndLabelEdit) | |
382 | TREE_EVENT_HANDLER(OnDeleteItem) | |
383 | TREE_EVENT_HANDLER(OnGetInfo) | |
384 | TREE_EVENT_HANDLER(OnSetInfo) | |
385 | TREE_EVENT_HANDLER(OnItemExpanded) | |
386 | TREE_EVENT_HANDLER(OnItemExpanding) | |
387 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
388 | TREE_EVENT_HANDLER(OnSelChanged) | |
389 | TREE_EVENT_HANDLER(OnSelChanging) | |
390 | ||
391 | #undef TREE_EVENT_HANDLER | |
392 | ||
393 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) | |
394 | { | |
395 | wxLogMessage("OnItemCollapsing"); | |
396 | ||
397 | // for testing, prevent the user from collapsing the root item | |
398 | wxTreeItemId itemId = event.GetItem(); | |
399 | if ( !GetParent(itemId).IsOk() ) | |
400 | { | |
401 | wxMessageBox("You can't collapse the root item."); | |
402 | ||
403 | event.Veto(); | |
404 | } | |
405 | } | |
406 | ||
407 | void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event)) | |
408 | { | |
409 | wxLogMessage("OnTreeKeyDown"); | |
410 | } | |
411 | ||
412 | void MyTreeCtrl::OnItemActivated(wxTreeEvent&WXUNUSED(event)) | |
413 | { | |
414 | // show some info about this item | |
415 | wxTreeItemId itemId = GetSelection(); | |
416 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
417 | ||
418 | if ( item != NULL ) | |
419 | { | |
420 | item->ShowInfo(this); | |
421 | } | |
422 | ||
423 | wxLogMessage("OnItemActivated"); | |
424 | } | |
425 | ||
426 | static inline const char *Bool2String(bool b) | |
427 | { | |
428 | return b ? "" : "not "; | |
429 | } | |
430 | ||
431 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) | |
432 | { | |
433 | wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n" | |
434 | "%u children (%u immediately under this item).", | |
435 | m_desc.c_str(), | |
436 | Bool2String(tree->IsSelected(GetId())), | |
437 | Bool2String(tree->IsExpanded(GetId())), | |
438 | Bool2String(tree->IsBold(GetId())), | |
439 | tree->GetChildrenCount(GetId()), | |
440 | tree->GetChildrenCount(GetId(), FALSE)); | |
441 | } |