Drag and Drop

Wikipedia: In computer graphical user interfaces, drag-and-drop is the action of (or support for the action of) clicking on a virtual object and dragging it to a different location or onto another virtual object. In general, it can be used to invoke many kinds of actions, or create various types of associations between two abstract objects.

Drag and drop functionality is one of the most visible aspects of the graphical user interface. Drag and drop operation enables you to do complex things intuitively.

In drag and drop we basically drag some data from a data source to a data target. So we must have:

For drag & drop of text, wxWidgets has a predefined wxTextDropTarget class.

In the following example, we drag and drop file names from the upper list control to the bottom one.

textdrop.bmx <- Open source
SuperStrict Framework wx.wxApp Import wx.wxFrame Import wx.wxTextDropTarget Import wx.wxGenericDirCtrl Import wx.wxListCtrl Import wx.wxTreeCtrl Import wx.wxSplitterWindow Import wx.wxTextDataObject Import wx.wxDropSource New MyApp.Run() Type MyApp Extends wxApp Method OnInit:Int() Local td:TextDrop = TextDrop(New TextDrop.Create(Null, wxID_ANY, .. "TextDrop", -1, -1, 300, 200)) td.Show(True) Return True End Method End Type Type TextDrop Extends wxFrame Field m_gdir:wxGenericDirCtrl Field m_lc1:wxListCtrl Field m_lc2:wxListCtrl Method OnInit() Local spl1:wxSplitterWindow = New wxSplitterWindow.Create(.. Self, -1) Local spl2:wxSplitterWindow = New wxSplitterWindow.Create(.. spl1, -1) m_gdir = New wxGenericDirCtrl.Create(spl1, -1, "../..", .. -1, -1, -1, -1, wxDIRCTRL_DIR_ONLY) m_lc1 = New wxListCtrl.Create(spl2, -1, -1, -1, -1, -1, wxLC_LIST) m_lc2 = New wxListCtrl.Create(spl2, -1, -1, -1, -1, -1, wxLC_LIST) Local mdt:MyTextDropTarget = New MyTextDropTarget.CreateTarget(m_lc2) m_lc2.SetDropTarget(mdt) Connect(m_lc1.GetId(), wxEVT_COMMAND_LIST_BEGIN_DRAG, OnDragInit) Local tree:wxTreeCtrl = m_gdir.GetTreeCtrl() spl2.SplitHorizontally(m_lc1, m_lc2) spl1.SplitVertically(m_gdir, spl2) Connect(tree.GetId(), wxEVT_COMMAND_TREE_SEL_CHANGED, OnSelect) Centre() End Method Function OnSelect(event:wxEvent) Local drop:TextDrop = TextDrop(event.parent) Local filename:String Local path:String = drop.m_gdir.GetPath() Local dir:Int = ReadDir(path) If Not dir Then Return End If filename = NextFile(dir) Local i:Int drop.m_lc1.ClearAll() drop.m_lc2.ClearAll() While filename If FileType(path + "/" + filename) = FILETYPE_FILE Then drop.m_lc1.InsertStringItem(i, filename) End If filename = NextFile(dir) i:+ 1 Wend CloseDir(dir) End Function Function OnDragInit(event:wxEvent) Local drop:TextDrop = TextDrop(event.parent) Local text:String = drop.m_lc1.GetItemText(wxListEvent(event).GetIndex()) Local tdo:wxTextDataObject = New wxTextDataObject.Create(text) Local tds:wxDropSource = New wxDropSource.Create(tdo, drop.m_lc1) tds.DoDragDrop(wxDrag_CopyOnly) End Function End Type Type MyTextDropTarget Extends wxTextDropTarget Field m_owner:wxListCtrl Method CreateTarget:MyTextDropTarget(owner:wxListCtrl) m_owner = owner Super.Create() Return Self End Method Method OnDropText:Int(x:Int, y:Int, data:String) m_owner.InsertStringItem(0, data) Return True End Method End Type

In our example, we have a window separated into three parts. This is done by the wxSplitterWindow widget. In the left part of the window, we have a generic directory control. We display all directories available under our filesystem. In the right part there are two windows. The first displays all files under a selected directory. The second one is used for dragging the files.

 Local mdt:MyTextDropTarget = New MyTextDropTarget.CreateTarget(m_lc2)

Here we define a text drop target.

 Local text:String = m_lc1.GetItemText(event.GetIndex())

 Local tdo:wxTextDataObject = New wxTextDataObject.Create(text)
 Local tds:wxDropSource = New wxDropSource.Create(tdo, m_lc1)

In the OnDragInit() method, we define a text data object and a drop source object. We call the DoDragDrop() method. The wxDrag_CopyOnly constant allows only copying of data.

Method OnDropText:Int(x:Int, y:Int, data:String)
  m_owner.InsertItem(0, data)
  Return True
End Method

During the dropping operation, we insert the text data into the list control.

Drag & Drop
Figure: Drag & Drop