Version: 3.0.3
wxDataObject Class Referenceabstract

#include <wx/dataobj.h>

+ Inheritance diagram for wxDataObject:

Detailed Description

A wxDataObject represents data that can be copied to or from the clipboard, or dragged and dropped.

The important thing about wxDataObject is that this is a 'smart' piece of data unlike 'dumb' data containers such as memory buffers or files. Being 'smart' here means that the data object itself should know what data formats it supports and how to render itself in each of its supported formats.

A supported format, incidentally, is exactly the format in which the data can be requested from a data object or from which the data object may be set. In the general case, an object may support different formats on 'input' and 'output', i.e. it may be able to render itself in a given format but not be created from data on this format or vice versa. wxDataObject defines the wxDataObject::Direction enumeration type which distinguishes between them.

See wxDataFormat documentation for more about formats.

Not surprisingly, being 'smart' comes at a price of added complexity. This is reasonable for the situations when you really need to support multiple formats, but may be annoying if you only want to do something simple like cut and paste text.

To provide a solution for both cases, wxWidgets has two predefined classes which derive from wxDataObject: wxDataObjectSimple and wxDataObjectComposite. wxDataObjectSimple is the simplest wxDataObject possible and only holds data in a single format (such as HTML or text) and wxDataObjectComposite is the simplest way to implement a wxDataObject that does support multiple formats because it achieves this by simply holding several wxDataObjectSimple objects.

So, you have several solutions when you need a wxDataObject class (and you need one as soon as you want to transfer data via the clipboard or drag and drop):

  1. Use one of the built-in classes.
  2. Use wxDataObjectSimple
    • Deriving from wxDataObjectSimple is the simplest solution for custom data - you will only support one format and so probably won't be able to communicate with other programs, but data transfer will work in your program (or between different instances of it).
  3. Use wxDataObjectComposite
    • This is a simple but powerful solution which allows you to support any number of formats (either standard or custom if you combine it with the previous solution).
  4. Use wxDataObject directly
    • This is the solution for maximum flexibility and efficiency, but it is also the most difficult to implement.

Please note that the easiest way to use drag and drop and the clipboard with multiple formats is by using wxDataObjectComposite, but it is not the most efficient one as each wxDataObjectSimple would contain the whole data in its respective formats. Now imagine that you want to paste 200 pages of text in your proprietary format, as well as Word, RTF, HTML, Unicode and plain text to the clipboard and even today's computers are in trouble. For this case, you will have to derive from wxDataObject directly and make it enumerate its formats and provide the data in the requested format on demand.

Note that neither the GTK+ data transfer mechanisms for clipboard and drag and drop, nor OLE data transfer, copies any data until another application actually requests the data. This is in contrast to the 'feel' offered to the user of a program who would normally think that the data resides in the clipboard after having pressed 'Copy' - in reality it is only declared to be available.

You may also derive your own data object classes from wxCustomDataObject for user-defined types. The format of user-defined data is given as a mime-type string literal, such as "application/word" or "image/png". These strings are used as they are under Unix (so far only GTK+) to identify a format and are translated into their Windows equivalent under Win32 (using the OLE IDataObject for data exchange to and from the clipboard and for drag and drop). Note that the format string translation under Windows is not yet finished.

Each class derived directly from wxDataObject must override and implement all of its functions which are pure virtual in the base class. The data objects which only render their data or only set it (i.e. work in only one direction), should return 0 from GetFormatCount().

wxPerl Note: This class is not currently usable from wxPerl; you may use Wx::PlDataObjectSimple instead.

Library:  wxCore
Category:  Clipboard and Drag & Drop
See also
Drag and Drop Overview, Drag & Drop Sample, wxFileDataObject, wxTextDataObject, wxBitmapDataObject, wxCustomDataObject, wxDropTarget, wxDropSource, wxTextDropTarget, wxFileDropTarget

Public Types

enum  Direction {
  Get = 0x01,
  Set = 0x02,
  Both = 0x03
}
 

Public Member Functions

 wxDataObject ()
 Constructor. More...
 
virtual ~wxDataObject ()
 Destructor. More...
 
virtual void GetAllFormats (wxDataFormat *formats, Direction dir=Get) const =0
 Copies all formats supported in the given direction dir to the array pointed to by formats. More...
 
virtual bool GetDataHere (const wxDataFormat &format, void *buf) const =0
 The method will write the data of the format format to the buffer buf. More...
 
virtual size_t GetDataSize (const wxDataFormat &format) const =0
 Returns the data size of the given format format. More...
 
virtual size_t GetFormatCount (Direction dir=Get) const =0
 Returns the number of available formats for rendering or setting the data. More...
 
virtual wxDataFormat GetPreferredFormat (Direction dir=Get) const =0
 Returns the preferred format for either rendering the data (if dir is Get, its default value) or for setting it. More...
 
virtual bool SetData (const wxDataFormat &format, size_t len, const void *buf)
 Set the data in the format format of the length len provided in the buffer buf. More...
 
bool IsSupported (const wxDataFormat &format, Direction dir=Get) const
 Returns true if this format is supported. More...
 

Member Enumeration Documentation

Enumerator
Get 

Format is supported by GetDataHere()

Set 

Format is supported by SetData()

Both 

Format is supported by both GetDataHere() and SetData() (unused currently)

Constructor & Destructor Documentation

wxDataObject::wxDataObject ( )

Constructor.

virtual wxDataObject::~wxDataObject ( )
virtual

Destructor.

Member Function Documentation

virtual void wxDataObject::GetAllFormats ( wxDataFormat formats,
Direction  dir = Get 
) const
pure virtual

Copies all formats supported in the given direction dir to the array pointed to by formats.

There must be enough space for GetFormatCount(dir) formats in it.

wxPerl Note: In wxPerl this method only takes the dir parameter. In scalar context it returns the first format in the list, in list context it returns a list containing all the supported formats.

Implemented in wxTextDataObject.

virtual bool wxDataObject::GetDataHere ( const wxDataFormat format,
void *  buf 
) const
pure virtual

The method will write the data of the format format to the buffer buf.

In other words, copy the data from this object in the given format to the supplied buffer. Returns true on success, false on failure.

Implemented in wxRichTextBufferDataObject.

virtual size_t wxDataObject::GetDataSize ( const wxDataFormat format) const
pure virtual

Returns the data size of the given format format.

Implemented in wxRichTextBufferDataObject.

virtual size_t wxDataObject::GetFormatCount ( Direction  dir = Get) const
pure virtual

Returns the number of available formats for rendering or setting the data.

Implemented in wxTextDataObject.

virtual wxDataFormat wxDataObject::GetPreferredFormat ( Direction  dir = Get) const
pure virtual

Returns the preferred format for either rendering the data (if dir is Get, its default value) or for setting it.

Usually this will be the native format of the wxDataObject.

Implemented in wxRichTextBufferDataObject.

bool wxDataObject::IsSupported ( const wxDataFormat format,
Direction  dir = Get 
) const

Returns true if this format is supported.

virtual bool wxDataObject::SetData ( const wxDataFormat format,
size_t  len,
const void *  buf 
)
virtual

Set the data in the format format of the length len provided in the buffer buf.

In other words, copy length bytes of data from the buffer to this data object.

Parameters
formatThe format for which to set the data.
lenThe size of data in bytes.
bufNon-NULL pointer to the data.
Returns
true on success, false on failure.

Reimplemented in wxRichTextBufferDataObject.