]>
Commit | Line | Data |
---|---|---|
f6bcfd97 | 1 | \section{Drag and drop overview}\label{wxdndoverview} |
c18ecb11 | 2 | |
dface61c JS |
3 | Classes: \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 | 10 | Note that wxUSE\_DRAG\_AND\_DROP must be defined in setup.h in order |
f6bcfd97 | 11 | to use drag and drop in wxWindows. |
ad20983b | 12 | |
23efdd02 | 13 | See also: \helpref{wxDataObject overview}{wxdataobjectoverview} and \helpref{DnD sample}{samplednd} |
c18ecb11 | 14 | |
23efdd02 | 15 | It may be noted that data transfer to and from the clipboard is quite |
f6bcfd97 | 16 | similar to data transfer with drag and drop and the code to implement |
23efdd02 RR |
17 | these two types is almost the same. In particular, both data transfer |
18 | mechanisms store data in some kind of \helpref{wxDataObject}{wxdataobject} | |
19 | and identify its format(s) using the \helpref{wxDataFormat}{wxdataformat} | |
20 | class. | |
c18ecb11 | 21 | |
ad20983b | 22 | To be a {\it drag source}, i.e. to provide the data which may be dragged by |
80793cda | 23 | the user 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 | 27 | initialized 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} |
80793cda | 32 | \item{\bf Drag start:} To start the dragging process (typically in response to a |
23efdd02 RR |
33 | mouse click) you must call \helpref{wxDropSource::DoDragDrop}{wxdropsourcedodragdrop} |
34 | like this: | |
b1327f57 | 35 | |
c18ecb11 | 36 | \begin{verbatim} |
ab272c0b RR |
37 | wxDropSource dragSource( this ); |
38 | dragSource.SetData( my_data ); | |
80793cda | 39 | wxDragResult result = dragSource.DoDragDrop( TRUE ); |
ad20983b | 40 | \end{verbatim} |
80793cda JS |
41 | \item {\bf Dragging:} The call to DoDragDrop() blocks the program until the user releases the |
42 | mouse button (unless you override the \helpref{GiveFeedback}{wxdropsourcegivefeedback} function | |
ad20983b | 43 | to do something special). When the mouse moves in a window of a program which understands the |
330d6fd0 RR |
44 | same drag-and-drop protocol (any program under Windows or any program supporting the |
45 | XDnD protocol under X Windows), the corresponding \helpref{wxDropTarget}{wxdroptarget} methods | |
ad20983b | 46 | are called - see below. |
f60d0f94 | 47 | \item {\bf Processing the result:} DoDragDrop() returns an {\it effect code} which |
23efdd02 RR |
48 | is 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 | ||
80793cda | 60 | To be a {\it drop target}, i.e. to receive the data dropped by the user you should |
ad20983b VZ |
61 | follow the instructions below: |
62 | ||
63 | \begin{itemize}\itemsep=0pt | |
80793cda | 64 | \item {\bf Initialization:} For a window to be a drop target, it needs to have |
ad20983b | 65 | an associated \helpref{wxDropTarget}{wxdroptarget} object. Normally, you will |
62448488 | 66 | call \helpref{wxWindow::SetDropTarget}{wxwindowsetdroptarget} during window |
80793cda | 67 | creation associating your drop target with it. You must derive a class from |
ad20983b VZ |
68 | wxDropTarget and override its pure virtual methods. Alternatively, you may |
69 | derive from \helpref{wxTextDropTarget}{wxtextdroptarget} or | |
70 | \helpref{wxFileDropTarget}{wxfiledroptarget} and override their OnDropText() | |
71 | or OnDropFiles() method. | |
ad20983b | 72 | \item {\bf Drop:} When the user releases the mouse over a window, wxWindows |
80793cda | 73 | asks the associated wxDropTarget object if it accepts the data. For this, |
330d6fd0 RR |
74 | a \helpref{wxDataObject}{wxdataobject} must be associated with the drop target |
75 | and this data object will be responsible for the format negotiation between | |
76 | the drag source and the drop target. If all goes well, then \helpref{OnData}{wxdroptargetondata} | |
77 | will get called and the wxDataObject belonging to the drop target can get | |
78 | filled with data. | |
ad20983b | 79 | \item {\bf The end:} After processing the data, DoDragDrop() returns either |
80793cda JS |
80 | wxDragCopy or wxDragMove depending on the state of the keys <Ctrl>, <Shift> |
81 | and <Alt> at the moment of the drop. There is currently no way for the drop | |
ad20983b | 82 | target to change this return code. |
c18ecb11 VZ |
83 | \end{itemize} |
84 |