1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: SWIG definitions for the Data Object classes
7 // Created: 31-October-1999
9 // Copyright: (c) 2003 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
16 //---------------------------------------------------------------------------
19 #include <wx/dataobj.h>
22 //---------------------------------------------------------------------------
52 "A wx.DataFormat is an encapsulation of a platform-specific format
53 handle which is used by the system for the clipboard and drag and drop
54 operations. The applications are usually only interested in, for
55 example, pasting data from the clipboard only if the data is in a
56 format the program understands. A data format is is used to uniquely
57 identify this format.",
59 On the system level, a data format is usually just a number, (which
60 may be the CLIPFORMAT under Windows or Atom under X11, for example.)
62 The standard format IDs are:
64 ================ =====================================
65 wx.DF_INVALID An invalid format
66 wx.DF_TEXT Text format
67 wx.DF_BITMAP A bitmap (wx.Bitmap)
68 wx.DF_METAFILE A metafile (wx.Metafile, Windows only)
69 wx.DF_FILENAME A list of filenames
70 wx.DF_HTML An HTML string. This is only valid on
71 Windows and non-unicode builds
72 ================ =====================================
74 Besies the standard formats, the application may also use custom
75 formats which are identified by their names (strings) and not numeric
76 identifiers. Although internally custom format must be created (or
77 registered) first, you shouldn\'t care about it because it is done
78 automatically the first time the wxDataFormat object corresponding to
79 a given format name is created.
86 wxDataFormat( wxDataFormatId type ),
87 "Constructs a data format object for one of the standard data formats
88 or an empty data object (use SetType or SetId later in this case)", "");
91 wxDataFormat(const wxString& format),
92 "Constructs a data format object for a custom format identified by its
100 %nokwargs operator!=;
101 bool operator==(wxDataFormatId format) const;
102 bool operator!=(wxDataFormatId format) const;
103 bool operator==(const wxDataFormat& format) const;
104 bool operator!=(const wxDataFormat& format) const;
108 void , SetType(wxDataFormatId format),
109 "Sets the format to the given value, which should be one of wx.DF_XXX
113 wxDataFormatId , GetType() const,
114 "Returns the platform-specific number identifying the format.", "");
118 wxString , GetId() const,
119 "Returns the name of a custom format (this function will fail for a
120 standard format).", "");
123 void , SetId(const wxString& format),
124 "Sets the format to be the custom format identified by the given name.", "");
129 const wxDataFormat wxFormatInvalid;
132 //---------------------------------------------------------------------------
136 "A wx.DataObject represents data that can be copied to or from the
137 clipboard, or dragged and dropped. The important thing about
138 wx.DataObject is that this is a 'smart' piece of data unlike usual
139 'dumb' data containers such as memory buffers or files. Being 'smart'
140 here means that the data object itself should know what data formats
141 it supports and how to render itself in each of supported formats.
143 **NOTE**: This class is an abstract base class and can not be used
144 directly from Python. If you need a custom type of data object then
145 you should instead derive from `wx.PyDataObjectSimple` or use
146 `wx.CustomDataObject`.
148 Not surprisingly, being 'smart' comes at a price of added
149 complexity. This is reasonable for the situations when you really need
150 to support multiple formats, but may be annoying if you only want to
151 do something simple like cut and paste text.
153 To provide a solution for both cases, wxWidgets has two predefined
154 classes which derive from wx.DataObject: `wx.DataObjectSimple` and
155 `wx.DataObjectComposite`. `wx.DataObjectSimple` is the simplest
156 wx.DataObject possible and only holds data in a single format (such as
157 text or bitmap) and `wx.DataObjectComposite` is the simplest way to
158 implement a wx.DataObject which supports multiple simultaneous formats
159 because it achievs this by simply holding several
160 `wx.DataObjectSimple` objects.
162 Please note that the easiest way to use drag and drop and the
163 clipboard with multiple formats is by using `wx.DataObjectComposite`,
164 but it is not the most efficient one as each `wx.DataObjectSimple`
165 would contain the whole data in its respective formats. Now imagine
166 that you want to paste 200 pages of text in your proprietary format,
167 as well as Word, RTF, HTML, Unicode and plain text to the clipboard
168 and even today's computers are in trouble. For this case, you will
169 have to derive from wx.DataObject directly and make it enumerate its
170 formats and provide the data in the requested format on
171 demand. (**TODO**: This is currently not possible from Python. Make
174 Note that the platform transfer mechanisms for the clipboard and drag
175 and drop, do not copy any data out of the source application until
176 another application actually requests the data. This is in contrast to
177 the 'feel' offered to the user of a program who would normally think
178 that the data resides in the clipboard after having pressed 'Copy' -
179 in reality it is only declared to be available.
183 // There are several predefined data object classes derived from
184 // wxDataObjectSimple: wxFileDataObject, wxTextDataObject and
185 // wxBitmapDataObject which can be used without change.
187 // You may also derive your own data object classes from
188 // wxCustomDataObject for user-defined types. The format of user-defined
189 // data is given as mime-type string literal, such as 'application/word'
190 // or 'image/png'. These strings are used as they are under Unix (so far
191 // only GTK) to identify a format and are translated into their Windows
192 // equivalent under Win32 (using the OLE IDataObject for data exchange to
193 // and from the clipboard and for drag and drop). Note that the format
194 // string translation under Windows is not yet finished.
201 Get = 0x01, // format is supported by GetDataHere()
202 Set = 0x02, // format is supported by SetData()
203 Both = 0x03 // format is supported by both (unused currently)
206 // wxDataObject(); // *** It's an ABC.
210 virtual wxDataFormat , GetPreferredFormat(Direction dir = Get) const,
211 "Returns the preferred format for either rendering the data (if dir is
212 Get, its default value) or for setting it. Usually this will be the
213 native format of the wx.DataObject.", "");
217 virtual size_t , GetFormatCount(Direction dir = Get) const,
218 "Returns the number of available formats for rendering or setting the
223 bool , IsSupported(const wxDataFormat& format, Direction dir = Get) const,
224 "Returns True if this format is supported.", "");
228 virtual size_t , GetDataSize(const wxDataFormat& format) const,
229 "Get the (total) size of data for the given format", "");
233 // return all formats in the provided array (of size GetFormatCount())
234 //virtual void GetAllFormats(wxDataFormat *formats,
235 // Direction dir = Get) const;
236 DocAStr(GetAllFormats,
237 "GetAllFormats(self, int dir=Get) -> [formats]",
238 "Returns a list of all the wx.DataFormats that this dataobject supports
239 in the given direction.", "");
241 PyObject* GetAllFormats(Direction dir = Get) {
242 size_t count = self->GetFormatCount(dir);
243 wxDataFormat* formats = new wxDataFormat[count];
244 self->GetAllFormats(formats, dir);
246 wxPyBlock_t blocked = wxPyBeginBlockThreads();
247 PyObject* list = PyList_New(count);
248 for (size_t i=0; i<count; i++) {
249 wxDataFormat* format = new wxDataFormat(formats[i]);
250 PyObject* obj = wxPyConstructObject((void*)format, wxT("wxDataFormat"), true);
251 PyList_SET_ITEM(list, i, obj); // PyList_SET_ITEM steals a reference
253 wxPyEndBlockThreads(blocked);
261 // copy raw data (in the specified format) to the provided buffer, return
262 // True if data copied successfully, False otherwise
263 // virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
266 "GetDataHere(self, DataFormat format) -> String",
267 "Get the data bytes in the specified format, returns None on failure.
269 :todo: This should use the python buffer interface isntead...");
271 PyObject* GetDataHere(const wxDataFormat& format) {
272 PyObject* rval = NULL;
273 size_t size = self->GetDataSize(format);
274 wxPyBlock_t blocked = wxPyBeginBlockThreads();
276 char* buf = new char[size];
277 if (self->GetDataHere(format, buf))
278 rval = PyString_FromStringAndSize(buf, size);
285 wxPyEndBlockThreads(blocked);
291 // get data from the buffer of specified length (in the given format),
292 // return True if the data was read successfully, False otherwise
293 //virtual bool SetData(const wxDataFormat& format,
294 // size_t len, const void * buf);
296 "SetData(self, DataFormat format, String data) -> bool",
297 "Set the data in the specified format from the bytes in the the data string.
299 :todo: This should use the python buffer interface isntead...");
301 bool SetData(const wxDataFormat& format, PyObject* data) {
303 wxPyBlock_t blocked = wxPyBeginBlockThreads();
304 if (PyString_Check(data)) {
305 rval = self->SetData(format, PyString_Size(data), PyString_AsString(data));
308 // raise a TypeError if not a string
309 PyErr_SetString(PyExc_TypeError, "String expected.");
312 wxPyEndBlockThreads(blocked);
321 //---------------------------------------------------------------------------
324 DocStr(wxDataObjectSimple,
325 "wx.DataObjectSimple is a `wx.DataObject` which only supports one
326 format. This is the simplest possible `wx.DataObject` implementation.
328 This is still an \"abstract base class\" meaning that you can't use it
329 directly. You either need to use one of the predefined base classes,
330 or derive your own class from `wx.PyDataObjectSimple`.
333 class wxDataObjectSimple : public wxDataObject {
336 wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid),
337 "Constructor accepts the supported format (none by default) which may
338 also be set later with `SetFormat`.","");
342 const wxDataFormat& , GetFormat(),
343 "Returns the (one and only one) format supported by this object. It is
344 assumed that the format is supported in both directions.", "");
347 void , SetFormat(const wxDataFormat& format),
348 "Sets the supported format.", "");
351 virtual size_t , GetDataSize() const,
352 "Get the size of our data.", "");
357 "GetDataHere(self) -> String",
358 "Returns the data bytes from the data object as a string, returns None
359 on failure. Must be implemented in the derived class if the object
360 supports rendering its data.", "");
362 PyObject* GetDataHere() {
363 PyObject* rval = NULL;
364 size_t size = self->GetDataSize();
365 wxPyBlock_t blocked = wxPyBeginBlockThreads();
367 char* buf = new char[size];
368 if (self->GetDataHere(buf))
369 rval = PyString_FromStringAndSize(buf, size);
376 wxPyEndBlockThreads(blocked);
383 "SetData(self, String data) -> bool",
384 "Copy the data value to the data object. Must be implemented in the
385 derived class if the object supports setting its data.
388 bool SetData(PyObject* data) {
390 wxPyBlock_t blocked = wxPyBeginBlockThreads();
391 if (PyString_Check(data)) {
392 rval = self->SetData(PyString_Size(data), PyString_AsString(data));
395 // raise a TypeError if not a string
396 PyErr_SetString(PyExc_TypeError, "String expected.");
399 wxPyEndBlockThreads(blocked);
409 %{ // Create a new class for wxPython to use
410 class wxPyDataObjectSimple : public wxDataObjectSimple {
412 wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid)
413 : wxDataObjectSimple(format) {}
415 DEC_PYCALLBACK_SIZET__const(GetDataSize);
416 bool GetDataHere(void *buf) const;
417 bool SetData(size_t len, const void *buf) const;
421 IMP_PYCALLBACK_SIZET__const(wxPyDataObjectSimple, wxDataObjectSimple, GetDataSize);
423 bool wxPyDataObjectSimple::GetDataHere(void *buf) const {
424 // We need to get the data for this object and write it to buf. I think
425 // the best way to do this for wxPython is to have the Python method
426 // return either a string or None and then act appropriately with the
430 wxPyBlock_t blocked = wxPyBeginBlockThreads();
431 if (wxPyCBH_findCallback(m_myInst, "GetDataHere")) {
433 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
435 rval = (ro != Py_None && PyString_Check(ro));
437 memcpy(buf, PyString_AsString(ro), PyString_Size(ro));
441 wxPyEndBlockThreads(blocked);
445 bool wxPyDataObjectSimple::SetData(size_t len, const void *buf) const{
446 // For this one we simply need to make a string from buf and len
447 // and send it to the Python method.
449 wxPyBlock_t blocked = wxPyBeginBlockThreads();
450 if (wxPyCBH_findCallback(m_myInst, "SetData")) {
451 PyObject* data = PyString_FromStringAndSize((char*)buf, len);
452 rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", data));
455 wxPyEndBlockThreads(blocked);
462 // Now define it for SWIG
463 DocStr(wxPyDataObjectSimple,
464 "wx.PyDataObjectSimple is a version of `wx.DataObjectSimple` that is
465 Python-aware and knows how to reflect calls to its C++ virtual methods
466 to methods in the Python derived class. You should derive from this
467 class and overload `GetDataSize`, `GetDataHere` and `SetData` when you
468 need to create your own simple single-format type of `wx.DataObject`.
470 Here is a simple example::
472 class MyDataObject(wx.PyDataObjectSimple):
474 wx.PyDataObjectSimple.__init__(
475 self, wx.CustomDataFormat('MyDOFormat'))
478 def GetDataSize(self):
479 return len(self.data)
480 def GetDataHere(self):
481 return self.data # returns a string
482 def SetData(self, data):
486 Note that there is already a `wx.CustomDataObject` class that behaves
487 very similarly to this example. The value of creating your own
488 derived class like this is to be able to do additional things when the
489 data is requested or given via the clipboard or drag and drop
490 operation, such as generate the data value or decode it into needed
493 class wxPyDataObjectSimple : public wxDataObjectSimple {
495 %pythonAppend wxPyDataObjectSimple "self._setCallbackInfo(self, PyDataObjectSimple)"
497 wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid);
498 void _setCallbackInfo(PyObject* self, PyObject* _class);
502 //---------------------------------------------------------------------------
505 DocStr(wxDataObjectComposite,
506 "wx.DataObjectComposite is the simplest `wx.DataObject` derivation
507 which may be sued to support multiple formats. It contains several
508 'wx.DataObjectSimple` objects and supports any format supported by at
509 least one of them. Only one of these data objects is *preferred* (the
510 first one if not explicitly changed by using the second parameter of
511 `Add`) and its format determines the preferred format of the composite
514 See `wx.DataObject` documentation for the reasons why you might prefer
515 to use wx.DataObject directly instead of wx.DataObjectComposite for
519 class wxDataObjectComposite : public wxDataObject {
521 wxDataObjectComposite();
523 %apply SWIGTYPE *DISOWN { wxDataObjectSimple *dataObject };
526 void , Add(wxDataObjectSimple *dataObject, bool preferred = false),
527 "Adds the dataObject to the list of supported objects and it becomes
528 the preferred object if preferred is True.", "");
530 %clear wxDataObjectSimple *dataObject;
533 //---------------------------------------------------------------------------
535 DocStr(wxTextDataObject,
536 "wx.TextDataObject is a specialization of `wx.DataObject` for text
537 data. It can be used without change to paste data into the `wx.Clipboard`
538 or a `wx.DropSource`.
540 Alternativly, you may wish to derive a new class from the
541 `wx.PyTextDataObject` class for providing text on-demand in order to
542 minimize memory consumption when offering data in several formats,
543 such as plain text and RTF, because by default the text is stored in a
544 string in this class, but it might as well be generated on demand when
545 requested. For this, `GetTextLength` and `GetText` will have to be
547 class wxTextDataObject : public wxDataObjectSimple {
550 wxTextDataObject(const wxString& text = wxPyEmptyString),
551 "Constructor, may be used to initialise the text (otherwise `SetText`
552 should be used later).", "");
555 size_t , GetTextLength(),
556 "Returns the data size. By default, returns the size of the text data
557 set in the constructor or using `SetText`. This can be overridden (via
558 `wx.PyTextDataObject`) to provide text size data on-demand. It is
559 recommended to return the text length plus 1 for a trailing zero, but
560 this is not strictly required.", "");
563 wxString , GetText(),
564 "Returns the text associated with the data object.", "");
567 void , SetText(const wxString& text),
568 "Sets the text associated with the data object. This method is called
569 when the data object receives the data and, by default, copies the
570 text into the member variable. If you want to process the text on the
571 fly you may wish to override this function (via
572 `wx.PyTextDataObject`.)", "");
581 %{ // Create a new class for wxPython to use
582 class wxPyTextDataObject : public wxTextDataObject {
584 wxPyTextDataObject(const wxString& text = wxPyEmptyString)
585 : wxTextDataObject(text) {}
587 DEC_PYCALLBACK_SIZET__const(GetTextLength);
588 DEC_PYCALLBACK_STRING__const(GetText);
589 DEC_PYCALLBACK__STRING(SetText);
593 IMP_PYCALLBACK_SIZET__const(wxPyTextDataObject, wxTextDataObject, GetTextLength);
594 IMP_PYCALLBACK_STRING__const(wxPyTextDataObject, wxTextDataObject, GetText);
595 IMP_PYCALLBACK__STRING(wxPyTextDataObject, wxTextDataObject, SetText);
600 // Now define it for SWIG
602 DocStr(wxPyTextDataObject,
603 "wx.PyTextDataObject is a version of `wx.TextDataObject` that is
604 Python-aware and knows how to reflect calls to its C++ virtual methods
605 to methods in the Python derived class. You should derive from this
606 class and overload `GetTextLength`, `GetText`, and `SetText` when you
607 want to be able to provide text on demand instead of preloading it
608 into the data object.", "");
610 class wxPyTextDataObject : public wxTextDataObject {
612 %pythonAppend wxPyTextDataObject "self._setCallbackInfo(self, PyTextDataObject)"
614 wxPyTextDataObject(const wxString& text = wxPyEmptyString);
615 void _setCallbackInfo(PyObject* self, PyObject* _class);
618 //---------------------------------------------------------------------------
620 DocStr(wxBitmapDataObject,
621 "wx.BitmapDataObject is a specialization of wxDataObject for bitmap
622 data. It can be used without change to paste data into the `wx.Clipboard`
623 or a `wx.DropSource`.
625 :see: `wx.PyBitmapDataObject` if you wish to override `GetBitmap` to increase efficiency.");
627 class wxBitmapDataObject : public wxDataObjectSimple {
630 wxBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap),
631 "Constructor, optionally passing a bitmap (otherwise use `SetBitmap`
635 wxBitmap , GetBitmap() const,
636 "Returns the bitmap associated with the data object. You may wish to
637 override this method (by deriving from `wx.PyBitmapDataObject`) when
638 offering data on-demand, but this is not required by wxWidgets'
639 internals. Use this method to get data in bitmap form from the
640 `wx.Clipboard`.", "");
643 void , SetBitmap(const wxBitmap& bitmap),
644 "Sets the bitmap associated with the data object. This method is called
645 when the data object receives data. Usually there will be no reason to
646 override this function.", "");
654 %{ // Create a new class for wxPython to use
655 class wxPyBitmapDataObject : public wxBitmapDataObject {
657 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap)
658 : wxBitmapDataObject(bitmap) {}
660 wxBitmap GetBitmap() const;
661 void SetBitmap(const wxBitmap& bitmap);
665 wxBitmap wxPyBitmapDataObject::GetBitmap() const {
666 wxBitmap* rval = &wxNullBitmap;
667 wxPyBlock_t blocked = wxPyBeginBlockThreads();
668 if (wxPyCBH_findCallback(m_myInst, "GetBitmap")) {
671 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
673 if (wxPyConvertSwigPtr(ro, (void **)&ptr, wxT("wxBitmap")))
678 wxPyEndBlockThreads(blocked);
682 void wxPyBitmapDataObject::SetBitmap(const wxBitmap& bitmap) {
683 wxPyBlock_t blocked = wxPyBeginBlockThreads();
684 if (wxPyCBH_findCallback(m_myInst, "SetBitmap")) {
685 PyObject* bo = wxPyConstructObject((void*)&bitmap, wxT("wxBitmap"), false);
686 wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", bo));
689 wxPyEndBlockThreads(blocked);
696 DocStr(wxPyBitmapDataObject,
697 "wx.PyBitmapDataObject is a version of `wx.BitmapDataObject` that is
698 Python-aware and knows how to reflect calls to its C++ virtual methods
699 to methods in the Python derived class. To be able to provide bitmap
700 data on demand derive from this class and overload `GetBitmap`.", "");
702 class wxPyBitmapDataObject : public wxBitmapDataObject {
704 %pythonAppend wxPyBitmapDataObject "self._setCallbackInfo(self, PyBitmapDataObject)"
706 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
707 void _setCallbackInfo(PyObject* self, PyObject* _class);
710 //---------------------------------------------------------------------------
713 DocStr(wxFileDataObject,
714 "wx.FileDataObject is a specialization of `wx.DataObjectSimple` for
715 file names. The program works with it just as if it were a list of
716 absolute file names, but internally it uses the same format as
717 Explorer and other compatible programs under Windows or GNOME/KDE
718 filemanager under Unix which makes it possible to receive files from
719 them using this class.", "");
721 class wxFileDataObject : public wxDataObjectSimple
729 const wxArrayString& , GetFilenames(),
730 "GetFilenames(self) -> [names]",
731 "Returns a list of file names.", "");
734 void , AddFile(const wxString &filename),
735 "Adds a file to the list of files represented by this data object.", "");
739 //---------------------------------------------------------------------------
741 DocStr(wxCustomDataObject,
742 "wx.CustomDataObject is a specialization of `wx.DataObjectSimple` for
743 some application-specific data in arbitrary format. Python strings
744 are used for getting and setting data, but any picklable object can
745 easily be transfered via strings. A copy of the data is stored in the
748 class wxCustomDataObject : public wxDataObjectSimple {
750 %nokwargs wxCustomDataObject;
751 wxCustomDataObject(const wxDataFormat& format);
753 wxCustomDataObject(const wxString& formatName) {
754 return new wxCustomDataObject(wxDataFormat(formatName));
757 wxCustomDataObject();
761 "SetData(self, String data) -> bool",
762 "Copy the data value to the data object.", "");
764 bool SetData(PyObject* data) {
766 wxPyBlock_t blocked = wxPyBeginBlockThreads();
767 if (PyString_Check(data)) {
768 rval = self->SetData(PyString_Size(data), PyString_AsString(data));
771 // raise a TypeError if not a string
772 PyErr_SetString(PyExc_TypeError, "String expected.");
775 wxPyEndBlockThreads(blocked);
779 %pythoncode { TakeData = SetData }
783 "Get the size of the data.", "");
787 "GetData(self) -> String",
788 "Returns the data bytes from the data object as a string.", "");
790 PyObject* GetData() {
792 wxPyBlock_t blocked = wxPyBeginBlockThreads();
793 obj = PyString_FromStringAndSize((char*)self->GetData(), self->GetSize());
794 wxPyEndBlockThreads(blocked);
801 //---------------------------------------------------------------------------
803 DocStr(wxURLDataObject,
804 "This data object holds a URL in a format that is compatible with some
805 browsers such that it is able to be dragged to or from them.", "");
806 class wxURLDataObject : public wxDataObject/*Composite*/ {
812 "Returns a string containing the current URL.", "");
815 void , SetURL(const wxString& url),
820 //---------------------------------------------------------------------------
822 #if defined(__WXMSW__) || defined(__WXMAC__)
825 #include <wx/metafile.h>
828 class wxMetafileDataObject : public wxDataObjectSimple
831 wxMetafileDataObject();
833 void SetMetafile(const wxMetafile& metafile);
834 wxMetafile GetMetafile() const;
840 class wxMetafileDataObject : public wxDataObjectSimple
843 wxMetafileDataObject() { wxPyRaiseNotImplemented(); }
847 class wxMetafileDataObject : public wxDataObjectSimple
850 wxMetafileDataObject();
855 //---------------------------------------------------------------------------
856 //---------------------------------------------------------------------------