Anyone has a piece of sample code using drag and drop with a wxTreeCtrl?
I thought it was pretty sraight forward, following analogous code for
images, but I can't get it to work.
I'm having troubles using wxDragTreeItem
I havn't tried it yet, but there is some sample C++ code in
wxWindows/samples/treectrl that does it. Here's the relevant parts:
EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
{
// need to explicitly allow drag
if ( event.GetItem() != GetRootItem() )
{
m_draggedItem = event.GetItem();
wxLogMessage("OnBeginDrag: started dragging %s",
GetItemText(m_draggedItem).c_str());
event.Allow();
}
else
{
wxLogMessage("OnBeginDrag: this item can't be dragged.");
}
}
void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
{
wxTreeItemId itemSrc = m_draggedItem,
itemDst = event.GetItem();
m_draggedItem = (wxTreeItemId)0l;
// where to copy the item?
if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
{
// copy to the parent then
itemDst = GetParent(itemDst);
}
if ( !itemDst.IsOk() )
{
wxLogMessage("OnEndDrag: can't drop here.");
return;
}
wxString text = GetItemText(itemSrc);
wxLogMessage("OnEndDrag: '%s' copied to '%s'.",
text.c_str(), GetItemText(itemDst).c_str());
// just do append here - we could also insert it just before/after the
item
// on which it was dropped, but this requires slightly more work... we
also
// completely ignore the client data and icon of the old item but could
// copy them as well.
//
// Finally, we only copy one item here but we might copy the entire tree
if
// we were dragging a folder.
int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1;
AppendItem(itemDst, text, image);
}