1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: documentation for wxCustomDataObject class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 @class wxCustomDataObject
13 wxCustomDataObject is a specialization of
14 wxDataObjectSimple for some
15 application-specific data in arbitrary (either custom or one of the standard
16 ones). The only restriction is that it is supposed that this data can be
17 copied bitwise (i.e. with @c memcpy()), so it would be a bad idea to make
18 it contain a C++ object (though C struct is fine).
20 By default, wxCustomDataObject stores the data inside in a buffer. To put the
21 data into the buffer you may use either
22 wxCustomDataObject::SetData or
23 wxCustomDataObject::TakeData depending on whether you want
24 the object to make a copy of data or not.
26 If you already store the data in another place, it may be more convenient and
27 efficient to provide the data on-demand which is possible too if you override
28 the virtual functions mentioned below.
36 class wxCustomDataObject
: public wxDataObjectSimple
40 The constructor accepts a @a format argument which specifies the (single)
41 format supported by this object. If it isn't set here,
42 wxDataObjectSimple::SetFormat should be used.
44 wxCustomDataObject(const wxDataFormat
& format
= wxFormatInvalid
);
47 The destructor will free the data hold by the object. Notice that although it
48 calls a virtual Free() function, the base
49 class version will always be called (C++ doesn't allow calling virtual
50 functions from constructors or destructors), so if you override @c Free(), you
51 should override the destructor in your class as well (which would probably
52 just call the derived class' version of @c Free()).
54 ~wxCustomDataObject();
57 This function is called to allocate @a size bytes of memory from SetData().
58 The default version just uses the operator new.
60 virtual void* Alloc(size_t size
);
63 This function is called when the data is freed, you may override it to anything
64 you want (or may be nothing at all). The default version calls operator
70 Returns a pointer to the data.
72 virtual void* GetData();
75 Returns the data size in bytes.
77 virtual size_t GetSize();
80 Set the data. The data object will make an internal copy.
82 virtual void SetData(size_t size
, const void data
);
85 Like SetData(), but doesn't copy the data -
86 instead the object takes ownership of the pointer.
87 @b wxPython note: This method expects a string in wxPython. You can pass
88 nearly any object by pickling it first.
90 virtual void TakeData(size_t size
, const void data
);
95 @class wxDataObjectComposite
98 wxDataObjectComposite is the simplest
99 wxDataObject derivation which may be used to support
100 multiple formats. It contains several
101 wxDataObjectSimple objects and supports any
102 format supported by at least one of them. Only one of these data objects is
103 @e preferred (the first one if not explicitly changed by using the second
104 parameter of wxDataObjectComposite::Add) and its format determines
105 the preferred format of the composite data object as well.
107 See wxDataObject documentation for the reasons why you
108 might prefer to use wxDataObject directly instead of wxDataObjectComposite for
115 @ref overview_wxdndoverview "Clipboard and drag and drop overview",
116 wxDataObject, wxDataObjectSimple, wxFileDataObject, wxTextDataObject, wxBitmapDataObject
118 class wxDataObjectComposite
: public wxDataObject
122 The default constructor.
124 wxDataObjectComposite();
127 Adds the @a dataObject to the list of supported objects and it becomes the
128 preferred object if @a preferred is @true.
130 void Add(wxDataObjectSimple dataObject
, bool preferred
= false);
133 Report the format passed to the SetData method. This should be the
134 format of the data object within the composite that recieved data from
135 the clipboard or the DnD operation. You can use this method to find
136 out what kind of data object was recieved.
138 wxDataFormat
GetReceivedFormat();
143 @class wxDataObjectSimple
146 This is the simplest possible implementation of the
147 wxDataObject class. The data object of (a class derived
148 from) this class only supports one format, so the number of virtual functions
149 to be implemented is reduced.
151 Notice that this is still an abstract base class and cannot be used but should
154 @b wxPython note: If you wish to create a derived wxDataObjectSimple class in
155 wxPython you should derive the class from wxPyDataObjectSimple
156 in order to get Python-aware capabilities for the various virtual
159 @b wxPerl note: In wxPerl, you need to derive your data object class
160 from Wx::PlDataObjectSimple.
166 @ref overview_wxdndoverview "Clipboard and drag and drop overview", @ref
167 overview_samplednd "DnD sample", wxFileDataObject, wxTextDataObject, wxBitmapDataObject
169 class wxDataObjectSimple
: public wxDataObject
173 Constructor accepts the supported format (none by default) which may also be
174 set later with SetFormat().
176 wxDataObjectSimple(const wxDataFormat
& format
= wxFormatInvalid
);
179 Copy the data to the buffer, return @true on success. Must be implemented in the
180 derived class if the object supports rendering its data.
182 virtual bool GetDataHere(void buf
);
185 Gets the size of our data. Must be implemented in the derived class if the
186 object supports rendering its data.
188 virtual size_t GetDataSize();
191 Returns the (one and only one) format supported by this object. It is supposed
192 that the format is supported in both directions.
194 const wxDataFormat
GetFormat();
197 Copy the data from the buffer, return @true on success. Must be implemented in
198 the derived class if the object supports setting its data.
199 @b wxPython note: When implementing this method in wxPython, the data comes
200 as a single string parameter rather than the two shown here.
202 virtual bool SetData(size_t len
, const void buf
);
205 Sets the supported format.
207 void SetFormat(const wxDataFormat
& format
);
212 @class wxBitmapDataObject
215 wxBitmapDataObject is a specialization of wxDataObject for bitmap data. It can
216 be used without change to paste data into the
217 wxClipboard or a wxDropSource. A
218 user may wish to derive a new class from this class for providing a bitmap
219 on-demand in order to minimize memory consumption when offering data in several
220 formats, such as a bitmap and GIF.
222 @b wxPython note: If you wish to create a derived wxBitmapDataObject class in
223 wxPython you should derive the class from wxPyBitmapDataObject
224 in order to get Python-aware capabilities for the various virtual
231 @ref overview_wxdndoverview "Clipboard and drag and drop overview",
232 wxDataObject, wxDataObjectSimple, wxFileDataObject, wxTextDataObject, wxDataObject
234 class wxBitmapDataObject
: public wxDataObjectSimple
238 Constructor, optionally passing a bitmap (otherwise use
241 wxBitmapDataObject(const wxBitmap
& bitmap
= wxNullBitmap
);
244 Returns the bitmap associated with the data object. You may wish to override
245 this method when offering data on-demand, but this is not required by
246 wxWidgets' internals. Use this method to get data in bitmap form from
249 virtual wxBitmap
GetBitmap();
252 Sets the bitmap associated with the data object. This method is called when the
253 data object receives data. Usually there will be no reason to override this
256 virtual void SetBitmap(const wxBitmap
& bitmap
);
264 A wxDataFormat is an encapsulation of a platform-specific format handle which
265 is used by the system for the clipboard and drag and drop operations. The
266 applications are usually only interested in, for example, pasting data from the
267 clipboard only if the data is in a format the program understands and a data
268 format is something which uniquely identifies this format.
270 On the system level, a data format is usually just a number (@c CLIPFORMAT
271 under Windows or @c Atom under X11, for example) and the standard formats
272 are, indeed, just numbers which can be implicitly converted to wxDataFormat.
273 The standard formats are:
280 An invalid format - used as default argument for
281 functions taking a wxDataFormat argument sometimes
287 Text format (wxString)
299 A metafile (wxMetafile, Windows only)
311 An HTML string. This is only valid when passed to wxSetClipboardData
312 when compiled with Visual C++ in non-Unicode mode
316 As mentioned above, these standard formats may be passed to any function taking
317 wxDataFormat argument because wxDataFormat has an implicit conversion from
318 them (or, to be precise from the type @c wxDataFormat::NativeFormat which is
319 the type used by the underlying platform for data formats).
321 Aside the standard formats, the application may also use custom formats which
322 are identified by their names (strings) and not numeric identifiers. Although
323 internally custom format must be created (or @e registered) first, you
324 shouldn't care about it because it is done automatically the first time the
325 wxDataFormat object corresponding to a given format name is created. The only
326 implication of this is that you should avoid having global wxDataFormat objects
327 with non-default constructor because their constructors are executed before the
328 program has time to perform all necessary initialisations and so an attempt to
329 do clipboard format registration at this time will usually lead to a crash!
335 @ref overview_wxdndoverview "Clipboard and drag and drop overview", @ref
336 overview_samplednd "DnD sample", wxDataObject
342 Constructs a data format object for a custom format identified by its name
345 wxDataFormat(const wxChar format
);
348 Returns the name of a custom format (this function will fail for a standard
354 Returns the platform-specific number identifying the format.
356 NativeFormat
GetType();
359 Sets the format to be the custom format identified by the given name.
361 void SetId(const wxChar format
);
364 Sets the format to the given value, which should be one of wxDF_XXX constants.
366 void SetType(NativeFormat format
);
369 Returns @true if the formats are different.
371 bool operator !=(const wxDataFormat
& format
);
374 Returns @true if the formats are equal.
376 bool operator ==(const wxDataFormat
& format
);
381 @class wxURLDataObject
384 wxURLDataObject is a wxDataObject containing an URL
385 and can be used e.g. when you need to put an URL on or retrieve it from the
389 wxTheClipboard-SetData(new wxURLDataObject(url));
396 @ref overview_wxdndoverview "Clipboard and drag and drop overview", wxDataObject
398 class wxURLDataObject
402 Constructor, may be used to initialize the URL. If @a url is empty,
403 SetURL() can be used later.
405 wxURLDataObject(const wxString
& url
= wxEmptyString
);
408 Returns the URL stored by this object, as a string.
413 Sets the URL stored by this object.
415 void SetURL(const wxString
& url
);
423 A wxDataObject represents data that can be copied to or from the clipboard, or
424 dragged and dropped. The important thing about wxDataObject is that this is a
425 'smart' piece of data unlike 'dumb' data containers such as memory
426 buffers or files. Being 'smart' here means that the data object itself should
427 know what data formats it supports and how to render itself in each of
428 its supported formats.
430 A supported format, incidentally, is exactly the format in which the data can
431 be requested from a data object or from which the data object may be set. In
432 the general case, an object may support different formats on 'input' and
433 'output', i.e. it may be able to render itself in a given format but not be
434 created from data on this format or vice versa. wxDataObject defines an
440 Get = 0x01, // format is supported by GetDataHere()
441 Set = 0x02 // format is supported by SetData()
445 which distinguishes between them. See
446 wxDataFormat documentation for more about formats.
448 Not surprisingly, being 'smart' comes at a price of added complexity. This is
449 reasonable for the situations when you really need to support multiple formats,
450 but may be annoying if you only want to do something simple like cut and paste
453 To provide a solution for both cases, wxWidgets has two predefined classes
454 which derive from wxDataObject: wxDataObjectSimple and
455 wxDataObjectComposite.
456 wxDataObjectSimple is
457 the simplest wxDataObject possible and only holds data in a single format (such
458 as HTML or text) and wxDataObjectComposite is
459 the simplest way to implement a wxDataObject that does support multiple formats
460 because it achieves this by simply holding several wxDataObjectSimple objects.
462 So, you have several solutions when you need a wxDataObject class (and you need
463 one as soon as you want to transfer data via the clipboard or drag and drop):
467 @b 1. Use one of the built-in classes
470 You may use wxTextDataObject,
471 wxBitmapDataObject or wxFileDataObject in the simplest cases when you only need
472 to support one format and your data is either text, bitmap or list of files.
475 @b 2. Use wxDataObjectSimple
478 Deriving from wxDataObjectSimple is the simplest
479 solution for custom data - you will only support one format and so probably
480 won't be able to communicate with other programs, but data transfer will work
481 in your program (or between different copies of it).
484 @b 3. Use wxDataObjectComposite
487 This is a simple but powerful
488 solution which allows you to support any number of formats (either
489 standard or custom if you combine it with the previous solution).
492 @b 4. Use wxDataObject directly
495 This is the solution for
496 maximal flexibility and efficiency, but it is also the most difficult to
501 Please note that the easiest way to use drag and drop and the clipboard with
502 multiple formats is by using wxDataObjectComposite, but it is not the most
503 efficient one as each wxDataObjectSimple would contain the whole data in its
504 respective formats. Now imagine that you want to paste 200 pages of text in
505 your proprietary format, as well as Word, RTF, HTML, Unicode and plain text to
506 the clipboard and even today's computers are in trouble. For this case, you
507 will have to derive from wxDataObject directly and make it enumerate its
508 formats and provide the data in the requested format on demand.
510 Note that neither the GTK+ data transfer mechanisms for clipboard and
511 drag and drop, nor OLE data transfer, copy any data until another application
512 actually requests the data. This is in contrast to the 'feel' offered to the
513 user of a program who would normally think that the data resides in the
514 clipboard after having pressed 'Copy' - in reality it is only declared to be
517 There are several predefined data object classes derived from
518 wxDataObjectSimple: wxFileDataObject,
520 wxBitmapDataObject and
522 which can be used without change.
524 You may also derive your own data object classes from
525 wxCustomDataObject for user-defined types. The
526 format of user-defined data is given as a mime-type string literal, such as
527 "application/word" or "image/png". These strings are used as they are under
528 Unix (so far only GTK+) to identify a format and are translated into their
529 Windows equivalent under Win32 (using the OLE IDataObject for data exchange to
530 and from the clipboard and for drag and drop). Note that the format string
531 translation under Windows is not yet finished.
533 @b wxPython note: At this time this class is not directly usable from wxPython.
534 Derive a class from wxPyDataObjectSimple
537 @b wxPerl note: This class is not currently usable from wxPerl; you may
538 use Wx::PlDataObjectSimple instead.
544 @ref overview_wxdndoverview "Clipboard and drag and drop overview", @ref
545 overview_samplednd "DnD sample", wxFileDataObject, wxTextDataObject, wxBitmapDataObject, wxCustomDataObject, wxDropTarget, wxDropSource, wxTextDropTarget, wxFileDropTarget
561 Copy all supported formats in the given direction to the array pointed to by
562 @e formats. There is enough space for GetFormatCount(dir) formats in it.
564 virtual void GetAllFormats(wxDataFormat
* formats
,
565 Direction dir
= Get
);
568 The method will write the data of the format @a format in the buffer @a buf and
569 return @true on success, @false on failure.
571 virtual bool GetDataHere(const wxDataFormat
& format
, void buf
);
574 Returns the data size of the given format @e format.
576 virtual size_t GetDataSize(const wxDataFormat
& format
);
579 Returns the number of available formats for rendering or setting the data.
581 virtual size_t GetFormatCount(Direction dir
= Get
);
584 Returns the preferred format for either rendering the data (if @a dir is @c Get,
585 its default value) or for setting it. Usually this will be the
586 native format of the wxDataObject.
588 virtual wxDataFormat
GetPreferredFormat(Direction dir
= Get
);
591 Set the data in the format @a format of the length @a len provided in the
593 Returns @true on success, @false on failure.
595 virtual bool SetData(const wxDataFormat
& format
, size_t len
,
601 @class wxTextDataObject
604 wxTextDataObject is a specialization of wxDataObject for text data. It can be
605 used without change to paste data into the wxClipboard
606 or a wxDropSource. A user may wish to derive a new
607 class from this class for providing text on-demand in order to minimize memory
608 consumption when offering data in several formats, such as plain text and RTF
609 because by default the text is stored in a string in this class, but it might
610 as well be generated when requested. For this,
611 wxTextDataObject::GetTextLength and
612 wxTextDataObject::GetText will have to be overridden.
614 Note that if you already have the text inside a string, you will not achieve
615 any efficiency gain by overriding these functions because copying wxStrings is
616 already a very efficient operation (data is not actually copied because
617 wxStrings are reference counted).
619 @b wxPython note: If you wish to create a derived wxTextDataObject class in
620 wxPython you should derive the class from wxPyTextDataObject
621 in order to get Python-aware capabilities for the various virtual
628 @ref overview_wxdndoverview "Clipboard and drag and drop overview",
629 wxDataObject, wxDataObjectSimple, wxFileDataObject, wxBitmapDataObject
631 class wxTextDataObject
: public wxDataObjectSimple
635 Constructor, may be used to initialise the text (otherwise
636 SetText() should be used later).
638 wxTextDataObject(const wxString
& text
= wxEmptyString
);
641 Returns the text associated with the data object. You may wish to override
642 this method when offering data on-demand, but this is not required by
643 wxWidgets' internals. Use this method to get data in text form from
646 virtual wxString
GetText();
649 Returns the data size. By default, returns the size of the text data
650 set in the constructor or using SetText().
651 This can be overridden to provide text size data on-demand. It is recommended
652 to return the text length plus 1 for a trailing zero, but this is not
655 virtual size_t GetTextLength();
658 Sets the text associated with the data object. This method is called
659 when the data object receives the data and, by default, copies the text into
660 the member variable. If you want to process the text on the fly you may wish to
661 override this function.
663 virtual void SetText(const wxString
& strText
);
668 @class wxFileDataObject
671 wxFileDataObject is a specialization of wxDataObject
672 for file names. The program works with it just as if it were a list of absolute
674 names, but internally it uses the same format as
675 Explorer and other compatible programs under Windows or GNOME/KDE filemanager
676 under Unix which makes it possible to receive files from them using this
679 @b Warning: Under all non-Windows platforms this class is currently
680 "input-only", i.e. you can receive the files from another application, but
681 copying (or dragging) file(s) from a wxWidgets application is not currently
682 supported. PS: GTK2 should work as well.
688 wxDataObject, wxDataObjectSimple, wxTextDataObject, wxBitmapDataObject,
691 class wxFileDataObject
: public wxDataObjectSimple
700 @b MSW only: adds a file to the file list represented by this data object.
702 virtual void AddFile(const wxString
& file
);
705 Returns the array of file names.
707 const wxArrayString
GetFilenames();