1999-12-29 06:52:51 -05:00
|
|
|
\section{wxDataObject overview}\label{wxdataobjectoverview}
|
1999-10-17 19:57:10 -04:00
|
|
|
|
|
|
|
Classes: \helpref{wxDataObject}{wxdataobject},
|
1999-10-22 09:12:08 -04:00
|
|
|
\helpref{wxClipboard}{wxclipboard},
|
|
|
|
\helpref{wxDataFormat}{wxdataformat},
|
|
|
|
\helpref{wxDropSource}{wxdropsource},
|
|
|
|
\helpref{wxDropTarget}{wxdroptarget}
|
1999-10-17 19:57:10 -04:00
|
|
|
|
1999-12-29 14:18:01 -05:00
|
|
|
See also: \helpref{Drag and drop overview}{wxdndoverview} and \helpref{DnD sample}{samplednd}
|
|
|
|
|
1999-10-17 19:57:10 -04:00
|
|
|
This overview discusses data transfer through clipboard or drag and drop. In
|
|
|
|
wxWindows, these two ways to transfer data (either between different
|
|
|
|
applications or inside one and the same) are very similar which allows to
|
1999-11-01 20:25:43 -05:00
|
|
|
implement both of them using almost the same code - or, in other
|
1999-10-17 19:57:10 -04:00
|
|
|
words, if you implement drag and drop support for your application, you get
|
|
|
|
clipboard support for free and vice versa.
|
|
|
|
|
1999-11-02 05:02:01 -05:00
|
|
|
At the heart of both clipboard and drag and drop operations lies the
|
1999-11-01 20:25:43 -05:00
|
|
|
\helpref{wxDataObject}{wxdataobject} class. The objects of this class (or, to
|
|
|
|
be precise, classes derived from it) represent the data which is being carried
|
|
|
|
by the mouse during drag and drop operation or copied to or pasted from the
|
|
|
|
clipboard. wxDataObject is a "smart" piece of data because it knows which
|
|
|
|
formats it supports (see GetFormatCount and GetAllFormats) and knows how to
|
|
|
|
render itself in any of them (see GetDataHere). It can also receive its value
|
|
|
|
from the outside in a format it supports if it implements the SetData method.
|
|
|
|
Please see the documentation of this class for more details.
|
1999-10-17 19:57:10 -04:00
|
|
|
|
|
|
|
Both clipboard and drag and drop operations have two sides: the source and
|
1999-10-22 09:12:08 -04:00
|
|
|
target, the data provider and the data receiver. These which may be in the same
|
1999-10-17 19:57:10 -04:00
|
|
|
application and even the same window when, for example, you drag some text from
|
1999-10-22 09:12:08 -04:00
|
|
|
one position to another in a word processor. Let us describe what each of them
|
1999-10-17 19:57:10 -04:00
|
|
|
should do.
|
|
|
|
|
1999-11-02 05:02:01 -05:00
|
|
|
\subsection{The data provider (source) duties}\label{wxdataobjectsource}
|
1999-10-17 19:57:10 -04:00
|
|
|
|
|
|
|
The data provider is responsible for creating a
|
1999-11-01 20:25:43 -05:00
|
|
|
\helpref{wxDataObject}{wxdataobject} containing the data to be
|
|
|
|
transfered. Then it should either pass it to the clipboard using
|
|
|
|
\helpref{SetData}{wxclipboardsetdata} function or to
|
|
|
|
\helpref{wxDropSource}{wxdropsource} and call
|
1999-10-17 19:57:10 -04:00
|
|
|
\helpref{DoDragDrop}{wxdropsourcedodragdrop} function.
|
|
|
|
|
|
|
|
The only (but important) difference is that the object for the clipboard
|
|
|
|
transfer must always be created on the heap (i.e. using {\tt new}) and it will
|
|
|
|
be freed by the clipboard when it is no longer needed (indeed, it is not known
|
|
|
|
in advance when, if ever, the data will be pasted from the clipboard). On the
|
|
|
|
other hand, the object for drag and drop operation must only exist while
|
|
|
|
\helpref{DoDragDrop}{wxdropsourcedodragdrop} executes and may be safely deleted
|
|
|
|
afterwards and so can be created either on heap or on stack (i.e. as a local
|
|
|
|
variable).
|
|
|
|
|
1999-10-22 09:12:08 -04:00
|
|
|
Another small difference is that in the case of clipboard operation, the
|
1999-10-17 19:57:10 -04:00
|
|
|
application usually knows in advance whether it copies or cuts (i.e. copies and
|
|
|
|
deletes) data - in fact, this usually depends on which menu item the user
|
|
|
|
chose. But for drag and drop it can only know it after
|
|
|
|
\helpref{DoDragDrop}{wxdropsourcedodragdrop} returns (from its return value).
|
|
|
|
|
1999-11-02 05:02:01 -05:00
|
|
|
\subsection{The data receiver (target) duties}\label{wxdataobjecttarget}
|
1999-10-17 19:57:10 -04:00
|
|
|
|
|
|
|
To receive (paste in usual terminology) data from the clipboard, you should
|
|
|
|
create a \helpref{wxDataObject}{wxdataobject} derived class which supports the
|
|
|
|
data formats you need and pass it as argument to
|
|
|
|
\helpref{wxClipboard::GetData}{wxclipboardgetdata}. If it returns {\tt FALSE},
|
|
|
|
no data in (any of) the supported format(s) is available. If it returns {\tt
|
|
|
|
TRUE}, the data has been successfully transfered to wxDataObject.
|
|
|
|
|
1999-11-01 20:25:43 -05:00
|
|
|
For drag and drop case, the \helpref{wxDropTarget::OnData}{wxdroptargetondata}
|
|
|
|
virtual function will be called when a data object is dropped, from which the
|
|
|
|
data itself may be requested by calling
|
|
|
|
\helpref{wxDropTarget::GetData}{wxdroptargetwxdroptarget} method which fills
|
|
|
|
the data object.
|
|
|
|
|