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"
37 // comment out this line to test multiple selection even under MSW (where
38 // it looks ugly - but works)
39 #define NO_MULTIPLE_SELECTION
42 #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_MENU(TreeTest_Quit
, MyFrame::OnQuit
)
67 EVT_MENU(TreeTest_About
, MyFrame::OnAbout
)
68 EVT_MENU(TreeTest_Dump
, MyFrame::OnDump
)
69 #ifndef NO_MULTIPLE_SELECTION
70 EVT_MENU(TreeTest_DumpSelected
, MyFrame::OnDumpSelected
)
71 EVT_MENU(TreeTest_Select
, MyFrame::OnSelect
)
72 EVT_MENU(TreeTest_Unselect
, MyFrame::OnUnselect
)
73 #endif // NO_MULTIPLE_SELECTION
74 EVT_MENU(TreeTest_Rename
, MyFrame::OnRename
)
75 EVT_MENU(TreeTest_Count
, MyFrame::OnCount
)
76 EVT_MENU(TreeTest_CountRec
, MyFrame::OnCountRec
)
77 EVT_MENU(TreeTest_Sort
, MyFrame::OnSort
)
78 EVT_MENU(TreeTest_SortRev
, MyFrame::OnSortRev
)
79 EVT_MENU(TreeTest_Bold
, MyFrame::OnSetBold
)
80 EVT_MENU(TreeTest_UnBold
, MyFrame::OnClearBold
)
81 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
82 EVT_MENU(TreeTest_DeleteChildren
, MyFrame::OnDeleteChildren
)
83 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
84 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
85 EVT_MENU(TreeTest_CollapseAndReset
, MyFrame::OnCollapseAndReset
)
86 EVT_MENU(TreeTest_EnsureVisible
, MyFrame::OnEnsureVisible
)
87 EVT_MENU(TreeTest_AddItem
, MyFrame::OnAddItem
)
88 EVT_MENU(TreeTest_InsertItem
, MyFrame::OnInsertItem
)
89 EVT_MENU(TreeTest_IncIndent
, MyFrame::OnIncIndent
)
90 EVT_MENU(TreeTest_DecIndent
, MyFrame::OnDecIndent
)
91 EVT_MENU(TreeTest_IncSpacing
, MyFrame::OnIncSpacing
)
92 EVT_MENU(TreeTest_DecSpacing
, MyFrame::OnDecSpacing
)
93 EVT_MENU(TreeTest_ToggleIcon
, MyFrame::OnToggleIcon
)
96 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
97 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
98 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
99 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
100 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
101 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
102 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
103 #if 0 // there are so many of those that logging them causes flicker
104 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
106 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
107 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
108 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
109 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
110 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
111 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
112 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
113 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
114 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
115 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
122 // Create the main frame window
123 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
133 // My frame constructor
134 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
135 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
137 // This reduces flicker effects - even better would be to define
138 // OnEraseBackground to do nothing. When the tree control's scrollbars are
139 // show or hidden, the frame is sent a background erase event.
140 SetBackgroundColour(wxColour(255, 255, 255));
143 SetIcon(wxICON(mondrian
));
146 wxMenu
*file_menu
= new wxMenu
,
147 *tree_menu
= new wxMenu
,
148 *item_menu
= new wxMenu
;
150 file_menu
->Append(TreeTest_About
, "&About...");
151 file_menu
->AppendSeparator();
152 file_menu
->Append(TreeTest_Quit
, "E&xit\tAlt-X");
154 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
155 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
156 tree_menu
->AppendSeparator();
157 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
158 tree_menu
->Append(TreeTest_InsertItem
, "&Insert a new item");
159 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
160 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
161 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
162 tree_menu
->AppendSeparator();
163 tree_menu
->Append(TreeTest_Count
, "Count children of current item");
164 tree_menu
->Append(TreeTest_CountRec
, "Recursively count children of current item");
165 tree_menu
->AppendSeparator();
166 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
167 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
168 tree_menu
->AppendSeparator();
169 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
170 tree_menu
->AppendSeparator();
171 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
172 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
173 tree_menu
->AppendSeparator();
174 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
175 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
177 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
178 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
180 item_menu
->AppendSeparator();
181 item_menu
->Append(TreeTest_Bold
, "Make item &bold");
182 item_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
183 item_menu
->AppendSeparator();
184 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the items &icon");
186 #ifndef NO_MULTIPLE_SELECTION
187 item_menu
->AppendSeparator();
188 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
189 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
190 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
193 wxMenuBar
*menu_bar
= new wxMenuBar
;
194 menu_bar
->Append(file_menu
, "&File");
195 menu_bar
->Append(tree_menu
, "&Tree");
196 menu_bar
->Append(item_menu
, "&Item");
197 SetMenuBar(menu_bar
);
199 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
200 wxDefaultPosition
, wxDefaultSize
,
203 #ifndef NO_VARIABLE_HEIGHT
204 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
208 m_treeCtrl
->SetBackgroundColour(wxColour(204, 205, 79));
210 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
211 wxDefaultPosition
, wxDefaultSize
,
212 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
214 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
215 c
->top
.SameAs(this, wxTop
);
216 c
->left
.SameAs(this, wxLeft
);
217 c
->right
.SameAs(this, wxRight
);
218 c
->height
.PercentOf(this, wxHeight
, 66);
219 m_treeCtrl
->SetConstraints(c
);
221 c
= new wxLayoutConstraints
;
222 c
->top
.Below(m_treeCtrl
);
223 c
->left
.SameAs(this, wxLeft
);
224 c
->right
.SameAs(this, wxRight
);
225 c
->bottom
.SameAs(this, wxBottom
);
226 textCtrl
->SetConstraints(c
);
229 // create a status bar with 3 panes
231 SetStatusText("", 0);
234 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
235 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
236 delete wxLog::SetActiveTarget(new wxLogStderr
);
238 // set our text control as the log target
239 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
240 delete wxLog::SetActiveTarget(logWindow
);
246 delete wxLog::SetActiveTarget(NULL
);
249 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
254 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
256 wxMessageBox("Tree test sample\n"
257 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
259 wxOK
| wxICON_INFORMATION
, this);
262 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
264 wxTreeItemId item
= m_treeCtrl
->GetSelection();
268 // old code - now we edit in place
270 static wxString s_text
;
271 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
273 if ( !s_text
.IsEmpty() )
275 m_treeCtrl
->SetItemText(item
, s_text
);
279 // TODO demonstrate creating a custom edit control...
280 (void)m_treeCtrl
->EditLabel(item
);
283 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
285 wxTreeItemId item
= m_treeCtrl
->GetSelection();
289 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
291 wxLogMessage(wxT("%d children"), i
);
294 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
296 wxTreeItemId item
= m_treeCtrl
->GetSelection();
300 int i
= m_treeCtrl
->GetChildrenCount( item
);
302 wxLogMessage(wxT("%d children"), i
);
305 void MyFrame::DoSort(bool reverse
)
307 wxTreeItemId item
= m_treeCtrl
->GetSelection();
311 m_treeCtrl
->DoSortChildren(item
, reverse
);
314 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
316 wxTreeItemId root
= m_treeCtrl
->GetSelection();
320 m_treeCtrl
->GetItemsRecursively(root
, -1);
323 #ifndef NO_MULTIPLE_SELECTION
325 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
327 wxArrayTreeItemIds array
;
329 size_t count
= m_treeCtrl
->GetSelections(array
);
330 wxLogMessage(wxT("%u items selected"), count
);
332 for ( size_t n
= 0; n
< count
; n
++ )
334 wxLogMessage("\t%s", m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
338 void MyFrame::OnSelect(wxCommandEvent
& event
)
340 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
343 void MyFrame::OnUnselect(wxCommandEvent
& event
)
345 m_treeCtrl
->UnselectAll();
348 #endif // NO_MULTIPLE_SELECTION
350 void MyFrame::DoSetBold(bool bold
)
352 wxTreeItemId item
= m_treeCtrl
->GetSelection();
356 m_treeCtrl
->SetItemBold(item
, bold
);
359 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
361 wxTreeItemId item
= m_treeCtrl
->GetSelection();
365 m_treeCtrl
->Delete(item
);
368 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
370 wxTreeItemId item
= m_treeCtrl
->GetSelection();
374 m_treeCtrl
->DeleteChildren(item
);
377 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
379 m_treeCtrl
->DeleteAllItems();
382 void MyFrame::OnRecreate(wxCommandEvent
& event
)
385 m_treeCtrl
->AddTestItemsToTree(3, 2);
388 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
390 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
393 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
395 m_treeCtrl
->DoEnsureVisible();
398 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
400 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), 1, "2nd item");
403 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
405 static int s_num
= 0;
408 text
.Printf("Item #%d", ++s_num
);
410 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
412 MyTreeCtrl::TreeCtrlIcon_File */ );
415 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
417 unsigned int indent
= m_treeCtrl
->GetIndent();
419 m_treeCtrl
->SetIndent( indent
+5 );
422 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
424 unsigned int indent
= m_treeCtrl
->GetIndent();
426 m_treeCtrl
->SetIndent( indent
-5 );
429 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
431 unsigned int indent
= m_treeCtrl
->GetSpacing();
433 m_treeCtrl
->SetSpacing( indent
+5 );
436 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
438 unsigned int indent
= m_treeCtrl
->GetSpacing();
440 m_treeCtrl
->SetSpacing( indent
-5 );
443 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
445 wxTreeItemId item
= m_treeCtrl
->GetSelection();
449 m_treeCtrl
->DoToggleIcon(item
);
452 // MyTreeCtrl implementation
453 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
455 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
456 const wxPoint
& pos
, const wxSize
& size
,
458 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
460 #ifndef NO_VARIABLE_HEIGHT
462 wxImage::AddHandler(new wxJPEGHandler
);
465 image
.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG
);
469 m_reverseSort
= FALSE
;
471 // Make an image list containing small icons
472 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
474 // should correspond to TreeCtrlIcon_xxx enum
475 #if defined(__WXMSW__) && defined(__WIN16__)
476 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
477 // icon image, so it comes out stretched
478 # ifndef NO_VARIABLE_HEIGHT
479 m_imageListNormal
->Add(image
.ConvertToBitmap());
481 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
483 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
484 m_imageListNormal
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
485 m_imageListNormal
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
486 m_imageListNormal
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
488 # ifndef NO_VARIABLE_HEIGHT
489 m_imageListNormal
->Add(image
.ConvertToBitmap());
491 m_imageListNormal
->Add(wxICON(icon1
));
493 m_imageListNormal
->Add(wxICON(icon2
));
494 m_imageListNormal
->Add(wxICON(icon3
));
495 m_imageListNormal
->Add(wxICON(icon4
));
496 m_imageListNormal
->Add(wxICON(icon5
));
499 SetImageList(m_imageListNormal
);
501 // Add some items to the tree
502 AddTestItemsToTree(3, 2);
505 MyTreeCtrl::~MyTreeCtrl()
507 delete m_imageListNormal
;
510 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
511 const wxTreeItemId
& item2
)
515 // just exchange 1st and 2nd items
516 return wxTreeCtrl::OnCompareItems(item2
, item1
);
520 return wxTreeCtrl::OnCompareItems(item1
, item2
);
524 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
531 bool hasChildren
= depth
> 1;
534 for ( size_t n
= 0; n
< numChildren
; n
++ )
536 // at depth 1 elements won't have any more children
538 str
.Printf("%s child %d", "Folder", n
+ 1);
540 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
542 // here we pass to AppendItem() normal and selected item images (we
543 // suppose that selected image follows the normal one in the enum)
544 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
545 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
+ 1,
546 new MyTreeItemData(str
));
548 // and now we also set the expanded one (only for the folders)
551 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
552 wxTreeItemIcon_Expanded
);
555 // remember the last child for OnEnsureVisible()
556 if ( !hasChildren
&& n
== numChildren
- 1 )
561 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
567 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
570 wxTreeItemId rootId
= AddRoot("Root",
571 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
572 new MyTreeItemData("Root item"));
573 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
575 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
577 // set some colours/fonts for testing
578 SetItemFont(rootId
, *wxITALIC_FONT
);
581 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
582 SetItemTextColour(id
, *wxBLUE
);
584 id
= GetNextChild(rootId
, cookie
);
585 id
= GetNextChild(rootId
, cookie
);
586 SetItemTextColour(id
, *wxRED
);
587 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
590 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
595 id
= GetFirstChild(idParent
, cookie
);
597 id
= GetNextChild(idParent
, cookie
);
602 wxString text
= GetItemText(id
);
605 if (ItemHasChildren(id
))
606 GetItemsRecursively(id
,-1);
608 GetItemsRecursively(idParent
, cookie
);
611 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
613 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
614 : TreeCtrlIcon_Folder
;
616 SetItemImage(item
, image
);
621 #define TREE_EVENT_HANDLER(name) \
622 void MyTreeCtrl::name(wxTreeEvent& event) \
624 wxLogMessage(#name); \
628 TREE_EVENT_HANDLER(OnBeginRDrag
)
629 TREE_EVENT_HANDLER(OnDeleteItem
)
630 TREE_EVENT_HANDLER(OnGetInfo
)
631 TREE_EVENT_HANDLER(OnSetInfo
)
632 TREE_EVENT_HANDLER(OnItemExpanded
)
633 TREE_EVENT_HANDLER(OnItemExpanding
)
634 TREE_EVENT_HANDLER(OnItemCollapsed
)
635 TREE_EVENT_HANDLER(OnSelChanged
)
636 TREE_EVENT_HANDLER(OnSelChanging
)
637 TREE_EVENT_HANDLER(OnTreeKeyDown
)
639 #undef TREE_EVENT_HANDLER
641 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
643 // need to explicitly allow drag
644 if ( event
.GetItem() != GetRootItem() )
646 m_draggedItem
= event
.GetItem();
648 wxLogMessage("OnBeginDrag: started dragging %s",
649 GetItemText(m_draggedItem
).c_str());
655 wxLogMessage("OnBeginDrag: this item can't be dragged.");
659 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
661 wxTreeItemId itemSrc
= m_draggedItem
,
662 itemDst
= event
.GetItem();
663 m_draggedItem
= (wxTreeItemId
)0l;
665 // where to copy the item?
666 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
668 // copy to the parent then
669 itemDst
= GetParent(itemDst
);
672 if ( !itemDst
.IsOk() )
674 wxLogMessage("OnEndDrag: can't drop here.");
679 wxString text
= GetItemText(itemSrc
);
680 wxLogMessage("OnEndDrag: '%s' copied to '%s'.",
681 text
.c_str(), GetItemText(itemDst
).c_str());
683 // just do append here - we could also insert it just before/after the item
684 // on which it was dropped, but this requires slightly more work... we also
685 // completely ignore the client data and icon of the old item but could
686 // copy them as well.
688 // Finally, we only copy one item here but we might copy the entire tree if
689 // we were dragging a folder.
690 AppendItem(itemDst
, text
, TreeCtrlIcon_File
);
693 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
695 wxLogMessage("OnBeginLabelEdit");
697 // for testing, prevent this items label editing
698 wxTreeItemId itemId
= event
.GetItem();
699 if ( IsTestItem(itemId
) )
701 wxMessageBox("You can't edit this item.");
707 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
709 wxLogMessage("OnEndLabelEdit");
711 // don't allow anything except letters in the labels
712 if ( !event
.GetLabel().IsWord() )
714 wxMessageBox("The label should contain only letters.");
720 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
722 wxLogMessage("OnItemCollapsing");
724 // for testing, prevent the user from collapsing the first child folder
725 wxTreeItemId itemId
= event
.GetItem();
726 if ( IsTestItem(itemId
) )
728 wxMessageBox("You can't collapse this item.");
734 void MyTreeCtrl::OnItemActivated(wxTreeEvent
&WXUNUSED(event
))
736 // show some info about this item
737 wxTreeItemId itemId
= GetSelection();
738 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
742 item
->ShowInfo(this);
745 wxLogMessage("OnItemActivated");
748 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
750 wxTreeItemId id
= HitTest(event
.GetPosition());
752 wxLogMessage("No item under mouse");
755 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
757 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
761 static inline const char *Bool2String(bool b
)
763 return b
? "" : "not ";
766 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
768 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
769 "%u children (%u immediately under this item).",
771 Bool2String(tree
->IsSelected(GetId())),
772 Bool2String(tree
->IsExpanded(GetId())),
773 Bool2String(tree
->IsBold(GetId())),
774 tree
->GetChildrenCount(GetId()),
775 tree
->GetChildrenCount(GetId(), FALSE
));