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==(wxDataFormatId format) const;
61 bool operator!=(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 %pythonAppend 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 %apply SWIGTYPE *DISOWN { wxDataObjectSimple *dataObject };
244 void Add(wxDataObjectSimple *dataObject, int preferred = False);
245 %clear wxDataObjectSimple *dataObject;
248 //---------------------------------------------------------------------------
250 // wxTextDataObject contains text data
251 class wxTextDataObject : public wxDataObjectSimple {
253 wxTextDataObject(const wxString& text = wxPyEmptyString);
255 size_t GetTextLength();
257 void SetText(const wxString& text);
262 %{ // Create a new class for wxPython to use
263 class wxPyTextDataObject : public wxTextDataObject {
265 wxPyTextDataObject(const wxString& text = wxPyEmptyString)
266 : wxTextDataObject(text) {}
268 DEC_PYCALLBACK_SIZET__const(GetTextLength);
269 DEC_PYCALLBACK_STRING__const(GetText);
270 DEC_PYCALLBACK__STRING(SetText);
274 IMP_PYCALLBACK_SIZET__const(wxPyTextDataObject, wxTextDataObject, GetTextLength);
275 IMP_PYCALLBACK_STRING__const(wxPyTextDataObject, wxTextDataObject, GetText);
276 IMP_PYCALLBACK__STRING(wxPyTextDataObject, wxTextDataObject, SetText);
281 // Now define it for SWIG
282 class wxPyTextDataObject : public wxTextDataObject {
284 %pythonAppend wxPyTextDataObject "self._setCallbackInfo(self, PyTextDataObject)"
286 wxPyTextDataObject(const wxString& text = wxPyEmptyString);
287 void _setCallbackInfo(PyObject* self, PyObject* _class);
290 //---------------------------------------------------------------------------
292 // wxBitmapDataObject contains a bitmap
293 class wxBitmapDataObject : public wxDataObjectSimple {
295 wxBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
297 wxBitmap GetBitmap() const;
298 void SetBitmap(const wxBitmap& bitmap);
303 %{ // Create a new class for wxPython to use
304 class wxPyBitmapDataObject : public wxBitmapDataObject {
306 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap)
307 : wxBitmapDataObject(bitmap) {}
309 wxBitmap GetBitmap() const;
310 void SetBitmap(const wxBitmap& bitmap);
314 wxBitmap wxPyBitmapDataObject::GetBitmap() const {
315 wxBitmap* rval = &wxNullBitmap;
316 wxPyBeginBlockThreads();
317 if (wxPyCBH_findCallback(m_myInst, "GetBitmap")) {
320 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
322 if (wxPyConvertSwigPtr(ro, (void **)&ptr, wxT("wxBitmap")))
327 wxPyEndBlockThreads();
331 void wxPyBitmapDataObject::SetBitmap(const wxBitmap& bitmap) {
332 wxPyBeginBlockThreads();
333 if (wxPyCBH_findCallback(m_myInst, "SetBitmap")) {
334 PyObject* bo = wxPyConstructObject((void*)&bitmap, wxT("wxBitmap"), False);
335 wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", bo));
338 wxPyEndBlockThreads();
344 // Now define it for SWIG
345 class wxPyBitmapDataObject : public wxBitmapDataObject {
347 %pythonAppend wxPyBitmapDataObject "self._setCallbackInfo(self, PyBitmapDataObject)"
349 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
350 void _setCallbackInfo(PyObject* self, PyObject* _class);
353 //---------------------------------------------------------------------------
356 // wxFileDataObject contains a list of filenames
357 class wxFileDataObject : public wxDataObjectSimple
362 const wxArrayString& GetFilenames();
363 void AddFile(const wxString &filename);
367 //---------------------------------------------------------------------------
369 // wxCustomDataObject contains arbitrary untyped user data.
370 // It is understood that this data can be copied bitwise.
371 class wxCustomDataObject : public wxDataObjectSimple {
373 wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
375 //void TakeData(size_t size, void *data);
376 //bool SetData(size_t size, const void *buf);
378 void TakeData(PyObject* data) {
379 if (PyString_Check(data)) {
380 // for Python we just call SetData here since we always need it to make a copy.
381 self->SetData(PyString_Size(data), PyString_AsString(data));
384 // raise a TypeError if not a string
385 PyErr_SetString(PyExc_TypeError, "String expected.");
388 bool SetData(PyObject* data) {
389 if (PyString_Check(data)) {
390 return self->SetData(PyString_Size(data), PyString_AsString(data));
393 // raise a TypeError if not a string
394 PyErr_SetString(PyExc_TypeError, "String expected.");
404 PyObject* GetData() {
405 return PyString_FromStringAndSize((char*)self->GetData(), self->GetSize());
411 // TODO: Implement wxPyCustomDataObject allowing GetSize, GetData and SetData
414 //---------------------------------------------------------------------------
416 class wxURLDataObject : public wxDataObjectComposite {
421 void SetURL(const wxString& url);
424 //---------------------------------------------------------------------------
426 #if defined(__WXMSW__) || defined(__WXMAC__)
429 #include <wx/metafile.h>
432 class wxMetafileDataObject : public wxDataObjectSimple
435 wxMetafileDataObject();
437 void SetMetafile(const wxMetafile& metafile);
438 wxMetafile GetMetafile() const;
444 class wxMetafileDataObject : public wxDataObjectSimple
447 wxMetafileDataObject() { wxPyRaiseNotImplemented(); }
451 class wxMetafileDataObject : public wxDataObjectSimple
454 wxMetafileDataObject();
459 //---------------------------------------------------------------------------
460 //---------------------------------------------------------------------------