************* wx.DataObject ************* Inheritance diagram for `wx.DataObject`: | .. inheritance-diagram:: wx.DataObject | Description =========== A `wx.DataObject` represents data that can be copied to or from the clipboard, or dragged and dropped. The important thing about `wx.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. 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, wxPython has two predefined classes which derive from `wx.DataObject`: `wx.DataObjectSimple `_ and `wx.DataObjectComposite `_. `wx.DataObjectSimple `_ is the simplest `wx.DataObject` possible and only holds data in a single format (such as HTML or text) and `wx.DataObjectComposite `_ is the simplest way to implement a `wx.DataObject` that does support multiple formats because it achieves this by simply holding several `wx.DataObjectSimple` objects. So, you have several solutions when you need a `wx.DataObject` class (and you need one as soon as you want to transfer data via the clipboard or drag and drop): ====================================================== ==================================== Possible Implementations Pros and Cons ====================================================== ==================================== Use one of the built-in classes You may use `wx.TextDataObject`, `wx.BitmapDataObject` or `wx.FileDataObject` in the simplest cases when you only need to support one format and your data is either text, bitmap or list of files. Use `wx.DataObjectSimple` Deriving from `wx.DataObjectSimple` 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 copies of it). Use `wx.DataObjectComposite` 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). Use `wx.DataObject` This is the solution for maximal 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 `wx.DataObjectComposite`, but it is not the most efficient one as each `wx.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 `wx.DataObject` directly and make it enumerate its formats and provide the data in the requested format on demand. .. note:: 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 `wx.DataObjectSimple`: `wx.FileDataObject `_, `wx.TextDataObject `_ and `wx.BitmapDataObject `_ which can be used without change. You may also derive your own data object classes from `wx.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:: Note that the format string translation under Windows is not yet finished. .. warning:: **wxPython note**: At this time this class is not directly usable from wxPython. Derive a class from `wx.PyDataObjectSimple `_ instead. .. seealso:: `wx.FileDataObject `_, `wx.TextDataObject `_, `wx.BitmapDataObject `_, `wx.CustomDataObject `_, `wx.DropTarget `_, `wx.DropSource `_, `wx.TextDropTarget `_, `wx.FileDropTarget `_ Known Subclasses ^^^^^^^^^^^^^^^^ `wx.BitmapDataObject `_, `wx.CustomDataObject `_, `wx.DataObjectComposite `_, `wx.DataObjectSimple `_, `wx.FileDataObject `_, `wx.MetafileDataObject `_, `wx.PyBitmapDataObject `_, `wx.PyDataObjectSimple `_, `wx.PyTextDataObject `_, `wx.TextDataObject `_, `wx.URLDataObject `_ Methods Summary ^^^^^^^^^^^^^^^ * `__init__ <#__init__>`_ * `GetAllFormats <#GetAllFormats>`_ * `GetDataHere <#GetDataHere>`_ * `GetDataSize <#GetDataSize>`_ * `GetFormatCount <#GetFormatCount>`_ * `GetPreferredFormat <#GetPreferredFormat>`_ * `IsSupported <#IsSupported>`_ * `SetData <#SetData>`_ Properties Summary ^^^^^^^^^^^^^^^^^^ * `AllFormats <#AllFormats>`_ * `DataHere <#DataHere>`_ * `DataSize <#DataSize>`_ * `FormatCount <#FormatCount>`_ * `PreferredFormat <#PreferredFormat>`_ Class API ========= Methods ^^^^^^^ .. method:: __init__() `No docstrings available for this method.` -------- .. method:: GetAllFormats(formats) Copy all supported formats in the given direction to the array pointed to by `formats`. There is enough space for `GetFormatCount <#GetFormatCount>`_ (`dir`) formats in it. **Parameters:** * `formats` (`wx.DataFormat `_) -------- .. method:: GetDataHere(format) The method will write the data of the format `format`. Returns ``None`` on failure. **Parameters:** * `format` (`wx.DataFormat `_) | **Returns:** `string` -------- .. method:: GetDataSize(format) Returns the data size of the given format `format`. **Parameters:** * `format` (`wx.DataFormat `_) | **Returns:** `long` -------- .. method:: GetFormatCount(dir=Get) Returns the number of available formats for rendering or setting the data. **Parameters:** * `dir` (int) | **Returns:** `long` -------- .. method:: GetPreferredFormat(dir=Get) 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 `wx.DataObject`. **Parameters:** * `dir` (int) | **Returns:** `wx.DataFormat `_ -------- .. method:: IsSupported(format, dir=Get) Returns ``True`` if this format is supported. **Parameters:** * `format` (`wx.DataFormat `_) * `dir` (int) | **Returns:** `bool` -------- .. method:: SetData(format, data) Set the data in the format `format` from the bytes in the string `data`. Returns ``True`` on success, ``False`` on failure. **Parameters:** * `format` (`wx.DataFormat `_) * `data` (string) | **Returns:** `bool` -------- Properties ^^^^^^^^^^ .. attribute:: AllFormats See `GetAllFormats <#GetAllFormats>`_ .. attribute:: DataHere See `GetDataHere <#GetDataHere>`_ .. attribute:: DataSize See `GetDataSize <#GetDataSize>`_ .. attribute:: FormatCount See `GetFormatCount <#GetFormatCount>`_ .. attribute:: PreferredFormat See `GetPreferredFormat <#GetPreferredFormat>`_