]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/dataobj.h
add wxVector(size_t size[, const value_type& value]) ctors
[wxWidgets.git] / interface / wx / dataobj.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dataobj.h
3 // Purpose: interface of wx*DataObject
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxCustomDataObject
11
12 wxCustomDataObject is a specialization of wxDataObjectSimple for some
13 application-specific data in arbitrary (either custom or one of the
14 standard ones). The only restriction is that it is supposed that this data
15 can be copied bitwise (i.e. with @c memcpy()), so it would be a bad idea to
16 make it contain a C++ object (though C struct is fine).
17
18 By default, wxCustomDataObject stores the data inside in a buffer. To put
19 the data into the buffer you may use either SetData() or TakeData()
20 depending on whether you want the object to make a copy of data or not.
21
22 This class may be used as is, but if you don't want store the data inside
23 the object but provide it on demand instead, you should override GetSize(),
24 GetData() and SetData() (or may be only the first two or only the last one
25 if you only allow reading/writing the data).
26
27 @library{wxcore}
28 @category{dnd}
29
30 @see wxDataObject
31 */
32 class wxCustomDataObject : public wxDataObjectSimple
33 {
34 public:
35 /**
36 The constructor accepts a @a format argument which specifies the
37 (single) format supported by this object. If it isn't set here,
38 wxDataObjectSimple::SetFormat() should be used.
39 */
40 wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
41
42 /**
43 The destructor will free the data held by the object. Notice that
44 although it calls the virtual Free() function, the base class version
45 will always be called (C++ doesn't allow calling virtual functions from
46 constructors or destructors), so if you override Free(), you should
47 override the destructor in your class as well (which would probably
48 just call the derived class' version of Free()).
49 */
50 virtual ~wxCustomDataObject();
51
52 /**
53 This function is called to allocate @a size bytes of memory from
54 SetData(). The default version just uses the operator new.
55 */
56 virtual void* Alloc(size_t size);
57
58 /**
59 This function is called when the data is freed, you may override it to
60 anything you want (or may be nothing at all). The default version calls
61 operator delete[] on the data.
62 */
63 virtual void Free();
64
65 /**
66 Returns a pointer to the data.
67 */
68 virtual void* GetData() const;
69
70 /**
71 Returns the data size in bytes.
72 */
73 virtual size_t GetSize() const;
74
75 /**
76 Set the data. The data object will make an internal copy.
77
78 @beginWxPythonOnly
79 This method expects a string in wxPython. You can pass nearly any
80 object by pickling it first.
81 @endWxPythonOnly
82 */
83 virtual bool SetData(size_t size, const void* data);
84
85 /**
86 Like SetData(), but doesn't copy the data - instead the object takes
87 ownership of the pointer.
88
89 @beginWxPythonOnly
90 This method expects a string in wxPython. You can pass nearly any
91 object by pickling it first.
92 @endWxPythonOnly
93 */
94 void TakeData(size_t size, void* data);
95 };
96
97
98
99 /**
100 @class wxDataObjectComposite
101
102 wxDataObjectComposite is the simplest wxDataObject derivation which may be
103 used to support multiple formats. It contains several wxDataObjectSimple
104 objects and supports any format supported by at least one of them. Only one
105 of these data objects is @e preferred (the first one if not explicitly
106 changed by using the second parameter of Add()) and its format determines
107 the preferred format of the composite data object as well.
108
109 See wxDataObject documentation for the reasons why you might prefer to use
110 wxDataObject directly instead of wxDataObjectComposite for efficiency
111 reasons.
112
113 @library{wxcore}
114 @category{dnd}
115
116 @see @ref overview_dnd, wxDataObject, wxDataObjectSimple, wxFileDataObject,
117 wxTextDataObject, wxBitmapDataObject
118 */
119 class wxDataObjectComposite : public wxDataObject
120 {
121 public:
122 /**
123 The default constructor.
124 */
125 wxDataObjectComposite();
126
127 /**
128 Adds the @a dataObject to the list of supported objects and it becomes
129 the preferred object if @a preferred is @true.
130 */
131 void Add(wxDataObjectSimple* dataObject, bool preferred = false);
132
133 /**
134 Report the format passed to the SetData() method. This should be the
135 format of the data object within the composite that recieved data from
136 the clipboard or the DnD operation. You can use this method to find
137 out what kind of data object was recieved.
138 */
139 wxDataFormat GetReceivedFormat() const;
140 };
141
142
143
144 /**
145 @class wxDataObjectSimple
146
147 This is the simplest possible implementation of the wxDataObject class. The
148 data object of (a class derived from) this class only supports one format,
149 so the number of virtual functions to be implemented is reduced.
150
151 Notice that this is still an abstract base class and cannot be used
152 directly, it must be derived. The objects supporting rendering the data
153 must override GetDataSize() and GetDataHere() while the objects which may
154 be set must override SetData(). Of course, the objects supporting both
155 operations must override all three methods.
156
157 @beginWxPythonOnly
158 If you wish to create a derived wxDataObjectSimple class in wxPython you
159 should derive the class from wxPyDataObjectSimple in order to get
160 Python-aware capabilities for the various virtual methods.
161 @endWxPythonOnly
162
163 @beginWxPerlOnly
164 In wxPerl, you need to derive your data object class from
165 Wx::PlDataObjectSimple.
166 @endWxPerlOnly
167
168 @library{wxcore}
169 @category{dnd}
170
171 @see @ref overview_dnd, @ref page_samples_dnd, wxFileDataObject,
172 wxTextDataObject, wxBitmapDataObject
173 */
174 class wxDataObjectSimple : public wxDataObject
175 {
176 public:
177 /**
178 Constructor accepts the supported format (none by default) which may
179 also be set later with SetFormat().
180 */
181 wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid);
182
183 /**
184 Copy the data to the buffer, return @true on success. Must be
185 implemented in the derived class if the object supports rendering its
186 data.
187
188 @beginWxPythonOnly
189 When implementing this method in wxPython, no additional parameters are
190 required and the data should be returned from the method as a string.
191 @endWxPythonOnly
192 */
193 virtual bool GetDataHere(void* buf) const;
194
195 /**
196 Gets the size of our data. Must be implemented in the derived class if
197 the object supports rendering its data.
198 */
199 virtual size_t GetDataSize() const;
200
201 /**
202 Returns the (one and only one) format supported by this object. It is
203 assumed that the format is supported in both directions.
204 */
205 const wxDataFormat& GetFormat() const;
206
207 /**
208 Copy the data from the buffer, return @true on success. Must be
209 implemented in the derived class if the object supports setting its
210 data.
211
212 @beginWxPythonOnly
213 When implementing this method in wxPython, the data comes as a single
214 string parameter rather than the two shown here.
215 @endWxPythonOnly
216 */
217 virtual bool SetData(size_t len, const void* buf);
218
219 /**
220 Sets the supported format.
221 */
222 void SetFormat(const wxDataFormat& format);
223 };
224
225
226
227 /**
228 @class wxBitmapDataObject
229
230 wxBitmapDataObject is a specialization of wxDataObject for bitmap data. It
231 can be used without change to paste data into the wxClipboard or a
232 wxDropSource. A user may wish to derive a new class from this class for
233 providing a bitmap on-demand in order to minimize memory consumption when
234 offering data in several formats, such as a bitmap and GIF.
235
236 This class may be used as is, but GetBitmap() may be overridden to increase
237 efficiency.
238
239 @beginWxPythonOnly
240 If you wish to create a derived wxBitmapDataObject class in wxPython you
241 should derive the class from wxPyBitmapDataObject in order to get
242 Python-aware capabilities for the various virtual methods.
243 @endWxPythonOnly
244
245 @library{wxcore}
246 @category{dnd}
247
248 @see @ref overview_dnd, wxDataObject, wxDataObjectSimple, wxFileDataObject,
249 wxTextDataObject, wxDataObject
250 */
251 class wxBitmapDataObject : public wxDataObjectSimple
252 {
253 public:
254 /**
255 Constructor, optionally passing a bitmap (otherwise use SetBitmap()
256 later).
257 */
258 wxBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
259
260 /**
261 Returns the bitmap associated with the data object. You may wish to
262 override this method when offering data on-demand, but this is not
263 required by wxWidgets' internals. Use this method to get data in bitmap
264 form from the wxClipboard.
265 */
266 virtual wxBitmap GetBitmap() const;
267
268 /**
269 Sets the bitmap associated with the data object. This method is called
270 when the data object receives data. Usually there will be no reason to
271 override this function.
272 */
273 virtual void SetBitmap(const wxBitmap& bitmap);
274 };
275
276
277
278 /**
279 @class wxURLDataObject
280
281 wxURLDataObject is a wxDataObject containing an URL and can be used e.g.
282 when you need to put an URL on or retrieve it from the clipboard:
283
284 @code
285 wxTheClipboard->SetData(new wxURLDataObject(url));
286 @endcode
287
288 @note This class is derived from wxDataObjectComposite on Windows rather
289 than wxTextDataObject on all other platforms.
290
291 @library{wxcore}
292 @category{dnd}
293
294 @see @ref overview_dnd, wxDataObject
295 */
296 class wxURLDataObject: public wxTextDataObject
297 {
298 public:
299 /**
300 Constructor, may be used to initialize the URL. If @a url is empty,
301 SetURL() can be used later.
302 */
303 wxURLDataObject(const wxString& url = wxEmptyString);
304
305 /**
306 Returns the URL stored by this object, as a string.
307 */
308 wxString GetURL() const;
309
310 /**
311 Sets the URL stored by this object.
312 */
313 void SetURL(const wxString& url);
314 };
315
316
317 /**
318 @class wxTextDataObject
319
320 wxTextDataObject is a specialization of wxDataObject for text data. It can
321 be used without change to paste data into the wxClipboard or a
322 wxDropSource. A user may wish to derive a new class from this class for
323 providing text on-demand in order to minimize memory consumption when
324 offering data in several formats, such as plain text and RTF because by
325 default the text is stored in a string in this class, but it might as well
326 be generated when requested. For this, GetTextLength() and GetText() will
327 have to be overridden.
328
329 Note that if you already have the text inside a string, you will not
330 achieve any efficiency gain by overriding these functions because copying
331 wxStrings is already a very efficient operation (data is not actually
332 copied because wxStrings are reference counted).
333
334 @beginWxPythonOnly
335 If you wish to create a derived wxTextDataObject class in wxPython you
336 should derive the class from wxPyTextDataObject in order to get
337 Python-aware capabilities for the various virtual methods.
338 @endWxPythonOnly
339
340 @library{wxcore}
341 @category{dnd}
342
343 @see @ref overview_dnd, wxDataObject, wxDataObjectSimple, wxFileDataObject,
344 wxBitmapDataObject
345 */
346 class wxTextDataObject : public wxDataObjectSimple
347 {
348 public:
349 /**
350 Constructor, may be used to initialise the text (otherwise SetText()
351 should be used later).
352 */
353 wxTextDataObject(const wxString& text = wxEmptyString);
354
355 /**
356 Returns the text associated with the data object. You may wish to
357 override this method when offering data on-demand, but this is not
358 required by wxWidgets' internals. Use this method to get data in text
359 form from the wxClipboard.
360 */
361 virtual wxString GetText() const;
362
363 /**
364 Returns the data size. By default, returns the size of the text data
365 set in the constructor or using SetText(). This can be overridden to
366 provide text size data on-demand. It is recommended to return the text
367 length plus 1 for a trailing zero, but this is not strictly required.
368 */
369 virtual size_t GetTextLength() const;
370
371 /**
372 Sets the text associated with the data object. This method is called
373 when the data object receives the data and, by default, copies the text
374 into the member variable. If you want to process the text on the fly
375 you may wish to override this function.
376 */
377 virtual void SetText(const wxString& strText);
378 };
379
380
381
382 /**
383 @class wxFileDataObject
384
385 wxFileDataObject is a specialization of wxDataObject for file names. The
386 program works with it just as if it were a list of absolute file names, but
387 internally it uses the same format as Explorer and other compatible
388 programs under Windows or GNOME/KDE filemanager under Unix which makes it
389 possible to receive files from them using this class.
390
391 @warning Under all non-Windows platforms this class is currently
392 "input-only", i.e. you can receive the files from another
393 application, but copying (or dragging) file(s) from a wxWidgets
394 application is not currently supported. PS: GTK2 should work as
395 well.
396
397 @library{wxcore}
398 @category{dnd}
399
400 @see wxDataObject, wxDataObjectSimple, wxTextDataObject,
401 wxBitmapDataObject, wxDataObject
402 */
403 class wxFileDataObject : public wxDataObjectSimple
404 {
405 public:
406 /**
407 Constructor.
408 */
409 wxFileDataObject();
410
411 /**
412 Adds a file to the file list represented by this data object (Windows only).
413 */
414 void AddFile(const wxString& file);
415
416 /**
417 Returns the array of file names.
418 */
419 const wxArrayString& GetFilenames() const;
420 };
421
422
423
424 /**
425 @class wxDataFormat
426
427 A wxDataFormat is an encapsulation of a platform-specific format handle
428 which is used by the system for the clipboard and drag and drop operations.
429 The applications are usually only interested in, for example, pasting data
430 from the clipboard only if the data is in a format the program understands
431 and a data format is something which uniquely identifies this format.
432
433 On the system level, a data format is usually just a number (@c CLIPFORMAT
434 under Windows or @c Atom under X11, for example) and the standard formats
435 are, indeed, just numbers which can be implicitly converted to wxDataFormat.
436 The standard formats are:
437
438 @beginDefList
439 @itemdef{wxDF_INVALID,
440 An invalid format - used as default argument for functions taking
441 a wxDataFormat argument sometimes.}
442 @itemdef{wxDF_TEXT,
443 Text format (wxString).}
444 @itemdef{wxDF_BITMAP,
445 A bitmap (wxBitmap).}
446 @itemdef{wxDF_METAFILE,
447 A metafile (wxMetafile, Windows only).}
448 @itemdef{wxDF_FILENAME,
449 A list of filenames.}
450 @itemdef{wxDF_HTML,
451 An HTML string. This is only valid when passed to
452 wxSetClipboardData when compiled with Visual C++ in non-Unicode
453 mode.}
454 @endDefList
455
456 As mentioned above, these standard formats may be passed to any function
457 taking wxDataFormat argument because wxDataFormat has an implicit
458 conversion from them (or, to be precise from the type
459 @c wxDataFormat::NativeFormat which is the type used by the underlying
460 platform for data formats).
461
462 Aside the standard formats, the application may also use custom formats
463 which are identified by their names (strings) and not numeric identifiers.
464 Although internally custom format must be created (or @e registered) first,
465 you shouldn't care about it because it is done automatically the first time
466 the wxDataFormat object corresponding to a given format name is created.
467 The only implication of this is that you should avoid having global
468 wxDataFormat objects with non-default constructor because their
469 constructors are executed before the program has time to perform all
470 necessary initialisations and so an attempt to do clipboard format
471 registration at this time will usually lead to a crash!
472
473 @library{wxbase}
474 @category{dnd}
475
476 @see @ref overview_dnd, @ref page_samples_dnd, wxDataObject
477 */
478 class wxDataFormat
479 {
480 public:
481 /**
482 Constructs a data format object for one of the standard data formats or
483 an empty data object (use SetType() or SetId() later in this case).
484 */
485 wxDataFormat(wxDataFormatId format = wxDF_INVALID);
486
487 /**
488 Constructs a data format object for a custom format identified by its
489 name @a format.
490 */
491 wxDataFormat(const wxString& format);
492
493 /**
494 Returns the name of a custom format (this function will fail for a
495 standard format).
496 */
497 wxString GetId() const;
498
499 /**
500 Returns the platform-specific number identifying the format.
501 */
502 wxDataFormatId GetType() const;
503
504 /**
505 Sets the format to be the custom format identified by the given name.
506 */
507 void SetId(const wxString& format);
508
509 /**
510 Sets the format to the given value, which should be one of wxDF_XXX
511 constants.
512 */
513 void SetType(wxDataFormatId type);
514
515 /**
516 Returns @true if the formats are different.
517 */
518 bool operator !=(wxDataFormatId format) const;
519
520 /**
521 Returns @true if the formats are equal.
522 */
523 bool operator ==(wxDataFormatId format) const;
524 };
525
526
527
528 /**
529 @class wxDataObject
530
531 A wxDataObject represents data that can be copied to or from the clipboard,
532 or dragged and dropped. The important thing about wxDataObject is that this
533 is a 'smart' piece of data unlike 'dumb' data containers such as memory
534 buffers or files. Being 'smart' here means that the data object itself
535 should know what data formats it supports and how to render itself in each
536 of its supported formats.
537
538 A supported format, incidentally, is exactly the format in which the data
539 can be requested from a data object or from which the data object may be
540 set. In the general case, an object may support different formats on
541 'input' and 'output', i.e. it may be able to render itself in a given
542 format but not be created from data on this format or vice versa.
543 wxDataObject defines an enumeration type which distinguishes between them:
544
545 @code
546 enum Direction
547 {
548 Get = 0x01, // format is supported by GetDataHere()
549 Set = 0x02 // format is supported by SetData()
550 };
551 @endcode
552
553 See wxDataFormat documentation for more about formats.
554
555 Not surprisingly, being 'smart' comes at a price of added complexity. This
556 is reasonable for the situations when you really need to support multiple
557 formats, but may be annoying if you only want to do something simple like
558 cut and paste text.
559
560 To provide a solution for both cases, wxWidgets has two predefined classes
561 which derive from wxDataObject: wxDataObjectSimple and
562 wxDataObjectComposite. wxDataObjectSimple is the simplest wxDataObject
563 possible and only holds data in a single format (such as HTML or text) and
564 wxDataObjectComposite is the simplest way to implement a wxDataObject that
565 does support multiple formats because it achieves this by simply holding
566 several wxDataObjectSimple objects.
567
568 So, you have several solutions when you need a wxDataObject class (and you
569 need one as soon as you want to transfer data via the clipboard or drag and
570 drop):
571
572 -# Use one of the built-in classes.
573 - You may use wxTextDataObject, wxBitmapDataObject or wxFileDataObject
574 in the simplest cases when you only need to support one format and
575 your data is either text, bitmap or list of files.
576 -# Use wxDataObjectSimple
577 - Deriving from wxDataObjectSimple is the simplest solution for custom
578 data - you will only support one format and so probably won't be able
579 to communicate with other programs, but data transfer will work in
580 your program (or between different copies of it).
581 -# Use wxDataObjectComposite
582 - This is a simple but powerful solution which allows you to support
583 any number of formats (either standard or custom if you combine it
584 with the previous solution).
585 -# Use wxDataObject Directly
586 - This is the solution for maximal flexibility and efficiency, but it
587 is also the most difficult to implement.
588
589 Please note that the easiest way to use drag and drop and the clipboard
590 with multiple formats is by using wxDataObjectComposite, but it is not the
591 most efficient one as each wxDataObjectSimple would contain the whole data
592 in its respective formats. Now imagine that you want to paste 200 pages of
593 text in your proprietary format, as well as Word, RTF, HTML, Unicode and
594 plain text to the clipboard and even today's computers are in trouble. For
595 this case, you will have to derive from wxDataObject directly and make it
596 enumerate its formats and provide the data in the requested format on
597 demand.
598
599 Note that neither the GTK+ data transfer mechanisms for clipboard and drag
600 and drop, nor OLE data transfer, copy any data until another application
601 actually requests the data. This is in contrast to the 'feel' offered to
602 the user of a program who would normally think that the data resides in the
603 clipboard after having pressed 'Copy' - in reality it is only declared to
604 be available.
605
606 There are several predefined data object classes derived from
607 wxDataObjectSimple: wxFileDataObject, wxTextDataObject, wxBitmapDataObject
608 and wxURLDataObject which can be used without change.
609
610 You may also derive your own data object classes from wxCustomDataObject
611 for user-defined types. The format of user-defined data is given as a
612 mime-type string literal, such as "application/word" or "image/png". These
613 strings are used as they are under Unix (so far only GTK+) to identify a
614 format and are translated into their Windows equivalent under Win32 (using
615 the OLE IDataObject for data exchange to and from the clipboard and for
616 drag and drop). Note that the format string translation under Windows is
617 not yet finished.
618
619 Each class derived directly from wxDataObject must override and implement
620 all of its functions which are pure virtual in the base class. The data
621 objects which only render their data or only set it (i.e. work in only one
622 direction), should return 0 from GetFormatCount().
623
624 @beginWxPythonOnly
625 At this time this class is not directly usable from wxPython. Derive a
626 class from wxPyDataObjectSimple() instead.
627 @endWxPythonOnly
628
629 @beginWxPerlOnly
630 This class is not currently usable from wxPerl; you may use
631 Wx::PlDataObjectSimple instead.
632 @endWxPerlOnly
633
634 @library{wxcore}
635 @category{dnd}
636
637 @see @ref overview_dnd, @ref page_samples_dnd, wxFileDataObject,
638 wxTextDataObject, wxBitmapDataObject, wxCustomDataObject,
639 wxDropTarget, wxDropSource, wxTextDropTarget, wxFileDropTarget
640 */
641 class wxDataObject
642 {
643 public:
644 /**
645 Constructor.
646 */
647 wxDataObject();
648
649 /**
650 Destructor.
651 */
652 virtual ~wxDataObject();
653
654 /**
655 Copy all supported formats in the given direction to the array pointed
656 to by @a formats. There is enough space for GetFormatCount(dir) formats
657 in it.
658 */
659 virtual void GetAllFormats(wxDataFormat* formats,
660 Direction dir = Get) const = 0;
661
662 /**
663 The method will write the data of the format @a format in the buffer
664 @a buf and return @true on success, @false on failure.
665 */
666 virtual bool GetDataHere(const wxDataFormat& format, void* buf) const = 0;
667
668 /**
669 Returns the data size of the given format @a format.
670 */
671 virtual size_t GetDataSize(const wxDataFormat& format) const = 0;
672
673 /**
674 Returns the number of available formats for rendering or setting the
675 data.
676 */
677 virtual size_t GetFormatCount(Direction dir = Get) const = 0;
678
679 /**
680 Returns the preferred format for either rendering the data (if @a dir
681 is @c Get, its default value) or for setting it. Usually this will be
682 the native format of the wxDataObject.
683 */
684 virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const = 0;
685
686 /**
687 Set the data in the format @a format of the length @a len provided in
688 the buffer @a buf.
689
690 @return @true on success, @false on failure.
691 */
692 virtual bool SetData(const wxDataFormat& format, size_t len, const void* buf);
693 };
694