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