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 Aside 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;
133 //---------------------------------------------------------------------------
136 // wxDataObject represents a piece of data which knows which formats it
137 // supports and knows how to render itself in each of them - GetDataHere(),
138 // and how to restore data from the buffer (SetData()).
140 // Although this class may be used directly (i.e. custom classes may be
141 // derived from it), in many cases it might be simpler to use either
142 // wxDataObjectSimple or wxDataObjectComposite classes.
144 // A data object may be "read only", i.e. support only GetData() functions or
145 // "read-write", i.e. support both GetData() and SetData() (in principle, it
146 // might be "write only" too, but this is rare). Moreover, it doesn't have to
147 // support the same formats in Get() and Set() directions: for example, a data
148 // object containing JPEG image might accept BMPs in GetData() because JPEG
149 // image may be easily transformed into BMP but not in SetData(). Accordingly,
150 // all methods dealing with formats take an additional "direction" argument
151 // which is either SET or GET and which tells the function if the format needs
152 // to be supported by SetData() or GetDataHere().
156 Get = 0x01, // format is supported by GetDataHere()
157 Set = 0x02, // format is supported by SetData()
158 Both = 0x03 // format is supported by both (unused currently)
161 // wxDataObject(); // *** It's an ABC.
164 // get the best suited format for rendering our data
165 virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const;
167 // get the number of formats we support
168 virtual size_t GetFormatCount(Direction dir = Get) const;
170 // returns True if this format is supported
171 bool IsSupported(const wxDataFormat& format, Direction dir = Get) const;
173 // get the (total) size of data for the given format
174 virtual size_t GetDataSize(const wxDataFormat& format) const;
177 //-------------------------------------------------------------------
178 // TODO: Fix these three methods to do the right Pythonic things...
179 //-------------------------------------------------------------------
181 // return all formats in the provided array (of size GetFormatCount())
182 virtual void GetAllFormats(wxDataFormat *formats,
183 Direction dir = Get) const;
185 // copy raw data (in the specified format) to the provided buffer, return
186 // True if data copied successfully, False otherwise
187 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
189 // get data from the buffer of specified length (in the given format),
190 // return True if the data was read successfully, False otherwise
191 virtual bool SetData(const wxDataFormat& format,
192 size_t len, const void * buf);
196 //---------------------------------------------------------------------------
200 // wxDataObjectSimple is a wxDataObject which only supports one format (in
201 // both Get and Set directions, but you may return False from GetDataHere() or
202 // SetData() if one of them is not supported). This is the simplest possible
203 // wxDataObject implementation.
205 // This is still an "abstract base class" (although it doesn't have any pure
206 // virtual functions), to use it you should derive from it and implement
207 // GetDataSize(), GetDataHere() and SetData() functions because the base class
208 // versions don't do anything - they just return "not implemented".
210 // This class should be used when you provide data in only one format (no
211 // conversion to/from other formats), either a standard or a custom one.
212 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
213 class wxDataObjectSimple : public wxDataObject {
215 wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid);
217 // get/set the format we support
218 const wxDataFormat& GetFormat();
219 void SetFormat(const wxDataFormat& format);
224 %{ // Create a new class for wxPython to use
225 class wxPyDataObjectSimple : public wxDataObjectSimple {
227 wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid)
228 : wxDataObjectSimple(format) {}
230 DEC_PYCALLBACK_SIZET__const(GetDataSize);
231 bool GetDataHere(void *buf) const;
232 bool SetData(size_t len, const void *buf) const;
236 IMP_PYCALLBACK_SIZET__const(wxPyDataObjectSimple, wxDataObjectSimple, GetDataSize);
238 bool wxPyDataObjectSimple::GetDataHere(void *buf) const {
239 // We need to get the data for this object and write it to buf. I think
240 // the best way to do this for wxPython is to have the Python method
241 // return either a string or None and then act appropriately with the
245 bool blocked = wxPyBeginBlockThreads();
246 if (wxPyCBH_findCallback(m_myInst, "GetDataHere")) {
248 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
250 rval = (ro != Py_None && PyString_Check(ro));
252 memcpy(buf, PyString_AsString(ro), PyString_Size(ro));
256 wxPyEndBlockThreads(blocked);
260 bool wxPyDataObjectSimple::SetData(size_t len, const void *buf) const{
261 // For this one we simply need to make a string from buf and len
262 // and send it to the Python method.
264 bool blocked = wxPyBeginBlockThreads();
265 if (wxPyCBH_findCallback(m_myInst, "SetData")) {
266 PyObject* data = PyString_FromStringAndSize((char*)buf, len);
267 rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", data));
270 wxPyEndBlockThreads(blocked);
277 // Now define it for SWIG
278 class wxPyDataObjectSimple : public wxDataObjectSimple {
280 %pythonAppend wxPyDataObjectSimple "self._setCallbackInfo(self, PyDataObjectSimple)"
282 wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid);
283 void _setCallbackInfo(PyObject* self, PyObject* _class);
287 //---------------------------------------------------------------------------
290 // wxDataObjectComposite is the simplest way to implement wxDataObject
291 // supporting multiple formats. It contains several wxDataObjectSimple and
292 // supports all formats supported by any of them.
294 class wxDataObjectComposite : public wxDataObject {
296 wxDataObjectComposite();
298 %apply SWIGTYPE *DISOWN { wxDataObjectSimple *dataObject };
299 void Add(wxDataObjectSimple *dataObject, int preferred = False);
300 %clear wxDataObjectSimple *dataObject;
303 //---------------------------------------------------------------------------
305 // wxTextDataObject contains text data
306 class wxTextDataObject : public wxDataObjectSimple {
308 wxTextDataObject(const wxString& text = wxPyEmptyString);
310 size_t GetTextLength();
312 void SetText(const wxString& text);
317 %{ // Create a new class for wxPython to use
318 class wxPyTextDataObject : public wxTextDataObject {
320 wxPyTextDataObject(const wxString& text = wxPyEmptyString)
321 : wxTextDataObject(text) {}
323 DEC_PYCALLBACK_SIZET__const(GetTextLength);
324 DEC_PYCALLBACK_STRING__const(GetText);
325 DEC_PYCALLBACK__STRING(SetText);
329 IMP_PYCALLBACK_SIZET__const(wxPyTextDataObject, wxTextDataObject, GetTextLength);
330 IMP_PYCALLBACK_STRING__const(wxPyTextDataObject, wxTextDataObject, GetText);
331 IMP_PYCALLBACK__STRING(wxPyTextDataObject, wxTextDataObject, SetText);
336 // Now define it for SWIG
337 class wxPyTextDataObject : public wxTextDataObject {
339 %pythonAppend wxPyTextDataObject "self._setCallbackInfo(self, PyTextDataObject)"
341 wxPyTextDataObject(const wxString& text = wxPyEmptyString);
342 void _setCallbackInfo(PyObject* self, PyObject* _class);
345 //---------------------------------------------------------------------------
347 // wxBitmapDataObject contains a bitmap
348 class wxBitmapDataObject : public wxDataObjectSimple {
350 wxBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
352 wxBitmap GetBitmap() const;
353 void SetBitmap(const wxBitmap& bitmap);
358 %{ // Create a new class for wxPython to use
359 class wxPyBitmapDataObject : public wxBitmapDataObject {
361 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap)
362 : wxBitmapDataObject(bitmap) {}
364 wxBitmap GetBitmap() const;
365 void SetBitmap(const wxBitmap& bitmap);
369 wxBitmap wxPyBitmapDataObject::GetBitmap() const {
370 wxBitmap* rval = &wxNullBitmap;
371 bool blocked = wxPyBeginBlockThreads();
372 if (wxPyCBH_findCallback(m_myInst, "GetBitmap")) {
375 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
377 if (wxPyConvertSwigPtr(ro, (void **)&ptr, wxT("wxBitmap")))
382 wxPyEndBlockThreads(blocked);
386 void wxPyBitmapDataObject::SetBitmap(const wxBitmap& bitmap) {
387 bool blocked = wxPyBeginBlockThreads();
388 if (wxPyCBH_findCallback(m_myInst, "SetBitmap")) {
389 PyObject* bo = wxPyConstructObject((void*)&bitmap, wxT("wxBitmap"), False);
390 wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", bo));
393 wxPyEndBlockThreads(blocked);
399 // Now define it for SWIG
400 class wxPyBitmapDataObject : public wxBitmapDataObject {
402 %pythonAppend wxPyBitmapDataObject "self._setCallbackInfo(self, PyBitmapDataObject)"
404 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
405 void _setCallbackInfo(PyObject* self, PyObject* _class);
408 //---------------------------------------------------------------------------
411 // wxFileDataObject contains a list of filenames
412 class wxFileDataObject : public wxDataObjectSimple
417 const wxArrayString& GetFilenames();
418 void AddFile(const wxString &filename);
422 //---------------------------------------------------------------------------
424 // wxCustomDataObject contains arbitrary untyped user data.
425 // It is understood that this data can be copied bitwise.
426 class wxCustomDataObject : public wxDataObjectSimple {
428 wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
430 //void TakeData(size_t size, void *data);
431 //bool SetData(size_t size, const void *buf);
433 void TakeData(PyObject* data) {
434 if (PyString_Check(data)) {
435 // for Python we just call SetData here since we always need it to make a copy.
436 self->SetData(PyString_Size(data), PyString_AsString(data));
439 // raise a TypeError if not a string
440 PyErr_SetString(PyExc_TypeError, "String expected.");
443 bool SetData(PyObject* data) {
444 if (PyString_Check(data)) {
445 return self->SetData(PyString_Size(data), PyString_AsString(data));
448 // raise a TypeError if not a string
449 PyErr_SetString(PyExc_TypeError, "String expected.");
459 PyObject* GetData() {
460 return PyString_FromStringAndSize((char*)self->GetData(), self->GetSize());
466 // TODO: Implement wxPyCustomDataObject allowing GetSize, GetData and SetData
469 //---------------------------------------------------------------------------
471 class wxURLDataObject : public wxDataObjectComposite {
476 void SetURL(const wxString& url);
479 //---------------------------------------------------------------------------
481 #if defined(__WXMSW__) || defined(__WXMAC__)
484 #include <wx/metafile.h>
487 class wxMetafileDataObject : public wxDataObjectSimple
490 wxMetafileDataObject();
492 void SetMetafile(const wxMetafile& metafile);
493 wxMetafile GetMetafile() const;
499 class wxMetafileDataObject : public wxDataObjectSimple
502 wxMetafileDataObject() { wxPyRaiseNotImplemented(); }
506 class wxMetafileDataObject : public wxDataObjectSimple
509 wxMetafileDataObject();
514 //---------------------------------------------------------------------------
515 //---------------------------------------------------------------------------