]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tdnd.tex
toplevel code transferred to wxTopLevelWindow
[wxWidgets.git] / docs / latex / wx / tdnd.tex
CommitLineData
f6bcfd97 1\section{Drag and drop overview}\label{wxdndoverview}
c18ecb11 2
dface61c
JS
3Classes: \helpref{wxDataObject}{wxdataobject},
4\helpref{wxTextDataObject}{wxtextdataobject},
5\helpref{wxDropSource}{wxdropsource},
6\helpref{wxDropTarget}{wxdroptarget},
7\helpref{wxTextDropTarget}{wxtextdroptarget},
8\helpref{wxFileDropTarget}{wxfiledroptarget}
c18ecb11 9
ab272c0b 10Note that wxUSE\_DRAG\_AND\_DROP must be defined in setup.h in order
f6bcfd97 11to use drag and drop in wxWindows.
ad20983b 12
23efdd02 13See also: \helpref{wxDataObject overview}{wxdataobjectoverview} and \helpref{DnD sample}{samplednd}
c18ecb11 14
23efdd02 15It may be noted that data transfer to and from the clipboard is quite
f6bcfd97 16similar to data transfer with drag and drop and the code to implement
23efdd02
RR
17these two types is almost the same. In particular, both data transfer
18mechanisms store data in some kind of \helpref{wxDataObject}{wxdataobject}
19and identify its format(s) using the \helpref{wxDataFormat}{wxdataformat}
20class.
c18ecb11 21
ad20983b 22To be a {\it drag source}, i.e. to provide the data which may be dragged by
f60d0f94 23user elsewhere, you should implement the following steps:
b1327f57 24
c18ecb11 25\begin{itemize}\itemsep=0pt
23efdd02 26\item {\bf Preparation:} First of all, a data object must be created and
f60d0f94 27initialized with the data you wish to drag. For example:
b1327f57 28
c18ecb11 29\begin{verbatim}
23efdd02 30 wxTextDataObject my_data("This text will be dragged.");
ad20983b 31\end{verbatim}
ad20983b 32\item{\bf Drag start:} To start dragging process (typically in response to a
23efdd02
RR
33mouse click) you must call \helpref{wxDropSource::DoDragDrop}{wxdropsourcedodragdrop}
34like this:
b1327f57 35
c18ecb11 36\begin{verbatim}
ab272c0b
RR
37 wxDropSource dragSource( this );
38 dragSource.SetData( my_data );
23efdd02 39 wxDragResult result = dragSource.DoDragDrop( TRUE );
ad20983b 40\end{verbatim}
23efdd02 41\item {\bf Dragging:} The call to DoDragDrop() blocks the program until the user release the
ad20983b
VZ
42mouse button (unless you override \helpref{GiveFeedback}{wxdropsourcegivefeedback} function
43to do something special). When the mouse moves in a window of a program which understands the
330d6fd0
RR
44same drag-and-drop protocol (any program under Windows or any program supporting the
45XDnD protocol under X Windows), the corresponding \helpref{wxDropTarget}{wxdroptarget} methods
ad20983b 46are called - see below.
f60d0f94 47\item {\bf Processing the result:} DoDragDrop() returns an {\it effect code} which
23efdd02
RR
48is one of the values of {\tt wxDragResult} enum (explained \helpref{here}{wxdroptarget}):
49
50\begin{verbatim}
51 switch (result)
52 {
53 case wxDragCopy: /* copy the data */ break;
54 case wxDragMove: /* move the data */ break;
55 default: /* do nothing */ break;
56 }
57\end{verbatim}
ad20983b
VZ
58\end{itemize}
59
ad20983b
VZ
60To be a {\it drop target}, i.e. to receive the data dropped by user you should
61follow the instructions below:
62
63\begin{itemize}\itemsep=0pt
64\item {\bf Initialization:} For a window to be drop target, it needs to have
65an associated \helpref{wxDropTarget}{wxdroptarget} object. Normally, you will
62448488 66call \helpref{wxWindow::SetDropTarget}{wxwindowsetdroptarget} during window
ad20983b
VZ
67creation associating you drop target with it. You must derive a class from
68wxDropTarget and override its pure virtual methods. Alternatively, you may
69derive from \helpref{wxTextDropTarget}{wxtextdroptarget} or
70\helpref{wxFileDropTarget}{wxfiledroptarget} and override their OnDropText()
71or OnDropFiles() method.
ad20983b 72\item {\bf Drop:} When the user releases the mouse over a window, wxWindows
330d6fd0
RR
73queries the associated wxDropTarget object if it accepts the data. For this,
74a \helpref{wxDataObject}{wxdataobject} must be associated with the drop target
75and this data object will be responsible for the format negotiation between
76the drag source and the drop target. If all goes well, then \helpref{OnData}{wxdroptargetondata}
77will get called and the wxDataObject belonging to the drop target can get
78filled with data.
ad20983b
VZ
79\item {\bf The end:} After processing the data, DoDragDrop() returns either
80wxDragCopy or wxDragMove depending on the state of the keys (<Ctrl>, <Shift>
81and <Alt>) at the moment of drop. There is currently no way for the drop
82target to change this return code.
c18ecb11
VZ
83\end{itemize}
84