]>
Commit | Line | Data |
---|---|---|
15b6757b FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dnd | |
3 | // Purpose: topic overview | |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /*! | |
36c9828f | 10 | |
15b6757b | 11 | @page dnd_overview Drag and drop overview |
36c9828f FM |
12 | |
13 | Classes: #wxDataObject, | |
14 | #wxTextDataObject, | |
15 | #wxDropSource, | |
16 | #wxDropTarget, | |
17 | #wxTextDropTarget, | |
15b6757b FM |
18 | #wxFileDropTarget |
19 | Note that wxUSE_DRAG_AND_DROP must be defined in setup.h in order | |
20 | to use drag and drop in wxWidgets. | |
21 | See also: @ref dataobject_overview and @ref samplednd_overview | |
22 | It may be noted that data transfer to and from the clipboard is quite | |
23 | similar to data transfer with drag and drop and the code to implement | |
24 | these two types is almost the same. In particular, both data transfer | |
25 | mechanisms store data in some kind of #wxDataObject | |
26 | and identify its format(s) using the #wxDataFormat | |
27 | class. | |
28 | To be a @e drag source, i.e. to provide the data which may be dragged by | |
29 | the user elsewhere, you should implement the following steps: | |
36c9828f FM |
30 | |
31 | ||
15b6757b FM |
32 | @b Preparation: First of all, a data object must be created and |
33 | initialized with the data you wish to drag. For example: | |
36c9828f | 34 | |
15b6757b FM |
35 | @code |
36 | wxTextDataObject my_data("This text will be dragged."); | |
37 | @endcode | |
36c9828f | 38 | |
15b6757b FM |
39 | @b Drag start: To start the dragging process (typically in response to a |
40 | mouse click) you must call wxDropSource::DoDragDrop | |
41 | like this: | |
36c9828f | 42 | |
15b6757b FM |
43 | @code |
44 | wxDropSource dragSource( this ); | |
36c9828f FM |
45 | dragSource.SetData( my_data ); |
46 | wxDragResult result = dragSource.DoDragDrop( TRUE ); | |
15b6757b | 47 | @endcode |
36c9828f | 48 | |
15b6757b FM |
49 | @b Dragging: The call to DoDragDrop() blocks the program until the user releases the |
50 | mouse button (unless you override the #GiveFeedback function | |
51 | to do something special). When the mouse moves in a window of a program which understands the | |
52 | same drag-and-drop protocol (any program under Windows or any program supporting the | |
53 | XDnD protocol under X Windows), the corresponding #wxDropTarget methods | |
54 | are called - see below. | |
55 | @b Processing the result: DoDragDrop() returns an @e effect code which | |
56 | is one of the values of @c wxDragResult enum (explained #here): | |
36c9828f | 57 | |
15b6757b FM |
58 | @code |
59 | switch (result) | |
36c9828f FM |
60 | { |
61 | case wxDragCopy: /* copy the data */ break; | |
62 | case wxDragMove: /* move the data */ | |
63 | break; | |
64 | default: /* do nothing */ | |
65 | break; | |
66 | } | |
67 | @endcode | |
68 | ||
69 | ||
70 | ||
71 | To be a @e drop target, i.e. to receive the data dropped by the user you should | |
72 | follow the instructions below: | |
73 | ||
74 | ||
75 | @b Initialization: For a window to be a drop target, it needs to have | |
76 | an associated #wxDropTarget object. Normally, you will | |
77 | call wxWindow::SetDropTarget during window | |
78 | creation associating your drop target with it. You must derive a class from | |
79 | wxDropTarget and override its pure virtual methods. Alternatively, you may | |
80 | derive from #wxTextDropTarget or | |
81 | #wxFileDropTarget and override their OnDropText() | |
82 | or OnDropFiles() method. | |
83 | @b Drop: When the user releases the mouse over a window, wxWidgets | |
84 | asks the associated wxDropTarget object if it accepts the data. For this, | |
85 | a #wxDataObject must be associated with the drop target | |
86 | and this data object will be responsible for the format negotiation between | |
87 | the drag source and the drop target. If all goes well, then #OnData | |
88 | will get called and the wxDataObject belonging to the drop target can get | |
89 | filled with data. | |
90 | @b The end: After processing the data, DoDragDrop() returns either | |
91 | wxDragCopy or wxDragMove depending on the state of the keys Ctrl, Shift | |
92 | and Alt at the moment of the drop. There is currently no way for the drop | |
93 | target to change this return code. | |
94 | ||
95 | */ | |
96 | ||
97 |