]>
Commit | Line | Data |
---|---|---|
23efdd02 | 1 | \section{wxDataObject overview}\label{wxdataobjectoverview} |
23d277e6 VZ |
2 | |
3 | Classes: \helpref{wxDataObject}{wxdataobject}, | |
88b1927c JS |
4 | \helpref{wxClipboard}{wxclipboard}, |
5 | \helpref{wxDataFormat}{wxdataformat}, | |
6 | \helpref{wxDropSource}{wxdropsource}, | |
7 | \helpref{wxDropTarget}{wxdroptarget} | |
23d277e6 VZ |
8 | |
9 | This overview discusses data transfer through clipboard or drag and drop. In | |
10 | wxWindows, these two ways to transfer data (either between different | |
11 | applications or inside one and the same) are very similar which allows to | |
717a57c2 | 12 | implement both of them using almost the same code - or, in other |
23d277e6 VZ |
13 | words, if you implement drag and drop support for your application, you get |
14 | clipboard support for free and vice versa. | |
15 | ||
407f3681 | 16 | At the heart of both clipboard and drag and drop operations lies the |
717a57c2 VZ |
17 | \helpref{wxDataObject}{wxdataobject} class. The objects of this class (or, to |
18 | be precise, classes derived from it) represent the data which is being carried | |
19 | by the mouse during drag and drop operation or copied to or pasted from the | |
20 | clipboard. wxDataObject is a "smart" piece of data because it knows which | |
21 | formats it supports (see GetFormatCount and GetAllFormats) and knows how to | |
22 | render itself in any of them (see GetDataHere). It can also receive its value | |
23 | from the outside in a format it supports if it implements the SetData method. | |
24 | Please see the documentation of this class for more details. | |
23d277e6 VZ |
25 | |
26 | Both clipboard and drag and drop operations have two sides: the source and | |
88b1927c | 27 | target, the data provider and the data receiver. These which may be in the same |
23d277e6 | 28 | application and even the same window when, for example, you drag some text from |
88b1927c | 29 | one position to another in a word processor. Let us describe what each of them |
23d277e6 VZ |
30 | should do. |
31 | ||
407f3681 | 32 | \subsection{The data provider (source) duties}\label{wxdataobjectsource} |
23d277e6 VZ |
33 | |
34 | The data provider is responsible for creating a | |
717a57c2 VZ |
35 | \helpref{wxDataObject}{wxdataobject} containing the data to be |
36 | transfered. Then it should either pass it to the clipboard using | |
37 | \helpref{SetData}{wxclipboardsetdata} function or to | |
38 | \helpref{wxDropSource}{wxdropsource} and call | |
23d277e6 VZ |
39 | \helpref{DoDragDrop}{wxdropsourcedodragdrop} function. |
40 | ||
41 | The only (but important) difference is that the object for the clipboard | |
42 | transfer must always be created on the heap (i.e. using {\tt new}) and it will | |
43 | be freed by the clipboard when it is no longer needed (indeed, it is not known | |
44 | in advance when, if ever, the data will be pasted from the clipboard). On the | |
45 | other hand, the object for drag and drop operation must only exist while | |
46 | \helpref{DoDragDrop}{wxdropsourcedodragdrop} executes and may be safely deleted | |
47 | afterwards and so can be created either on heap or on stack (i.e. as a local | |
48 | variable). | |
49 | ||
88b1927c | 50 | Another small difference is that in the case of clipboard operation, the |
23d277e6 VZ |
51 | application usually knows in advance whether it copies or cuts (i.e. copies and |
52 | deletes) data - in fact, this usually depends on which menu item the user | |
53 | chose. But for drag and drop it can only know it after | |
54 | \helpref{DoDragDrop}{wxdropsourcedodragdrop} returns (from its return value). | |
55 | ||
407f3681 | 56 | \subsection{The data receiver (target) duties}\label{wxdataobjecttarget} |
23d277e6 VZ |
57 | |
58 | To receive (paste in usual terminology) data from the clipboard, you should | |
59 | create a \helpref{wxDataObject}{wxdataobject} derived class which supports the | |
60 | data formats you need and pass it as argument to | |
61 | \helpref{wxClipboard::GetData}{wxclipboardgetdata}. If it returns {\tt FALSE}, | |
62 | no data in (any of) the supported format(s) is available. If it returns {\tt | |
63 | TRUE}, the data has been successfully transfered to wxDataObject. | |
64 | ||
717a57c2 VZ |
65 | For drag and drop case, the \helpref{wxDropTarget::OnData}{wxdroptargetondata} |
66 | virtual function will be called when a data object is dropped, from which the | |
67 | data itself may be requested by calling | |
68 | \helpref{wxDropTarget::GetData}{wxdroptargetwxdroptarget} method which fills | |
69 | the data object. | |
70 |