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
)
78 MENU_LINK(TogFullHighlight
)
80 #ifndef NO_MULTIPLE_SELECTION
81 MENU_LINK(DumpSelected
)
85 #endif // NO_MULTIPLE_SELECTION
94 MENU_LINK(DeleteChildren
)
97 MENU_LINK(ToggleImages
)
98 MENU_LINK(ToggleButtons
)
99 MENU_LINK(SetImageSize
)
100 MENU_LINK(CollapseAndReset
)
101 MENU_LINK(EnsureVisible
)
103 MENU_LINK(InsertItem
)
106 MENU_LINK(IncSpacing
)
107 MENU_LINK(DecSpacing
)
108 MENU_LINK(ToggleIcon
)
113 #if USE_GENERIC_TREECTRL
114 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
116 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
118 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
119 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
120 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
121 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
122 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
123 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
124 #if 0 // there are so many of those that logging them causes flicker
125 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
127 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
128 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
129 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
130 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
131 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
132 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRightClick
)
134 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
135 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
136 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
137 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
138 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
139 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
146 // Create the main frame window
147 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
157 // My frame constructor
158 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
159 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
160 m_treeCtrl(NULL
), m_textCtrl(NULL
)
162 // This reduces flicker effects - even better would be to define
163 // OnEraseBackground to do nothing. When the tree control's scrollbars are
164 // show or hidden, the frame is sent a background erase event.
165 SetBackgroundColour(wxColour(255, 255, 255));
168 SetIcon(wxICON(mondrian
));
172 wxMenu
*file_menu
= new wxMenu
,
173 *style_menu
= new wxMenu
,
174 *tree_menu
= new wxMenu
,
175 *item_menu
= new wxMenu
;
177 file_menu
->Append(TreeTest_About
, wxT("&About..."));
178 file_menu
->AppendSeparator();
179 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
181 style_menu
->Append(TreeTest_TogButtons
, "Toggle &normal buttons");
182 style_menu
->Append(TreeTest_TogTwist
, "Toggle &twister buttons");
183 style_menu
->Append(TreeTest_ToggleButtons
, "Toggle image &buttons");
184 style_menu
->AppendSeparator();
185 style_menu
->Append(TreeTest_TogLines
, "Toggle &connecting lines");
186 style_menu
->Append(TreeTest_TogRootLines
, "Toggle &lines at root");
187 style_menu
->Append(TreeTest_TogHideRoot
, "Toggle &hidden root");
188 style_menu
->Append(TreeTest_TogBorder
, "Toggle &item border");
189 style_menu
->Append(TreeTest_TogFullHighlight
, "Toggle &full row highlight");
190 style_menu
->Append(TreeTest_TogEdit
, "Toggle &edit mode");
191 #ifndef NO_MULTIPLE_SELECTION
192 style_menu
->Append(TreeTest_ToggleSel
, wxT("Toggle &selection mode"));
193 #endif // NO_MULTIPLE_SELECTION
194 style_menu
->Append(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
195 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
197 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
198 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
199 tree_menu
->AppendSeparator();
200 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
201 tree_menu
->Append(TreeTest_InsertItem
, "&Insert a new item");
202 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
203 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
204 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
205 tree_menu
->AppendSeparator();
206 tree_menu
->Append(TreeTest_Count
, "Count children of current item");
207 tree_menu
->Append(TreeTest_CountRec
, "Recursively count children of current item");
208 tree_menu
->AppendSeparator();
209 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
210 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
211 tree_menu
->AppendSeparator();
212 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
213 tree_menu
->AppendSeparator();
214 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
215 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
216 tree_menu
->AppendSeparator();
217 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
218 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
220 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
221 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
223 item_menu
->AppendSeparator();
224 item_menu
->Append(TreeTest_SetBold
, "Make item &bold");
225 item_menu
->Append(TreeTest_ClearBold
, "Make item ¬ bold");
226 item_menu
->AppendSeparator();
227 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the item's &icon");
229 #ifndef NO_MULTIPLE_SELECTION
230 item_menu
->AppendSeparator();
231 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
232 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
233 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
234 #endif // NO_MULTIPLE_SELECTION
236 wxMenuBar
*menu_bar
= new wxMenuBar
;
237 menu_bar
->Append(file_menu
, "&File");
238 menu_bar
->Append(style_menu
, "&Style");
239 menu_bar
->Append(tree_menu
, "&Tree");
240 menu_bar
->Append(item_menu
, "&Item");
241 SetMenuBar(menu_bar
);
243 //menu_bar->Check(TreeTest_ToggleImages, TRUE);
244 #endif // wxUSE_MENUS
246 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
247 wxDefaultPosition
, wxDefaultSize
,
248 wxTR_DEFAULT_STYLE
| wxTR_EDIT_LABELS
|
249 #ifndef NO_VARIABLE_HEIGHT
250 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
254 // m_treeCtrl->SetBackgroundColour( *wxLIGHT_GREY );
256 m_textCtrl
= new wxTextCtrl(this, -1, "",
257 wxDefaultPosition
, wxDefaultSize
,
258 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
260 // create a status bar with 3 panes
262 SetStatusText("", 0);
265 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
266 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
267 delete wxLog::SetActiveTarget(new wxLogStderr
);
269 // set our text control as the log target
270 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
271 delete wxLog::SetActiveTarget(logWindow
);
277 delete wxLog::SetActiveTarget(NULL
);
280 void MyFrame::TogStyle(long flag
)
282 m_treeCtrl
->SetWindowStyle(m_treeCtrl
->GetWindowStyle() ^ flag
);
285 void MyFrame::OnSize(wxSizeEvent
& event
)
287 if ( m_treeCtrl
&& m_textCtrl
)
289 Resize(GetClientSize());
295 void MyFrame::Resize(const wxSize
& size
)
297 m_treeCtrl
->SetSize(0, 0, size
.x
, 2*size
.y
/3);
298 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
301 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
306 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
308 wxMessageBox("Tree test sample\n"
309 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
311 wxOK
| wxICON_INFORMATION
, this);
314 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
316 wxTreeItemId item
= m_treeCtrl
->GetSelection();
320 // old code - now we edit in place
322 static wxString s_text
;
323 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
325 if ( !s_text
.IsEmpty() )
327 m_treeCtrl
->SetItemText(item
, s_text
);
331 // TODO demonstrate creating a custom edit control...
332 (void)m_treeCtrl
->EditLabel(item
);
335 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
337 wxTreeItemId item
= m_treeCtrl
->GetSelection();
341 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
343 wxLogMessage(wxT("%d children"), i
);
346 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
348 wxTreeItemId item
= m_treeCtrl
->GetSelection();
352 int i
= m_treeCtrl
->GetChildrenCount( item
);
354 wxLogMessage(wxT("%d children"), i
);
357 void MyFrame::DoSort(bool reverse
)
359 wxTreeItemId item
= m_treeCtrl
->GetSelection();
363 m_treeCtrl
->DoSortChildren(item
, reverse
);
366 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
368 wxTreeItemId root
= m_treeCtrl
->GetSelection();
372 m_treeCtrl
->GetItemsRecursively(root
, -1);
375 #ifndef NO_MULTIPLE_SELECTION
377 void MyFrame::OnToggleSel(wxCommandEvent
& WXUNUSED(event
))
379 TogStyle(wxTR_MULTIPLE
);
381 long style
= m_treeCtrl
->GetWindowStyle();
382 if ( style
& wxTR_MULTIPLE
)
383 style
&= ~wxTR_MULTIPLE
;
385 style
|= wxTR_MULTIPLE
;
389 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
390 wxDefaultPosition
, wxDefaultSize
,
392 Resize(GetClientSize());
396 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
398 wxArrayTreeItemIds array
;
400 size_t count
= m_treeCtrl
->GetSelections(array
);
401 wxLogMessage(wxT("%u items selected"), count
);
403 for ( size_t n
= 0; n
< count
; n
++ )
405 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
409 void MyFrame::OnSelect(wxCommandEvent
& event
)
411 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
414 void MyFrame::OnUnselect(wxCommandEvent
& event
)
416 m_treeCtrl
->UnselectAll();
419 #endif // NO_MULTIPLE_SELECTION
421 void MyFrame::DoSetBold(bool bold
)
423 wxTreeItemId item
= m_treeCtrl
->GetSelection();
427 m_treeCtrl
->SetItemBold(item
, bold
);
430 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
432 wxTreeItemId item
= m_treeCtrl
->GetSelection();
436 m_treeCtrl
->Delete(item
);
439 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
441 wxTreeItemId item
= m_treeCtrl
->GetSelection();
445 m_treeCtrl
->DeleteChildren(item
);
448 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
450 m_treeCtrl
->DeleteAllItems();
453 void MyFrame::OnRecreate(wxCommandEvent
& event
)
456 m_treeCtrl
->AddTestItemsToTree(5, 2);
459 void MyFrame::OnSetImageSize(wxCommandEvent
& event
)
461 int size
= wxGetNumberFromUser("Enter the size for the images to use",
464 m_treeCtrl
->ImageSize());
468 m_treeCtrl
->CreateImageList(size
);
469 wxGetApp().SetShowImages(TRUE
);
472 void MyFrame::OnToggleImages(wxCommandEvent
& event
)
474 if ( wxGetApp().ShowImages() )
476 m_treeCtrl
->CreateImageList(-1);
477 wxGetApp().SetShowImages(FALSE
);
481 m_treeCtrl
->CreateImageList(0);
482 wxGetApp().SetShowImages(TRUE
);
486 void MyFrame::OnToggleButtons(wxCommandEvent
& event
)
488 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
489 if ( wxGetApp().ShowButtons() )
491 m_treeCtrl
->CreateButtonsImageList(-1);
492 wxGetApp().SetShowButtons(FALSE
);
496 m_treeCtrl
->CreateButtonsImageList(15);
497 wxGetApp().SetShowButtons(TRUE
);
502 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
504 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
507 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
509 m_treeCtrl
->DoEnsureVisible();
512 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
514 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
515 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, "2nd item");
518 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
520 static int s_num
= 0;
523 text
.Printf(wxT("Item #%d"), ++s_num
);
525 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
527 MyTreeCtrl::TreeCtrlIcon_File */ );
530 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
532 unsigned int indent
= m_treeCtrl
->GetIndent();
534 m_treeCtrl
->SetIndent( indent
+5 );
537 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
539 unsigned int indent
= m_treeCtrl
->GetIndent();
541 m_treeCtrl
->SetIndent( indent
-5 );
544 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
546 unsigned int indent
= m_treeCtrl
->GetSpacing();
548 m_treeCtrl
->SetSpacing( indent
+5 );
551 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
553 unsigned int indent
= m_treeCtrl
->GetSpacing();
555 m_treeCtrl
->SetSpacing( indent
-5 );
558 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
560 wxTreeItemId item
= m_treeCtrl
->GetSelection();
564 m_treeCtrl
->DoToggleIcon(item
);
567 // MyTreeCtrl implementation
568 #if USE_GENERIC_TREECTRL
569 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
571 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
574 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
575 const wxPoint
& pos
, const wxSize
& size
,
577 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
579 m_reverseSort
= FALSE
;
583 // Add some items to the tree
584 AddTestItemsToTree(5, 2);
587 void MyTreeCtrl::CreateImageList(int size
)
599 // Make an image list containing small icons
600 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
602 // should correspond to TreeCtrlIcon_xxx enum
603 #if defined(__WXMSW__) && defined(__WIN16__)
604 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
605 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
606 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
607 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
608 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
612 icons
[0] = wxICON(icon1
);
613 icons
[1] = wxICON(icon2
);
614 icons
[2] = wxICON(icon3
);
615 icons
[3] = wxICON(icon4
);
616 icons
[4] = wxICON(icon5
);
618 int sizeOrig
= icons
[0].GetWidth();
619 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
621 if ( size
== sizeOrig
)
623 images
->Add(icons
[i
]);
627 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
633 AssignImageList(images
);
636 void MyTreeCtrl::CreateButtonsImageList(int size
)
638 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
641 SetButtonsImageList(NULL
);
645 // Make an image list containing small icons
646 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
648 // should correspond to TreeCtrlIcon_xxx enum
649 #if defined(__WXMSW__) && defined(__WIN16__)
650 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
651 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
652 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
653 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
654 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
658 icons
[0] = wxICON(icon3
); // closed
659 icons
[1] = wxICON(icon3
); // closed, selected
660 icons
[2] = wxICON(icon5
); // open
661 icons
[3] = wxICON(icon5
); // open, selected
663 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
665 int sizeOrig
= icons
[i
].GetWidth();
666 if ( size
== sizeOrig
)
668 images
->Add(icons
[i
]);
672 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
678 AssignButtonsImageList(images
);
682 MyTreeCtrl::~MyTreeCtrl()
686 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
687 const wxTreeItemId
& item2
)
691 // just exchange 1st and 2nd items
692 return wxTreeCtrl::OnCompareItems(item2
, item1
);
696 return wxTreeCtrl::OnCompareItems(item1
, item2
);
700 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
707 bool hasChildren
= depth
> 1;
710 for ( size_t n
= 0; n
< numChildren
; n
++ )
712 // at depth 1 elements won't have any more children
714 str
.Printf(wxT("%s child %d"), wxT("Folder"), n
+ 1);
716 str
.Printf(wxT("%s child %d.%d"), wxT("File"), folder
, n
+ 1);
718 // here we pass to AppendItem() normal and selected item images (we
719 // suppose that selected image follows the normal one in the enum)
721 if ( wxGetApp().ShowImages() )
723 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
724 imageSel
= image
+ 1;
728 image
= imageSel
= -1;
730 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
731 new MyTreeItemData(str
));
733 // and now we also set the expanded one (only for the folders)
734 if ( hasChildren
&& wxGetApp().ShowImages() )
736 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
737 wxTreeItemIcon_Expanded
);
740 // remember the last child for OnEnsureVisible()
741 if ( !hasChildren
&& n
== numChildren
- 1 )
746 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
752 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
755 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
756 wxTreeItemId rootId
= AddRoot("Root",
758 new MyTreeItemData("Root item"));
761 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
764 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
766 // set some colours/fonts for testing
767 SetItemFont(rootId
, *wxITALIC_FONT
);
770 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
771 SetItemTextColour(id
, *wxBLUE
);
773 id
= GetNextChild(rootId
, cookie
);
774 id
= GetNextChild(rootId
, cookie
);
775 SetItemTextColour(id
, *wxRED
);
776 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
779 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
784 id
= GetFirstChild(idParent
, cookie
);
786 id
= GetNextChild(idParent
, cookie
);
791 wxString text
= GetItemText(id
);
794 if (ItemHasChildren(id
))
795 GetItemsRecursively(id
,-1);
797 GetItemsRecursively(idParent
, cookie
);
800 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
802 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
803 : TreeCtrlIcon_Folder
;
805 SetItemImage(item
, image
);
810 #define TREE_EVENT_HANDLER(name) \
811 void MyTreeCtrl::name(wxTreeEvent& event) \
813 wxLogMessage(wxT(#name)); \
817 TREE_EVENT_HANDLER(OnBeginRDrag
)
818 TREE_EVENT_HANDLER(OnDeleteItem
)
819 TREE_EVENT_HANDLER(OnGetInfo
)
820 TREE_EVENT_HANDLER(OnSetInfo
)
821 TREE_EVENT_HANDLER(OnItemExpanded
)
822 TREE_EVENT_HANDLER(OnItemExpanding
)
823 TREE_EVENT_HANDLER(OnItemCollapsed
)
824 TREE_EVENT_HANDLER(OnSelChanged
)
825 TREE_EVENT_HANDLER(OnSelChanging
)
826 TREE_EVENT_HANDLER(OnTreeKeyDown
)
828 #undef TREE_EVENT_HANDLER
830 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
832 // need to explicitly allow drag
833 if ( event
.GetItem() != GetRootItem() )
835 m_draggedItem
= event
.GetItem();
837 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
838 GetItemText(m_draggedItem
).c_str());
844 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
848 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
850 wxTreeItemId itemSrc
= m_draggedItem
,
851 itemDst
= event
.GetItem();
852 m_draggedItem
= (wxTreeItemId
)0l;
854 // where to copy the item?
855 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
857 // copy to the parent then
858 itemDst
= GetParent(itemDst
);
861 if ( !itemDst
.IsOk() )
863 wxLogMessage(wxT("OnEndDrag: can't drop here."));
868 wxString text
= GetItemText(itemSrc
);
869 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
870 text
.c_str(), GetItemText(itemDst
).c_str());
872 // just do append here - we could also insert it just before/after the item
873 // on which it was dropped, but this requires slightly more work... we also
874 // completely ignore the client data and icon of the old item but could
875 // copy them as well.
877 // Finally, we only copy one item here but we might copy the entire tree if
878 // we were dragging a folder.
879 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
880 AppendItem(itemDst
, text
, image
);
883 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
885 wxLogMessage(wxT("OnBeginLabelEdit"));
887 // for testing, prevent this item's label editing
888 wxTreeItemId itemId
= event
.GetItem();
889 if ( IsTestItem(itemId
) )
891 wxMessageBox(wxT("You can't edit this item."));
897 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
899 wxLogMessage(wxT("OnEndLabelEdit"));
901 // don't allow anything except letters in the labels
902 if ( !event
.GetLabel().IsWord() )
904 wxMessageBox(wxT("The label should contain only letters."));
910 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
912 wxLogMessage(wxT("OnItemCollapsing"));
914 // for testing, prevent the user from collapsing the first child folder
915 wxTreeItemId itemId
= event
.GetItem();
916 if ( IsTestItem(itemId
) )
918 wxMessageBox(wxT("You can't collapse this item."));
924 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
926 // show some info about this item
927 wxTreeItemId itemId
= event
.GetItem();
928 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
932 item
->ShowInfo(this);
935 wxLogMessage(wxT("OnItemActivated"));
938 void MyTreeCtrl::OnItemRightClick(wxTreeEvent
& event
)
940 ShowMenu(event
.GetItem(), event
.GetPoint());
943 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
945 wxPoint pt
= event
.GetPosition();
946 ShowMenu(HitTest(pt
), pt
);
949 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
954 title
<< _T("Menu for ") << GetItemText(id
);
958 title
= _T("Menu for no particular item");
963 menu
.Append(TreeTest_About
, _T("&About..."));
964 menu
.Append(TreeTest_Dump
, _T("&Dump"));
966 PopupMenu(&menu
, pt
);
967 #endif // wxUSE_MENUS
970 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
972 wxTreeItemId id
= HitTest(event
.GetPosition());
974 wxLogMessage(wxT("No item under mouse"));
977 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
979 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
983 static inline const wxChar
*Bool2String(bool b
)
985 return b
? wxT("") : wxT("not ");
988 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
990 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
991 wxT("%u children (%u immediately under this item)."),
993 Bool2String(tree
->IsSelected(GetId())),
994 Bool2String(tree
->IsExpanded(GetId())),
995 Bool2String(tree
->IsBold(GetId())),
996 tree
->GetChildrenCount(GetId()),
997 tree
->GetChildrenCount(GetId(), FALSE
));