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