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