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(wxT("Please select some item first!"), \
59 wxT("Tree sample error"), \
60 wxOK | wxICON_EXCLAMATION, \
65 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
66 EVT_SIZE(MyFrame::OnSize
)
68 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
75 MENU_LINK(TogHideRoot
)
76 MENU_LINK(TogRootLines
)
79 #ifndef NO_MULTIPLE_SELECTION
80 MENU_LINK(DumpSelected
)
84 #endif // NO_MULTIPLE_SELECTION
93 MENU_LINK(DeleteChildren
)
96 MENU_LINK(ToggleImages
)
97 MENU_LINK(ToggleButtons
)
98 MENU_LINK(SetImageSize
)
99 MENU_LINK(CollapseAndReset
)
100 MENU_LINK(EnsureVisible
)
102 MENU_LINK(InsertItem
)
105 MENU_LINK(IncSpacing
)
106 MENU_LINK(DecSpacing
)
107 MENU_LINK(ToggleIcon
)
112 #if USE_GENERIC_TREECTRL
113 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
115 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
117 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
118 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
119 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
120 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
121 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
122 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
123 #if 0 // there are so many of those that logging them causes flicker
124 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
126 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
127 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
128 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
129 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
130 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
131 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRightClick
)
133 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
134 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
135 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
136 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
137 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
138 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
145 // Create the main frame window
146 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
156 // My frame constructor
157 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
158 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
159 m_treeCtrl(NULL
), m_textCtrl(NULL
)
161 // This reduces flicker effects - even better would be to define
162 // OnEraseBackground to do nothing. When the tree control's scrollbars are
163 // show or hidden, the frame is sent a background erase event.
164 SetBackgroundColour(wxColour(255, 255, 255));
167 SetIcon(wxICON(mondrian
));
171 wxMenu
*file_menu
= new wxMenu
,
172 *style_menu
= new wxMenu
,
173 *tree_menu
= new wxMenu
,
174 *item_menu
= new wxMenu
;
176 file_menu
->Append(TreeTest_About
, wxT("&About..."));
177 file_menu
->AppendSeparator();
178 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
180 style_menu
->Append(TreeTest_TogButtons
, "Toggle &normal buttons");
181 style_menu
->Append(TreeTest_TogTwist
, "Toggle &twister buttons");
182 style_menu
->Append(TreeTest_ToggleButtons
, "Toggle image &buttons");
183 style_menu
->AppendSeparator();
184 style_menu
->Append(TreeTest_TogLines
, "Toggle &connecting lines");
185 style_menu
->Append(TreeTest_TogRootLines
, "Toggle &lines at root");
186 style_menu
->Append(TreeTest_TogHideRoot
, "Toggle &hidden root");
187 style_menu
->Append(TreeTest_TogBorder
, "Toggle &item border");
188 style_menu
->Append(TreeTest_TogEdit
, "Toggle &edit mode");
189 #ifndef NO_MULTIPLE_SELECTION
190 style_menu
->Append(TreeTest_ToggleSel
, wxT("Toggle &selection mode"));
191 #endif // NO_MULTIPLE_SELECTION
192 style_menu
->Append(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
193 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
195 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
196 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
197 tree_menu
->AppendSeparator();
198 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
199 tree_menu
->Append(TreeTest_InsertItem
, "&Insert a new item");
200 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
201 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
202 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
203 tree_menu
->AppendSeparator();
204 tree_menu
->Append(TreeTest_Count
, "Count children of current item");
205 tree_menu
->Append(TreeTest_CountRec
, "Recursively count children of current item");
206 tree_menu
->AppendSeparator();
207 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
208 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
209 tree_menu
->AppendSeparator();
210 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
211 tree_menu
->AppendSeparator();
212 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
213 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
214 tree_menu
->AppendSeparator();
215 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
216 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
218 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
219 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
221 item_menu
->AppendSeparator();
222 item_menu
->Append(TreeTest_SetBold
, "Make item &bold");
223 item_menu
->Append(TreeTest_ClearBold
, "Make item ¬ bold");
224 item_menu
->AppendSeparator();
225 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the item's &icon");
227 #ifndef NO_MULTIPLE_SELECTION
228 item_menu
->AppendSeparator();
229 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
230 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
231 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
232 #endif // NO_MULTIPLE_SELECTION
234 wxMenuBar
*menu_bar
= new wxMenuBar
;
235 menu_bar
->Append(file_menu
, "&File");
236 menu_bar
->Append(style_menu
, "&Style");
237 menu_bar
->Append(tree_menu
, "&Tree");
238 menu_bar
->Append(item_menu
, "&Item");
239 SetMenuBar(menu_bar
);
241 //menu_bar->Check(TreeTest_ToggleImages, TRUE);
242 #endif // wxUSE_MENUS
244 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
245 wxDefaultPosition
, wxDefaultSize
,
246 wxTR_DEFAULT_STYLE
| wxTR_EDIT_LABELS
|
247 #ifndef NO_VARIABLE_HEIGHT
248 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
252 // m_treeCtrl->SetBackgroundColour( *wxLIGHT_GREY );
254 m_textCtrl
= new wxTextCtrl(this, -1, "",
255 wxDefaultPosition
, wxDefaultSize
,
256 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
258 // create a status bar with 3 panes
260 SetStatusText("", 0);
263 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
264 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
265 delete wxLog::SetActiveTarget(new wxLogStderr
);
267 // set our text control as the log target
268 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
269 delete wxLog::SetActiveTarget(logWindow
);
275 delete wxLog::SetActiveTarget(NULL
);
278 void MyFrame::TogStyle(long flag
)
280 m_treeCtrl
->SetWindowStyle(m_treeCtrl
->GetWindowStyle() ^ flag
);
283 void MyFrame::OnSize(wxSizeEvent
& event
)
285 if ( m_treeCtrl
&& m_textCtrl
)
287 Resize(GetClientSize());
293 void MyFrame::Resize(const wxSize
& size
)
295 m_treeCtrl
->SetSize(0, 0, size
.x
, 2*size
.y
/3);
296 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
299 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
304 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
306 wxMessageBox("Tree test sample\n"
307 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
309 wxOK
| wxICON_INFORMATION
, this);
312 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
314 wxTreeItemId item
= m_treeCtrl
->GetSelection();
318 // old code - now we edit in place
320 static wxString s_text
;
321 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
323 if ( !s_text
.IsEmpty() )
325 m_treeCtrl
->SetItemText(item
, s_text
);
329 // TODO demonstrate creating a custom edit control...
330 (void)m_treeCtrl
->EditLabel(item
);
333 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
335 wxTreeItemId item
= m_treeCtrl
->GetSelection();
339 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
341 wxLogMessage(wxT("%d children"), i
);
344 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
346 wxTreeItemId item
= m_treeCtrl
->GetSelection();
350 int i
= m_treeCtrl
->GetChildrenCount( item
);
352 wxLogMessage(wxT("%d children"), i
);
355 void MyFrame::DoSort(bool reverse
)
357 wxTreeItemId item
= m_treeCtrl
->GetSelection();
361 m_treeCtrl
->DoSortChildren(item
, reverse
);
364 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
366 wxTreeItemId root
= m_treeCtrl
->GetSelection();
370 m_treeCtrl
->GetItemsRecursively(root
, -1);
373 #ifndef NO_MULTIPLE_SELECTION
375 void MyFrame::OnToggleSel(wxCommandEvent
& WXUNUSED(event
))
377 TogStyle(wxTR_MULTIPLE
);
379 long style
= m_treeCtrl
->GetWindowStyle();
380 if ( style
& wxTR_MULTIPLE
)
381 style
&= ~wxTR_MULTIPLE
;
383 style
|= wxTR_MULTIPLE
;
387 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
388 wxDefaultPosition
, wxDefaultSize
,
390 Resize(GetClientSize());
394 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
396 wxArrayTreeItemIds array
;
398 size_t count
= m_treeCtrl
->GetSelections(array
);
399 wxLogMessage(wxT("%u items selected"), count
);
401 for ( size_t n
= 0; n
< count
; n
++ )
403 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
407 void MyFrame::OnSelect(wxCommandEvent
& event
)
409 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
412 void MyFrame::OnUnselect(wxCommandEvent
& event
)
414 m_treeCtrl
->UnselectAll();
417 #endif // NO_MULTIPLE_SELECTION
419 void MyFrame::DoSetBold(bool bold
)
421 wxTreeItemId item
= m_treeCtrl
->GetSelection();
425 m_treeCtrl
->SetItemBold(item
, bold
);
428 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
430 wxTreeItemId item
= m_treeCtrl
->GetSelection();
434 m_treeCtrl
->Delete(item
);
437 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
439 wxTreeItemId item
= m_treeCtrl
->GetSelection();
443 m_treeCtrl
->DeleteChildren(item
);
446 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
448 m_treeCtrl
->DeleteAllItems();
451 void MyFrame::OnRecreate(wxCommandEvent
& event
)
454 m_treeCtrl
->AddTestItemsToTree(5, 2);
457 void MyFrame::OnSetImageSize(wxCommandEvent
& event
)
459 int size
= wxGetNumberFromUser("Enter the size for the images to use",
462 m_treeCtrl
->ImageSize());
466 m_treeCtrl
->CreateImageList(size
);
467 wxGetApp().SetShowImages(TRUE
);
470 void MyFrame::OnToggleImages(wxCommandEvent
& event
)
472 if ( wxGetApp().ShowImages() )
474 m_treeCtrl
->CreateImageList(-1);
475 wxGetApp().SetShowImages(FALSE
);
479 m_treeCtrl
->CreateImageList(0);
480 wxGetApp().SetShowImages(TRUE
);
484 void MyFrame::OnToggleButtons(wxCommandEvent
& event
)
486 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
487 if ( wxGetApp().ShowButtons() )
489 m_treeCtrl
->CreateButtonsImageList(-1);
490 wxGetApp().SetShowButtons(FALSE
);
494 m_treeCtrl
->CreateButtonsImageList(15);
495 wxGetApp().SetShowButtons(TRUE
);
500 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
502 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
505 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
507 m_treeCtrl
->DoEnsureVisible();
510 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
512 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
513 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, "2nd item");
516 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
518 static int s_num
= 0;
521 text
.Printf(wxT("Item #%d"), ++s_num
);
523 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
525 MyTreeCtrl::TreeCtrlIcon_File */ );
528 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
530 unsigned int indent
= m_treeCtrl
->GetIndent();
532 m_treeCtrl
->SetIndent( indent
+5 );
535 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
537 unsigned int indent
= m_treeCtrl
->GetIndent();
539 m_treeCtrl
->SetIndent( indent
-5 );
542 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
544 unsigned int indent
= m_treeCtrl
->GetSpacing();
546 m_treeCtrl
->SetSpacing( indent
+5 );
549 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
551 unsigned int indent
= m_treeCtrl
->GetSpacing();
553 m_treeCtrl
->SetSpacing( indent
-5 );
556 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
558 wxTreeItemId item
= m_treeCtrl
->GetSelection();
562 m_treeCtrl
->DoToggleIcon(item
);
565 // MyTreeCtrl implementation
566 #if USE_GENERIC_TREECTRL
567 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
569 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
572 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
573 const wxPoint
& pos
, const wxSize
& size
,
575 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
577 m_reverseSort
= FALSE
;
581 // Add some items to the tree
582 AddTestItemsToTree(5, 2);
585 void MyTreeCtrl::CreateImageList(int size
)
597 // Make an image list containing small icons
598 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
600 // should correspond to TreeCtrlIcon_xxx enum
601 #if defined(__WXMSW__) && defined(__WIN16__)
602 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
603 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
604 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
605 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
606 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
610 icons
[0] = wxICON(icon1
);
611 icons
[1] = wxICON(icon2
);
612 icons
[2] = wxICON(icon3
);
613 icons
[3] = wxICON(icon4
);
614 icons
[4] = wxICON(icon5
);
616 int sizeOrig
= icons
[0].GetWidth();
617 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
619 if ( size
== sizeOrig
)
621 images
->Add(icons
[i
]);
625 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
631 AssignImageList(images
);
634 void MyTreeCtrl::CreateButtonsImageList(int size
)
636 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
639 SetButtonsImageList(NULL
);
643 // Make an image list containing small icons
644 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
646 // should correspond to TreeCtrlIcon_xxx enum
647 #if defined(__WXMSW__) && defined(__WIN16__)
648 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
649 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
650 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
651 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
652 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
656 icons
[0] = wxICON(icon3
); // closed
657 icons
[1] = wxICON(icon3
); // closed, selected
658 icons
[2] = wxICON(icon5
); // open
659 icons
[3] = wxICON(icon5
); // open, selected
661 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
663 int sizeOrig
= icons
[i
].GetWidth();
664 if ( size
== sizeOrig
)
666 images
->Add(icons
[i
]);
670 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
676 AssignButtonsImageList(images
);
680 MyTreeCtrl::~MyTreeCtrl()
684 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
685 const wxTreeItemId
& item2
)
689 // just exchange 1st and 2nd items
690 return wxTreeCtrl::OnCompareItems(item2
, item1
);
694 return wxTreeCtrl::OnCompareItems(item1
, item2
);
698 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
705 bool hasChildren
= depth
> 1;
708 for ( size_t n
= 0; n
< numChildren
; n
++ )
710 // at depth 1 elements won't have any more children
712 str
.Printf(wxT("%s child %d"), wxT("Folder"), n
+ 1);
714 str
.Printf(wxT("%s child %d.%d"), wxT("File"), folder
, n
+ 1);
716 // here we pass to AppendItem() normal and selected item images (we
717 // suppose that selected image follows the normal one in the enum)
719 if ( wxGetApp().ShowImages() )
721 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
722 imageSel
= image
+ 1;
726 image
= imageSel
= -1;
728 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
729 new MyTreeItemData(str
));
731 // and now we also set the expanded one (only for the folders)
732 if ( hasChildren
&& wxGetApp().ShowImages() )
734 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
735 wxTreeItemIcon_Expanded
);
738 // remember the last child for OnEnsureVisible()
739 if ( !hasChildren
&& n
== numChildren
- 1 )
744 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
750 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
753 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
754 wxTreeItemId rootId
= AddRoot("Root",
756 new MyTreeItemData("Root item"));
759 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
762 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
764 // set some colours/fonts for testing
765 SetItemFont(rootId
, *wxITALIC_FONT
);
768 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
769 SetItemTextColour(id
, *wxBLUE
);
771 id
= GetNextChild(rootId
, cookie
);
772 id
= GetNextChild(rootId
, cookie
);
773 SetItemTextColour(id
, *wxRED
);
774 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
777 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
782 id
= GetFirstChild(idParent
, cookie
);
784 id
= GetNextChild(idParent
, cookie
);
789 wxString text
= GetItemText(id
);
792 if (ItemHasChildren(id
))
793 GetItemsRecursively(id
,-1);
795 GetItemsRecursively(idParent
, cookie
);
798 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
800 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
801 : TreeCtrlIcon_Folder
;
803 SetItemImage(item
, image
);
808 #define TREE_EVENT_HANDLER(name) \
809 void MyTreeCtrl::name(wxTreeEvent& event) \
811 wxLogMessage(wxT(#name)); \
815 TREE_EVENT_HANDLER(OnBeginRDrag
)
816 TREE_EVENT_HANDLER(OnDeleteItem
)
817 TREE_EVENT_HANDLER(OnGetInfo
)
818 TREE_EVENT_HANDLER(OnSetInfo
)
819 TREE_EVENT_HANDLER(OnItemExpanded
)
820 TREE_EVENT_HANDLER(OnItemExpanding
)
821 TREE_EVENT_HANDLER(OnItemCollapsed
)
822 TREE_EVENT_HANDLER(OnSelChanged
)
823 TREE_EVENT_HANDLER(OnSelChanging
)
824 TREE_EVENT_HANDLER(OnTreeKeyDown
)
826 #undef TREE_EVENT_HANDLER
828 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
830 // need to explicitly allow drag
831 if ( event
.GetItem() != GetRootItem() )
833 m_draggedItem
= event
.GetItem();
835 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
836 GetItemText(m_draggedItem
).c_str());
842 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
846 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
848 wxTreeItemId itemSrc
= m_draggedItem
,
849 itemDst
= event
.GetItem();
850 m_draggedItem
= (wxTreeItemId
)0l;
852 // where to copy the item?
853 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
855 // copy to the parent then
856 itemDst
= GetParent(itemDst
);
859 if ( !itemDst
.IsOk() )
861 wxLogMessage(wxT("OnEndDrag: can't drop here."));
866 wxString text
= GetItemText(itemSrc
);
867 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
868 text
.c_str(), GetItemText(itemDst
).c_str());
870 // just do append here - we could also insert it just before/after the item
871 // on which it was dropped, but this requires slightly more work... we also
872 // completely ignore the client data and icon of the old item but could
873 // copy them as well.
875 // Finally, we only copy one item here but we might copy the entire tree if
876 // we were dragging a folder.
877 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
878 AppendItem(itemDst
, text
, image
);
881 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
883 wxLogMessage(wxT("OnBeginLabelEdit"));
885 // for testing, prevent this item's label editing
886 wxTreeItemId itemId
= event
.GetItem();
887 if ( IsTestItem(itemId
) )
889 wxMessageBox(wxT("You can't edit this item."));
895 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
897 wxLogMessage(wxT("OnEndLabelEdit"));
899 // don't allow anything except letters in the labels
900 if ( !event
.GetLabel().IsWord() )
902 wxMessageBox(wxT("The label should contain only letters."));
908 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
910 wxLogMessage(wxT("OnItemCollapsing"));
912 // for testing, prevent the user from collapsing the first child folder
913 wxTreeItemId itemId
= event
.GetItem();
914 if ( IsTestItem(itemId
) )
916 wxMessageBox(wxT("You can't collapse this item."));
922 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
924 // show some info about this item
925 wxTreeItemId itemId
= event
.GetItem();
926 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
930 item
->ShowInfo(this);
933 wxLogMessage(wxT("OnItemActivated"));
936 void MyTreeCtrl::OnItemRightClick(wxTreeEvent
& event
)
938 ShowMenu(event
.GetItem(), event
.GetPoint());
941 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
943 wxPoint pt
= event
.GetPosition();
944 ShowMenu(HitTest(pt
), pt
);
947 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
952 title
<< _T("Menu for ") << GetItemText(id
);
956 title
= _T("Menu for no particular item");
961 menu
.Append(TreeTest_About
, _T("&About..."));
962 menu
.Append(TreeTest_Dump
, _T("&Dump"));
964 PopupMenu(&menu
, pt
);
965 #endif // wxUSE_MENUS
968 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
970 wxTreeItemId id
= HitTest(event
.GetPosition());
972 wxLogMessage(wxT("No item under mouse"));
975 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
977 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
981 static inline const wxChar
*Bool2String(bool b
)
983 return b
? wxT("") : wxT("not ");
986 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
988 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
989 wxT("%u children (%u immediately under this item)."),
991 Bool2String(tree
->IsSelected(GetId())),
992 Bool2String(tree
->IsExpanded(GetId())),
993 Bool2String(tree
->IsBold(GetId())),
994 tree
->GetChildrenCount(GetId()),
995 tree
->GetChildrenCount(GetId(), FALSE
));