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