]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tdnd.tex
Some doc changes
[wxWidgets.git] / docs / latex / wx / tdnd.tex
CommitLineData
c18ecb11
VZ
1\section{Drag-and-drop and clipboard overview}\label{wxdndoverview}
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
RR
10Note that wxUSE\_DRAG\_AND\_DROP must be defined in setup.h in order
11to use Drag'n'Drop in wxWindows.
ad20983b 12
c18ecb11
VZ
13This overview describes wxWindows support for drag and drop and clipboard
14operations. Both of these topics are discussed here because, in fact, they're
f60d0f94 15quite related. Drag and drop and clipboard are just two ways of passing the
c18ecb11
VZ
16data around and so the code required to implement both types of the operations
17is almost the same.
18
f60d0f94
JS
19Both operations involve passing some data from one program to another,
20although the data can be received in the same program as the source. In the case
21of clipboard transfer, the data is first placed on the clipboard and then
22pasted into the destination program, while for a drag-and-drop operation the
ad20983b 23data object is not stored anywhere but is created when the user starts
f60d0f94
JS
24dragging and is destroyed as soon as he ends it, whether the operation was
25ended successfully or cancelled.
c18ecb11 26
ad20983b 27To be a {\it drag source}, i.e. to provide the data which may be dragged by
f60d0f94 28user elsewhere, you should implement the following steps:
b1327f57 29
c18ecb11 30\begin{itemize}\itemsep=0pt
ad20983b 31\item {\bf Preparation:} First of all, the data object must be created and
f60d0f94 32initialized with the data you wish to drag. For example:
b1327f57 33
c18ecb11 34\begin{verbatim}
ab272c0b 35 wxDataObject *my_data = new wxTextDataObject data("This string will be dragged.");
ad20983b 36\end{verbatim}
ad20983b 37\item{\bf Drag start:} To start dragging process (typically in response to a
f60d0f94
JS
38mouse click) you must call \helpref{DoDragDrop}{wxdropsourcedodragdrop} function
39of wxDropSource object which should be constructed like this:
b1327f57 40
c18ecb11 41\begin{verbatim}
ab272c0b
RR
42 wxDropSource dragSource( this );
43 dragSource.SetData( my_data );
ad20983b 44\end{verbatim}
ad20983b
VZ
45\item {\bf Dragging:} The call to DoDragDrop() blocks until the user release the
46mouse button (unless you override \helpref{GiveFeedback}{wxdropsourcegivefeedback} function
47to do something special). When the mouse moves in a window of a program which understands the
330d6fd0
RR
48same drag-and-drop protocol (any program under Windows or any program supporting the
49XDnD protocol under X Windows), the corresponding \helpref{wxDropTarget}{wxdroptarget} methods
ad20983b 50are called - see below.
f60d0f94 51\item {\bf Processing the result:} DoDragDrop() returns an {\it effect code} which
dface61c 52is one of the values of \helpref{wxDragResult}{wxdropsource} enum. Codes
ad20983b
VZ
53of wxDragError, wxDragNone and wxDragCancel have the obvious meaning and mean
54that there is nothing to do on the sending end (except of possibly logging the
55error in the first case). wxDragCopy means that the data has been successfully
56copied and doesn't require any specific actions neither. But wxDragMove is
57special because it means that the data must be deleted from where it was
58copied. If it doesn't make sense (dragging selected text from a read-only
59file) you should pass FALSE as parameter to DoDragDrop() in the previous step.
ad20983b
VZ
60\end{itemize}
61
ad20983b
VZ
62To be a {\it drop target}, i.e. to receive the data dropped by user you should
63follow the instructions below:
64
65\begin{itemize}\itemsep=0pt
66\item {\bf Initialization:} For a window to be drop target, it needs to have
67an associated \helpref{wxDropTarget}{wxdroptarget} object. Normally, you will
62448488 68call \helpref{wxWindow::SetDropTarget}{wxwindowsetdroptarget} during window
ad20983b
VZ
69creation associating you drop target with it. You must derive a class from
70wxDropTarget and override its pure virtual methods. Alternatively, you may
71derive from \helpref{wxTextDropTarget}{wxtextdroptarget} or
72\helpref{wxFileDropTarget}{wxfiledroptarget} and override their OnDropText()
73or OnDropFiles() method.
ad20983b 74\item {\bf Drop:} When the user releases the mouse over a window, wxWindows
330d6fd0
RR
75queries the associated wxDropTarget object if it accepts the data. For this,
76a \helpref{wxDataObject}{wxdataobject} must be associated with the drop target
77and this data object will be responsible for the format negotiation between
78the drag source and the drop target. If all goes well, then \helpref{OnData}{wxdroptargetondata}
79will get called and the wxDataObject belonging to the drop target can get
80filled with data.
ad20983b
VZ
81\item {\bf The end:} After processing the data, DoDragDrop() returns either
82wxDragCopy or wxDragMove depending on the state of the keys (<Ctrl>, <Shift>
83and <Alt>) at the moment of drop. There is currently no way for the drop
84target to change this return code.
c18ecb11
VZ
85\end{itemize}
86