1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
31 #include "wx/imaglist.h"
32 #include "wx/treectrl.h"
36 //#define NO_VARIABLE_HEIGHT
37 //#define NO_MULTIPLE_SELECTION
40 // comment out this line to test multiple selection even under MSW (where
41 // it looks ugly - but works)
42 #define NO_MULTIPLE_SELECTION
47 // under Windows the icons are in the .rc file
54 #include "mondrian.xpm"
57 // verify that the item is ok and insult the user if it is not
58 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
59 wxMessageBox("Please select some item first!", \
60 "Tree sample error", \
61 wxOK | wxICON_EXCLAMATION, \
66 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
67 EVT_MENU(TreeTest_Quit
, MyFrame::OnQuit
)
68 EVT_MENU(TreeTest_About
, MyFrame::OnAbout
)
69 EVT_MENU(TreeTest_Dump
, MyFrame::OnDump
)
70 #ifndef NO_MULTIPLE_SELECTION
71 EVT_MENU(TreeTest_DumpSelected
, MyFrame::OnDumpSelected
)
72 EVT_MENU(TreeTest_Select
, MyFrame::OnSelect
)
73 EVT_MENU(TreeTest_Unselect
, MyFrame::OnUnselect
)
74 #endif // NO_MULTIPLE_SELECTION
75 EVT_MENU(TreeTest_Rename
, MyFrame::OnRename
)
76 EVT_MENU(TreeTest_Count
, MyFrame::OnCount
)
77 EVT_MENU(TreeTest_CountRec
, MyFrame::OnCountRec
)
78 EVT_MENU(TreeTest_Sort
, MyFrame::OnSort
)
79 EVT_MENU(TreeTest_SortRev
, MyFrame::OnSortRev
)
80 EVT_MENU(TreeTest_Bold
, MyFrame::OnSetBold
)
81 EVT_MENU(TreeTest_UnBold
, MyFrame::OnClearBold
)
82 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
83 EVT_MENU(TreeTest_DeleteChildren
, MyFrame::OnDeleteChildren
)
84 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
85 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
86 EVT_MENU(TreeTest_CollapseAndReset
, MyFrame::OnCollapseAndReset
)
87 EVT_MENU(TreeTest_EnsureVisible
, MyFrame::OnEnsureVisible
)
88 EVT_MENU(TreeTest_AddItem
, MyFrame::OnAddItem
)
89 EVT_MENU(TreeTest_InsertItem
, MyFrame::OnInsertItem
)
90 EVT_MENU(TreeTest_IncIndent
, MyFrame::OnIncIndent
)
91 EVT_MENU(TreeTest_DecIndent
, MyFrame::OnDecIndent
)
92 EVT_MENU(TreeTest_IncSpacing
, MyFrame::OnIncSpacing
)
93 EVT_MENU(TreeTest_DecSpacing
, MyFrame::OnDecSpacing
)
94 EVT_MENU(TreeTest_ToggleIcon
, MyFrame::OnToggleIcon
)
97 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
98 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
99 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
100 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
101 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
102 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
103 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
104 #if 0 // there are so many of those that logging them causes flicker
105 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
107 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
108 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
109 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
110 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
111 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
112 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
113 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
114 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
115 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
116 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
123 // Create the main frame window
124 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
134 // My frame constructor
135 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
136 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
138 // This reduces flicker effects - even better would be to define
139 // OnEraseBackground to do nothing. When the tree control's scrollbars are
140 // show or hidden, the frame is sent a background erase event.
141 SetBackgroundColour(wxColour(255, 255, 255));
144 SetIcon(wxICON(mondrian
));
147 wxMenu
*file_menu
= new wxMenu
,
148 *tree_menu
= new wxMenu
,
149 *item_menu
= new wxMenu
;
151 file_menu
->Append(TreeTest_About
, "&About...");
152 file_menu
->AppendSeparator();
153 file_menu
->Append(TreeTest_Quit
, "E&xit\tAlt-X");
155 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
156 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
157 tree_menu
->AppendSeparator();
158 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
159 tree_menu
->Append(TreeTest_InsertItem
, "&Insert a new item");
160 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
161 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
162 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
163 tree_menu
->AppendSeparator();
164 tree_menu
->Append(TreeTest_Count
, "Count children of current item");
165 tree_menu
->Append(TreeTest_CountRec
, "Recursively count children of current item");
166 tree_menu
->AppendSeparator();
167 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
168 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
169 tree_menu
->AppendSeparator();
170 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
171 tree_menu
->AppendSeparator();
172 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
173 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
174 tree_menu
->AppendSeparator();
175 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
176 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
178 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
179 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
181 item_menu
->AppendSeparator();
182 item_menu
->Append(TreeTest_Bold
, "Make item &bold");
183 item_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
184 item_menu
->AppendSeparator();
185 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the items &icon");
187 #ifndef NO_MULTIPLE_SELECTION
188 item_menu
->AppendSeparator();
189 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
190 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
191 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
194 wxMenuBar
*menu_bar
= new wxMenuBar
;
195 menu_bar
->Append(file_menu
, "&File");
196 menu_bar
->Append(tree_menu
, "&Tree");
197 menu_bar
->Append(item_menu
, "&Item");
198 SetMenuBar(menu_bar
);
200 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
201 wxDefaultPosition
, wxDefaultSize
,
204 #ifndef NO_MULTIPLE_SELECTION
207 #ifndef NO_VARIABLE_HEIGHT
208 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
212 m_treeCtrl
->SetBackgroundColour(wxColour(204, 205, 79));
214 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
215 wxDefaultPosition
, wxDefaultSize
,
216 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
218 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
219 c
->top
.SameAs(this, wxTop
);
220 c
->left
.SameAs(this, wxLeft
);
221 c
->right
.SameAs(this, wxRight
);
222 c
->height
.PercentOf(this, wxHeight
, 66);
223 m_treeCtrl
->SetConstraints(c
);
225 c
= new wxLayoutConstraints
;
226 c
->top
.Below(m_treeCtrl
);
227 c
->left
.SameAs(this, wxLeft
);
228 c
->right
.SameAs(this, wxRight
);
229 c
->bottom
.SameAs(this, wxBottom
);
230 textCtrl
->SetConstraints(c
);
233 // create a status bar with 3 panes
235 SetStatusText("", 0);
238 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
239 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
240 delete wxLog::SetActiveTarget(new wxLogStderr
);
242 // set our text control as the log target
243 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
244 delete wxLog::SetActiveTarget(logWindow
);
250 delete wxLog::SetActiveTarget(NULL
);
253 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
258 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
260 wxMessageBox("Tree test sample\n"
261 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
263 wxOK
| wxICON_INFORMATION
, this);
266 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
268 wxTreeItemId item
= m_treeCtrl
->GetSelection();
272 // old code - now we edit in place
274 static wxString s_text
;
275 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
277 if ( !s_text
.IsEmpty() )
279 m_treeCtrl
->SetItemText(item
, s_text
);
283 // TODO demonstrate creating a custom edit control...
284 (void)m_treeCtrl
->EditLabel(item
);
287 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
289 wxTreeItemId item
= m_treeCtrl
->GetSelection();
293 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
295 wxLogMessage(wxT("%d children"), i
);
298 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
300 wxTreeItemId item
= m_treeCtrl
->GetSelection();
304 int i
= m_treeCtrl
->GetChildrenCount( item
);
306 wxLogMessage(wxT("%d children"), i
);
309 void MyFrame::DoSort(bool reverse
)
311 wxTreeItemId item
= m_treeCtrl
->GetSelection();
315 m_treeCtrl
->DoSortChildren(item
, reverse
);
318 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
320 wxTreeItemId root
= m_treeCtrl
->GetSelection();
324 m_treeCtrl
->GetItemsRecursively(root
, -1);
327 #ifndef NO_MULTIPLE_SELECTION
329 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
331 wxArrayTreeItemIds array
;
333 size_t count
= m_treeCtrl
->GetSelections(array
);
334 wxLogMessage(wxT("%u items selected"), count
);
336 for ( size_t n
= 0; n
< count
; n
++ )
338 wxLogMessage("\t%s", m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
342 void MyFrame::OnSelect(wxCommandEvent
& event
)
344 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
347 void MyFrame::OnUnselect(wxCommandEvent
& event
)
349 m_treeCtrl
->UnselectAll();
352 #endif // NO_MULTIPLE_SELECTION
354 void MyFrame::DoSetBold(bool bold
)
356 wxTreeItemId item
= m_treeCtrl
->GetSelection();
360 m_treeCtrl
->SetItemBold(item
, bold
);
363 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
365 wxTreeItemId item
= m_treeCtrl
->GetSelection();
369 m_treeCtrl
->Delete(item
);
372 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
374 wxTreeItemId item
= m_treeCtrl
->GetSelection();
378 m_treeCtrl
->DeleteChildren(item
);
381 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
383 m_treeCtrl
->DeleteAllItems();
386 void MyFrame::OnRecreate(wxCommandEvent
& event
)
389 m_treeCtrl
->AddTestItemsToTree(3, 2);
392 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
394 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
397 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
399 m_treeCtrl
->DoEnsureVisible();
402 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
404 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), 1, "2nd item");
407 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
409 static int s_num
= 0;
412 text
.Printf("Item #%d", ++s_num
);
414 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
416 MyTreeCtrl::TreeCtrlIcon_File */ );
419 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
421 unsigned int indent
= m_treeCtrl
->GetIndent();
423 m_treeCtrl
->SetIndent( indent
+5 );
426 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
428 unsigned int indent
= m_treeCtrl
->GetIndent();
430 m_treeCtrl
->SetIndent( indent
-5 );
433 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
435 unsigned int indent
= m_treeCtrl
->GetSpacing();
437 m_treeCtrl
->SetSpacing( indent
+5 );
440 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
442 unsigned int indent
= m_treeCtrl
->GetSpacing();
444 m_treeCtrl
->SetSpacing( indent
-5 );
447 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
449 wxTreeItemId item
= m_treeCtrl
->GetSelection();
453 m_treeCtrl
->DoToggleIcon(item
);
456 // MyTreeCtrl implementation
457 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
459 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
460 const wxPoint
& pos
, const wxSize
& size
,
462 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
464 #ifndef NO_VARIABLE_HEIGHT
466 wxImage::AddHandler(new wxJPEGHandler
);
469 image
.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG
);
473 m_reverseSort
= FALSE
;
475 // Make an image list containing small icons
476 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
478 // should correspond to TreeCtrlIcon_xxx enum
479 #if defined(__WXMSW__) && defined(__WIN16__)
480 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
481 // icon image, so it comes out stretched
482 # ifndef NO_VARIABLE_HEIGHT
483 m_imageListNormal
->Add(image
.ConvertToBitmap());
485 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
487 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
488 m_imageListNormal
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
489 m_imageListNormal
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
490 m_imageListNormal
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
492 # ifndef NO_VARIABLE_HEIGHT
493 m_imageListNormal
->Add(image
.ConvertToBitmap());
495 m_imageListNormal
->Add(wxICON(icon1
));
497 m_imageListNormal
->Add(wxICON(icon2
));
498 m_imageListNormal
->Add(wxICON(icon3
));
499 m_imageListNormal
->Add(wxICON(icon4
));
500 m_imageListNormal
->Add(wxICON(icon5
));
503 SetImageList(m_imageListNormal
);
505 // Add some items to the tree
506 AddTestItemsToTree(3, 2);
509 MyTreeCtrl::~MyTreeCtrl()
511 delete m_imageListNormal
;
514 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
515 const wxTreeItemId
& item2
)
519 // just exchange 1st and 2nd items
520 return wxTreeCtrl::OnCompareItems(item2
, item1
);
524 return wxTreeCtrl::OnCompareItems(item1
, item2
);
528 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
535 bool hasChildren
= depth
> 1;
538 for ( size_t n
= 0; n
< numChildren
; n
++ )
540 // at depth 1 elements won't have any more children
542 str
.Printf("%s child %d", "Folder", n
+ 1);
544 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
546 // here we pass to AppendItem() normal and selected item images (we
547 // suppose that selected image follows the normal one in the enum)
548 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
549 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
+ 1,
550 new MyTreeItemData(str
));
552 // and now we also set the expanded one (only for the folders)
555 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
556 wxTreeItemIcon_Expanded
);
559 // remember the last child for OnEnsureVisible()
560 if ( !hasChildren
&& n
== numChildren
- 1 )
565 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
571 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
574 wxTreeItemId rootId
= AddRoot("Root",
575 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
576 new MyTreeItemData("Root item"));
577 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
579 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
581 // set some colours/fonts for testing
582 SetItemFont(rootId
, *wxITALIC_FONT
);
585 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
586 SetItemTextColour(id
, *wxBLUE
);
588 id
= GetNextChild(rootId
, cookie
);
589 id
= GetNextChild(rootId
, cookie
);
590 SetItemTextColour(id
, *wxRED
);
591 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
594 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
599 id
= GetFirstChild(idParent
, cookie
);
601 id
= GetNextChild(idParent
, cookie
);
606 wxString text
= GetItemText(id
);
609 if (ItemHasChildren(id
))
610 GetItemsRecursively(id
,-1);
612 GetItemsRecursively(idParent
, cookie
);
615 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
617 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
618 : TreeCtrlIcon_Folder
;
620 SetItemImage(item
, image
);
625 #define TREE_EVENT_HANDLER(name) \
626 void MyTreeCtrl::name(wxTreeEvent& event) \
628 wxLogMessage(#name); \
632 TREE_EVENT_HANDLER(OnBeginRDrag
)
633 TREE_EVENT_HANDLER(OnDeleteItem
)
634 TREE_EVENT_HANDLER(OnGetInfo
)
635 TREE_EVENT_HANDLER(OnSetInfo
)
636 TREE_EVENT_HANDLER(OnItemExpanded
)
637 TREE_EVENT_HANDLER(OnItemExpanding
)
638 TREE_EVENT_HANDLER(OnItemCollapsed
)
639 TREE_EVENT_HANDLER(OnSelChanged
)
640 TREE_EVENT_HANDLER(OnSelChanging
)
641 TREE_EVENT_HANDLER(OnTreeKeyDown
)
643 #undef TREE_EVENT_HANDLER
645 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
647 // need to explicitly allow drag
648 if ( event
.GetItem() != GetRootItem() )
650 m_draggedItem
= event
.GetItem();
652 wxLogMessage("OnBeginDrag: started dragging %s",
653 GetItemText(m_draggedItem
).c_str());
659 wxLogMessage("OnBeginDrag: this item can't be dragged.");
663 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
665 wxTreeItemId itemSrc
= m_draggedItem
,
666 itemDst
= event
.GetItem();
667 m_draggedItem
= (wxTreeItemId
)0l;
669 // where to copy the item?
670 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
672 // copy to the parent then
673 itemDst
= GetParent(itemDst
);
676 if ( !itemDst
.IsOk() )
678 wxLogMessage("OnEndDrag: can't drop here.");
683 wxString text
= GetItemText(itemSrc
);
684 wxLogMessage("OnEndDrag: '%s' copied to '%s'.",
685 text
.c_str(), GetItemText(itemDst
).c_str());
687 // just do append here - we could also insert it just before/after the item
688 // on which it was dropped, but this requires slightly more work... we also
689 // completely ignore the client data and icon of the old item but could
690 // copy them as well.
692 // Finally, we only copy one item here but we might copy the entire tree if
693 // we were dragging a folder.
694 AppendItem(itemDst
, text
, TreeCtrlIcon_File
);
697 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
699 wxLogMessage("OnBeginLabelEdit");
701 // for testing, prevent this items label editing
702 wxTreeItemId itemId
= event
.GetItem();
703 if ( IsTestItem(itemId
) )
705 wxMessageBox("You can't edit this item.");
711 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
713 wxLogMessage("OnEndLabelEdit");
715 // don't allow anything except letters in the labels
716 if ( !event
.GetLabel().IsWord() )
718 wxMessageBox("The label should contain only letters.");
724 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
726 wxLogMessage("OnItemCollapsing");
728 // for testing, prevent the user from collapsing the first child folder
729 wxTreeItemId itemId
= event
.GetItem();
730 if ( IsTestItem(itemId
) )
732 wxMessageBox("You can't collapse this item.");
738 void MyTreeCtrl::OnItemActivated(wxTreeEvent
&WXUNUSED(event
))
740 // show some info about this item
741 wxTreeItemId itemId
= GetSelection();
742 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
746 item
->ShowInfo(this);
749 wxLogMessage("OnItemActivated");
752 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
754 wxTreeItemId id
= HitTest(event
.GetPosition());
756 wxLogMessage("No item under mouse");
759 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
761 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
765 static inline const char *Bool2String(bool b
)
767 return b
? "" : "not ";
770 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
772 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
773 "%u children (%u immediately under this item).",
775 Bool2String(tree
->IsSelected(GetId())),
776 Bool2String(tree
->IsExpanded(GetId())),
777 Bool2String(tree
->IsBold(GetId())),
778 tree
->GetChildrenCount(GetId()),
779 tree
->GetChildrenCount(GetId(), FALSE
));