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
));
160 wxMenu
*file_menu
= new wxMenu
,
161 *tree_menu
= new wxMenu
,
162 *item_menu
= new wxMenu
;
164 file_menu
->Append(TreeTest_About
, "&About...");
165 file_menu
->AppendSeparator();
166 file_menu
->Append(TreeTest_Quit
, "E&xit\tAlt-X");
168 #ifndef NO_MULTIPLE_SELECTION
169 tree_menu
->Append(TreeTest_ToggleSel
, "&Toggle selection mode");
170 #endif // NO_MULTIPLE_SELECTION
171 tree_menu
->Append(TreeTest_ToggleImages
, "&Show images", "", TRUE
);
172 tree_menu
->Append(TreeTest_SetImageSize
, "Set image si&ze...");
173 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
174 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
175 tree_menu
->AppendSeparator();
176 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
177 tree_menu
->Append(TreeTest_InsertItem
, "&Insert a new item");
178 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
179 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
180 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
181 tree_menu
->AppendSeparator();
182 tree_menu
->Append(TreeTest_Count
, "Count children of current item");
183 tree_menu
->Append(TreeTest_CountRec
, "Recursively count children of current item");
184 tree_menu
->AppendSeparator();
185 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
186 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
187 tree_menu
->AppendSeparator();
188 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
189 tree_menu
->AppendSeparator();
190 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
191 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
192 tree_menu
->AppendSeparator();
193 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
194 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
196 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
197 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
199 item_menu
->AppendSeparator();
200 item_menu
->Append(TreeTest_Bold
, "Make item &bold");
201 item_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
202 item_menu
->AppendSeparator();
203 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the items &icon");
205 #ifndef NO_MULTIPLE_SELECTION
206 item_menu
->AppendSeparator();
207 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
208 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
209 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
210 #endif // NO_MULTIPLE_SELECTION
212 wxMenuBar
*menu_bar
= new wxMenuBar
;
213 menu_bar
->Append(file_menu
, "&File");
214 menu_bar
->Append(tree_menu
, "&Tree");
215 menu_bar
->Append(item_menu
, "&Item");
216 SetMenuBar(menu_bar
);
218 menu_bar
->Check(TreeTest_ToggleImages
, TRUE
);
219 #endif // wxUSE_MENUS
221 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
222 wxDefaultPosition
, wxDefaultSize
,
223 wxTR_MAC_BUTTONS
| wxTR_NO_LINES
|
225 #ifndef NO_VARIABLE_HEIGHT
226 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
230 // m_treeCtrl->SetBackgroundColour( *wxLIGHT_GREY );
232 m_textCtrl
= new wxTextCtrl(this, -1, "",
233 wxDefaultPosition
, wxDefaultSize
,
234 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
236 // create a status bar with 3 panes
238 SetStatusText("", 0);
241 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
242 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
243 delete wxLog::SetActiveTarget(new wxLogStderr
);
245 // set our text control as the log target
246 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
247 delete wxLog::SetActiveTarget(logWindow
);
253 delete wxLog::SetActiveTarget(NULL
);
256 void MyFrame::OnSize(wxSizeEvent
& event
)
258 if ( m_treeCtrl
&& m_textCtrl
)
260 Resize(GetClientSize());
266 void MyFrame::Resize(const wxSize
& size
)
268 m_treeCtrl
->SetSize(0, 0, size
.x
, 2*size
.y
/3);
269 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
272 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
277 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
279 wxMessageBox("Tree test sample\n"
280 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
282 wxOK
| wxICON_INFORMATION
, this);
285 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
287 wxTreeItemId item
= m_treeCtrl
->GetSelection();
291 // old code - now we edit in place
293 static wxString s_text
;
294 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
296 if ( !s_text
.IsEmpty() )
298 m_treeCtrl
->SetItemText(item
, s_text
);
302 // TODO demonstrate creating a custom edit control...
303 (void)m_treeCtrl
->EditLabel(item
);
306 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
308 wxTreeItemId item
= m_treeCtrl
->GetSelection();
312 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
314 wxLogMessage(wxT("%d children"), i
);
317 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
319 wxTreeItemId item
= m_treeCtrl
->GetSelection();
323 int i
= m_treeCtrl
->GetChildrenCount( item
);
325 wxLogMessage(wxT("%d children"), i
);
328 void MyFrame::DoSort(bool reverse
)
330 wxTreeItemId item
= m_treeCtrl
->GetSelection();
334 m_treeCtrl
->DoSortChildren(item
, reverse
);
337 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
339 wxTreeItemId root
= m_treeCtrl
->GetSelection();
343 m_treeCtrl
->GetItemsRecursively(root
, -1);
346 #ifndef NO_MULTIPLE_SELECTION
348 void MyFrame::OnToggleSel(wxCommandEvent
& WXUNUSED(event
))
350 long style
= m_treeCtrl
->GetWindowStyle();
351 if ( style
& wxTR_MULTIPLE
)
352 style
&= ~wxTR_MULTIPLE
;
354 style
|= wxTR_MULTIPLE
;
358 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
359 wxDefaultPosition
, wxDefaultSize
,
361 Resize(GetClientSize());
364 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
366 wxArrayTreeItemIds array
;
368 size_t count
= m_treeCtrl
->GetSelections(array
);
369 wxLogMessage(wxT("%u items selected"), count
);
371 for ( size_t n
= 0; n
< count
; n
++ )
373 wxLogMessage("\t%s", m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
377 void MyFrame::OnSelect(wxCommandEvent
& event
)
379 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
382 void MyFrame::OnUnselect(wxCommandEvent
& event
)
384 m_treeCtrl
->UnselectAll();
387 #endif // NO_MULTIPLE_SELECTION
389 void MyFrame::DoSetBold(bool bold
)
391 wxTreeItemId item
= m_treeCtrl
->GetSelection();
395 m_treeCtrl
->SetItemBold(item
, bold
);
398 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
400 wxTreeItemId item
= m_treeCtrl
->GetSelection();
404 m_treeCtrl
->Delete(item
);
407 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
409 wxTreeItemId item
= m_treeCtrl
->GetSelection();
413 m_treeCtrl
->DeleteChildren(item
);
416 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
418 m_treeCtrl
->DeleteAllItems();
421 void MyFrame::OnRecreate(wxCommandEvent
& event
)
424 m_treeCtrl
->AddTestItemsToTree(3, 2);
427 void MyFrame::OnSetImageSize(wxCommandEvent
& event
)
429 long size
= wxGetNumberFromUser("Enter the size for the images to use",
436 m_treeCtrl
->CreateImageList((int)size
);
441 void MyFrame::OnToggleImages(wxCommandEvent
& event
)
443 if ( wxGetApp().ShowImages() )
445 m_treeCtrl
->CreateImageList(-1);
447 wxGetApp().SetShowImages(FALSE
);
451 m_treeCtrl
->CreateImageList();
453 wxGetApp().SetShowImages(TRUE
);
459 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
461 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
464 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
466 m_treeCtrl
->DoEnsureVisible();
469 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
471 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
472 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, "2nd item");
475 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
477 static int s_num
= 0;
480 text
.Printf("Item #%d", ++s_num
);
482 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
484 MyTreeCtrl::TreeCtrlIcon_File */ );
487 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
489 unsigned int indent
= m_treeCtrl
->GetIndent();
491 m_treeCtrl
->SetIndent( indent
+5 );
494 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
496 unsigned int indent
= m_treeCtrl
->GetIndent();
498 m_treeCtrl
->SetIndent( indent
-5 );
501 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
503 unsigned int indent
= m_treeCtrl
->GetSpacing();
505 m_treeCtrl
->SetSpacing( indent
+5 );
508 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
510 unsigned int indent
= m_treeCtrl
->GetSpacing();
512 m_treeCtrl
->SetSpacing( indent
-5 );
515 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
517 wxTreeItemId item
= m_treeCtrl
->GetSelection();
521 m_treeCtrl
->DoToggleIcon(item
);
524 // MyTreeCtrl implementation
525 #if USE_GENERIC_TREECTRL
526 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
528 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
531 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
532 const wxPoint
& pos
, const wxSize
& size
,
534 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
536 m_reverseSort
= FALSE
;
537 m_imageListNormal
= NULL
;
541 // Add some items to the tree
542 AddTestItemsToTree(3, 2);
545 void MyTreeCtrl::CreateImageList(int size
)
547 delete m_imageListNormal
;
551 m_imageListNormal
= NULL
;
555 // Make an image list containing small icons
556 m_imageListNormal
= new wxImageList(size
, size
, TRUE
);
558 // should correspond to TreeCtrlIcon_xxx enum
559 #if defined(__WXMSW__) && defined(__WIN16__)
560 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
561 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
562 m_imageListNormal
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
563 m_imageListNormal
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
564 m_imageListNormal
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
567 icons
[0] = wxICON(icon1
);
568 icons
[1] = wxICON(icon2
);
569 icons
[2] = wxICON(icon3
);
570 icons
[3] = wxICON(icon4
);
571 icons
[4] = wxICON(icon5
);
573 int sizeOrig
= icons
[0].GetWidth();
574 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
576 if ( size
== sizeOrig
)
578 m_imageListNormal
->Add(icons
[i
]);
582 m_imageListNormal
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
589 SetImageList(m_imageListNormal
);
592 MyTreeCtrl::~MyTreeCtrl()
594 delete m_imageListNormal
;
597 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
598 const wxTreeItemId
& item2
)
602 // just exchange 1st and 2nd items
603 return wxTreeCtrl::OnCompareItems(item2
, item1
);
607 return wxTreeCtrl::OnCompareItems(item1
, item2
);
611 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
618 bool hasChildren
= depth
> 1;
621 for ( size_t n
= 0; n
< numChildren
; n
++ )
623 // at depth 1 elements won't have any more children
625 str
.Printf("%s child %d", "Folder", n
+ 1);
627 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
629 // here we pass to AppendItem() normal and selected item images (we
630 // suppose that selected image follows the normal one in the enum)
632 if ( wxGetApp().ShowImages() )
634 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
635 imageSel
= image
+ 1;
639 image
= imageSel
= -1;
641 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
642 new MyTreeItemData(str
));
644 // and now we also set the expanded one (only for the folders)
645 if ( hasChildren
&& wxGetApp().ShowImages() )
647 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
648 wxTreeItemIcon_Expanded
);
651 // remember the last child for OnEnsureVisible()
652 if ( !hasChildren
&& n
== numChildren
- 1 )
657 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
663 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
666 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
667 wxTreeItemId rootId
= AddRoot("Root",
669 new MyTreeItemData("Root item"));
672 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
675 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
677 // set some colours/fonts for testing
678 SetItemFont(rootId
, *wxITALIC_FONT
);
681 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
682 SetItemTextColour(id
, *wxBLUE
);
684 id
= GetNextChild(rootId
, cookie
);
685 id
= GetNextChild(rootId
, cookie
);
686 SetItemTextColour(id
, *wxRED
);
687 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
690 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
695 id
= GetFirstChild(idParent
, cookie
);
697 id
= GetNextChild(idParent
, cookie
);
702 wxString text
= GetItemText(id
);
705 if (ItemHasChildren(id
))
706 GetItemsRecursively(id
,-1);
708 GetItemsRecursively(idParent
, cookie
);
711 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
713 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
714 : TreeCtrlIcon_Folder
;
716 SetItemImage(item
, image
);
721 #define TREE_EVENT_HANDLER(name) \
722 void MyTreeCtrl::name(wxTreeEvent& event) \
724 wxLogMessage(#name); \
728 TREE_EVENT_HANDLER(OnBeginRDrag
)
729 TREE_EVENT_HANDLER(OnDeleteItem
)
730 TREE_EVENT_HANDLER(OnGetInfo
)
731 TREE_EVENT_HANDLER(OnSetInfo
)
732 TREE_EVENT_HANDLER(OnItemExpanded
)
733 TREE_EVENT_HANDLER(OnItemExpanding
)
734 TREE_EVENT_HANDLER(OnItemCollapsed
)
735 TREE_EVENT_HANDLER(OnSelChanged
)
736 TREE_EVENT_HANDLER(OnSelChanging
)
737 TREE_EVENT_HANDLER(OnTreeKeyDown
)
739 #undef TREE_EVENT_HANDLER
741 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
743 // need to explicitly allow drag
744 if ( event
.GetItem() != GetRootItem() )
746 m_draggedItem
= event
.GetItem();
748 wxLogMessage("OnBeginDrag: started dragging %s",
749 GetItemText(m_draggedItem
).c_str());
755 wxLogMessage("OnBeginDrag: this item can't be dragged.");
759 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
761 wxTreeItemId itemSrc
= m_draggedItem
,
762 itemDst
= event
.GetItem();
763 m_draggedItem
= (wxTreeItemId
)0l;
765 // where to copy the item?
766 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
768 // copy to the parent then
769 itemDst
= GetParent(itemDst
);
772 if ( !itemDst
.IsOk() )
774 wxLogMessage("OnEndDrag: can't drop here.");
779 wxString text
= GetItemText(itemSrc
);
780 wxLogMessage("OnEndDrag: '%s' copied to '%s'.",
781 text
.c_str(), GetItemText(itemDst
).c_str());
783 // just do append here - we could also insert it just before/after the item
784 // on which it was dropped, but this requires slightly more work... we also
785 // completely ignore the client data and icon of the old item but could
786 // copy them as well.
788 // Finally, we only copy one item here but we might copy the entire tree if
789 // we were dragging a folder.
790 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
791 AppendItem(itemDst
, text
, image
);
794 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
796 wxLogMessage("OnBeginLabelEdit");
798 // for testing, prevent this items label editing
799 wxTreeItemId itemId
= event
.GetItem();
800 if ( IsTestItem(itemId
) )
802 wxMessageBox("You can't edit this item.");
808 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
810 wxLogMessage("OnEndLabelEdit");
812 // don't allow anything except letters in the labels
813 if ( !event
.GetLabel().IsWord() )
815 wxMessageBox("The label should contain only letters.");
821 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
823 wxLogMessage("OnItemCollapsing");
825 // for testing, prevent the user from collapsing the first child folder
826 wxTreeItemId itemId
= event
.GetItem();
827 if ( IsTestItem(itemId
) )
829 wxMessageBox("You can't collapse this item.");
835 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
837 // show some info about this item
838 wxTreeItemId itemId
= event
.GetItem();
839 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
843 item
->ShowInfo(this);
846 wxLogMessage("OnItemActivated");
849 void MyTreeCtrl::OnItemRightClick(wxTreeEvent
& event
)
851 ShowMenu(event
.GetItem(), event
.GetPoint());
854 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
856 wxPoint pt
= event
.GetPosition();
857 ShowMenu(HitTest(pt
), pt
);
860 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
865 title
<< _T("Menu for ") << GetItemText(id
);
869 title
= _T("Menu for no particular item");
874 menu
.Append(TreeTest_About
, _T("&About..."));
875 menu
.Append(TreeTest_Dump
, _T("&Dump"));
877 PopupMenu(&menu
, pt
);
878 #endif // wxUSE_MENUS
881 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
883 wxTreeItemId id
= HitTest(event
.GetPosition());
885 wxLogMessage("No item under mouse");
888 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
890 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
894 static inline const char *Bool2String(bool b
)
896 return b
? "" : "not ";
899 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
901 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
902 "%u children (%u immediately under this item).",
904 Bool2String(tree
->IsSelected(GetId())),
905 Bool2String(tree
->IsExpanded(GetId())),
906 Bool2String(tree
->IsBold(GetId())),
907 tree
->GetChildrenCount(GetId()),
908 tree
->GetChildrenCount(GetId(), FALSE
));