wxRuby Documentation Home

# Wx::DataObject

Note: fully updated ruby documentation for DataObject classes is
needed. For the time being, the test case in test/test_clipboard.rb and
the dnd sample demonstrate how to use data object classes in some
detail. For many purposes the ready-to-use classes
TextDataObject,
FiletDataObject, and
BitmapDataObject will suffice.

A DataObject represents data that can be copied to or from the clipboard, or
dragged and dropped. The important thing about DataObject 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. DataObject defines an
enumeration type

Get = 0×01 // format is supported by GetDataHere() Set = 0×02 // format is supported by SetData()

which distinguishes between them. See
DataFormat 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, Widgets has two predefined classes
which derive from DataObject: DataObjectSimple and
DataObjectComposite.
DataObjectSimple is
the simplest DataObject possible and only holds data in a single format (such
as HTML or text) and DataObjectComposite is
the simplest way to implement a DataObject that does support multiple formats
because it achieves this by simply holding several DataObjectSimple objects.

So, you have several solutions when you need a DataObject 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 | You may use TextDataObject,BitmapDataObject or FileDataObject in the simplest cases when you only needto support one format and your data is either text, bitmap or list of files.
2. Use DataObjectSimple | Deriving from DataObjectSimple is the simplestsolution for custom data – you will only support one format and so probablywon’t be able to communicate with other programs, but data transfer will workin your program (or between different copies of it).
3. Use DataObjectComposite | This is a simple but powerfulsolution which allows you to support any number of formats (eitherstandard or custom if you combine it with the previous solution).
4. Use DataObject directly | This is the solution formaximal flexibility and efficiency, but it is also the most difficult toimplement.

Please note that the easiest way to use drag and drop and the clipboard with
multiple formats is by using DataObjectComposite, but it is not the most
efficient one as each DataObjectSimple 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 DataObject 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, copy 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.

There are several predefined data object classes derived from
DataObjectSimple: FileDataObject,
TextDataObject and
BitmapDataObject which can be used without
change.

You may also derive your own data object classes from
CustomDataObject 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.

## Virtual functions to override

Each class derived directly from DataObject 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
get_format_count.

## Derived from

None

## See also

Clipboard and drag and drop overview,
DnD sample,
FileDataObject,
TextDataObject,
BitmapDataObject,
CustomDataObject,
DropTarget,
DropSource,
TextDropTarget,
FileDropTarget

## Methods

- DataObject.new
- DataObject#get_all_formats
- DataObject#get_data_here
- DataObject#get_data_size
- DataObject#get_format_count
- DataObject#get_preferred_format
- DataObject#set_data

### DataObject.new

### DataObject#get_all_formats

Array get_all_formats( Integer dir = Get)

Should return an Array of DataFormat objects supported
in the given direction by this class.

### DataObject#get_data_here

String get_data_here(%(arg-type)DataFormat% format )

Should return a String containing the object’s data, or `nil` on failure.

### DataObject#get_data_size

Integer get_data_size(%(arg-type)DataFormat% format )

Returns the data size of the given format format; may be overridden if desired.

### DataObject#get_format_count

Integer get_format_count(%(arg-type)Integer% direction = 1)

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

### DataObject#get_preferred_format

DataFormat get_preferred_format(%(arg-type)Integer% direction = 1)

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 DataObject.

By default, the first member of the list returned by `get_all_formats`
is the preferred format, but this method may be overridden.

### DataObject#set_data

Boolean set_data( DataFormat format, String data )

Should store and accept the data data for the format format.

Should returns true on success, false on failure.

[This page automatically generated from the Textile source at 2023-06-09 00:45:32 +0000]