drag and drop overview
classes: wxdataobject,
wxtextdataobject,
wxdropsource,
wxdroptarget,
wxtextdroptarget,
wxfiledroptarget
note that wxuse_drag_and_drop must be defined in setup.h in order
to use drag and drop in wxwidgets.
see also: wxdataobject overview and dnd sample
it may be noted that data transfer to and from the clipboard is quite
similar to data transfer with drag and drop and the code to implement
these two types is almost the same. in particular, both data transfer
mechanisms store data in some kind of wxdataobject
and identify its format(s) using the wxdataformat
class.
to be a drag source, i.e. to provide the data which may be dragged by
the user elsewhere, you should implement the following steps:
- preparation: first of all, a data object must be created and
initialized with the data you wish to drag. for example:
wxtextdataobject my_data("this text will be dragged.");
- drag start: to start the dragging process (typically in response to a
mouse click) you must call wxdropsource::dodragdrop
like this:
wxdropsource dragsource( this );
dragsource.setdata( my_data );
wxdragresult result = dragsource.dodragdrop( true );
- dragging: the call to dodragdrop() blocks the program until the user releases the
mouse button (unless you override the givefeedback function
to do something special). when the mouse moves in a window of a program which understands the
same drag-and-drop protocol (any program under windows or any program supporting the
xdnd protocol under x windows), the corresponding wxdroptarget methods
are called - see below.
- processing the result: dodragdrop() returns an effect code which
is one of the values of wxdragresult enum (explained here):
switch (result)
{
case wxdragcopy: /* copy the data */ break;
case wxdragmove: /* move the data */ break;
default: /* do nothing */ break;
}
to be a drop target, i.e. to receive the data dropped by the user you should
follow the instructions below:
- initialization: for a window to be a drop target, it needs to have
an associated wxdroptarget object. normally, you will
call wxwindow::setdroptarget during window
creation associating your drop target with it. you must derive a class from
wxdroptarget and override its pure virtual methods. alternatively, you may
derive from wxtextdroptarget or
wxfiledroptarget and override their ondroptext()
or ondropfiles() method.
- drop: when the user releases the mouse over a window, wxwidgets
asks the associated wxdroptarget object if it accepts the data. for this,
a wxdataobject must be associated with the drop target
and this data object will be responsible for the format negotiation between
the drag source and the drop target. if all goes well, then ondata
will get called and the wxdataobject belonging to the drop target can get
filled with data.
- the end: after processing the data, dodragdrop() returns either
wxdragcopy or wxdragmove depending on the state of the keys <ctrl>, <shift>
and <alt> at the moment of the drop. there is currently no way for the drop
target to change this return code.
|