1
/////////////////////////////////////////////////////////////////////////////
3
// Purpose: interface of wx*DataObject
4
// Author: wxWidgets team
5
// RCS-ID: $Id: dataobj.h 71610 2012-05-30 19:21:42Z RD $
6
// Licence: wxWindows licence
7
/////////////////////////////////////////////////////////////////////////////
13
A wxDataFormat is an encapsulation of a platform-specific format handle
14
which is used by the system for the clipboard and drag and drop operations.
15
The applications are usually only interested in, for example, pasting data
16
from the clipboard only if the data is in a format the program understands
17
and a data format is something which uniquely identifies this format.
19
On the system level, a data format is usually just a number (@c CLIPFORMAT
20
under Windows or @c Atom under X11, for example) and the standard formats
21
are, indeed, just numbers which can be implicitly converted to wxDataFormat.
22
The standard formats are:
25
@itemdef{wxDF_INVALID,
26
An invalid format - used as default argument for functions taking
27
a wxDataFormat argument sometimes.}
29
Text format (wxString).}
32
@itemdef{wxDF_METAFILE,
33
A metafile (wxMetafile, Windows only).}
34
@itemdef{wxDF_FILENAME,
37
An HTML string. This is currently only valid on Mac and MSW.}
40
As mentioned above, these standard formats may be passed to any function
41
taking wxDataFormat argument because wxDataFormat has an implicit
42
conversion from them (or, to be precise from the type
43
@c wxDataFormat::NativeFormat which is the type used by the underlying
44
platform for data formats).
46
Aside the standard formats, the application may also use custom formats
47
which are identified by their names (strings) and not numeric identifiers.
48
Although internally custom format must be created (or @e registered) first,
49
you shouldn't care about it because it is done automatically the first time
50
the wxDataFormat object corresponding to a given format name is created.
51
The only implication of this is that you should avoid having global
52
wxDataFormat objects with non-default constructor because their
53
constructors are executed before the program has time to perform all
54
necessary initialisations and so an attempt to do clipboard format
55
registration at this time will usually lead to a crash!
60
@see @ref overview_dnd, @ref page_samples_dnd, wxDataObject
66
Constructs a data format object for one of the standard data formats or
67
an empty data object (use SetType() or SetId() later in this case).
70
In wxPerl use Wx::Bitmap->newNative(format).
73
wxDataFormat(wxDataFormatId format = wxDF_INVALID);
76
Constructs a data format object for a custom format identified by its
80
In wxPerl use Wx::Bitmap->newUser(format).
83
wxDataFormat(const wxString& format);
86
Returns the name of a custom format (this function will fail for a
89
wxString GetId() const;
92
Returns the platform-specific number identifying the format.
94
wxDataFormatId GetType() const;
97
Sets the format to be the custom format identified by the given name.
99
void SetId(const wxString& format);
102
Sets the format to the given value, which should be one of wxDF_XXX
105
void SetType(wxDataFormatId type);
108
Returns @true if the formats are different.
110
bool operator !=(const wxDataFormat& format) const;
113
Returns @true if the formats are different.
115
bool operator !=(wxDataFormatId format) const;
118
Returns @true if the formats are equal.
120
bool operator ==(const wxDataFormat& format) const;
123
Returns @true if the formats are equal.
125
bool operator ==(wxDataFormatId format) const;
129
const wxDataFormat wxFormatInvalid;
135
A wxDataObject represents data that can be copied to or from the clipboard,
136
or dragged and dropped. The important thing about wxDataObject is that this
137
is a 'smart' piece of data unlike 'dumb' data containers such as memory
138
buffers or files. Being 'smart' here means that the data object itself
139
should know what data formats it supports and how to render itself in each
140
of its supported formats.
142
A supported format, incidentally, is exactly the format in which the data
143
can be requested from a data object or from which the data object may be
144
set. In the general case, an object may support different formats on
145
'input' and 'output', i.e. it may be able to render itself in a given
146
format but not be created from data on this format or vice versa.
147
wxDataObject defines the wxDataObject::Direction enumeration type which
148
distinguishes between them.
150
See wxDataFormat documentation for more about formats.
152
Not surprisingly, being 'smart' comes at a price of added complexity. This
153
is reasonable for the situations when you really need to support multiple
154
formats, but may be annoying if you only want to do something simple like
157
To provide a solution for both cases, wxWidgets has two predefined classes
158
which derive from wxDataObject: wxDataObjectSimple and
159
wxDataObjectComposite. wxDataObjectSimple is the simplest wxDataObject
160
possible and only holds data in a single format (such as HTML or text) and
161
wxDataObjectComposite is the simplest way to implement a wxDataObject that
162
does support multiple formats because it achieves this by simply holding
163
several wxDataObjectSimple objects.
165
So, you have several solutions when you need a wxDataObject class (and you
166
need one as soon as you want to transfer data via the clipboard or drag and
169
-# Use one of the built-in classes.
170
- You may use wxTextDataObject, wxBitmapDataObject wxFileDataObject,
171
wxURLDataObject in the simplest cases when you only need to support
172
one format and your data is either text, bitmap or list of files.
173
-# Use wxDataObjectSimple
174
- Deriving from wxDataObjectSimple is the simplest solution for custom
175
data - you will only support one format and so probably won't be able
176
to communicate with other programs, but data transfer will work in
177
your program (or between different instances of it).
178
-# Use wxDataObjectComposite
179
- This is a simple but powerful solution which allows you to support
180
any number of formats (either standard or custom if you combine it
181
with the previous solution).
182
-# Use wxDataObject directly
183
- This is the solution for maximum flexibility and efficiency, but it
184
is also the most difficult to implement.
186
Please note that the easiest way to use drag and drop and the clipboard
187
with multiple formats is by using wxDataObjectComposite, but it is not the
188
most efficient one as each wxDataObjectSimple would contain the whole data
189
in its respective formats. Now imagine that you want to paste 200 pages of
190
text in your proprietary format, as well as Word, RTF, HTML, Unicode and
191
plain text to the clipboard and even today's computers are in trouble. For
192
this case, you will have to derive from wxDataObject directly and make it
193
enumerate its formats and provide the data in the requested format on
196
Note that neither the GTK+ data transfer mechanisms for clipboard and drag
197
and drop, nor OLE data transfer, @e copies any data until another application
198
actually requests the data. This is in contrast to the 'feel' offered to
199
the user of a program who would normally think that the data resides in the
200
clipboard after having pressed 'Copy' - in reality it is only declared to
203
You may also derive your own data object classes from wxCustomDataObject
204
for user-defined types. The format of user-defined data is given as a
205
mime-type string literal, such as "application/word" or "image/png". These
206
strings are used as they are under Unix (so far only GTK+) to identify a
207
format and are translated into their Windows equivalent under Win32 (using
208
the OLE IDataObject for data exchange to and from the clipboard and for
209
drag and drop). Note that the format string translation under Windows is
212
Each class derived directly from wxDataObject must override and implement
213
all of its functions which are pure virtual in the base class. The data
214
objects which only render their data or only set it (i.e. work in only one
215
direction), should return 0 from GetFormatCount().
218
This class is not currently usable from wxPerl; you may use
219
Wx::PlDataObjectSimple instead.
225
@see @ref overview_dnd, @ref page_samples_dnd, wxFileDataObject,
226
wxTextDataObject, wxBitmapDataObject, wxCustomDataObject,
227
wxDropTarget, wxDropSource, wxTextDropTarget, wxFileDropTarget
234
/** Format is supported by GetDataHere() */
237
/** Format is supported by SetData() */
241
Format is supported by both GetDataHere() and SetData()
255
virtual ~wxDataObject();
258
Copies all formats supported in the given direction @a dir to the array
259
pointed to by @a formats.
260
There must be enough space for GetFormatCount(dir) formats in it.
263
In wxPerl this method only takes the @a dir parameter. In scalar
264
context it returns the first format in the list, in list
265
context it returns a list containing all the supported
269
virtual void GetAllFormats(wxDataFormat* formats,
270
Direction dir = Get) const = 0;
273
The method will write the data of the format @a format to the buffer
274
@a buf. In other words, copy the data from this object in the given
275
format to the supplied buffer. Returns @true on success, @false on
278
virtual bool GetDataHere(const wxDataFormat& format, void* buf) const = 0;
281
Returns the data size of the given format @a format.
283
virtual size_t GetDataSize(const wxDataFormat& format) const = 0;
286
Returns the number of available formats for rendering or setting the
289
virtual size_t GetFormatCount(Direction dir = Get) const = 0;
292
Returns the preferred format for either rendering the data (if @a dir
293
is @c Get, its default value) or for setting it. Usually this will be
294
the native format of the wxDataObject.
296
virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const = 0;
299
Set the data in the format @a format of the length @a len provided in
300
the buffer @a buf. In other words, copy length bytes of data from the
301
buffer to this data object.
304
The format for which to set the data.
306
The size of data in bytes.
308
Non-@NULL pointer to the data.
310
@true on success, @false on failure.
312
virtual bool SetData(const wxDataFormat& format, size_t len, const void* buf);
315
Returns true if this format is supported.
317
bool IsSupported(const wxDataFormat& format, Direction dir = Get) const;
322
@class wxCustomDataObject
324
wxCustomDataObject is a specialization of wxDataObjectSimple for some
325
application-specific data in arbitrary (either custom or one of the
326
standard ones). The only restriction is that it is supposed that this data
327
can be copied bitwise (i.e. with @c memcpy()), so it would be a bad idea to
328
make it contain a C++ object (though C struct is fine).
330
By default, wxCustomDataObject stores the data inside in a buffer. To put
331
the data into the buffer you may use either SetData() or TakeData()
332
depending on whether you want the object to make a copy of data or not.
334
This class may be used as is, but if you don't want store the data inside
335
the object but provide it on demand instead, you should override GetSize(),
336
GetData() and SetData() (or may be only the first two or only the last one
337
if you only allow reading/writing the data).
344
class wxCustomDataObject : public wxDataObjectSimple
348
The constructor accepts a @a format argument which specifies the
349
(single) format supported by this object. If it isn't set here,
350
wxDataObjectSimple::SetFormat() should be used.
352
wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
355
The destructor will free the data held by the object. Notice that
356
although it calls the virtual Free() function, the base class version
357
will always be called (C++ doesn't allow calling virtual functions from
358
constructors or destructors), so if you override Free(), you should
359
override the destructor in your class as well (which would probably
360
just call the derived class' version of Free()).
362
virtual ~wxCustomDataObject();
365
This function is called to allocate @a size bytes of memory from
366
SetData(). The default version just uses the operator new.
368
virtual void* Alloc(size_t size);
371
This function is called when the data is freed, you may override it to
372
anything you want (or may be nothing at all). The default version calls
373
operator delete[] on the data.
378
Returns a pointer to the data.
380
virtual void* GetData() const;
383
Returns the data size in bytes.
385
virtual size_t GetSize() const;
388
Set the data. The data object will make an internal copy.
390
virtual bool SetData(size_t size, const void* data);
393
Like SetData(), but doesn't copy the data - instead the object takes
394
ownership of the pointer.
396
void TakeData(size_t size, void* data);
402
@class wxDataObjectComposite
404
wxDataObjectComposite is the simplest wxDataObject derivation which may be
405
used to support multiple formats. It contains several wxDataObjectSimple
406
objects and supports any format supported by at least one of them. Only one
407
of these data objects is @e preferred (the first one if not explicitly
408
changed by using the second parameter of Add()) and its format determines
409
the preferred format of the composite data object as well.
411
See wxDataObject documentation for the reasons why you might prefer to use
412
wxDataObject directly instead of wxDataObjectComposite for efficiency
415
This example shows how a composite data object capable of storing either
416
bitmaps or file names (presumably of bitmap files) can be initialized and
420
MyDropTarget::MyDropTarget()
422
wxDataObjectComposite* dataobj = new wxDataObjectComposite();
423
dataobj->Add(new wxBitmapDataObject(), true);
424
dataobj->Add(new wxFileDataObject());
425
SetDataObject(dataobj);
428
wxDragResult MyDropTarget::OnData(wxCoord x, wxCoord y,
429
wxDragResult defaultDragResult)
431
wxDragResult dragResult = wxDropTarget::OnData(x, y, defaultDragResult);
432
if ( dragResult == defaultDragResult )
434
wxDataObjectComposite *
435
dataobjComp = static_cast<wxDataObjectComposite *>(GetDataObject());
437
wxDataFormat format = dataObjects->GetReceivedFormat();
438
wxDataObject *dataobj = dataobjComp->GetObject(format);
439
switch ( format.GetType() )
444
dataobjBitmap = static_cast<wxBitmapDataObject *>(dataobj);
446
... use dataobj->GetBitmap() ...
453
dataobjFile = static_cast<wxFileDataObject *>(dataobj);
455
... use dataobj->GetFilenames() ...
460
wxFAIL_MSG( "unexpected data object format" );
471
@see @ref overview_dnd, wxDataObject, wxDataObjectSimple, wxFileDataObject,
472
wxTextDataObject, wxBitmapDataObject
474
class wxDataObjectComposite : public wxDataObject
478
The default constructor.
480
wxDataObjectComposite();
483
Adds the @a dataObject to the list of supported objects and it becomes
484
the preferred object if @a preferred is @true.
486
void Add(wxDataObjectSimple* dataObject, bool preferred = false);
489
Report the format passed to the SetData() method. This should be the
490
format of the data object within the composite that received data from
491
the clipboard or the DnD operation. You can use this method to find
492
out what kind of data object was received.
494
wxDataFormat GetReceivedFormat() const;
497
Returns the pointer to the object which supports the passed format for
498
the specified direction.
500
@NULL is returned if the specified @a format is not supported for this
501
direction @a dir. The returned pointer is owned by wxDataObjectComposite
502
itself and shouldn't be deleted by caller.
506
wxDataObjectSimple *GetObject(const wxDataFormat& format,
507
wxDataObject::Direction dir = wxDataObject::Get) const;
513
@class wxDataObjectSimple
515
This is the simplest possible implementation of the wxDataObject class.
516
The data object of (a class derived from) this class only supports
517
<strong>one format</strong>, so the number of virtual functions to
518
be implemented is reduced.
520
Notice that this is still an abstract base class and cannot be used
521
directly, it must be derived. The objects supporting rendering the data
522
must override GetDataSize() and GetDataHere() while the objects which may
523
be set must override SetData(). Of course, the objects supporting both
524
operations must override all three methods.
527
In wxPerl, you need to derive your data object class from
528
Wx::PlDataObjectSimple.
534
@see @ref overview_dnd, @ref page_samples_dnd, wxFileDataObject,
535
wxTextDataObject, wxBitmapDataObject
537
class wxDataObjectSimple : public wxDataObject
541
Constructor accepts the supported format (none by default) which may
542
also be set later with SetFormat().
544
wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid);
547
Copy the data to the buffer, return @true on success.
548
Must be implemented in the derived class if the object supports rendering
551
virtual bool GetDataHere(void* buf) const;
554
Gets the size of our data. Must be implemented in the derived class if
555
the object supports rendering its data.
557
virtual size_t GetDataSize() const;
560
Returns the (one and only one) format supported by this object.
561
It is assumed that the format is supported in both directions.
563
const wxDataFormat& GetFormat() const;
566
Copy the data from the buffer, return @true on success.
567
Must be implemented in the derived class if the object supports setting
570
virtual bool SetData(size_t len, const void* buf);
573
Sets the supported format.
575
void SetFormat(const wxDataFormat& format);
581
@class wxBitmapDataObject
583
wxBitmapDataObject is a specialization of wxDataObject for bitmap data. It
584
can be used without change to paste data into the wxClipboard or a
585
wxDropSource. A user may wish to derive a new class from this class for
586
providing a bitmap on-demand in order to minimize memory consumption when
587
offering data in several formats, such as a bitmap and GIF.
589
This class may be used as is, but GetBitmap() may be overridden to increase
595
@see @ref overview_dnd, wxDataObject, wxDataObjectSimple, wxFileDataObject,
596
wxTextDataObject, wxDataObject
598
class wxBitmapDataObject : public wxDataObjectSimple
602
Constructor, optionally passing a bitmap (otherwise use SetBitmap()
605
wxBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
608
Returns the bitmap associated with the data object. You may wish to
609
override this method when offering data on-demand, but this is not
610
required by wxWidgets' internals. Use this method to get data in bitmap
611
form from the wxClipboard.
613
virtual wxBitmap GetBitmap() const;
616
Sets the bitmap associated with the data object. This method is called
617
when the data object receives data. Usually there will be no reason to
618
override this function.
620
virtual void SetBitmap(const wxBitmap& bitmap);
626
@class wxURLDataObject
628
wxURLDataObject is a wxDataObject containing an URL and can be used e.g.
629
when you need to put an URL on or retrieve it from the clipboard:
632
wxTheClipboard->SetData(new wxURLDataObject(url));
635
@note This class is derived from wxDataObjectComposite on Windows rather
636
than wxTextDataObject on all other platforms.
641
@see @ref overview_dnd, wxDataObject
643
class wxURLDataObject: public wxTextDataObject
647
Constructor, may be used to initialize the URL. If @a url is empty,
648
SetURL() can be used later.
650
wxURLDataObject(const wxString& url = wxEmptyString);
653
Returns the URL stored by this object, as a string.
655
wxString GetURL() const;
658
Sets the URL stored by this object.
660
void SetURL(const wxString& url);
665
@class wxTextDataObject
667
wxTextDataObject is a specialization of wxDataObjectSimple for text data.
668
It can be used without change to paste data into the wxClipboard or a
669
wxDropSource. A user may wish to derive a new class from this class for
670
providing text on-demand in order to minimize memory consumption when
671
offering data in several formats, such as plain text and RTF because by
672
default the text is stored in a string in this class, but it might as well
673
be generated when requested. For this, GetTextLength() and GetText() will
674
have to be overridden.
676
Note that if you already have the text inside a string, you will not
677
achieve any efficiency gain by overriding these functions because copying
678
wxStrings is already a very efficient operation (data is not actually
679
copied because wxStrings are reference counted).
684
@see @ref overview_dnd, wxDataObject, wxDataObjectSimple, wxFileDataObject,
687
class wxTextDataObject : public wxDataObjectSimple
691
Constructor, may be used to initialise the text (otherwise SetText()
692
should be used later).
694
wxTextDataObject(const wxString& text = wxEmptyString);
697
Returns the text associated with the data object. You may wish to
698
override this method when offering data on-demand, but this is not
699
required by wxWidgets' internals. Use this method to get data in text
700
form from the wxClipboard.
702
virtual wxString GetText() const;
705
Returns the data size. By default, returns the size of the text data
706
set in the constructor or using SetText(). This can be overridden to
707
provide text size data on-demand. It is recommended to return the text
708
length plus 1 for a trailing zero, but this is not strictly required.
710
virtual size_t GetTextLength() const;
713
Returns 2 under wxMac and wxGTK, where text data coming from the
714
clipboard may be provided as ANSI (@c wxDF_TEXT) or as Unicode text
715
(@c wxDF_UNICODETEXT, but only when @c wxUSE_UNICODE==1).
717
Returns 1 under other platforms (e.g. wxMSW) or when building in ANSI mode
718
(@c wxUSE_UNICODE==0).
720
virtual size_t GetFormatCount(wxDataObject::Direction dir = wxDataObject::Get) const;
723
Returns the preferred format supported by this object.
725
This is @c wxDF_TEXT or @c wxDF_UNICODETEXT depending on the platform
726
and from the build mode (i.e. from @c wxUSE_UNICODE).
728
const wxDataFormat& GetFormat() const;
731
Returns all the formats supported by wxTextDataObject.
733
Under wxMac and wxGTK they are @c wxDF_TEXT and @c wxDF_UNICODETEXT,
734
under other ports returns only one of the two, depending on the build mode.
736
virtual void GetAllFormats(wxDataFormat* formats,
737
wxDataObject::Direction dir = wxDataObject::Get) const;
740
Sets the text associated with the data object. This method is called
741
when the data object receives the data and, by default, copies the text
742
into the member variable. If you want to process the text on the fly
743
you may wish to override this function.
745
virtual void SetText(const wxString& strText);
751
@class wxFileDataObject
753
wxFileDataObject is a specialization of wxDataObject for file names. The
754
program works with it just as if it were a list of absolute file names, but
755
internally it uses the same format as Explorer and other compatible
756
programs under Windows or GNOME/KDE filemanager under Unix which makes it
757
possible to receive files from them using this class.
759
@warning Under all non-Windows platforms this class is currently
760
"input-only", i.e. you can receive the files from another
761
application, but copying (or dragging) file(s) from a wxWidgets
762
application is not currently supported. PS: GTK2 should work as
768
@see wxDataObject, wxDataObjectSimple, wxTextDataObject,
769
wxBitmapDataObject, wxDataObject
771
class wxFileDataObject : public wxDataObjectSimple
780
Adds a file to the file list represented by this data object (Windows only).
782
void AddFile(const wxString& file);
785
Returns the array of file names.
787
const wxArrayString& GetFilenames() const;
791
@class wxHTMLDataObject
793
wxHTMLDataObject is used for working with HTML-formatted text.
798
@see wxDataObject, wxDataObjectSimple
800
class wxHTMLDataObject : public wxDataObjectSimple
806
wxHTMLDataObject(const wxString& html = wxEmptyString);
809
Returns the HTML string.
811
virtual wxString GetHTML() const;
814
Sets the HTML string.
816
virtual void SetHTML(const wxString& html);