]>
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 |
f60d0f94 | 23 | 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} |
ad20983b | 32 | \item{\bf Drag start:} To start 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 ); | |
23efdd02 | 39 | wxDragResult result = dragSource.DoDragDrop( TRUE ); |
ad20983b | 40 | \end{verbatim} |
23efdd02 | 41 | \item {\bf Dragging:} The call to DoDragDrop() blocks the program until the user release the |
ad20983b VZ |
42 | mouse button (unless you override \helpref{GiveFeedback}{wxdropsourcegivefeedback} function |
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 | ||
ad20983b VZ |
60 | To be a {\it drop target}, i.e. to receive the data dropped by user you should |
61 | follow the instructions below: | |
62 | ||
63 | \begin{itemize}\itemsep=0pt | |
64 | \item {\bf Initialization:} For a window to be drop target, it needs to have | |
65 | an associated \helpref{wxDropTarget}{wxdroptarget} object. Normally, you will | |
62448488 | 66 | call \helpref{wxWindow::SetDropTarget}{wxwindowsetdroptarget} during window |
ad20983b VZ |
67 | creation associating you drop target with it. You must derive a class from |
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 |
330d6fd0 RR |
73 | queries the associated wxDropTarget object if it accepts the data. For this, |
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 VZ |
79 | \item {\bf The end:} After processing the data, DoDragDrop() returns either |
80 | wxDragCopy or wxDragMove depending on the state of the keys (<Ctrl>, <Shift> | |
81 | and <Alt>) at the moment of drop. There is currently no way for the drop | |
82 | target to change this return code. | |
c18ecb11 VZ |
83 | \end{itemize} |
84 |