]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/tclipbrd.tex
acdf8cac9e70a169820981bf6bdcea62e7774445
[wxWidgets.git] / docs / latex / wx / tclipbrd.tex
1 \section{wxDataObject overview}\label{wxdataobjectoverview}
2
3 Classes: \helpref{wxDataObject}{wxdataobject},
4 \helpref{wxClipboard}{wxclipboard},
5 \helpref{wxDataFormat}{wxdataformat},
6 \helpref{wxDropSource}{wxdropsource},
7 \helpref{wxDropTarget}{wxdroptarget}
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
12 implement both of them using almost the same code - or, in other
13 words, if you implement drag and drop support for your application, you get
14 clipboard support for free and vice versa.
15
16 At the heart of both clipboard and drag and drop operations lies the
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.
25
26 Both clipboard and drag and drop operations have two sides: the source and
27 target, the data provider and the data receiver. These which may be in the same
28 application and even the same window when, for example, you drag some text from
29 one position to another in a word processor. Let us describe what each of them
30 should do.
31
32 \subsection{The data provider (source) duties}\label{wxdataobjectsource}
33
34 The data provider is responsible for creating a
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
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
50 Another small difference is that in the case of clipboard operation, the
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
56 \subsection{The data receiver (target) duties}\label{wxdataobjecttarget}
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
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