]>
Commit | Line | Data |
---|---|---|
457814b5 JS |
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 | |
3a5a2f56 | 9 | // Licence: wxWindows license |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
3a5a2f56 VZ |
13 | #pragma implementation |
14 | #pragma interface | |
457814b5 JS |
15 | #endif |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
3a5a2f56 | 21 | #pragma hdrstop |
457814b5 JS |
22 | #endif |
23 | ||
24 | #ifndef WX_PRECOMP | |
3a5a2f56 | 25 | #include "wx/wx.h" |
457814b5 JS |
26 | #endif |
27 | ||
3a5a2f56 | 28 | // under Windows the icons are in the .rc file |
c89a6106 VZ |
29 | #ifndef __WXMSW__ |
30 | #include "icon1.xpm" | |
31 | #include "icon2.xpm" | |
32 | #include "mondrian.xpm" | |
47908e25 RR |
33 | #endif |
34 | ||
3a5a2f56 VZ |
35 | #include "wx/log.h" |
36 | ||
37 | #include "wx/imaglist.h" | |
457814b5 JS |
38 | #include "wx/treectrl.h" |
39 | ||
cf724bce RR |
40 | #include "math.h" |
41 | ||
457814b5 JS |
42 | #include "treetest.h" |
43 | ||
e1ee62bd VZ |
44 | // verify that the item is ok and insult the user if it is not |
45 | #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ | |
46 | wxMessageBox("Please select some item first!", \ | |
47 | "Tree sample error", \ | |
48 | wxOK | wxICON_EXCLAMATION, \ | |
49 | this); \ | |
50 | return; \ | |
5f939e78 VZ |
51 | } |
52 | ||
457814b5 | 53 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
5f939e78 VZ |
54 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) |
55 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
56 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) | |
57 | EVT_MENU(TreeTest_Rename, MyFrame::OnRename) | |
58 | EVT_MENU(TreeTest_Sort, MyFrame::OnSort) | |
59 | EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev) | |
60 | EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold) | |
61 | EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold) | |
62 | EVT_MENU(TreeTest_Delete, MyFrame::OnDelete) | |
63 | EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren) | |
64 | EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll) | |
65 | EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate) | |
66 | EVT_MENU(TreeTest_CollapseAndReset, MyFrame::OnCollapseAndReset) | |
67 | EVT_MENU(TreeTest_EnsureVisible, MyFrame::OnEnsureVisible) | |
68 | EVT_MENU(TreeTest_AddItem, MyFrame::OnAddItem) | |
cf724bce RR |
69 | EVT_MENU(TreeTest_IncIndent, MyFrame::OnIncIndent) |
70 | EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent) | |
71 | EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing) | |
72 | EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing) | |
457814b5 JS |
73 | END_EVENT_TABLE() |
74 | ||
75 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
5f939e78 VZ |
76 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) |
77 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
78 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
79 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
80 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
81 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) | |
82 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) | |
83 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
84 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
85 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
86 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
87 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
88 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
89 | EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) | |
90 | EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) | |
91 | EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick) | |
457814b5 JS |
92 | END_EVENT_TABLE() |
93 | ||
94 | IMPLEMENT_APP(MyApp) | |
95 | ||
c89a6106 | 96 | bool MyApp::OnInit() |
457814b5 | 97 | { |
5f939e78 VZ |
98 | // Create the main frame window |
99 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340); | |
457814b5 | 100 | |
5f939e78 VZ |
101 | // Show the frame |
102 | frame->Show(TRUE); | |
103 | SetTopWindow(frame); | |
457814b5 | 104 | |
5f939e78 | 105 | return TRUE; |
3a5a2f56 | 106 | } |
457814b5 | 107 | |
457814b5 | 108 | |
3a5a2f56 VZ |
109 | // My frame constructor |
110 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
111 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)) | |
112 | { | |
5f939e78 VZ |
113 | // This reduces flicker effects - even better would be to define |
114 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
115 | // show or hidden, the frame is sent a background erase event. | |
116 | SetBackgroundColour(wxColour(255, 255, 255)); | |
117 | ||
118 | // Give it an icon | |
119 | SetIcon(wxICON(mondrian)); | |
120 | ||
121 | // Make a menubar | |
122 | wxMenu *file_menu = new wxMenu, | |
123 | *tree_menu = new wxMenu, | |
124 | *item_menu = new wxMenu; | |
125 | ||
126 | file_menu->Append(TreeTest_About, "&About..."); | |
127 | file_menu->AppendSeparator(); | |
128 | file_menu->Append(TreeTest_Quit, "E&xit"); | |
129 | ||
130 | tree_menu->Append(TreeTest_Recreate, "&Recreate the tree"); | |
131 | tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset"); | |
132 | tree_menu->AppendSeparator(); | |
133 | tree_menu->Append(TreeTest_AddItem, "Append a &new item"); | |
134 | tree_menu->Append(TreeTest_Delete, "&Delete this item"); | |
135 | tree_menu->Append(TreeTest_DeleteChildren, "Delete &children"); | |
136 | tree_menu->Append(TreeTest_DeleteAll, "Delete &all items"); | |
137 | tree_menu->AppendSeparator(); | |
138 | tree_menu->Append(TreeTest_Sort, "Sort children of current item"); | |
139 | tree_menu->Append(TreeTest_SortRev, "Sort in reversed order"); | |
140 | tree_menu->AppendSeparator(); | |
141 | tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible"); | |
cf724bce RR |
142 | tree_menu->AppendSeparator(); |
143 | tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I"); | |
144 | tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R"); | |
145 | tree_menu->AppendSeparator(); | |
146 | tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I"); | |
147 | tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R"); | |
5f939e78 VZ |
148 | |
149 | item_menu->Append(TreeTest_Dump, "&Dump item children"); | |
150 | item_menu->AppendSeparator(); | |
151 | item_menu->Append(TreeTest_Rename, "&Rename item..."); | |
152 | item_menu->AppendSeparator(); | |
153 | item_menu->Append(TreeTest_Bold, "Make item &bold"); | |
154 | item_menu->Append(TreeTest_UnBold, "Make item ¬ bold"); | |
155 | ||
156 | wxMenuBar *menu_bar = new wxMenuBar; | |
157 | menu_bar->Append(file_menu, "&File"); | |
158 | menu_bar->Append(tree_menu, "&Tree"); | |
159 | menu_bar->Append(item_menu, "&Item"); | |
160 | SetMenuBar(menu_bar); | |
161 | ||
162 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
163 | wxDefaultPosition, wxDefaultSize, | |
164 | wxTR_HAS_BUTTONS | wxSUNKEN_BORDER); | |
165 | wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "", | |
166 | wxDefaultPosition, wxDefaultSize, | |
167 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
168 | ||
169 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
170 | c->top.SameAs(this, wxTop); | |
171 | c->left.SameAs(this, wxLeft); | |
172 | c->right.SameAs(this, wxRight); | |
173 | c->height.PercentOf(this, wxHeight, 66); | |
174 | m_treeCtrl->SetConstraints(c); | |
175 | ||
176 | c = new wxLayoutConstraints; | |
177 | c->top.Below(m_treeCtrl); | |
178 | c->left.SameAs(this, wxLeft); | |
179 | c->right.SameAs(this, wxRight); | |
180 | c->bottom.SameAs(this, wxBottom); | |
181 | textCtrl->SetConstraints(c); | |
182 | SetAutoLayout(TRUE); | |
183 | ||
184 | // create a status bar with 3 panes | |
185 | CreateStatusBar(3); | |
186 | SetStatusText("", 0); | |
3a5a2f56 | 187 | |
a367b9b3 | 188 | #ifdef __WXMOTIF__ |
5f939e78 VZ |
189 | // For some reason, we get a memcpy crash in wxLogStream::DoLogStream |
190 | // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc? | |
191 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
a367b9b3 | 192 | #else |
5f939e78 VZ |
193 | // set our text control as the log target |
194 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl); | |
195 | delete wxLog::SetActiveTarget(logWindow); | |
a367b9b3 | 196 | #endif |
457814b5 JS |
197 | } |
198 | ||
c89a6106 | 199 | MyFrame::~MyFrame() |
457814b5 | 200 | { |
5f939e78 | 201 | delete wxLog::SetActiveTarget(NULL); |
457814b5 JS |
202 | } |
203 | ||
3a5a2f56 | 204 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 205 | { |
5f939e78 | 206 | Close(TRUE); |
457814b5 JS |
207 | } |
208 | ||
3a5a2f56 | 209 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 210 | { |
5f939e78 VZ |
211 | wxMessageBox("Tree test sample\n" |
212 | "Julian Smart (c) 1997,\n" | |
213 | "Vadim Zeitlin (c) 1998", | |
214 | "About tree test", | |
215 | wxOK | wxICON_INFORMATION, this); | |
e1ee62bd VZ |
216 | } |
217 | ||
218 | void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) | |
219 | { | |
5f939e78 | 220 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 221 | |
5f939e78 | 222 | CHECK_ITEM( item ); |
e1ee62bd | 223 | |
5f939e78 VZ |
224 | static wxString s_text; |
225 | s_text = wxGetTextFromUser("New name: ", "Tree sample question", | |
226 | s_text, this); | |
227 | if ( !s_text.IsEmpty() ) | |
228 | { | |
229 | m_treeCtrl->SetItemText(item, s_text); | |
230 | } | |
e1ee62bd VZ |
231 | } |
232 | ||
233 | void MyFrame::DoSort(bool reverse) | |
234 | { | |
5f939e78 | 235 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 236 | |
5f939e78 | 237 | CHECK_ITEM( item ); |
457814b5 | 238 | |
5f939e78 | 239 | m_treeCtrl->DoSortChildren(item, reverse); |
457814b5 JS |
240 | } |
241 | ||
4832f7c0 VZ |
242 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) |
243 | { | |
5f939e78 | 244 | wxTreeItemId root = m_treeCtrl->GetSelection(); |
e1ee62bd | 245 | |
5f939e78 | 246 | CHECK_ITEM( root ); |
e1ee62bd | 247 | |
5f939e78 | 248 | m_treeCtrl->GetItemsRecursively(root, -1); |
4832f7c0 VZ |
249 | } |
250 | ||
add28c55 VZ |
251 | void MyFrame::DoSetBold(bool bold) |
252 | { | |
5f939e78 | 253 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 254 | |
5f939e78 | 255 | CHECK_ITEM( item ); |
e1ee62bd | 256 | |
5f939e78 | 257 | m_treeCtrl->SetItemBold(item, bold); |
add28c55 VZ |
258 | } |
259 | ||
ff5bf259 VZ |
260 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) |
261 | { | |
5f939e78 | 262 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 263 | |
5f939e78 | 264 | CHECK_ITEM( item ); |
e1ee62bd | 265 | |
5f939e78 | 266 | m_treeCtrl->Delete(item); |
ff5bf259 VZ |
267 | } |
268 | ||
372edb9d VZ |
269 | void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event)) |
270 | { | |
5f939e78 | 271 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 272 | |
5f939e78 | 273 | CHECK_ITEM( item ); |
e1ee62bd | 274 | |
5f939e78 | 275 | m_treeCtrl->DeleteChildren(item); |
372edb9d VZ |
276 | } |
277 | ||
ff5bf259 VZ |
278 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) |
279 | { | |
5f939e78 | 280 | m_treeCtrl->DeleteAllItems(); |
ff5bf259 VZ |
281 | } |
282 | ||
283 | void MyFrame::OnRecreate(wxCommandEvent& event) | |
284 | { | |
5f939e78 VZ |
285 | OnDeleteAll(event); |
286 | m_treeCtrl->AddTestItemsToTree(3, 2); | |
ff5bf259 VZ |
287 | } |
288 | ||
dd3646fd VZ |
289 | void MyFrame::OnCollapseAndReset(wxCommandEvent& event) |
290 | { | |
5f939e78 | 291 | m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem()); |
dd3646fd VZ |
292 | } |
293 | ||
f65635b5 VZ |
294 | void MyFrame::OnEnsureVisible(wxCommandEvent& event) |
295 | { | |
296 | m_treeCtrl->DoEnsureVisible(); | |
297 | } | |
298 | ||
5f939e78 VZ |
299 | void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event)) |
300 | { | |
301 | static int s_num = 0; | |
302 | ||
303 | wxString text; | |
304 | text.Printf("Item #%d", ++s_num); | |
305 | ||
306 | m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), | |
307 | text, | |
308 | MyTreeCtrl::TreeCtrlIcon_File); | |
309 | } | |
310 | ||
cf724bce RR |
311 | void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event)) |
312 | { | |
313 | unsigned int indent = m_treeCtrl->GetIndent(); | |
314 | if (indent < 100) | |
315 | m_treeCtrl->SetIndent( indent+5 ); | |
316 | } | |
317 | ||
318 | void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event)) | |
319 | { | |
320 | unsigned int indent = m_treeCtrl->GetIndent(); | |
321 | if (indent > 10) | |
322 | m_treeCtrl->SetIndent( indent-5 ); | |
323 | } | |
324 | ||
325 | void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event)) | |
326 | { | |
327 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
328 | if (indent < 100) | |
329 | m_treeCtrl->SetSpacing( indent+5 ); | |
330 | } | |
331 | ||
332 | void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event)) | |
333 | { | |
334 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
335 | if (indent > 10) | |
336 | m_treeCtrl->SetSpacing( indent-5 ); | |
337 | } | |
338 | ||
3a5a2f56 | 339 | // MyTreeCtrl implementation |
23fd5130 VZ |
340 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl) |
341 | ||
3a5a2f56 VZ |
342 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, |
343 | const wxPoint& pos, const wxSize& size, | |
344 | long style) | |
345 | : wxTreeCtrl(parent, id, pos, size, style) | |
457814b5 | 346 | { |
5f939e78 | 347 | m_reverseSort = FALSE; |
e1ee62bd | 348 | |
5f939e78 VZ |
349 | // Make an image list containing small icons |
350 | m_imageListNormal = new wxImageList(16, 16, TRUE); | |
457814b5 | 351 | |
5f939e78 | 352 | // should correspond to TreeCtrlIcon_xxx enum |
f60d0f94 | 353 | #if defined(__WXMSW__) && defined(__WIN16__) |
5f939e78 VZ |
354 | // This is required in 16-bit Windows mode only because we can't load a specific (16x16) |
355 | // icon image, so it comes out stretched | |
356 | m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE)); | |
357 | m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE)); | |
f60d0f94 | 358 | #else |
5f939e78 VZ |
359 | m_imageListNormal->Add(wxICON(icon1)); |
360 | m_imageListNormal->Add(wxICON(icon2)); | |
f60d0f94 | 361 | #endif |
457814b5 | 362 | |
5f939e78 | 363 | SetImageList(m_imageListNormal); |
457814b5 | 364 | |
5f939e78 VZ |
365 | // Add some items to the tree |
366 | AddTestItemsToTree(3, 2); | |
457814b5 JS |
367 | } |
368 | ||
3a5a2f56 | 369 | MyTreeCtrl::~MyTreeCtrl() |
457814b5 | 370 | { |
5f939e78 | 371 | delete m_imageListNormal; |
457814b5 JS |
372 | } |
373 | ||
e1ee62bd VZ |
374 | int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
375 | const wxTreeItemId& item2) | |
376 | { | |
5f939e78 VZ |
377 | if ( m_reverseSort ) |
378 | { | |
379 | // just exchange 1st and 2nd items | |
380 | return wxTreeCtrl::OnCompareItems(item2, item1); | |
381 | } | |
382 | else | |
383 | { | |
384 | return wxTreeCtrl::OnCompareItems(item1, item2); | |
385 | } | |
e1ee62bd VZ |
386 | } |
387 | ||
3a5a2f56 VZ |
388 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, |
389 | size_t numChildren, | |
4832f7c0 VZ |
390 | size_t depth, |
391 | size_t folder) | |
457814b5 | 392 | { |
f65635b5 | 393 | if ( depth > 0 ) |
3a5a2f56 | 394 | { |
f65635b5 VZ |
395 | wxString str; |
396 | for ( size_t n = 0; n < numChildren; n++ ) | |
397 | { | |
398 | // at depth 1 elements won't have any more children | |
399 | if (depth == 1) | |
400 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
401 | else | |
402 | str.Printf("%s child %d", "Folder", n + 1); | |
403 | ||
404 | int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; | |
405 | wxTreeItemId id = AppendItem(idParent, str, image, image, | |
406 | new MyTreeItemData(str)); | |
407 | ||
408 | // remember the last child for OnEnsureVisible() | |
409 | if ( depth == 1 && n == numChildren - 1 ) | |
410 | { | |
411 | m_lastItem = id; | |
412 | } | |
413 | ||
414 | AddItemsRecursively(id, numChildren, depth - 1, n + 1); | |
415 | } | |
3a5a2f56 | 416 | } |
f65635b5 | 417 | //else: done! |
457814b5 JS |
418 | } |
419 | ||
3a5a2f56 VZ |
420 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, |
421 | size_t depth) | |
457814b5 | 422 | { |
5f939e78 VZ |
423 | wxTreeItemId rootId = AddRoot("Root", |
424 | TreeCtrlIcon_Folder, TreeCtrlIcon_Folder, | |
425 | new MyTreeItemData("Root item")); | |
457814b5 | 426 | |
5f939e78 | 427 | AddItemsRecursively(rootId, numChildren, depth, 0); |
4832f7c0 VZ |
428 | } |
429 | ||
430 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
431 | { | |
5f939e78 | 432 | wxTreeItemId id; |
4832f7c0 | 433 | |
5f939e78 VZ |
434 | if( cookie == -1 ) |
435 | id = GetFirstChild(idParent, cookie); | |
436 | else | |
437 | id = GetNextChild(idParent, cookie); | |
4832f7c0 | 438 | |
5f939e78 VZ |
439 | if(id <= 0) |
440 | return; | |
4832f7c0 | 441 | |
5f939e78 VZ |
442 | wxString text=GetItemText(id); |
443 | wxLogMessage(text); | |
4832f7c0 | 444 | |
5f939e78 VZ |
445 | if (ItemHasChildren(id)) |
446 | GetItemsRecursively(id,-1); | |
4832f7c0 | 447 | |
5f939e78 | 448 | GetItemsRecursively(idParent, cookie); |
457814b5 JS |
449 | } |
450 | ||
4832f7c0 | 451 | |
3a5a2f56 | 452 | // avoid repetition |
5f939e78 VZ |
453 | #define TREE_EVENT_HANDLER(name) \ |
454 | void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \ | |
455 | { \ | |
456 | wxLogMessage(#name); \ | |
457 | } | |
457814b5 | 458 | |
3a5a2f56 VZ |
459 | TREE_EVENT_HANDLER(OnBeginDrag) |
460 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
461 | TREE_EVENT_HANDLER(OnBeginLabelEdit) | |
462 | TREE_EVENT_HANDLER(OnEndLabelEdit) | |
463 | TREE_EVENT_HANDLER(OnDeleteItem) | |
464 | TREE_EVENT_HANDLER(OnGetInfo) | |
465 | TREE_EVENT_HANDLER(OnSetInfo) | |
466 | TREE_EVENT_HANDLER(OnItemExpanded) | |
467 | TREE_EVENT_HANDLER(OnItemExpanding) | |
468 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
469 | TREE_EVENT_HANDLER(OnSelChanged) | |
470 | TREE_EVENT_HANDLER(OnSelChanging) | |
471 | ||
472 | #undef TREE_EVENT_HANDLER | |
473 | ||
474 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) | |
457814b5 | 475 | { |
5f939e78 | 476 | wxLogMessage("OnItemCollapsing"); |
f135ff73 | 477 | |
5f939e78 VZ |
478 | // for testing, prevent the user from collapsing the first child folder |
479 | wxTreeItemId itemId = event.GetItem(); | |
480 | if ( GetParent(itemId) == GetRootItem() && !GetPrevSibling(itemId) ) | |
481 | { | |
482 | wxMessageBox("You can't collapse this item."); | |
457814b5 | 483 | |
5f939e78 VZ |
484 | event.Veto(); |
485 | } | |
457814b5 JS |
486 | } |
487 | ||
6daa0637 | 488 | void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event)) |
435fe83e | 489 | { |
5f939e78 | 490 | wxLogMessage("OnTreeKeyDown"); |
435fe83e RR |
491 | } |
492 | ||
493 | void MyTreeCtrl::OnItemActivated(wxTreeEvent&WXUNUSED(event)) | |
457814b5 | 494 | { |
5f939e78 VZ |
495 | // show some info about this item |
496 | wxTreeItemId itemId = GetSelection(); | |
497 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
457814b5 | 498 | |
5f939e78 VZ |
499 | if ( item != NULL ) |
500 | { | |
501 | item->ShowInfo(this); | |
502 | } | |
457814b5 | 503 | |
5f939e78 VZ |
504 | wxLogMessage("OnItemActivated"); |
505 | } | |
506 | ||
507 | void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) | |
508 | { | |
509 | wxTreeItemId id = HitTest(event.GetPosition()); | |
510 | if ( !id ) | |
511 | wxLogMessage("No item under mouse"); | |
512 | else | |
513 | { | |
514 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(id); | |
515 | if ( item ) | |
516 | wxLogMessage("Item '%s' under mouse", item->GetDesc()); | |
517 | } | |
457814b5 JS |
518 | } |
519 | ||
3a5a2f56 | 520 | static inline const char *Bool2String(bool b) |
457814b5 | 521 | { |
5f939e78 | 522 | return b ? "" : "not "; |
457814b5 JS |
523 | } |
524 | ||
9769e589 | 525 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) |
457814b5 | 526 | { |
5f939e78 VZ |
527 | wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n" |
528 | "%u children (%u immediately under this item).", | |
529 | m_desc.c_str(), | |
530 | Bool2String(tree->IsSelected(GetId())), | |
531 | Bool2String(tree->IsExpanded(GetId())), | |
532 | Bool2String(tree->IsBold(GetId())), | |
533 | tree->GetChildrenCount(GetId()), | |
534 | tree->GetChildrenCount(GetId(), FALSE)); | |
457814b5 | 535 | } |