The wxWidgets programming tutorial (9. Drag and Drop)

Author: Jan Bodnar. Link to original: http://zetcode.com/tutorials/wxwidgetstutorial/dragdrop/ (English).
A more in-depth tutorial covering menus, toolbars, layout, events, standard and custom dialogs, common and custom controls, drag & drop, and drawing with device contexts, written by Jan Bodnar. This is a wxWidgets tutorial for the C++ programming language. wxWidgets is a cross platform toolkit or framework for creating C++ GUI applications. After reading this tutorial, you will be able to program non trivial wxWidgets applications.

Translations of this material:

into Russian: Руководство по программированию с wxWidgets (9. Перетаскивание). Translation complete.
Submitted for translation by ber113 21.03.2010 Published 2 years, 2 months ago.

Text

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:

* Data object

* Data source

* Data target

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.

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.

MyTextDropTarget *mdt = new MyTextDropTarget(m_lc2);

m_lc2->SetDropTarget(mdt);

Here we define a text drop target.

wxString text = m_lc1->GetItemText(event.GetIndex());

wxTextDataObject tdo(text);

wxDropSource tds(tdo, m_lc1);

tds.DoDragDrop(wxDrag_CopyOnly);

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.

bool MyTextDropTarget::OnDropText(wxCoord x, wxCoord y,

const wxString& data)

{

m_owner->InsertItem(0, data);

return true;

}

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