]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
part of Ictrl -> Iscntrl change I forgot to commit (not important, anyhow)
[wxWidgets.git] / samples / treectrl / treetest.cpp
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
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 #include "wx/log.h"
27 #endif
28
29 #include "wx/colordlg.h"
30
31 #include "wx/image.h"
32 #include "wx/imaglist.h"
33 #include "wx/treectrl.h"
34
35 #include "math.h"
36
37 #ifdef __WIN32__
38 // this is not supported by native control
39 #define NO_VARIABLE_HEIGHT
40 #endif
41
42 #include "treetest.h"
43
44 #include "icon1.xpm"
45 #include "icon2.xpm"
46 #include "icon3.xpm"
47 #include "icon4.xpm"
48 #include "icon5.xpm"
49
50 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
51 #include "mondrian.xpm"
52 #endif
53
54
55 // verify that the item is ok and insult the user if it is not
56 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
57 wxMessageBox(wxT("Please select some item first!"), \
58 wxT("Tree sample error"), \
59 wxOK | wxICON_EXCLAMATION, \
60 this); \
61 return; \
62 }
63
64 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
65
66 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
67 EVT_IDLE(MyFrame::OnIdle)
68 EVT_SIZE(MyFrame::OnSize)
69
70 MENU_LINK(Quit)
71 MENU_LINK(About)
72
73 MENU_LINK(TogButtons)
74 MENU_LINK(TogTwist)
75 MENU_LINK(TogLines)
76 MENU_LINK(TogEdit)
77 MENU_LINK(TogHideRoot)
78 MENU_LINK(TogRootLines)
79 MENU_LINK(TogBorder)
80 MENU_LINK(TogFullHighlight)
81 MENU_LINK(SetFgColour)
82 MENU_LINK(SetBgColour)
83 MENU_LINK(ResetStyle)
84
85 MENU_LINK(Dump)
86 #ifndef NO_MULTIPLE_SELECTION
87 MENU_LINK(DumpSelected)
88 MENU_LINK(Select)
89 MENU_LINK(Unselect)
90 MENU_LINK(ToggleSel)
91 #endif // NO_MULTIPLE_SELECTION
92 MENU_LINK(Rename)
93 MENU_LINK(Count)
94 MENU_LINK(CountRec)
95 MENU_LINK(Sort)
96 MENU_LINK(SortRev)
97 MENU_LINK(SetBold)
98 MENU_LINK(ClearBold)
99 MENU_LINK(Delete)
100 MENU_LINK(DeleteChildren)
101 MENU_LINK(DeleteAll)
102 MENU_LINK(Recreate)
103 MENU_LINK(ToggleImages)
104 MENU_LINK(ToggleButtons)
105 MENU_LINK(SetImageSize)
106 MENU_LINK(CollapseAndReset)
107 MENU_LINK(EnsureVisible)
108 MENU_LINK(AddItem)
109 MENU_LINK(InsertItem)
110 MENU_LINK(IncIndent)
111 MENU_LINK(DecIndent)
112 MENU_LINK(IncSpacing)
113 MENU_LINK(DecSpacing)
114 MENU_LINK(ToggleIcon)
115 #undef MENU_LINK
116
117 END_EVENT_TABLE()
118
119 #if USE_GENERIC_TREECTRL
120 BEGIN_EVENT_TABLE(MyTreeCtrl, wxGenericTreeCtrl)
121 #else
122 BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
123 #endif
124 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
125 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
126 EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag)
127 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
128 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
129 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
130 #if 0 // there are so many of those that logging them causes flicker
131 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
132 #endif
133 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
134 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
135 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
136 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
137 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
138 //EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRightClick)
139
140 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu)
141 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
142 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
143 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
144 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated)
145 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
146 END_EVENT_TABLE()
147
148 IMPLEMENT_APP(MyApp)
149
150 bool MyApp::OnInit()
151 {
152 // Create the main frame window
153 MyFrame *frame = new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
154
155 // Show the frame
156 frame->Show(TRUE);
157 SetTopWindow(frame);
158
159 return TRUE;
160 }
161
162
163 // My frame constructor
164 MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
165 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)),
166 m_treeCtrl(NULL), m_textCtrl(NULL)
167 {
168 // This reduces flicker effects - even better would be to define
169 // OnEraseBackground to do nothing. When the tree control's scrollbars are
170 // show or hidden, the frame is sent a background erase event.
171 SetBackgroundColour(wxColour(255, 255, 255));
172
173 // Give it an icon
174 SetIcon(wxICON(mondrian));
175
176 #if wxUSE_MENUS
177 // Make a menubar
178 wxMenu *file_menu = new wxMenu,
179 *style_menu = new wxMenu,
180 *tree_menu = new wxMenu,
181 *item_menu = new wxMenu;
182
183 file_menu->Append(TreeTest_About, wxT("&About..."));
184 file_menu->AppendSeparator();
185 file_menu->Append(TreeTest_Quit, wxT("E&xit\tAlt-X"));
186
187 style_menu->Append(TreeTest_TogButtons, wxT("Toggle &normal buttons"), wxT(""), TRUE);
188 style_menu->Append(TreeTest_TogTwist, wxT("Toggle &twister buttons"), wxT(""), TRUE);
189 style_menu->Append(TreeTest_ToggleButtons, wxT("Toggle image &buttons"), wxT(""), TRUE);
190 style_menu->AppendSeparator();
191 style_menu->Append(TreeTest_TogLines, wxT("Toggle &connecting lines"), wxT(""), TRUE);
192 style_menu->Append(TreeTest_TogRootLines, wxT("Toggle &lines at root"), wxT(""), TRUE);
193 style_menu->Append(TreeTest_TogHideRoot, wxT("Toggle &hidden root"), wxT(""), TRUE);
194 style_menu->Append(TreeTest_TogBorder, wxT("Toggle &item border"), wxT(""), TRUE);
195 style_menu->Append(TreeTest_TogFullHighlight, wxT("Toggle &full row highlight"), wxT(""), TRUE);
196 style_menu->Append(TreeTest_TogEdit, wxT("Toggle &edit mode"), wxT(""), TRUE);
197 #ifndef NO_MULTIPLE_SELECTION
198 style_menu->Append(TreeTest_ToggleSel, wxT("Toggle &selection mode"), wxT(""), TRUE);
199 #endif // NO_MULTIPLE_SELECTION
200 style_menu->Append(TreeTest_ToggleImages, wxT("Toggle show ima&ges"), wxT(""), TRUE);
201 style_menu->Append(TreeTest_SetImageSize, wxT("Set image si&ze..."));
202 style_menu->AppendSeparator();
203 style_menu->Append(TreeTest_SetFgColour, wxT("Set &foreground colour..."));
204 style_menu->Append(TreeTest_SetBgColour, wxT("Set &background colour..."));
205 style_menu->AppendSeparator();
206 style_menu->Append(TreeTest_ResetStyle, wxT("&Reset to default\tF10"));
207
208 tree_menu->Append(TreeTest_Recreate, wxT("&Recreate the tree"));
209 tree_menu->Append(TreeTest_CollapseAndReset, wxT("C&ollapse and reset"));
210 tree_menu->AppendSeparator();
211 tree_menu->Append(TreeTest_AddItem, wxT("Append a &new item"));
212 tree_menu->Append(TreeTest_InsertItem, wxT("&Insert a new item"));
213 tree_menu->Append(TreeTest_Delete, wxT("&Delete this item"));
214 tree_menu->Append(TreeTest_DeleteChildren, wxT("Delete &children"));
215 tree_menu->Append(TreeTest_DeleteAll, wxT("Delete &all items"));
216 tree_menu->AppendSeparator();
217 tree_menu->Append(TreeTest_Count, wxT("Count children of current item"));
218 tree_menu->Append(TreeTest_CountRec, wxT("Recursively count children of current item"));
219 tree_menu->AppendSeparator();
220 tree_menu->Append(TreeTest_Sort, wxT("Sort children of current item"));
221 tree_menu->Append(TreeTest_SortRev, wxT("Sort in reversed order"));
222 tree_menu->AppendSeparator();
223 tree_menu->Append(TreeTest_EnsureVisible, wxT("Make the last item &visible"));
224 tree_menu->AppendSeparator();
225 tree_menu->Append(TreeTest_IncIndent, wxT("Add 5 points to indentation\tAlt-I"));
226 tree_menu->Append(TreeTest_DecIndent, wxT("Reduce indentation by 5 points\tAlt-R"));
227 tree_menu->AppendSeparator();
228 tree_menu->Append(TreeTest_IncSpacing, wxT("Add 5 points to spacing\tCtrl-I"));
229 tree_menu->Append(TreeTest_DecSpacing, wxT("Reduce spacing by 5 points\tCtrl-R"));
230
231 item_menu->Append(TreeTest_Dump, wxT("&Dump item children"));
232 item_menu->Append(TreeTest_Rename, wxT("&Rename item..."));
233
234 item_menu->AppendSeparator();
235 item_menu->Append(TreeTest_SetBold, wxT("Make item &bold"));
236 item_menu->Append(TreeTest_ClearBold, wxT("Make item &not bold"));
237 item_menu->AppendSeparator();
238 item_menu->Append(TreeTest_ToggleIcon, wxT("Toggle the item's &icon"));
239
240 #ifndef NO_MULTIPLE_SELECTION
241 item_menu->AppendSeparator();
242 item_menu->Append(TreeTest_DumpSelected, wxT("Dump selected items\tAlt-D"));
243 item_menu->Append(TreeTest_Select, wxT("Select current item\tAlt-S"));
244 item_menu->Append(TreeTest_Unselect, wxT("Unselect everything\tAlt-U"));
245 #endif // NO_MULTIPLE_SELECTION
246
247 wxMenuBar *menu_bar = new wxMenuBar;
248 menu_bar->Append(file_menu, wxT("&File"));
249 menu_bar->Append(style_menu, wxT("&Style"));
250 menu_bar->Append(tree_menu, wxT("&Tree"));
251 menu_bar->Append(item_menu, wxT("&Item"));
252 SetMenuBar(menu_bar);
253 #endif // wxUSE_MENUS
254
255 // create the controls
256 m_textCtrl = new wxTextCtrl(this, -1, wxT(""),
257 wxDefaultPosition, wxDefaultSize,
258 wxTE_MULTILINE | wxSUNKEN_BORDER);
259
260 CreateTreeWithDefStyle();
261
262 menu_bar->Check(TreeTest_ToggleImages, TRUE);
263
264 #if wxUSE_STATUSBAR
265 // create a status bar
266 CreateStatusBar(2);
267 #endif // wxUSE_STATUSBAR
268
269 #ifdef __WXMOTIF__
270 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
271 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
272 delete wxLog::SetActiveTarget(new wxLogStderr);
273 #else
274 // set our text control as the log target
275 wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl);
276 delete wxLog::SetActiveTarget(logWindow);
277 #endif
278 }
279
280 MyFrame::~MyFrame()
281 {
282 delete wxLog::SetActiveTarget(NULL);
283 }
284
285 void MyFrame::CreateTreeWithDefStyle()
286 {
287 long style = wxTR_DEFAULT_STYLE |
288 #ifndef NO_VARIABLE_HEIGHT
289 wxTR_HAS_VARIABLE_ROW_HEIGHT |
290 #endif
291 wxTR_EDIT_LABELS;
292
293 CreateTree(style | wxSUNKEN_BORDER);
294
295 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
296 // everything
297 wxMenuBar *mbar = GetMenuBar();
298 mbar->Check(TreeTest_TogButtons, (style & wxTR_HAS_BUTTONS) != 0);
299 mbar->Check(TreeTest_TogButtons, (style & wxTR_TWIST_BUTTONS) != 0);
300 mbar->Check(TreeTest_TogLines, (style & wxTR_NO_LINES) == 0);
301 mbar->Check(TreeTest_TogRootLines, (style & wxTR_LINES_AT_ROOT) != 0);
302 mbar->Check(TreeTest_TogHideRoot, (style & wxTR_HIDE_ROOT) != 0);
303 mbar->Check(TreeTest_TogEdit, (style & wxTR_EDIT_LABELS) != 0);
304 mbar->Check(TreeTest_TogBorder, (style & wxTR_ROW_LINES) != 0);
305 mbar->Check(TreeTest_TogFullHighlight, (style & wxTR_FULL_ROW_HIGHLIGHT) != 0);
306 }
307
308 void MyFrame::CreateTree(long style)
309 {
310 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
311 wxDefaultPosition, wxDefaultSize,
312 style);
313 Resize();
314 }
315
316 void MyFrame::TogStyle(int id, long flag)
317 {
318 long style = m_treeCtrl->GetWindowStyle() ^ flag;
319
320 // most treectrl styles can't be changed on the fly using the native
321 // control and the tree must be recreated
322 #ifndef __WXMSW__
323 m_treeCtrl->SetWindowStyle(style);
324 #else // MSW
325 delete m_treeCtrl;
326 CreateTree(style);
327 #endif // !MSW/MSW
328
329 GetMenuBar()->Check(id, (style & flag) != 0);
330 }
331
332 void MyFrame::OnIdle(wxIdleEvent& event)
333 {
334 #if wxUSE_STATUSBAR
335 if ( m_treeCtrl )
336 {
337 wxTreeItemId idRoot = m_treeCtrl->GetRootItem();
338
339 SetStatusText(wxString::Format
340 (
341 _T("Root/last item is %svisible/%svisible"),
342 m_treeCtrl->IsVisible(idRoot) ? _T("") : _T("not "),
343 m_treeCtrl->IsVisible(m_treeCtrl->GetLastChild(idRoot))
344 ? _T("") : _T("not ")
345 ), 1);
346 }
347 #endif // wxUSE_STATUSBAR
348
349 event.Skip();
350 }
351
352 void MyFrame::OnSize(wxSizeEvent& event)
353 {
354 if ( m_treeCtrl && m_textCtrl )
355 {
356 Resize();
357 }
358
359 event.Skip();
360 }
361
362 void MyFrame::Resize()
363 {
364 wxSize size = GetClientSize();
365 m_treeCtrl->SetSize(0, 0, size.x, 2*size.y/3);
366 m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3);
367 }
368
369 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
370 {
371 Close(TRUE);
372 }
373
374 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
375 {
376 wxMessageBox(wxT("Tree test sample\n")
377 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
378 wxT("About tree test"),
379 wxOK | wxICON_INFORMATION, this);
380 }
381
382 void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
383 {
384 wxTreeItemId item = m_treeCtrl->GetSelection();
385
386 CHECK_ITEM( item );
387
388 // old code - now we edit in place
389 #if 0
390 static wxString s_text;
391 s_text = wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
392 s_text, this);
393 if ( !s_text.IsEmpty() )
394 {
395 m_treeCtrl->SetItemText(item, s_text);
396 }
397 #endif // 0
398
399 // TODO demonstrate creating a custom edit control...
400 (void)m_treeCtrl->EditLabel(item);
401 }
402
403 void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event))
404 {
405 wxTreeItemId item = m_treeCtrl->GetSelection();
406
407 CHECK_ITEM( item );
408
409 int i = m_treeCtrl->GetChildrenCount( item, FALSE );
410
411 wxLogMessage(wxT("%d children"), i);
412 }
413
414 void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event))
415 {
416 wxTreeItemId item = m_treeCtrl->GetSelection();
417
418 CHECK_ITEM( item );
419
420 int i = m_treeCtrl->GetChildrenCount( item );
421
422 wxLogMessage(wxT("%d children"), i);
423 }
424
425 void MyFrame::DoSort(bool reverse)
426 {
427 wxTreeItemId item = m_treeCtrl->GetSelection();
428
429 CHECK_ITEM( item );
430
431 m_treeCtrl->DoSortChildren(item, reverse);
432 }
433
434 void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
435 {
436 wxTreeItemId root = m_treeCtrl->GetSelection();
437
438 CHECK_ITEM( root );
439
440 m_treeCtrl->GetItemsRecursively(root);
441 }
442
443 #ifndef NO_MULTIPLE_SELECTION
444
445 void MyFrame::OnToggleSel(wxCommandEvent& event)
446 {
447 TogStyle(event.GetId(), wxTR_MULTIPLE);
448 }
449
450 void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
451 {
452 wxArrayTreeItemIds array;
453
454 size_t count = m_treeCtrl->GetSelections(array);
455 wxLogMessage(wxT("%u items selected"), count);
456
457 for ( size_t n = 0; n < count; n++ )
458 {
459 wxLogMessage(wxT("\t%s"), m_treeCtrl->GetItemText(array.Item(n)).c_str());
460 }
461 }
462
463 void MyFrame::OnSelect(wxCommandEvent& WXUNUSED(event))
464 {
465 m_treeCtrl->SelectItem(m_treeCtrl->GetSelection());
466 }
467
468 void MyFrame::OnUnselect(wxCommandEvent& WXUNUSED(event))
469 {
470 m_treeCtrl->UnselectAll();
471 }
472
473 #endif // NO_MULTIPLE_SELECTION
474
475 void MyFrame::DoSetBold(bool bold)
476 {
477 wxTreeItemId item = m_treeCtrl->GetSelection();
478
479 CHECK_ITEM( item );
480
481 m_treeCtrl->SetItemBold(item, bold);
482 }
483
484 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
485 {
486 wxTreeItemId item = m_treeCtrl->GetSelection();
487
488 CHECK_ITEM( item );
489
490 m_treeCtrl->Delete(item);
491 }
492
493 void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
494 {
495 wxTreeItemId item = m_treeCtrl->GetSelection();
496
497 CHECK_ITEM( item );
498
499 m_treeCtrl->DeleteChildren(item);
500 }
501
502 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
503 {
504 m_treeCtrl->DeleteAllItems();
505 }
506
507 void MyFrame::OnRecreate(wxCommandEvent& event)
508 {
509 OnDeleteAll(event);
510 m_treeCtrl->AddTestItemsToTree(5, 2);
511 }
512
513 void MyFrame::OnSetImageSize(wxCommandEvent& WXUNUSED(event))
514 {
515 int size = wxGetNumberFromUser(wxT("Enter the size for the images to use"),
516 wxT("Size: "),
517 wxT("TreeCtrl sample"),
518 m_treeCtrl->ImageSize());
519 if ( size == -1 )
520 return;
521
522 m_treeCtrl->CreateImageList(size);
523 wxGetApp().SetShowImages(TRUE);
524 }
525
526 void MyFrame::OnToggleImages(wxCommandEvent& WXUNUSED(event))
527 {
528 if ( wxGetApp().ShowImages() )
529 {
530 m_treeCtrl->CreateImageList(-1);
531 wxGetApp().SetShowImages(FALSE);
532 }
533 else
534 {
535 m_treeCtrl->CreateImageList(0);
536 wxGetApp().SetShowImages(TRUE);
537 }
538 }
539
540 void MyFrame::OnToggleButtons(wxCommandEvent& WXUNUSED(event))
541 {
542 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
543 if ( wxGetApp().ShowButtons() )
544 {
545 m_treeCtrl->CreateButtonsImageList(-1);
546 wxGetApp().SetShowButtons(FALSE);
547 }
548 else
549 {
550 m_treeCtrl->CreateButtonsImageList(15);
551 wxGetApp().SetShowButtons(TRUE);
552 }
553 #endif
554 }
555
556 void MyFrame::OnCollapseAndReset(wxCommandEvent& WXUNUSED(event))
557 {
558 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
559 }
560
561 void MyFrame::OnEnsureVisible(wxCommandEvent& WXUNUSED(event))
562 {
563 m_treeCtrl->DoEnsureVisible();
564 }
565
566 void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
567 {
568 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1;
569 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, wxT("2nd item"));
570 }
571
572 void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
573 {
574 static int s_num = 0;
575
576 wxString text;
577 text.Printf(wxT("Item #%d"), ++s_num);
578
579 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
580 text /*,
581 MyTreeCtrl::TreeCtrlIcon_File */ );
582 }
583
584 void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
585 {
586 unsigned int indent = m_treeCtrl->GetIndent();
587 if (indent < 100)
588 m_treeCtrl->SetIndent( indent+5 );
589 }
590
591 void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
592 {
593 unsigned int indent = m_treeCtrl->GetIndent();
594 if (indent > 10)
595 m_treeCtrl->SetIndent( indent-5 );
596 }
597
598 void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
599 {
600 unsigned int indent = m_treeCtrl->GetSpacing();
601 if (indent < 100)
602 m_treeCtrl->SetSpacing( indent+5 );
603 }
604
605 void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
606 {
607 unsigned int indent = m_treeCtrl->GetSpacing();
608 if (indent > 10)
609 m_treeCtrl->SetSpacing( indent-5 );
610 }
611
612 void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
613 {
614 wxTreeItemId item = m_treeCtrl->GetSelection();
615
616 CHECK_ITEM( item );
617
618 m_treeCtrl->DoToggleIcon(item);
619 }
620
621 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
622 {
623 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetForegroundColour());
624 if ( col.Ok() )
625 m_treeCtrl->SetForegroundColour(col);
626 }
627
628 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
629 {
630 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetBackgroundColour());
631 if ( col.Ok() )
632 m_treeCtrl->SetBackgroundColour(col);
633 }
634
635 // MyTreeCtrl implementation
636 #if USE_GENERIC_TREECTRL
637 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl)
638 #else
639 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
640 #endif
641
642 MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
643 const wxPoint& pos, const wxSize& size,
644 long style)
645 : wxTreeCtrl(parent, id, pos, size, style)
646 {
647 m_reverseSort = FALSE;
648
649 CreateImageList();
650
651 // Add some items to the tree
652 AddTestItemsToTree(5, 2);
653 }
654
655 void MyTreeCtrl::CreateImageList(int size)
656 {
657 if ( size == -1 )
658 {
659 SetImageList(NULL);
660 return;
661 }
662 if ( size == 0 )
663 size = m_imageSize;
664 else
665 m_imageSize = size;
666
667 // Make an image list containing small icons
668 wxImageList *images = new wxImageList(size, size, TRUE);
669
670 // should correspond to TreeCtrlIcon_xxx enum
671 wxBusyCursor wait;
672 wxIcon icons[5];
673 icons[0] = wxIcon(icon1_xpm);
674 icons[1] = wxIcon(icon2_xpm);
675 icons[2] = wxIcon(icon3_xpm);
676 icons[3] = wxIcon(icon4_xpm);
677 icons[4] = wxIcon(icon5_xpm);
678
679 int sizeOrig = icons[0].GetWidth();
680 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
681 {
682 if ( size == sizeOrig )
683 {
684 images->Add(icons[i]);
685 }
686 else
687 {
688 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
689 }
690 }
691
692 AssignImageList(images);
693 }
694
695 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
696 void MyTreeCtrl::CreateButtonsImageList(int size)
697 {
698 if ( size == -1 )
699 {
700 SetButtonsImageList(NULL);
701 return;
702 }
703
704 // Make an image list containing small icons
705 wxImageList *images = new wxImageList(size, size, TRUE);
706
707 // should correspond to TreeCtrlIcon_xxx enum
708 wxBusyCursor wait;
709 wxIcon icons[4];
710 icons[0] = wxIcon(icon3_xpm); // closed
711 icons[1] = wxIcon(icon3_xpm); // closed, selected
712 icons[2] = wxIcon(icon5_xpm); // open
713 icons[3] = wxIcon(icon5_xpm); // open, selected
714
715 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
716 {
717 int sizeOrig = icons[i].GetWidth();
718 if ( size == sizeOrig )
719 {
720 images->Add(icons[i]);
721 }
722 else
723 {
724 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
725 }
726 }
727
728 AssignButtonsImageList(images);
729 #else
730 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size))
731 {
732 #endif
733 }
734
735 MyTreeCtrl::~MyTreeCtrl()
736 {
737 }
738
739 int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
740 const wxTreeItemId& item2)
741 {
742 if ( m_reverseSort )
743 {
744 // just exchange 1st and 2nd items
745 return wxTreeCtrl::OnCompareItems(item2, item1);
746 }
747 else
748 {
749 return wxTreeCtrl::OnCompareItems(item1, item2);
750 }
751 }
752
753 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
754 size_t numChildren,
755 size_t depth,
756 size_t folder)
757 {
758 if ( depth > 0 )
759 {
760 bool hasChildren = depth > 1;
761
762 wxString str;
763 for ( size_t n = 0; n < numChildren; n++ )
764 {
765 // at depth 1 elements won't have any more children
766 if ( hasChildren )
767 str.Printf(wxT("%s child %d"), wxT("Folder"), n + 1);
768 else
769 str.Printf(wxT("%s child %d.%d"), wxT("File"), folder, n + 1);
770
771 // here we pass to AppendItem() normal and selected item images (we
772 // suppose that selected image follows the normal one in the enum)
773 int image, imageSel;
774 if ( wxGetApp().ShowImages() )
775 {
776 image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
777 imageSel = image + 1;
778 }
779 else
780 {
781 image = imageSel = -1;
782 }
783 wxTreeItemId id = AppendItem(idParent, str, image, imageSel,
784 new MyTreeItemData(str));
785
786 // and now we also set the expanded one (only for the folders)
787 if ( hasChildren && wxGetApp().ShowImages() )
788 {
789 SetItemImage(id, TreeCtrlIcon_FolderOpened,
790 wxTreeItemIcon_Expanded);
791 }
792
793 // remember the last child for OnEnsureVisible()
794 if ( !hasChildren && n == numChildren - 1 )
795 {
796 m_lastItem = id;
797 }
798
799 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
800 }
801 }
802 //else: done!
803 }
804
805 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
806 size_t depth)
807 {
808 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1;
809 wxTreeItemId rootId = AddRoot(wxT("Root"),
810 image, image,
811 new MyTreeItemData(wxT("Root item")));
812 if ( image != -1 )
813 {
814 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
815 }
816
817 AddItemsRecursively(rootId, numChildren, depth, 0);
818
819 // set some colours/fonts for testing
820 SetItemFont(rootId, *wxITALIC_FONT);
821
822 wxTreeItemIdValue cookie;
823 wxTreeItemId id = GetFirstChild(rootId, cookie);
824 SetItemTextColour(id, *wxBLUE);
825
826 id = GetNextChild(rootId, cookie);
827 id = GetNextChild(rootId, cookie);
828 SetItemTextColour(id, *wxRED);
829 SetItemBackgroundColour(id, *wxLIGHT_GREY);
830 }
831
832 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent,
833 wxTreeItemIdValue cookie)
834 {
835 wxTreeItemId id;
836
837 if ( !cookie )
838 id = GetFirstChild(idParent, cookie);
839 else
840 id = GetNextChild(idParent, cookie);
841
842 if ( !id.IsOk() )
843 return;
844
845 wxString text = GetItemText(id);
846 wxLogMessage(text);
847
848 if (ItemHasChildren(id))
849 GetItemsRecursively(id);
850
851 GetItemsRecursively(idParent, cookie);
852 }
853
854 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
855 {
856 int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File
857 : TreeCtrlIcon_Folder;
858
859 SetItemImage(item, image);
860 }
861
862
863 // avoid repetition
864 #define TREE_EVENT_HANDLER(name) \
865 void MyTreeCtrl::name(wxTreeEvent& event) \
866 { \
867 wxLogMessage(wxT(#name)); \
868 SetLastItem(wxTreeItemId()); \
869 event.Skip(); \
870 }
871
872 TREE_EVENT_HANDLER(OnBeginRDrag)
873 TREE_EVENT_HANDLER(OnDeleteItem)
874 TREE_EVENT_HANDLER(OnGetInfo)
875 TREE_EVENT_HANDLER(OnSetInfo)
876 TREE_EVENT_HANDLER(OnItemExpanded)
877 TREE_EVENT_HANDLER(OnItemExpanding)
878 TREE_EVENT_HANDLER(OnItemCollapsed)
879 TREE_EVENT_HANDLER(OnSelChanged)
880 TREE_EVENT_HANDLER(OnSelChanging)
881
882 #undef TREE_EVENT_HANDLER
883
884 void LogKeyEvent(const wxChar *name, const wxKeyEvent& event)
885 {
886 wxString key;
887 long keycode = event.GetKeyCode();
888 {
889 switch ( keycode )
890 {
891 case WXK_BACK: key = wxT("BACK"); break;
892 case WXK_TAB: key = wxT("TAB"); break;
893 case WXK_RETURN: key = wxT("RETURN"); break;
894 case WXK_ESCAPE: key = wxT("ESCAPE"); break;
895 case WXK_SPACE: key = wxT("SPACE"); break;
896 case WXK_DELETE: key = wxT("DELETE"); break;
897 case WXK_START: key = wxT("START"); break;
898 case WXK_LBUTTON: key = wxT("LBUTTON"); break;
899 case WXK_RBUTTON: key = wxT("RBUTTON"); break;
900 case WXK_CANCEL: key = wxT("CANCEL"); break;
901 case WXK_MBUTTON: key = wxT("MBUTTON"); break;
902 case WXK_CLEAR: key = wxT("CLEAR"); break;
903 case WXK_SHIFT: key = wxT("SHIFT"); break;
904 case WXK_ALT: key = wxT("ALT"); break;
905 case WXK_CONTROL: key = wxT("CONTROL"); break;
906 case WXK_MENU: key = wxT("MENU"); break;
907 case WXK_PAUSE: key = wxT("PAUSE"); break;
908 case WXK_CAPITAL: key = wxT("CAPITAL"); break;
909 case WXK_PRIOR: key = wxT("PRIOR"); break;
910 case WXK_NEXT: key = wxT("NEXT"); break;
911 case WXK_END: key = wxT("END"); break;
912 case WXK_HOME: key = wxT("HOME"); break;
913 case WXK_LEFT: key = wxT("LEFT"); break;
914 case WXK_UP: key = wxT("UP"); break;
915 case WXK_RIGHT: key = wxT("RIGHT"); break;
916 case WXK_DOWN: key = wxT("DOWN"); break;
917 case WXK_SELECT: key = wxT("SELECT"); break;
918 case WXK_PRINT: key = wxT("PRINT"); break;
919 case WXK_EXECUTE: key = wxT("EXECUTE"); break;
920 case WXK_SNAPSHOT: key = wxT("SNAPSHOT"); break;
921 case WXK_INSERT: key = wxT("INSERT"); break;
922 case WXK_HELP: key = wxT("HELP"); break;
923 case WXK_NUMPAD0: key = wxT("NUMPAD0"); break;
924 case WXK_NUMPAD1: key = wxT("NUMPAD1"); break;
925 case WXK_NUMPAD2: key = wxT("NUMPAD2"); break;
926 case WXK_NUMPAD3: key = wxT("NUMPAD3"); break;
927 case WXK_NUMPAD4: key = wxT("NUMPAD4"); break;
928 case WXK_NUMPAD5: key = wxT("NUMPAD5"); break;
929 case WXK_NUMPAD6: key = wxT("NUMPAD6"); break;
930 case WXK_NUMPAD7: key = wxT("NUMPAD7"); break;
931 case WXK_NUMPAD8: key = wxT("NUMPAD8"); break;
932 case WXK_NUMPAD9: key = wxT("NUMPAD9"); break;
933 case WXK_MULTIPLY: key = wxT("MULTIPLY"); break;
934 case WXK_ADD: key = wxT("ADD"); break;
935 case WXK_SEPARATOR: key = wxT("SEPARATOR"); break;
936 case WXK_SUBTRACT: key = wxT("SUBTRACT"); break;
937 case WXK_DECIMAL: key = wxT("DECIMAL"); break;
938 case WXK_DIVIDE: key = wxT("DIVIDE"); break;
939 case WXK_F1: key = wxT("F1"); break;
940 case WXK_F2: key = wxT("F2"); break;
941 case WXK_F3: key = wxT("F3"); break;
942 case WXK_F4: key = wxT("F4"); break;
943 case WXK_F5: key = wxT("F5"); break;
944 case WXK_F6: key = wxT("F6"); break;
945 case WXK_F7: key = wxT("F7"); break;
946 case WXK_F8: key = wxT("F8"); break;
947 case WXK_F9: key = wxT("F9"); break;
948 case WXK_F10: key = wxT("F10"); break;
949 case WXK_F11: key = wxT("F11"); break;
950 case WXK_F12: key = wxT("F12"); break;
951 case WXK_F13: key = wxT("F13"); break;
952 case WXK_F14: key = wxT("F14"); break;
953 case WXK_F15: key = wxT("F15"); break;
954 case WXK_F16: key = wxT("F16"); break;
955 case WXK_F17: key = wxT("F17"); break;
956 case WXK_F18: key = wxT("F18"); break;
957 case WXK_F19: key = wxT("F19"); break;
958 case WXK_F20: key = wxT("F20"); break;
959 case WXK_F21: key = wxT("F21"); break;
960 case WXK_F22: key = wxT("F22"); break;
961 case WXK_F23: key = wxT("F23"); break;
962 case WXK_F24: key = wxT("F24"); break;
963 case WXK_NUMLOCK: key = wxT("NUMLOCK"); break;
964 case WXK_SCROLL: key = wxT("SCROLL"); break;
965 case WXK_PAGEUP: key = wxT("PAGEUP"); break;
966 case WXK_PAGEDOWN: key = wxT("PAGEDOWN"); break;
967 case WXK_NUMPAD_SPACE: key = wxT("NUMPAD_SPACE"); break;
968 case WXK_NUMPAD_TAB: key = wxT("NUMPAD_TAB"); break;
969 case WXK_NUMPAD_ENTER: key = wxT("NUMPAD_ENTER"); break;
970 case WXK_NUMPAD_F1: key = wxT("NUMPAD_F1"); break;
971 case WXK_NUMPAD_F2: key = wxT("NUMPAD_F2"); break;
972 case WXK_NUMPAD_F3: key = wxT("NUMPAD_F3"); break;
973 case WXK_NUMPAD_F4: key = wxT("NUMPAD_F4"); break;
974 case WXK_NUMPAD_HOME: key = wxT("NUMPAD_HOME"); break;
975 case WXK_NUMPAD_LEFT: key = wxT("NUMPAD_LEFT"); break;
976 case WXK_NUMPAD_UP: key = wxT("NUMPAD_UP"); break;
977 case WXK_NUMPAD_RIGHT: key = wxT("NUMPAD_RIGHT"); break;
978 case WXK_NUMPAD_DOWN: key = wxT("NUMPAD_DOWN"); break;
979 case WXK_NUMPAD_PRIOR: key = wxT("NUMPAD_PRIOR"); break;
980 case WXK_NUMPAD_PAGEUP: key = wxT("NUMPAD_PAGEUP"); break;
981 case WXK_NUMPAD_PAGEDOWN: key = wxT("NUMPAD_PAGEDOWN"); break;
982 case WXK_NUMPAD_END: key = wxT("NUMPAD_END"); break;
983 case WXK_NUMPAD_BEGIN: key = wxT("NUMPAD_BEGIN"); break;
984 case WXK_NUMPAD_INSERT: key = wxT("NUMPAD_INSERT"); break;
985 case WXK_NUMPAD_DELETE: key = wxT("NUMPAD_DELETE"); break;
986 case WXK_NUMPAD_EQUAL: key = wxT("NUMPAD_EQUAL"); break;
987 case WXK_NUMPAD_MULTIPLY: key = wxT("NUMPAD_MULTIPLY"); break;
988 case WXK_NUMPAD_ADD: key = wxT("NUMPAD_ADD"); break;
989 case WXK_NUMPAD_SEPARATOR: key = wxT("NUMPAD_SEPARATOR"); break;
990 case WXK_NUMPAD_SUBTRACT: key = wxT("NUMPAD_SUBTRACT"); break;
991 case WXK_NUMPAD_DECIMAL: key = wxT("NUMPAD_DECIMAL"); break;
992
993 default:
994 {
995 if ( wxIsprint((int)keycode) )
996 key.Printf(wxT("'%c'"), (char)keycode);
997 else if ( keycode > 0 && keycode < 27 )
998 key.Printf(_("Ctrl-%c"), wxT('A') + keycode - 1);
999 else
1000 key.Printf(wxT("unknown (%ld)"), keycode);
1001 }
1002 }
1003 }
1004
1005 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1006 name,
1007 key.c_str(),
1008 event.ControlDown() ? wxT('C') : wxT('-'),
1009 event.AltDown() ? wxT('A') : wxT('-'),
1010 event.ShiftDown() ? wxT('S') : wxT('-'),
1011 event.MetaDown() ? wxT('M') : wxT('-'));
1012 }
1013
1014 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent& event)
1015 {
1016 LogKeyEvent(wxT("Tree key down "), event.GetKeyEvent());
1017
1018 event.Skip();
1019 }
1020
1021 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
1022 {
1023 // need to explicitly allow drag
1024 if ( event.GetItem() != GetRootItem() )
1025 {
1026 m_draggedItem = event.GetItem();
1027
1028 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
1029 GetItemText(m_draggedItem).c_str());
1030
1031 event.Allow();
1032 }
1033 else
1034 {
1035 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1036 }
1037 }
1038
1039 void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
1040 {
1041 wxTreeItemId itemSrc = m_draggedItem,
1042 itemDst = event.GetItem();
1043 m_draggedItem = (wxTreeItemId)0l;
1044
1045 // where to copy the item?
1046 if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
1047 {
1048 // copy to the parent then
1049 itemDst = GetItemParent(itemDst);
1050 }
1051
1052 if ( !itemDst.IsOk() )
1053 {
1054 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1055
1056 return;
1057 }
1058
1059 wxString text = GetItemText(itemSrc);
1060 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1061 text.c_str(), GetItemText(itemDst).c_str());
1062
1063 // just do append here - we could also insert it just before/after the item
1064 // on which it was dropped, but this requires slightly more work... we also
1065 // completely ignore the client data and icon of the old item but could
1066 // copy them as well.
1067 //
1068 // Finally, we only copy one item here but we might copy the entire tree if
1069 // we were dragging a folder.
1070 int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1;
1071 AppendItem(itemDst, text, image);
1072 }
1073
1074 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
1075 {
1076 wxLogMessage(wxT("OnBeginLabelEdit"));
1077
1078 // for testing, prevent this item's label editing
1079 wxTreeItemId itemId = event.GetItem();
1080 if ( IsTestItem(itemId) )
1081 {
1082 wxMessageBox(wxT("You can't edit this item."));
1083
1084 event.Veto();
1085 }
1086 }
1087
1088 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
1089 {
1090 wxLogMessage(wxT("OnEndLabelEdit"));
1091
1092 // don't allow anything except letters in the labels
1093 if ( !event.GetLabel().IsWord() )
1094 {
1095 wxMessageBox(wxT("The new label should be a single word."));
1096
1097 event.Veto();
1098 }
1099 }
1100
1101 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
1102 {
1103 wxLogMessage(wxT("OnItemCollapsing"));
1104
1105 // for testing, prevent the user from collapsing the first child folder
1106 wxTreeItemId itemId = event.GetItem();
1107 if ( IsTestItem(itemId) )
1108 {
1109 wxMessageBox(wxT("You can't collapse this item."));
1110
1111 event.Veto();
1112 }
1113 }
1114
1115 void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
1116 {
1117 // show some info about this item
1118 wxTreeItemId itemId = event.GetItem();
1119 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
1120
1121 if ( item != NULL )
1122 {
1123 item->ShowInfo(this);
1124 }
1125
1126 wxLogMessage(wxT("OnItemActivated"));
1127 }
1128
1129 void MyTreeCtrl::OnItemRightClick(wxTreeEvent& event)
1130 {
1131 ShowMenu(event.GetItem(), event.GetPoint());
1132 }
1133
1134 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event)
1135 {
1136 wxPoint pt = event.GetPosition();
1137 wxTreeItemId item = GetSelection();
1138 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt.x, pt.y);
1139
1140 //check if event was generated by keyboard (MSW-specific?)
1141 if (pt.x==-1 && pt.y==-1) //(this is how MSW indicates it)
1142 {
1143 //attempt to guess where to show the menu
1144 if (item.IsOk())
1145 {
1146 //if an item was clicked, show menu to the right of it
1147 wxRect rect;
1148 GetBoundingRect(item, rect, true); //true = only the label
1149 pt = wxPoint(rect.GetRight(), rect.GetTop());
1150 }
1151 else
1152 {
1153 pt = wxPoint(0, 0);
1154 }
1155 }
1156 else
1157 {
1158 //event was generated by mouse, use supplied coords
1159 pt = ScreenToClient(pt);
1160 }
1161
1162 ShowMenu(item, pt);
1163 }
1164
1165 void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
1166 {
1167 wxString title;
1168 if ( id.IsOk() )
1169 {
1170 title << wxT("Menu for ") << GetItemText(id);
1171 }
1172 else
1173 {
1174 title = wxT("Menu for no particular item");
1175 }
1176
1177 #if wxUSE_MENUS
1178 wxMenu menu(title);
1179 menu.Append(TreeTest_About, wxT("&About..."));
1180 menu.Append(TreeTest_Dump, wxT("&Dump"));
1181
1182 PopupMenu(&menu, pt);
1183 #endif // wxUSE_MENUS
1184 }
1185
1186 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
1187 {
1188 wxTreeItemId id = HitTest(event.GetPosition());
1189 if ( !id )
1190 wxLogMessage(wxT("No item under mouse"));
1191 else
1192 {
1193 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
1194 if ( item )
1195 wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc());
1196 }
1197 }
1198
1199 static inline const wxChar *Bool2String(bool b)
1200 {
1201 return b ? wxT("") : wxT("not ");
1202 }
1203
1204 void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
1205 {
1206 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1207 wxT("%u children (%u immediately under this item)."),
1208 m_desc.c_str(),
1209 Bool2String(tree->IsSelected(GetId())),
1210 Bool2String(tree->IsExpanded(GetId())),
1211 Bool2String(tree->IsBold(GetId())),
1212 tree->GetChildrenCount(GetId()),
1213 tree->GetChildrenCount(GetId(), FALSE));
1214 }