1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // important: the #pragma argument must be different from treectrl.cpp,
13 // otherwise gcc gets confused (as there is also treectrl.cpp in the library
14 // which has identical #pragma) and the sample crashes on startup!
16 #pragma interface "treetest.cpp"
17 #pragma implementation "treetest.cpp"
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
34 #include "wx/imaglist.h"
35 #include "wx/treectrl.h"
40 // this is not supported by native control
41 #define NO_VARIABLE_HEIGHT
46 // under Windows the icons are in the .rc file
53 #include "mondrian.xpm"
56 // verify that the item is ok and insult the user if it is not
57 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
58 wxMessageBox("Please select some item first!", \
59 "Tree sample error", \
60 wxOK | wxICON_EXCLAMATION, \
65 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
66 EVT_SIZE(MyFrame::OnSize
)
68 EVT_MENU(TreeTest_Quit
, MyFrame::OnQuit
)
69 EVT_MENU(TreeTest_About
, MyFrame::OnAbout
)
70 EVT_MENU(TreeTest_Dump
, MyFrame::OnDump
)
71 #ifndef NO_MULTIPLE_SELECTION
72 EVT_MENU(TreeTest_DumpSelected
, MyFrame::OnDumpSelected
)
73 EVT_MENU(TreeTest_Select
, MyFrame::OnSelect
)
74 EVT_MENU(TreeTest_Unselect
, MyFrame::OnUnselect
)
75 EVT_MENU(TreeTest_ToggleSel
, MyFrame::OnToggleSel
)
76 #endif // NO_MULTIPLE_SELECTION
77 EVT_MENU(TreeTest_Rename
, MyFrame::OnRename
)
78 EVT_MENU(TreeTest_Count
, MyFrame::OnCount
)
79 EVT_MENU(TreeTest_CountRec
, MyFrame::OnCountRec
)
80 EVT_MENU(TreeTest_Sort
, MyFrame::OnSort
)
81 EVT_MENU(TreeTest_SortRev
, MyFrame::OnSortRev
)
82 EVT_MENU(TreeTest_Bold
, MyFrame::OnSetBold
)
83 EVT_MENU(TreeTest_UnBold
, MyFrame::OnClearBold
)
84 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
85 EVT_MENU(TreeTest_DeleteChildren
, MyFrame::OnDeleteChildren
)
86 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
87 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
88 EVT_MENU(TreeTest_ToggleImages
, MyFrame::OnToggleImages
)
89 EVT_MENU(TreeTest_SetImageSize
, MyFrame::OnSetImageSize
)
90 EVT_MENU(TreeTest_CollapseAndReset
, MyFrame::OnCollapseAndReset
)
91 EVT_MENU(TreeTest_EnsureVisible
, MyFrame::OnEnsureVisible
)
92 EVT_MENU(TreeTest_AddItem
, MyFrame::OnAddItem
)
93 EVT_MENU(TreeTest_InsertItem
, MyFrame::OnInsertItem
)
94 EVT_MENU(TreeTest_IncIndent
, MyFrame::OnIncIndent
)
95 EVT_MENU(TreeTest_DecIndent
, MyFrame::OnDecIndent
)
96 EVT_MENU(TreeTest_IncSpacing
, MyFrame::OnIncSpacing
)
97 EVT_MENU(TreeTest_DecSpacing
, MyFrame::OnDecSpacing
)
98 EVT_MENU(TreeTest_ToggleIcon
, MyFrame::OnToggleIcon
)
101 #if USE_GENERIC_TREECTRL
102 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
104 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
106 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
107 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
108 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
109 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
110 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
111 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
112 #if 0 // there are so many of those that logging them causes flicker
113 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
115 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
116 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
117 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
118 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
119 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
120 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRightClick
)
122 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
123 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
124 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
125 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
126 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
127 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
134 // Create the main frame window
135 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 600);
145 // My frame constructor
146 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
147 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
148 m_treeCtrl(NULL
), m_textCtrl(NULL
)
150 // This reduces flicker effects - even better would be to define
151 // OnEraseBackground to do nothing. When the tree control's scrollbars are
152 // show or hidden, the frame is sent a background erase event.
153 SetBackgroundColour(wxColour(255, 255, 255));
156 SetIcon(wxICON(mondrian
));
159 wxMenu
*file_menu
= new wxMenu
,
160 *tree_menu
= new wxMenu
,
161 *item_menu
= new wxMenu
;
163 file_menu
->Append(TreeTest_About
, "&About...");
164 file_menu
->AppendSeparator();
165 file_menu
->Append(TreeTest_Quit
, "E&xit\tAlt-X");
167 #ifndef NO_MULTIPLE_SELECTION
168 tree_menu
->Append(TreeTest_ToggleSel
, "&Toggle selection mode");
169 #endif // NO_MULTIPLE_SELECTION
170 tree_menu
->Append(TreeTest_ToggleImages
, "&Show images", "", TRUE
);
171 tree_menu
->Append(TreeTest_SetImageSize
, "Set image si&ze...");
172 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
173 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
174 tree_menu
->AppendSeparator();
175 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
176 tree_menu
->Append(TreeTest_InsertItem
, "&Insert a new item");
177 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
178 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
179 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
180 tree_menu
->AppendSeparator();
181 tree_menu
->Append(TreeTest_Count
, "Count children of current item");
182 tree_menu
->Append(TreeTest_CountRec
, "Recursively count children of current item");
183 tree_menu
->AppendSeparator();
184 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
185 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
186 tree_menu
->AppendSeparator();
187 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
188 tree_menu
->AppendSeparator();
189 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
190 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
191 tree_menu
->AppendSeparator();
192 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
193 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
195 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
196 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
198 item_menu
->AppendSeparator();
199 item_menu
->Append(TreeTest_Bold
, "Make item &bold");
200 item_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
201 item_menu
->AppendSeparator();
202 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the items &icon");
204 #ifndef NO_MULTIPLE_SELECTION
205 item_menu
->AppendSeparator();
206 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
207 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
208 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
209 #endif // NO_MULTIPLE_SELECTION
211 wxMenuBar
*menu_bar
= new wxMenuBar
;
212 menu_bar
->Append(file_menu
, "&File");
213 menu_bar
->Append(tree_menu
, "&Tree");
214 menu_bar
->Append(item_menu
, "&Item");
215 SetMenuBar(menu_bar
);
217 menu_bar
->Check(TreeTest_ToggleImages
, TRUE
);
219 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
220 wxDefaultPosition
, wxDefaultSize
,
221 wxTR_MAC_BUTTONS
| wxTR_NO_LINES
|
223 #ifndef NO_VARIABLE_HEIGHT
224 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
228 // m_treeCtrl->SetBackgroundColour( *wxLIGHT_GREY );
230 m_textCtrl
= new wxTextCtrl(this, -1, "",
231 wxDefaultPosition
, wxDefaultSize
,
232 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
234 // create a status bar with 3 panes
236 SetStatusText("", 0);
239 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
240 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
241 delete wxLog::SetActiveTarget(new wxLogStderr
);
243 // set our text control as the log target
244 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
245 delete wxLog::SetActiveTarget(logWindow
);
251 delete wxLog::SetActiveTarget(NULL
);
254 void MyFrame::OnSize(wxSizeEvent
& event
)
256 if ( m_treeCtrl
&& m_textCtrl
)
258 Resize(GetClientSize());
264 void MyFrame::Resize(const wxSize
& size
)
266 m_treeCtrl
->SetSize(0, 0, size
.x
, 2*size
.y
/3);
267 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
270 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
275 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
277 wxMessageBox("Tree test sample\n"
278 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
280 wxOK
| wxICON_INFORMATION
, this);
283 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
285 wxTreeItemId item
= m_treeCtrl
->GetSelection();
289 // old code - now we edit in place
291 static wxString s_text
;
292 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
294 if ( !s_text
.IsEmpty() )
296 m_treeCtrl
->SetItemText(item
, s_text
);
300 // TODO demonstrate creating a custom edit control...
301 (void)m_treeCtrl
->EditLabel(item
);
304 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
306 wxTreeItemId item
= m_treeCtrl
->GetSelection();
310 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
312 wxLogMessage(wxT("%d children"), i
);
315 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
317 wxTreeItemId item
= m_treeCtrl
->GetSelection();
321 int i
= m_treeCtrl
->GetChildrenCount( item
);
323 wxLogMessage(wxT("%d children"), i
);
326 void MyFrame::DoSort(bool reverse
)
328 wxTreeItemId item
= m_treeCtrl
->GetSelection();
332 m_treeCtrl
->DoSortChildren(item
, reverse
);
335 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
337 wxTreeItemId root
= m_treeCtrl
->GetSelection();
341 m_treeCtrl
->GetItemsRecursively(root
, -1);
344 #ifndef NO_MULTIPLE_SELECTION
346 void MyFrame::OnToggleSel(wxCommandEvent
& WXUNUSED(event
))
348 long style
= m_treeCtrl
->GetWindowStyle();
349 if ( style
& wxTR_MULTIPLE
)
350 style
&= ~wxTR_MULTIPLE
;
352 style
|= wxTR_MULTIPLE
;
356 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
357 wxDefaultPosition
, wxDefaultSize
,
359 Resize(GetClientSize());
362 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
364 wxArrayTreeItemIds array
;
366 size_t count
= m_treeCtrl
->GetSelections(array
);
367 wxLogMessage(wxT("%u items selected"), count
);
369 for ( size_t n
= 0; n
< count
; n
++ )
371 wxLogMessage("\t%s", m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
375 void MyFrame::OnSelect(wxCommandEvent
& event
)
377 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
380 void MyFrame::OnUnselect(wxCommandEvent
& event
)
382 m_treeCtrl
->UnselectAll();
385 #endif // NO_MULTIPLE_SELECTION
387 void MyFrame::DoSetBold(bool bold
)
389 wxTreeItemId item
= m_treeCtrl
->GetSelection();
393 m_treeCtrl
->SetItemBold(item
, bold
);
396 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
398 wxTreeItemId item
= m_treeCtrl
->GetSelection();
402 m_treeCtrl
->Delete(item
);
405 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
407 wxTreeItemId item
= m_treeCtrl
->GetSelection();
411 m_treeCtrl
->DeleteChildren(item
);
414 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
416 m_treeCtrl
->DeleteAllItems();
419 void MyFrame::OnRecreate(wxCommandEvent
& event
)
422 m_treeCtrl
->AddTestItemsToTree(3, 2);
425 void MyFrame::OnSetImageSize(wxCommandEvent
& event
)
427 long size
= wxGetNumberFromUser("Enter the size for the images to use",
434 m_treeCtrl
->CreateImageList((int)size
);
439 void MyFrame::OnToggleImages(wxCommandEvent
& event
)
441 if ( wxGetApp().ShowImages() )
443 m_treeCtrl
->CreateImageList(-1);
445 wxGetApp().SetShowImages(FALSE
);
449 m_treeCtrl
->CreateImageList();
451 wxGetApp().SetShowImages(TRUE
);
457 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
459 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
462 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
464 m_treeCtrl
->DoEnsureVisible();
467 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
469 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
470 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, "2nd item");
473 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
475 static int s_num
= 0;
478 text
.Printf("Item #%d", ++s_num
);
480 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
482 MyTreeCtrl::TreeCtrlIcon_File */ );
485 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
487 unsigned int indent
= m_treeCtrl
->GetIndent();
489 m_treeCtrl
->SetIndent( indent
+5 );
492 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
494 unsigned int indent
= m_treeCtrl
->GetIndent();
496 m_treeCtrl
->SetIndent( indent
-5 );
499 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
501 unsigned int indent
= m_treeCtrl
->GetSpacing();
503 m_treeCtrl
->SetSpacing( indent
+5 );
506 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
508 unsigned int indent
= m_treeCtrl
->GetSpacing();
510 m_treeCtrl
->SetSpacing( indent
-5 );
513 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
515 wxTreeItemId item
= m_treeCtrl
->GetSelection();
519 m_treeCtrl
->DoToggleIcon(item
);
522 // MyTreeCtrl implementation
523 #if USE_GENERIC_TREECTRL
524 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
526 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
529 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
530 const wxPoint
& pos
, const wxSize
& size
,
532 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
534 m_reverseSort
= FALSE
;
535 m_imageListNormal
= NULL
;
539 // Add some items to the tree
540 AddTestItemsToTree(3, 2);
543 void MyTreeCtrl::CreateImageList(int size
)
545 delete m_imageListNormal
;
549 m_imageListNormal
= NULL
;
553 // Make an image list containing small icons
554 m_imageListNormal
= new wxImageList(size
, size
, TRUE
);
556 // should correspond to TreeCtrlIcon_xxx enum
557 #if defined(__WXMSW__) && defined(__WIN16__)
558 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
559 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
560 m_imageListNormal
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
561 m_imageListNormal
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
562 m_imageListNormal
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
565 icons
[0] = wxICON(icon1
);
566 icons
[1] = wxICON(icon2
);
567 icons
[2] = wxICON(icon3
);
568 icons
[3] = wxICON(icon4
);
569 icons
[4] = wxICON(icon5
);
571 int sizeOrig
= icons
[0].GetWidth();
572 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
574 if ( size
== sizeOrig
)
576 m_imageListNormal
->Add(icons
[i
]);
580 m_imageListNormal
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
587 SetImageList(m_imageListNormal
);
590 MyTreeCtrl::~MyTreeCtrl()
592 delete m_imageListNormal
;
595 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
596 const wxTreeItemId
& item2
)
600 // just exchange 1st and 2nd items
601 return wxTreeCtrl::OnCompareItems(item2
, item1
);
605 return wxTreeCtrl::OnCompareItems(item1
, item2
);
609 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
616 bool hasChildren
= depth
> 1;
619 for ( size_t n
= 0; n
< numChildren
; n
++ )
621 // at depth 1 elements won't have any more children
623 str
.Printf("%s child %d", "Folder", n
+ 1);
625 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
627 // here we pass to AppendItem() normal and selected item images (we
628 // suppose that selected image follows the normal one in the enum)
630 if ( wxGetApp().ShowImages() )
632 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
633 imageSel
= image
+ 1;
637 image
= imageSel
= -1;
639 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
640 new MyTreeItemData(str
));
642 // and now we also set the expanded one (only for the folders)
643 if ( hasChildren
&& wxGetApp().ShowImages() )
645 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
646 wxTreeItemIcon_Expanded
);
649 // remember the last child for OnEnsureVisible()
650 if ( !hasChildren
&& n
== numChildren
- 1 )
655 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
661 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
664 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
665 wxTreeItemId rootId
= AddRoot("Root",
667 new MyTreeItemData("Root item"));
670 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
673 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
675 // set some colours/fonts for testing
676 SetItemFont(rootId
, *wxITALIC_FONT
);
679 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
680 SetItemTextColour(id
, *wxBLUE
);
682 id
= GetNextChild(rootId
, cookie
);
683 id
= GetNextChild(rootId
, cookie
);
684 SetItemTextColour(id
, *wxRED
);
685 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
688 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
693 id
= GetFirstChild(idParent
, cookie
);
695 id
= GetNextChild(idParent
, cookie
);
700 wxString text
= GetItemText(id
);
703 if (ItemHasChildren(id
))
704 GetItemsRecursively(id
,-1);
706 GetItemsRecursively(idParent
, cookie
);
709 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
711 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
712 : TreeCtrlIcon_Folder
;
714 SetItemImage(item
, image
);
719 #define TREE_EVENT_HANDLER(name) \
720 void MyTreeCtrl::name(wxTreeEvent& event) \
722 wxLogMessage(#name); \
726 TREE_EVENT_HANDLER(OnBeginRDrag
)
727 TREE_EVENT_HANDLER(OnDeleteItem
)
728 TREE_EVENT_HANDLER(OnGetInfo
)
729 TREE_EVENT_HANDLER(OnSetInfo
)
730 TREE_EVENT_HANDLER(OnItemExpanded
)
731 TREE_EVENT_HANDLER(OnItemExpanding
)
732 TREE_EVENT_HANDLER(OnItemCollapsed
)
733 TREE_EVENT_HANDLER(OnSelChanged
)
734 TREE_EVENT_HANDLER(OnSelChanging
)
735 TREE_EVENT_HANDLER(OnTreeKeyDown
)
737 #undef TREE_EVENT_HANDLER
739 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
741 // need to explicitly allow drag
742 if ( event
.GetItem() != GetRootItem() )
744 m_draggedItem
= event
.GetItem();
746 wxLogMessage("OnBeginDrag: started dragging %s",
747 GetItemText(m_draggedItem
).c_str());
753 wxLogMessage("OnBeginDrag: this item can't be dragged.");
757 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
759 wxTreeItemId itemSrc
= m_draggedItem
,
760 itemDst
= event
.GetItem();
761 m_draggedItem
= (wxTreeItemId
)0l;
763 // where to copy the item?
764 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
766 // copy to the parent then
767 itemDst
= GetParent(itemDst
);
770 if ( !itemDst
.IsOk() )
772 wxLogMessage("OnEndDrag: can't drop here.");
777 wxString text
= GetItemText(itemSrc
);
778 wxLogMessage("OnEndDrag: '%s' copied to '%s'.",
779 text
.c_str(), GetItemText(itemDst
).c_str());
781 // just do append here - we could also insert it just before/after the item
782 // on which it was dropped, but this requires slightly more work... we also
783 // completely ignore the client data and icon of the old item but could
784 // copy them as well.
786 // Finally, we only copy one item here but we might copy the entire tree if
787 // we were dragging a folder.
788 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
789 AppendItem(itemDst
, text
, image
);
792 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
794 wxLogMessage("OnBeginLabelEdit");
796 // for testing, prevent this items label editing
797 wxTreeItemId itemId
= event
.GetItem();
798 if ( IsTestItem(itemId
) )
800 wxMessageBox("You can't edit this item.");
806 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
808 wxLogMessage("OnEndLabelEdit");
810 // don't allow anything except letters in the labels
811 if ( !event
.GetLabel().IsWord() )
813 wxMessageBox("The label should contain only letters.");
819 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
821 wxLogMessage("OnItemCollapsing");
823 // for testing, prevent the user from collapsing the first child folder
824 wxTreeItemId itemId
= event
.GetItem();
825 if ( IsTestItem(itemId
) )
827 wxMessageBox("You can't collapse this item.");
833 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
835 // show some info about this item
836 wxTreeItemId itemId
= event
.GetItem();
837 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
841 item
->ShowInfo(this);
844 wxLogMessage("OnItemActivated");
847 void MyTreeCtrl::OnItemRightClick(wxTreeEvent
& event
)
849 ShowMenu(event
.GetItem(), event
.GetPoint());
852 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
854 wxPoint pt
= event
.GetPosition();
855 ShowMenu(HitTest(pt
), pt
);
858 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
863 title
<< _T("Menu for ") << GetItemText(id
);
867 title
= _T("Menu for no particular item");
871 menu
.Append(TreeTest_About
, _T("&About..."));
872 menu
.Append(TreeTest_Dump
, _T("&Dump"));
874 PopupMenu(&menu
, pt
);
877 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
879 wxTreeItemId id
= HitTest(event
.GetPosition());
881 wxLogMessage("No item under mouse");
884 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
886 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
890 static inline const char *Bool2String(bool b
)
892 return b
? "" : "not ";
895 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
897 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
898 "%u children (%u immediately under this item).",
900 Bool2String(tree
->IsSelected(GetId())),
901 Bool2String(tree
->IsExpanded(GetId())),
902 Bool2String(tree
->IsBold(GetId())),
903 tree
->GetChildrenCount(GetId()),
904 tree
->GetChildrenCount(GetId(), FALSE
));