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 //---------------------------------------------------------------------------
53 wxDataFormat( wxDataFormatId type );
54 %name(CustomDataFormat) wxDataFormat(const wxString& format);
60 bool operator==(int /*wxDataFormatId*/ format) const;
61 bool operator!=(int /*wxDataFormatId*/ format) const;
62 bool operator==(const wxDataFormat& format) const;
63 bool operator!=(const wxDataFormat& format) const;
65 void SetType(wxDataFormatId format);
66 wxDataFormatId GetType() const;
68 wxString GetId() const;
69 void SetId(const wxString& format);
74 const wxDataFormat wxFormatInvalid;
78 //---------------------------------------------------------------------------
81 // wxDataObject represents a piece of data which knows which formats it
82 // supports and knows how to render itself in each of them - GetDataHere(),
83 // and how to restore data from the buffer (SetData()).
85 // Although this class may be used directly (i.e. custom classes may be
86 // derived from it), in many cases it might be simpler to use either
87 // wxDataObjectSimple or wxDataObjectComposite classes.
89 // A data object may be "read only", i.e. support only GetData() functions or
90 // "read-write", i.e. support both GetData() and SetData() (in principle, it
91 // might be "write only" too, but this is rare). Moreover, it doesn't have to
92 // support the same formats in Get() and Set() directions: for example, a data
93 // object containing JPEG image might accept BMPs in GetData() because JPEG
94 // image may be easily transformed into BMP but not in SetData(). Accordingly,
95 // all methods dealing with formats take an additional "direction" argument
96 // which is either SET or GET and which tells the function if the format needs
97 // to be supported by SetData() or GetDataHere().
101 Get = 0x01, // format is supported by GetDataHere()
102 Set = 0x02, // format is supported by SetData()
103 Both = 0x03 // format is supported by both (unused currently)
106 // wxDataObject(); // *** It's an ABC.
109 // get the best suited format for rendering our data
110 virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const;
112 // get the number of formats we support
113 virtual size_t GetFormatCount(Direction dir = Get) const;
115 // returns True if this format is supported
116 bool IsSupported(const wxDataFormat& format, Direction dir = Get) const;
118 // get the (total) size of data for the given format
119 virtual size_t GetDataSize(const wxDataFormat& format) const;
122 //-------------------------------------------------------------------
123 // TODO: Fix these three methods to do the right Pythonic things...
124 //-------------------------------------------------------------------
126 // return all formats in the provided array (of size GetFormatCount())
127 virtual void GetAllFormats(wxDataFormat *formats,
128 Direction dir = Get) const;
130 // copy raw data (in the specified format) to the provided buffer, return
131 // True if data copied successfully, False otherwise
132 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
134 // get data from the buffer of specified length (in the given format),
135 // return True if the data was read successfully, False otherwise
136 virtual bool SetData(const wxDataFormat& format,
137 size_t len, const void * buf);
141 //---------------------------------------------------------------------------
145 // wxDataObjectSimple is a wxDataObject which only supports one format (in
146 // both Get and Set directions, but you may return False from GetDataHere() or
147 // SetData() if one of them is not supported). This is the simplest possible
148 // wxDataObject implementation.
150 // This is still an "abstract base class" (although it doesn't have any pure
151 // virtual functions), to use it you should derive from it and implement
152 // GetDataSize(), GetDataHere() and SetData() functions because the base class
153 // versions don't do anything - they just return "not implemented".
155 // This class should be used when you provide data in only one format (no
156 // conversion to/from other formats), either a standard or a custom one.
157 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
158 class wxDataObjectSimple : public wxDataObject {
160 wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid);
162 // get/set the format we support
163 const wxDataFormat& GetFormat();
164 void SetFormat(const wxDataFormat& format);
169 %{ // Create a new class for wxPython to use
170 class wxPyDataObjectSimple : public wxDataObjectSimple {
172 wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid)
173 : wxDataObjectSimple(format) {}
175 DEC_PYCALLBACK_SIZET__const(GetDataSize);
176 bool GetDataHere(void *buf) const;
177 bool SetData(size_t len, const void *buf) const;
181 IMP_PYCALLBACK_SIZET__const(wxPyDataObjectSimple, wxDataObjectSimple, GetDataSize);
183 bool wxPyDataObjectSimple::GetDataHere(void *buf) const {
184 // We need to get the data for this object and write it to buf. I think
185 // the best way to do this for wxPython is to have the Python method
186 // return either a string or None and then act appropriately with the
190 wxPyBeginBlockThreads();
191 if (wxPyCBH_findCallback(m_myInst, "GetDataHere")) {
193 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
195 rval = (ro != Py_None && PyString_Check(ro));
197 memcpy(buf, PyString_AsString(ro), PyString_Size(ro));
201 wxPyEndBlockThreads();
205 bool wxPyDataObjectSimple::SetData(size_t len, const void *buf) const{
206 // For this one we simply need to make a string from buf and len
207 // and send it to the Python method.
209 wxPyBeginBlockThreads();
210 if (wxPyCBH_findCallback(m_myInst, "SetData")) {
211 PyObject* data = PyString_FromStringAndSize((char*)buf, len);
212 rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", data));
215 wxPyEndBlockThreads();
222 // Now define it for SWIG
223 class wxPyDataObjectSimple : public wxDataObjectSimple {
225 %addtofunc wxPyDataObjectSimple "self._setCallbackInfo(self, PyDataObjectSimple)"
227 wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid);
228 void _setCallbackInfo(PyObject* self, PyObject* _class);
232 //---------------------------------------------------------------------------
235 // wxDataObjectComposite is the simplest way to implement wxDataObject
236 // supporting multiple formats. It contains several wxDataObjectSimple and
237 // supports all formats supported by any of them.
239 class wxDataObjectComposite : public wxDataObject {
241 wxDataObjectComposite();
243 %addtofunc Add "args[1].thisown = 0"
244 void Add(wxDataObjectSimple *dataObject, int preferred = False);
247 //---------------------------------------------------------------------------
249 // wxTextDataObject contains text data
250 class wxTextDataObject : public wxDataObjectSimple {
252 wxTextDataObject(const wxString& text = wxPyEmptyString);
254 size_t GetTextLength();
256 void SetText(const wxString& text);
261 %{ // Create a new class for wxPython to use
262 class wxPyTextDataObject : public wxTextDataObject {
264 wxPyTextDataObject(const wxString& text = wxPyEmptyString)
265 : wxTextDataObject(text) {}
267 DEC_PYCALLBACK_SIZET__const(GetTextLength);
268 DEC_PYCALLBACK_STRING__const(GetText);
269 DEC_PYCALLBACK__STRING(SetText);
273 IMP_PYCALLBACK_SIZET__const(wxPyTextDataObject, wxTextDataObject, GetTextLength);
274 IMP_PYCALLBACK_STRING__const(wxPyTextDataObject, wxTextDataObject, GetText);
275 IMP_PYCALLBACK__STRING(wxPyTextDataObject, wxTextDataObject, SetText);
280 // Now define it for SWIG
281 class wxPyTextDataObject : public wxTextDataObject {
283 %addtofunc wxPyTextDataObject "self._setCallbackInfo(self, PyTextDataObject)"
285 wxPyTextDataObject(const wxString& text = wxPyEmptyString);
286 void _setCallbackInfo(PyObject* self, PyObject* _class);
289 //---------------------------------------------------------------------------
291 // wxBitmapDataObject contains a bitmap
292 class wxBitmapDataObject : public wxDataObjectSimple {
294 wxBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
296 wxBitmap GetBitmap() const;
297 void SetBitmap(const wxBitmap& bitmap);
302 %{ // Create a new class for wxPython to use
303 class wxPyBitmapDataObject : public wxBitmapDataObject {
305 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap)
306 : wxBitmapDataObject(bitmap) {}
308 wxBitmap GetBitmap() const;
309 void SetBitmap(const wxBitmap& bitmap);
313 wxBitmap wxPyBitmapDataObject::GetBitmap() const {
314 wxBitmap* rval = &wxNullBitmap;
315 wxPyBeginBlockThreads();
316 if (wxPyCBH_findCallback(m_myInst, "GetBitmap")) {
319 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
321 if (wxPyConvertSwigPtr(ro, (void **)&ptr, wxT("wxBitmap")))
326 wxPyEndBlockThreads();
330 void wxPyBitmapDataObject::SetBitmap(const wxBitmap& bitmap) {
331 wxPyBeginBlockThreads();
332 if (wxPyCBH_findCallback(m_myInst, "SetBitmap")) {
333 PyObject* bo = wxPyConstructObject((void*)&bitmap, wxT("wxBitmap"), False);
334 wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", bo));
337 wxPyEndBlockThreads();
343 // Now define it for SWIG
344 class wxPyBitmapDataObject : public wxBitmapDataObject {
346 %addtofunc wxPyBitmapDataObject "self._setCallbackInfo(self, PyBitmapDataObject)"
348 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
349 void _setCallbackInfo(PyObject* self, PyObject* _class);
352 //---------------------------------------------------------------------------
355 // wxFileDataObject contains a list of filenames
356 class wxFileDataObject : public wxDataObjectSimple
361 const wxArrayString& GetFilenames();
363 void AddFile(const wxString &filename);
368 //---------------------------------------------------------------------------
370 // wxCustomDataObject contains arbitrary untyped user data.
371 // It is understood that this data can be copied bitwise.
372 class wxCustomDataObject : public wxDataObjectSimple {
374 wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
376 //void TakeData(size_t size, void *data);
377 //bool SetData(size_t size, const void *buf);
379 void TakeData(PyObject* data) {
380 if (PyString_Check(data)) {
381 // for Python we just call SetData here since we always need it to make a copy.
382 self->SetData(PyString_Size(data), PyString_AsString(data));
385 // raise a TypeError if not a string
386 PyErr_SetString(PyExc_TypeError, "String expected.");
389 bool SetData(PyObject* data) {
390 if (PyString_Check(data)) {
391 return self->SetData(PyString_Size(data), PyString_AsString(data));
394 // raise a TypeError if not a string
395 PyErr_SetString(PyExc_TypeError, "String expected.");
405 PyObject* GetData() {
406 return PyString_FromStringAndSize((char*)self->GetData(), self->GetSize());
412 // TODO: Implement wxPyCustomDataObject allowing GetSize, GetData and SetData
415 //---------------------------------------------------------------------------
417 class wxURLDataObject : public wxDataObjectComposite {
422 void SetURL(const wxString& url);
425 //---------------------------------------------------------------------------
427 #if defined(__WXMSW__) || defined(__WXMAC__)
430 #include <wx/metafile.h>
433 class wxMetafileDataObject : public wxDataObjectSimple
436 wxMetafileDataObject();
438 void SetMetafile(const wxMetafile& metafile);
439 wxMetafile GetMetafile() const;
445 class wxMetafileDataObject : public wxDataObjectSimple
448 wxMetafileDataObject() { wxPyRaiseNotImplemented(); }
452 class wxMetafileDataObject : public wxDataObjectSimple
455 wxMetafileDataObject();
460 //---------------------------------------------------------------------------
461 //---------------------------------------------------------------------------