Drag and Drop
Introduction
Drag and Drop Source Example
The full source for this example is available from our GitHub repository: wxWidgetsTutorials/DragAndDrop/DragAndDropText1.
#ifndef _TUTORIALS_WXWIDGETS_DRAGANDDROPTEXT1FRAME_H_
#define _TUTORIALS_WXWIDGETS_DRAGANDDROPTEXT1FRAME_H_
#include <wx/frame.h>
#include <wx/stattext.h>
class DragAndDropText1Frame : public wxFrame
{
public:
DragAndDropText1Frame(const wxString& title);
private:
void OnLeftButtonDown(wxMouseEvent& evt);
private:
wxStaticText* m_sourceTextCtrl;
wxDECLARE_EVENT_TABLE();
};
#endif
#include "DragAndDropText1Frame.h"
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/dataobj.h>
#include <wx/dnd.h>
DragAndDropText1Frame::DragAndDropText1Frame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title), m_sourceTextCtrl(0)
{
// Create a top-level panel to hold all the contents of the frame
wxPanel* panel = new wxPanel(this, wxID_ANY);
// Create a control that will be the source of of the drag and
// drop operation. We connect an event handler for the
// left mouse button event.
m_sourceTextCtrl = new wxStaticText(panel, wxID_ANY,
"Hello World!", wxDefaultPosition, wxSize(250,100));
m_sourceTextCtrl->Connect(wxEVT_LEFT_DOWN,
(wxObjectEventFunction)&DragAndDropText1Frame::OnLeftButtonDown, 0, this);
// Set up the sizer for the panel
wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
panelSizer->Add(m_sourceTextCtrl, 0, wxEXPAND | wxALL, 5);
panel->SetSizer(panelSizer);
// Set up the sizer for the frame and resize the frame
// according to its contents
wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
topSizer->Add(panel, 1, wxEXPAND);
SetSizerAndFit(topSizer);
}
void DragAndDropText1Frame::OnLeftButtonDown(wxMouseEvent& evt)
{
wxTextDataObject dragData(m_sourceTextCtrl->GetLabelText());
wxDropSource dragSource(m_sourceTextCtrl);
dragSource.SetData(dragData);
wxDragResult result = dragSource.DoDragDrop();
}
wxBEGIN_EVENT_TABLE(DragAndDropText1Frame, wxFrame)
EVT_LEFT_DOWN(DragAndDropText1Frame::OnLeftButtonDown)
wxEND_EVENT_TABLE()
#ifndef _TUTORIALS_WXWIDGETS_DRAGANDDROPTEXT1APP_H_
#define _TUTORIALS_WXWIDGETS_DRAGANDDROPTEXT1APP_H_
#include <wx/app.h>
class DragAndDropText1App : public wxApp
{
public:
virtual bool OnInit();
};
#endif
#include "DragAndDropText1App.h"
#include "DragAndDropText1Frame.h"
wxIMPLEMENT_APP(DragAndDropText1App);
bool DragAndDropText1App::OnInit()
{
DragAndDropText1Frame* frame = new DragAndDropText1Frame("DragAndDropText1");
frame->Show(true);
return true;
}
Drag and Drop Target Example
The full source for this example is available from our GitHub repository: wxWidgetsTutorials/DragAndDrop/DragAndDropText2.
#ifndef _TUTORIALS_WXWIDGETS_DRAGANDDROPTEXT2FRAME_H_
#define _TUTORIALS_WXWIDGETS_DRAGANDDROPTEXT2FRAME_H_
#include <wx/frame.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/dnd.h>
class DragAndDropText2Frame : public wxFrame
{
public:
DragAndDropText2Frame(const wxString& title);
private:
void OnLeftButtonDown(wxMouseEvent& evt);
private:
wxStaticText* m_sourceTextCtrl;
wxDECLARE_EVENT_TABLE();
private:
class DropTarget : public wxTextDropTarget
{
public:
DropTarget(wxTextCtrl& dropTarget);
virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& data);
private:
wxTextCtrl& m_dropTarget;
};
};
#endif
#include "DragAndDropText2Frame.h"
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/dataobj.h>
DragAndDropText2Frame::DragAndDropText2Frame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title), m_sourceTextCtrl(0)
{
// Create a top-level panel to hold all the contents of the frame
wxPanel* panel = new wxPanel(this, wxID_ANY);
// Create a control that will be the source of of the drag and
// drop operation. We connect an event handler for the
// left mouse button event.
m_sourceTextCtrl = new wxStaticText(panel, wxID_ANY,
"Hello World!", wxDefaultPosition, wxSize(250,100));
m_sourceTextCtrl->Connect(wxEVT_LEFT_DOWN,
(wxObjectEventFunction)&DragAndDropText2Frame::OnLeftButtonDown, 0, this);
// Create a text control that can be the target of a drag an drop operation
wxTextCtrl* targetTextCtrl = new wxTextCtrl(panel, wxID_ANY,
"", wxDefaultPosition, wxSize(250, 100));
DropTarget* dropTarget = new DropTarget(*targetTextCtrl);
targetTextCtrl->SetDropTarget(dropTarget);
// Set up the sizer for the panel
wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
panelSizer->Add(m_sourceTextCtrl, 0, wxEXPAND | wxALL, 5);
panelSizer->Add(targetTextCtrl, 0, wxEXPAND | wxALL, 5);
panel->SetSizer(panelSizer);
// Set up the sizer for the frame and resize the frame
// according to its contents
wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
topSizer->Add(panel, 1, wxEXPAND);
SetSizerAndFit(topSizer);
}
void DragAndDropText2Frame::OnLeftButtonDown(wxMouseEvent& evt)
{
wxTextDataObject dragData(m_sourceTextCtrl->GetLabelText());
wxDropSource dragSource(m_sourceTextCtrl);
dragSource.SetData(dragData);
wxDragResult result = dragSource.DoDragDrop();
}
wxBEGIN_EVENT_TABLE(DragAndDropText2Frame, wxFrame)
EVT_LEFT_DOWN(DragAndDropText2Frame::OnLeftButtonDown)
wxEND_EVENT_TABLE()
DragAndDropText2Frame::DropTarget::DropTarget(wxTextCtrl& dropTarget)
: m_dropTarget(dropTarget)
{
}
bool DragAndDropText2Frame::DropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data)
{
m_dropTarget.SetValue(data);
return true;
}
#ifndef _TUTORIALS_WXWIDGETS_DRAGANDDROPTEXT2APP_H_
#define _TUTORIALS_WXWIDGETS_DRAGANDDROPTEXT2APP_H_
#include <wx/app.h>
class DragAndDropText2App : public wxApp
{
public:
virtual bool OnInit();
};
#endif
#include "DragAndDropText2App.h"
#include "DragAndDropText2Frame.h"
wxIMPLEMENT_APP(DragAndDropText2App);
bool DragAndDropText2App::OnInit()
{
DragAndDropText2Frame* frame = new DragAndDropText2Frame("DragAndDropText2");
frame->Show(true);
return true;
}
