]> git.saurik.com Git - wxWidgets.git/blob - wxPython/src/_dataobj.i
Added docstrings
[wxWidgets.git] / wxPython / src / _dataobj.i
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: _dataobj.i
3 // Purpose: SWIG definitions for the Data Object classes
4 //
5 // Author: Robin Dunn
6 //
7 // Created: 31-October-1999
8 // RCS-ID: $Id$
9 // Copyright: (c) 2003 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 // Not a %module
14
15
16 //---------------------------------------------------------------------------
17
18 %{
19 #include <wx/dataobj.h>
20 %}
21
22 //---------------------------------------------------------------------------
23 %newgroup
24
25
26 enum wxDataFormatId
27 {
28 wxDF_INVALID,
29 wxDF_TEXT,
30 wxDF_BITMAP,
31 wxDF_METAFILE,
32 wxDF_SYLK,
33 wxDF_DIF,
34 wxDF_TIFF,
35 wxDF_OEMTEXT,
36 wxDF_DIB,
37 wxDF_PALETTE,
38 wxDF_PENDATA,
39 wxDF_RIFF,
40 wxDF_WAVE,
41 wxDF_UNICODETEXT,
42 wxDF_ENHMETAFILE,
43 wxDF_FILENAME,
44 wxDF_LOCALE,
45 wxDF_PRIVATE,
46 wxDF_HTML,
47 wxDF_MAX,
48 };
49
50
51 class wxDataFormat {
52 public:
53 wxDataFormat( wxDataFormatId type );
54 %name(CustomDataFormat) wxDataFormat(const wxString& format);
55
56 ~wxDataFormat();
57
58 %nokwargs operator==;
59 %nokwargs operator!=;
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;
64
65 void SetType(wxDataFormatId format);
66 wxDataFormatId GetType() const;
67
68 wxString GetId() const;
69 void SetId(const wxString& format);
70 };
71
72
73 %immutable;
74 const wxDataFormat wxFormatInvalid;
75 %mutable;
76
77
78 //---------------------------------------------------------------------------
79
80
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()).
84 //
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.
88 //
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().
98 class wxDataObject {
99 public:
100 enum Direction {
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)
104 };
105
106 // wxDataObject(); // *** It's an ABC.
107 ~wxDataObject();
108
109 // get the best suited format for rendering our data
110 virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const;
111
112 // get the number of formats we support
113 virtual size_t GetFormatCount(Direction dir = Get) const;
114
115 // returns True if this format is supported
116 bool IsSupported(const wxDataFormat& format, Direction dir = Get) const;
117
118 // get the (total) size of data for the given format
119 virtual size_t GetDataSize(const wxDataFormat& format) const;
120
121
122 //-------------------------------------------------------------------
123 // TODO: Fix these three methods to do the right Pythonic things...
124 //-------------------------------------------------------------------
125
126 // return all formats in the provided array (of size GetFormatCount())
127 virtual void GetAllFormats(wxDataFormat *formats,
128 Direction dir = Get) const;
129
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;
133
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);
138 };
139
140
141 //---------------------------------------------------------------------------
142
143
144
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.
149 //
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".
154 //
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 {
159 public:
160 wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid);
161
162 // get/set the format we support
163 const wxDataFormat& GetFormat();
164 void SetFormat(const wxDataFormat& format);
165
166 };
167
168
169 %{ // Create a new class for wxPython to use
170 class wxPyDataObjectSimple : public wxDataObjectSimple {
171 public:
172 wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid)
173 : wxDataObjectSimple(format) {}
174
175 DEC_PYCALLBACK_SIZET__const(GetDataSize);
176 bool GetDataHere(void *buf) const;
177 bool SetData(size_t len, const void *buf) const;
178 PYPRIVATE;
179 };
180
181 IMP_PYCALLBACK_SIZET__const(wxPyDataObjectSimple, wxDataObjectSimple, GetDataSize);
182
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
187 // C++ version.
188
189 bool rval = False;
190 wxPyBeginBlockThreads();
191 if (wxPyCBH_findCallback(m_myInst, "GetDataHere")) {
192 PyObject* ro;
193 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
194 if (ro) {
195 rval = (ro != Py_None && PyString_Check(ro));
196 if (rval)
197 memcpy(buf, PyString_AsString(ro), PyString_Size(ro));
198 Py_DECREF(ro);
199 }
200 }
201 wxPyEndBlockThreads();
202 return rval;
203 }
204
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.
208 bool rval = False;
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));
213 Py_DECREF(data);
214 }
215 wxPyEndBlockThreads();
216 return rval;
217 }
218 %}
219
220
221
222 // Now define it for SWIG
223 class wxPyDataObjectSimple : public wxDataObjectSimple {
224 public:
225 %pythonAppend wxPyDataObjectSimple "self._setCallbackInfo(self, PyDataObjectSimple)"
226
227 wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid);
228 void _setCallbackInfo(PyObject* self, PyObject* _class);
229 };
230
231
232 //---------------------------------------------------------------------------
233
234
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.
238 //
239 class wxDataObjectComposite : public wxDataObject {
240 public:
241 wxDataObjectComposite();
242
243 %apply SWIGTYPE *DISOWN { wxDataObjectSimple *dataObject };
244 void Add(wxDataObjectSimple *dataObject, int preferred = False);
245 %clear wxDataObjectSimple *dataObject;
246 };
247
248 //---------------------------------------------------------------------------
249
250 // wxTextDataObject contains text data
251 class wxTextDataObject : public wxDataObjectSimple {
252 public:
253 wxTextDataObject(const wxString& text = wxPyEmptyString);
254
255 size_t GetTextLength();
256 wxString GetText();
257 void SetText(const wxString& text);
258 };
259
260
261
262 %{ // Create a new class for wxPython to use
263 class wxPyTextDataObject : public wxTextDataObject {
264 public:
265 wxPyTextDataObject(const wxString& text = wxPyEmptyString)
266 : wxTextDataObject(text) {}
267
268 DEC_PYCALLBACK_SIZET__const(GetTextLength);
269 DEC_PYCALLBACK_STRING__const(GetText);
270 DEC_PYCALLBACK__STRING(SetText);
271 PYPRIVATE;
272 };
273
274 IMP_PYCALLBACK_SIZET__const(wxPyTextDataObject, wxTextDataObject, GetTextLength);
275 IMP_PYCALLBACK_STRING__const(wxPyTextDataObject, wxTextDataObject, GetText);
276 IMP_PYCALLBACK__STRING(wxPyTextDataObject, wxTextDataObject, SetText);
277
278 %}
279
280
281 // Now define it for SWIG
282 class wxPyTextDataObject : public wxTextDataObject {
283 public:
284 %pythonAppend wxPyTextDataObject "self._setCallbackInfo(self, PyTextDataObject)"
285
286 wxPyTextDataObject(const wxString& text = wxPyEmptyString);
287 void _setCallbackInfo(PyObject* self, PyObject* _class);
288 };
289
290 //---------------------------------------------------------------------------
291
292 // wxBitmapDataObject contains a bitmap
293 class wxBitmapDataObject : public wxDataObjectSimple {
294 public:
295 wxBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
296
297 wxBitmap GetBitmap() const;
298 void SetBitmap(const wxBitmap& bitmap);
299 };
300
301
302
303 %{ // Create a new class for wxPython to use
304 class wxPyBitmapDataObject : public wxBitmapDataObject {
305 public:
306 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap)
307 : wxBitmapDataObject(bitmap) {}
308
309 wxBitmap GetBitmap() const;
310 void SetBitmap(const wxBitmap& bitmap);
311 PYPRIVATE;
312 };
313
314 wxBitmap wxPyBitmapDataObject::GetBitmap() const {
315 wxBitmap* rval = &wxNullBitmap;
316 wxPyBeginBlockThreads();
317 if (wxPyCBH_findCallback(m_myInst, "GetBitmap")) {
318 PyObject* ro;
319 wxBitmap* ptr;
320 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
321 if (ro) {
322 if (wxPyConvertSwigPtr(ro, (void **)&ptr, wxT("wxBitmap")))
323 rval = ptr;
324 Py_DECREF(ro);
325 }
326 }
327 wxPyEndBlockThreads();
328 return *rval;
329 }
330
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));
336 Py_DECREF(bo);
337 }
338 wxPyEndBlockThreads();
339 }
340 %}
341
342
343
344 // Now define it for SWIG
345 class wxPyBitmapDataObject : public wxBitmapDataObject {
346 public:
347 %pythonAppend wxPyBitmapDataObject "self._setCallbackInfo(self, PyBitmapDataObject)"
348
349 wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
350 void _setCallbackInfo(PyObject* self, PyObject* _class);
351 };
352
353 //---------------------------------------------------------------------------
354
355
356 // wxFileDataObject contains a list of filenames
357 class wxFileDataObject : public wxDataObjectSimple
358 {
359 public:
360 wxFileDataObject();
361
362 const wxArrayString& GetFilenames();
363 #ifdef __WXMSW__
364 void AddFile(const wxString &filename);
365 #endif
366 };
367
368
369 //---------------------------------------------------------------------------
370
371 // wxCustomDataObject contains arbitrary untyped user data.
372 // It is understood that this data can be copied bitwise.
373 class wxCustomDataObject : public wxDataObjectSimple {
374 public:
375 wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
376
377 //void TakeData(size_t size, void *data);
378 //bool SetData(size_t size, const void *buf);
379 %extend {
380 void TakeData(PyObject* data) {
381 if (PyString_Check(data)) {
382 // for Python we just call SetData here since we always need it to make a copy.
383 self->SetData(PyString_Size(data), PyString_AsString(data));
384 }
385 else {
386 // raise a TypeError if not a string
387 PyErr_SetString(PyExc_TypeError, "String expected.");
388 }
389 }
390 bool SetData(PyObject* data) {
391 if (PyString_Check(data)) {
392 return self->SetData(PyString_Size(data), PyString_AsString(data));
393 }
394 else {
395 // raise a TypeError if not a string
396 PyErr_SetString(PyExc_TypeError, "String expected.");
397 return False;
398 }
399 }
400 }
401
402 size_t GetSize();
403
404 //void *GetData();
405 %extend {
406 PyObject* GetData() {
407 return PyString_FromStringAndSize((char*)self->GetData(), self->GetSize());
408 }
409 }
410 };
411
412
413 // TODO: Implement wxPyCustomDataObject allowing GetSize, GetData and SetData
414 // to be overloaded.
415
416 //---------------------------------------------------------------------------
417
418 class wxURLDataObject : public wxDataObjectComposite {
419 public:
420 wxURLDataObject();
421
422 wxString GetURL();
423 void SetURL(const wxString& url);
424 };
425
426 //---------------------------------------------------------------------------
427
428 #if defined(__WXMSW__) || defined(__WXMAC__)
429
430 %{
431 #include <wx/metafile.h>
432 %}
433
434 class wxMetafileDataObject : public wxDataObjectSimple
435 {
436 public:
437 wxMetafileDataObject();
438
439 void SetMetafile(const wxMetafile& metafile);
440 wxMetafile GetMetafile() const;
441 };
442
443
444 #else
445 %{
446 class wxMetafileDataObject : public wxDataObjectSimple
447 {
448 public:
449 wxMetafileDataObject() { wxPyRaiseNotImplemented(); }
450 };
451 %}
452
453 class wxMetafileDataObject : public wxDataObjectSimple
454 {
455 public:
456 wxMetafileDataObject();
457 };
458
459 #endif
460
461 //---------------------------------------------------------------------------
462 //---------------------------------------------------------------------------
463