]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
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 | ||
0facd0e5 RD |
51 | DocStr(wxDataFormat, |
52 | "A wx.DataFormat is an encapsulation of a platform-specific format | |
d07d2bc9 RD |
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.", | |
58 | " | |
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.) | |
61 | ||
62 | The standard format IDs are: | |
63 | ||
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 | ================ ===================================== | |
73 | ||
23f8f985 | 74 | Besies the standard formats, the application may also use custom |
d07d2bc9 RD |
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. | |
80 | ||
81 | "); | |
0facd0e5 | 82 | |
d14a1e28 RD |
83 | class wxDataFormat { |
84 | public: | |
0facd0e5 RD |
85 | DocCtorStr( |
86 | wxDataFormat( wxDataFormatId type ), | |
d07d2bc9 RD |
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)", ""); | |
0facd0e5 RD |
89 | |
90 | DocCtorStrName( | |
91 | wxDataFormat(const wxString& format), | |
d07d2bc9 RD |
92 | "Constructs a data format object for a custom format identified by its |
93 | name.", "", | |
0facd0e5 | 94 | CustomDataFormat); |
d14a1e28 RD |
95 | |
96 | ~wxDataFormat(); | |
97 | ||
0facd0e5 | 98 | |
d14a1e28 RD |
99 | %nokwargs operator==; |
100 | %nokwargs operator!=; | |
04e27155 RD |
101 | bool operator==(wxDataFormatId format) const; |
102 | bool operator!=(wxDataFormatId format) const; | |
d14a1e28 RD |
103 | bool operator==(const wxDataFormat& format) const; |
104 | bool operator!=(const wxDataFormat& format) const; | |
105 | ||
0facd0e5 RD |
106 | |
107 | DocDeclStr( | |
108 | void , SetType(wxDataFormatId format), | |
d07d2bc9 RD |
109 | "Sets the format to the given value, which should be one of wx.DF_XXX |
110 | constants.", ""); | |
0facd0e5 RD |
111 | |
112 | DocDeclStr( | |
113 | wxDataFormatId , GetType() const, | |
d07d2bc9 | 114 | "Returns the platform-specific number identifying the format.", ""); |
0facd0e5 | 115 | |
d14a1e28 | 116 | |
0facd0e5 RD |
117 | DocDeclStr( |
118 | wxString , GetId() const, | |
d07d2bc9 RD |
119 | "Returns the name of a custom format (this function will fail for a |
120 | standard format).", ""); | |
0facd0e5 RD |
121 | |
122 | DocDeclStr( | |
123 | void , SetId(const wxString& format), | |
d07d2bc9 | 124 | "Sets the format to be the custom format identified by the given name.", ""); |
0eae5d09 RD |
125 | |
126 | %property(Id, GetId, SetId, doc="See `GetId` and `SetId`"); | |
127 | %property(Type, GetType, SetType, doc="See `GetType` and `SetType`"); | |
d14a1e28 RD |
128 | }; |
129 | ||
130 | ||
131 | %immutable; | |
132 | const wxDataFormat wxFormatInvalid; | |
133 | %mutable; | |
134 | ||
d14a1e28 RD |
135 | //--------------------------------------------------------------------------- |
136 | ||
137 | ||
8e0ecdd9 RD |
138 | DocStr(wxDataObject, |
139 | "A wx.DataObject represents data that can be copied to or from the | |
140 | clipboard, or dragged and dropped. The important thing about | |
141 | wx.DataObject is that this is a 'smart' piece of data unlike usual | |
142 | 'dumb' data containers such as memory buffers or files. Being 'smart' | |
143 | here means that the data object itself should know what data formats | |
144 | it supports and how to render itself in each of supported formats. | |
145 | ||
146 | **NOTE**: This class is an abstract base class and can not be used | |
147 | directly from Python. If you need a custom type of data object then | |
148 | you should instead derive from `wx.PyDataObjectSimple` or use | |
149 | `wx.CustomDataObject`. | |
150 | ", " | |
151 | Not surprisingly, being 'smart' comes at a price of added | |
152 | complexity. This is reasonable for the situations when you really need | |
153 | to support multiple formats, but may be annoying if you only want to | |
154 | do something simple like cut and paste text. | |
155 | ||
156 | To provide a solution for both cases, wxWidgets has two predefined | |
157 | classes which derive from wx.DataObject: `wx.DataObjectSimple` and | |
158 | `wx.DataObjectComposite`. `wx.DataObjectSimple` is the simplest | |
159 | wx.DataObject possible and only holds data in a single format (such as | |
160 | text or bitmap) and `wx.DataObjectComposite` is the simplest way to | |
161 | implement a wx.DataObject which supports multiple simultaneous formats | |
162 | because it achievs this by simply holding several | |
163 | `wx.DataObjectSimple` objects. | |
164 | ||
165 | Please note that the easiest way to use drag and drop and the | |
166 | clipboard with multiple formats is by using `wx.DataObjectComposite`, | |
167 | but it is not the most efficient one as each `wx.DataObjectSimple` | |
168 | would contain the whole data in its respective formats. Now imagine | |
169 | that you want to paste 200 pages of text in your proprietary format, | |
170 | as well as Word, RTF, HTML, Unicode and plain text to the clipboard | |
171 | and even today's computers are in trouble. For this case, you will | |
172 | have to derive from wx.DataObject directly and make it enumerate its | |
173 | formats and provide the data in the requested format on | |
174 | demand. (**TODO**: This is currently not possible from Python. Make | |
175 | it so.) | |
176 | ||
177 | Note that the platform transfer mechanisms for the clipboard and drag | |
178 | and drop, do not copy any data out of the source application until | |
179 | another application actually requests the data. This is in contrast to | |
180 | the 'feel' offered to the user of a program who would normally think | |
181 | that the data resides in the clipboard after having pressed 'Copy' - | |
182 | in reality it is only declared to be available. | |
183 | "); | |
184 | ||
185 | // [other docs...] | |
186 | // There are several predefined data object classes derived from | |
187 | // wxDataObjectSimple: wxFileDataObject, wxTextDataObject and | |
188 | // wxBitmapDataObject which can be used without change. | |
189 | ||
190 | // You may also derive your own data object classes from | |
191 | // wxCustomDataObject for user-defined types. The format of user-defined | |
192 | // data is given as mime-type string literal, such as 'application/word' | |
193 | // or 'image/png'. These strings are used as they are under Unix (so far | |
194 | // only GTK) to identify a format and are translated into their Windows | |
195 | // equivalent under Win32 (using the OLE IDataObject for data exchange to | |
196 | // and from the clipboard and for drag and drop). Note that the format | |
197 | // string translation under Windows is not yet finished. | |
198 | ||
199 | ||
200 | ||
d14a1e28 RD |
201 | class wxDataObject { |
202 | public: | |
203 | enum Direction { | |
204 | Get = 0x01, // format is supported by GetDataHere() | |
205 | Set = 0x02, // format is supported by SetData() | |
206 | Both = 0x03 // format is supported by both (unused currently) | |
207 | }; | |
208 | ||
209 | // wxDataObject(); // *** It's an ABC. | |
210 | ~wxDataObject(); | |
211 | ||
8e0ecdd9 RD |
212 | DocDeclStr( |
213 | virtual wxDataFormat , GetPreferredFormat(Direction dir = Get) const, | |
214 | "Returns the preferred format for either rendering the data (if dir is | |
215 | Get, its default value) or for setting it. Usually this will be the | |
216 | native format of the wx.DataObject.", ""); | |
217 | ||
d14a1e28 | 218 | |
8e0ecdd9 RD |
219 | DocDeclStr( |
220 | virtual size_t , GetFormatCount(Direction dir = Get) const, | |
221 | "Returns the number of available formats for rendering or setting the | |
222 | data.", ""); | |
223 | ||
d14a1e28 | 224 | |
8e0ecdd9 RD |
225 | DocDeclStr( |
226 | bool , IsSupported(const wxDataFormat& format, Direction dir = Get) const, | |
227 | "Returns True if this format is supported.", ""); | |
228 | ||
d14a1e28 | 229 | |
8e0ecdd9 RD |
230 | DocDeclStr( |
231 | virtual size_t , GetDataSize(const wxDataFormat& format) const, | |
232 | "Get the (total) size of data for the given format", ""); | |
233 | ||
d14a1e28 | 234 | |
d14a1e28 | 235 | |
8e0ecdd9 | 236 | %extend { |
214c4fbe RD |
237 | DocAStr(GetAllFormats, |
238 | "GetAllFormats(self, int dir=Get) -> [formats]", | |
239 | "Returns a list of all the wx.DataFormats that this dataobject supports | |
240 | in the given direction.", ""); | |
8e0ecdd9 RD |
241 | PyObject* GetAllFormats(Direction dir = Get) { |
242 | size_t count = self->GetFormatCount(dir); | |
243 | wxDataFormat* formats = new wxDataFormat[count]; | |
244 | self->GetAllFormats(formats, dir); | |
245 | ||
6e6b3557 | 246 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
8e0ecdd9 RD |
247 | PyObject* list = PyList_New(count); |
248 | for (size_t i=0; i<count; i++) { | |
249 | wxDataFormat* format = new wxDataFormat(formats[i]); | |
a72f4631 | 250 | PyObject* obj = wxPyConstructObject((void*)format, wxT("wxDataFormat"), true); |
02b800ce | 251 | PyList_SET_ITEM(list, i, obj); // PyList_SET_ITEM steals a reference |
8e0ecdd9 RD |
252 | } |
253 | wxPyEndBlockThreads(blocked); | |
254 | delete [] formats; | |
255 | return list; | |
256 | } | |
257 | } | |
258 | ||
259 | ||
d14a1e28 RD |
260 | |
261 | // copy raw data (in the specified format) to the provided buffer, return | |
dd9f7fea | 262 | // True if data copied successfully, False otherwise |
8e0ecdd9 RD |
263 | // virtual bool GetDataHere(const wxDataFormat& format, void *buf) const; |
264 | ||
8e0ecdd9 | 265 | %extend { |
214c4fbe RD |
266 | DocAStr(GetDataHere, |
267 | "GetDataHere(self, DataFormat format) -> String", | |
268 | "Get the data bytes in the specified format, returns None on failure.", " | |
269 | :todo: This should use the python buffer interface isntead..."); | |
8e0ecdd9 RD |
270 | PyObject* GetDataHere(const wxDataFormat& format) { |
271 | PyObject* rval = NULL; | |
272 | size_t size = self->GetDataSize(format); | |
6e6b3557 | 273 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
8e0ecdd9 RD |
274 | if (size) { |
275 | char* buf = new char[size]; | |
276 | if (self->GetDataHere(format, buf)) | |
277 | rval = PyString_FromStringAndSize(buf, size); | |
278 | delete [] buf; | |
279 | } | |
280 | if (! rval) { | |
281 | rval = Py_None; | |
282 | Py_INCREF(rval); | |
283 | } | |
284 | wxPyEndBlockThreads(blocked); | |
285 | return rval; | |
286 | } | |
287 | } | |
d14a1e28 | 288 | |
8e0ecdd9 | 289 | |
d14a1e28 | 290 | // get data from the buffer of specified length (in the given format), |
dd9f7fea | 291 | // return True if the data was read successfully, False otherwise |
8e0ecdd9 RD |
292 | //virtual bool SetData(const wxDataFormat& format, |
293 | // size_t len, const void * buf); | |
294 | DocAStr(SetData, | |
295 | "SetData(self, DataFormat format, String data) -> bool", | |
296 | "Set the data in the specified format from the bytes in the the data string. | |
297 | ", " | |
298 | :todo: This should use the python buffer interface isntead..."); | |
299 | %extend { | |
300 | bool SetData(const wxDataFormat& format, PyObject* data) { | |
301 | bool rval; | |
6e6b3557 | 302 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
8e0ecdd9 RD |
303 | if (PyString_Check(data)) { |
304 | rval = self->SetData(format, PyString_Size(data), PyString_AsString(data)); | |
305 | } | |
306 | else { | |
307 | // raise a TypeError if not a string | |
308 | PyErr_SetString(PyExc_TypeError, "String expected."); | |
a72f4631 | 309 | rval = false; |
8e0ecdd9 RD |
310 | } |
311 | wxPyEndBlockThreads(blocked); | |
312 | return rval; | |
313 | } | |
314 | } | |
315 | ||
0eae5d09 RD |
316 | %property(AllFormats, GetAllFormats, doc="See `GetAllFormats`"); |
317 | %property(DataHere, GetDataHere, doc="See `GetDataHere`"); | |
318 | %property(DataSize, GetDataSize, doc="See `GetDataSize`"); | |
319 | %property(FormatCount, GetFormatCount, doc="See `GetFormatCount`"); | |
320 | %property(PreferredFormat, GetPreferredFormat, doc="See `GetPreferredFormat`"); | |
8e0ecdd9 | 321 | |
d14a1e28 RD |
322 | }; |
323 | ||
324 | ||
325 | //--------------------------------------------------------------------------- | |
326 | ||
327 | ||
8e0ecdd9 RD |
328 | DocStr(wxDataObjectSimple, |
329 | "wx.DataObjectSimple is a `wx.DataObject` which only supports one | |
330 | format. This is the simplest possible `wx.DataObject` implementation. | |
331 | ||
332 | This is still an \"abstract base class\" meaning that you can't use it | |
333 | directly. You either need to use one of the predefined base classes, | |
334 | or derive your own class from `wx.PyDataObjectSimple`. | |
335 | ", ""); | |
d14a1e28 | 336 | |
d14a1e28 RD |
337 | class wxDataObjectSimple : public wxDataObject { |
338 | public: | |
8e0ecdd9 RD |
339 | DocCtorStr( |
340 | wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid), | |
341 | "Constructor accepts the supported format (none by default) which may | |
342 | also be set later with `SetFormat`.",""); | |
343 | ||
d14a1e28 | 344 | |
8e0ecdd9 RD |
345 | DocDeclStr( |
346 | const wxDataFormat& , GetFormat(), | |
347 | "Returns the (one and only one) format supported by this object. It is | |
348 | assumed that the format is supported in both directions.", ""); | |
349 | ||
350 | DocDeclStr( | |
351 | void , SetFormat(const wxDataFormat& format), | |
352 | "Sets the supported format.", ""); | |
d14a1e28 | 353 | |
8e0ecdd9 RD |
354 | DocDeclStr( |
355 | virtual size_t , GetDataSize() const, | |
356 | "Get the size of our data.", ""); | |
357 | ||
358 | ||
359 | ||
214c4fbe RD |
360 | %extend { |
361 | DocAStr(GetDataHere, | |
362 | "GetDataHere(self) -> String", | |
363 | "Returns the data bytes from the data object as a string, returns None | |
8e0ecdd9 RD |
364 | on failure. Must be implemented in the derived class if the object |
365 | supports rendering its data.", ""); | |
8e0ecdd9 RD |
366 | PyObject* GetDataHere() { |
367 | PyObject* rval = NULL; | |
368 | size_t size = self->GetDataSize(); | |
6e6b3557 | 369 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
8e0ecdd9 RD |
370 | if (size) { |
371 | char* buf = new char[size]; | |
372 | if (self->GetDataHere(buf)) | |
373 | rval = PyString_FromStringAndSize(buf, size); | |
374 | delete [] buf; | |
375 | } | |
376 | if (! rval) { | |
377 | rval = Py_None; | |
378 | Py_INCREF(rval); | |
379 | } | |
380 | wxPyEndBlockThreads(blocked); | |
381 | return rval; | |
382 | } | |
383 | } | |
384 | ||
385 | ||
214c4fbe RD |
386 | %extend { |
387 | DocAStr(SetData, | |
388 | "SetData(self, String data) -> bool", | |
389 | "Copy the data value to the data object. Must be implemented in the | |
8e0ecdd9 RD |
390 | derived class if the object supports setting its data. |
391 | ", ""); | |
8e0ecdd9 RD |
392 | bool SetData(PyObject* data) { |
393 | bool rval; | |
6e6b3557 | 394 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
8e0ecdd9 RD |
395 | if (PyString_Check(data)) { |
396 | rval = self->SetData(PyString_Size(data), PyString_AsString(data)); | |
397 | } | |
398 | else { | |
399 | // raise a TypeError if not a string | |
400 | PyErr_SetString(PyExc_TypeError, "String expected."); | |
a72f4631 | 401 | rval = false; |
8e0ecdd9 RD |
402 | } |
403 | wxPyEndBlockThreads(blocked); | |
404 | return rval; | |
405 | } | |
406 | } | |
407 | ||
0eae5d09 | 408 | %property(Format, GetFormat, SetFormat, doc="See `GetFormat` and `SetFormat`"); |
d14a1e28 RD |
409 | }; |
410 | ||
411 | ||
8e0ecdd9 RD |
412 | |
413 | ||
d14a1e28 RD |
414 | %{ // Create a new class for wxPython to use |
415 | class wxPyDataObjectSimple : public wxDataObjectSimple { | |
416 | public: | |
417 | wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid) | |
418 | : wxDataObjectSimple(format) {} | |
419 | ||
420 | DEC_PYCALLBACK_SIZET__const(GetDataSize); | |
421 | bool GetDataHere(void *buf) const; | |
75216307 | 422 | bool SetData(size_t len, const void *buf); |
d14a1e28 RD |
423 | PYPRIVATE; |
424 | }; | |
425 | ||
426 | IMP_PYCALLBACK_SIZET__const(wxPyDataObjectSimple, wxDataObjectSimple, GetDataSize); | |
427 | ||
428 | bool wxPyDataObjectSimple::GetDataHere(void *buf) const { | |
429 | // We need to get the data for this object and write it to buf. I think | |
430 | // the best way to do this for wxPython is to have the Python method | |
431 | // return either a string or None and then act appropriately with the | |
432 | // C++ version. | |
433 | ||
a72f4631 | 434 | bool rval = false; |
6e6b3557 | 435 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
d14a1e28 RD |
436 | if (wxPyCBH_findCallback(m_myInst, "GetDataHere")) { |
437 | PyObject* ro; | |
438 | ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()")); | |
439 | if (ro) { | |
440 | rval = (ro != Py_None && PyString_Check(ro)); | |
441 | if (rval) | |
442 | memcpy(buf, PyString_AsString(ro), PyString_Size(ro)); | |
443 | Py_DECREF(ro); | |
444 | } | |
445 | } | |
da32eb53 | 446 | wxPyEndBlockThreads(blocked); |
d14a1e28 RD |
447 | return rval; |
448 | } | |
449 | ||
75216307 | 450 | bool wxPyDataObjectSimple::SetData(size_t len, const void *buf) { |
d14a1e28 RD |
451 | // For this one we simply need to make a string from buf and len |
452 | // and send it to the Python method. | |
a72f4631 | 453 | bool rval = false; |
6e6b3557 | 454 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
d14a1e28 RD |
455 | if (wxPyCBH_findCallback(m_myInst, "SetData")) { |
456 | PyObject* data = PyString_FromStringAndSize((char*)buf, len); | |
457 | rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", data)); | |
458 | Py_DECREF(data); | |
459 | } | |
da32eb53 | 460 | wxPyEndBlockThreads(blocked); |
d14a1e28 RD |
461 | return rval; |
462 | } | |
463 | %} | |
464 | ||
465 | ||
466 | ||
467 | // Now define it for SWIG | |
8e0ecdd9 RD |
468 | DocStr(wxPyDataObjectSimple, |
469 | "wx.PyDataObjectSimple is a version of `wx.DataObjectSimple` that is | |
470 | Python-aware and knows how to reflect calls to its C++ virtual methods | |
471 | to methods in the Python derived class. You should derive from this | |
472 | class and overload `GetDataSize`, `GetDataHere` and `SetData` when you | |
473 | need to create your own simple single-format type of `wx.DataObject`. | |
474 | ", " | |
475 | Here is a simple example:: | |
476 | ||
477 | class MyDataObject(wx.PyDataObjectSimple): | |
478 | def __init__(self): | |
479 | wx.PyDataObjectSimple.__init__( | |
480 | self, wx.CustomDataFormat('MyDOFormat')) | |
481 | self.data = '' | |
482 | ||
483 | def GetDataSize(self): | |
484 | return len(self.data) | |
485 | def GetDataHere(self): | |
486 | return self.data # returns a string | |
487 | def SetData(self, data): | |
488 | self.data = data | |
489 | return True | |
490 | ||
491 | Note that there is already a `wx.CustomDataObject` class that behaves | |
492 | very similarly to this example. The value of creating your own | |
493 | derived class like this is to be able to do additional things when the | |
494 | data is requested or given via the clipboard or drag and drop | |
495 | operation, such as generate the data value or decode it into needed | |
496 | data structures. | |
497 | "); | |
d14a1e28 RD |
498 | class wxPyDataObjectSimple : public wxDataObjectSimple { |
499 | public: | |
2b9048c5 | 500 | %pythonAppend wxPyDataObjectSimple "self._setCallbackInfo(self, PyDataObjectSimple)" |
d14a1e28 RD |
501 | |
502 | wxPyDataObjectSimple(const wxDataFormat& format = wxFormatInvalid); | |
503 | void _setCallbackInfo(PyObject* self, PyObject* _class); | |
504 | }; | |
505 | ||
506 | ||
507 | //--------------------------------------------------------------------------- | |
508 | ||
509 | ||
8e0ecdd9 RD |
510 | DocStr(wxDataObjectComposite, |
511 | "wx.DataObjectComposite is the simplest `wx.DataObject` derivation | |
512 | which may be sued to support multiple formats. It contains several | |
513 | 'wx.DataObjectSimple` objects and supports any format supported by at | |
514 | least one of them. Only one of these data objects is *preferred* (the | |
515 | first one if not explicitly changed by using the second parameter of | |
516 | `Add`) and its format determines the preferred format of the composite | |
517 | data object as well. | |
518 | ||
519 | See `wx.DataObject` documentation for the reasons why you might prefer | |
520 | to use wx.DataObject directly instead of wx.DataObjectComposite for | |
521 | efficiency reasons. | |
522 | ", ""); | |
523 | ||
d14a1e28 RD |
524 | class wxDataObjectComposite : public wxDataObject { |
525 | public: | |
526 | wxDataObjectComposite(); | |
527 | ||
214c4fbe | 528 | %disownarg( wxDataObjectSimple *dataObject ); |
8e0ecdd9 RD |
529 | |
530 | DocDeclStr( | |
a72f4631 | 531 | void , Add(wxDataObjectSimple *dataObject, bool preferred = false), |
8e0ecdd9 RD |
532 | "Adds the dataObject to the list of supported objects and it becomes |
533 | the preferred object if preferred is True.", ""); | |
534 | ||
214c4fbe | 535 | %cleardisown( wxDataObjectSimple *dataObject ); |
c072c757 RD |
536 | |
537 | DocDeclStr( | |
538 | wxDataFormat , GetReceivedFormat() const, | |
539 | "Report the format passed to the `SetData` method. This should be the | |
540 | format of the data object within the composite that recieved data from | |
541 | the clipboard or the DnD operation. You can use this method to find | |
542 | out what kind of data object was recieved.", ""); | |
543 | ||
0eae5d09 | 544 | %property(ReceivedFormat, GetReceivedFormat, doc="See `GetReceivedFormat`"); |
d14a1e28 RD |
545 | }; |
546 | ||
547 | //--------------------------------------------------------------------------- | |
548 | ||
8e0ecdd9 RD |
549 | DocStr(wxTextDataObject, |
550 | "wx.TextDataObject is a specialization of `wx.DataObject` for text | |
551 | data. It can be used without change to paste data into the `wx.Clipboard` | |
552 | or a `wx.DropSource`. | |
553 | ||
554 | Alternativly, you may wish to derive a new class from the | |
555 | `wx.PyTextDataObject` class for providing text on-demand in order to | |
556 | minimize memory consumption when offering data in several formats, | |
557 | such as plain text and RTF, because by default the text is stored in a | |
558 | string in this class, but it might as well be generated on demand when | |
559 | requested. For this, `GetTextLength` and `GetText` will have to be | |
560 | overridden.", ""); | |
d14a1e28 RD |
561 | class wxTextDataObject : public wxDataObjectSimple { |
562 | public: | |
8e0ecdd9 RD |
563 | DocCtorStr( |
564 | wxTextDataObject(const wxString& text = wxPyEmptyString), | |
565 | "Constructor, may be used to initialise the text (otherwise `SetText` | |
566 | should be used later).", ""); | |
d14a1e28 | 567 | |
8e0ecdd9 RD |
568 | DocDeclStr( |
569 | size_t , GetTextLength(), | |
570 | "Returns the data size. By default, returns the size of the text data | |
571 | set in the constructor or using `SetText`. This can be overridden (via | |
572 | `wx.PyTextDataObject`) to provide text size data on-demand. It is | |
573 | recommended to return the text length plus 1 for a trailing zero, but | |
574 | this is not strictly required.", ""); | |
575 | ||
576 | DocDeclStr( | |
577 | wxString , GetText(), | |
578 | "Returns the text associated with the data object.", ""); | |
579 | ||
580 | DocDeclStr( | |
581 | void , SetText(const wxString& text), | |
582 | "Sets the text associated with the data object. This method is called | |
583 | when the data object receives the data and, by default, copies the | |
584 | text into the member variable. If you want to process the text on the | |
585 | fly you may wish to override this function (via | |
586 | `wx.PyTextDataObject`.)", ""); | |
587 | ||
d14a1e28 RD |
588 | }; |
589 | ||
590 | ||
591 | ||
8e0ecdd9 RD |
592 | |
593 | ||
594 | ||
d14a1e28 RD |
595 | %{ // Create a new class for wxPython to use |
596 | class wxPyTextDataObject : public wxTextDataObject { | |
597 | public: | |
598 | wxPyTextDataObject(const wxString& text = wxPyEmptyString) | |
599 | : wxTextDataObject(text) {} | |
600 | ||
601 | DEC_PYCALLBACK_SIZET__const(GetTextLength); | |
602 | DEC_PYCALLBACK_STRING__const(GetText); | |
603 | DEC_PYCALLBACK__STRING(SetText); | |
604 | PYPRIVATE; | |
605 | }; | |
606 | ||
607 | IMP_PYCALLBACK_SIZET__const(wxPyTextDataObject, wxTextDataObject, GetTextLength); | |
608 | IMP_PYCALLBACK_STRING__const(wxPyTextDataObject, wxTextDataObject, GetText); | |
609 | IMP_PYCALLBACK__STRING(wxPyTextDataObject, wxTextDataObject, SetText); | |
610 | ||
611 | %} | |
612 | ||
613 | ||
614 | // Now define it for SWIG | |
8e0ecdd9 RD |
615 | |
616 | DocStr(wxPyTextDataObject, | |
617 | "wx.PyTextDataObject is a version of `wx.TextDataObject` that is | |
618 | Python-aware and knows how to reflect calls to its C++ virtual methods | |
619 | to methods in the Python derived class. You should derive from this | |
620 | class and overload `GetTextLength`, `GetText`, and `SetText` when you | |
621 | want to be able to provide text on demand instead of preloading it | |
622 | into the data object.", ""); | |
623 | ||
d14a1e28 RD |
624 | class wxPyTextDataObject : public wxTextDataObject { |
625 | public: | |
2b9048c5 | 626 | %pythonAppend wxPyTextDataObject "self._setCallbackInfo(self, PyTextDataObject)" |
d14a1e28 RD |
627 | |
628 | wxPyTextDataObject(const wxString& text = wxPyEmptyString); | |
629 | void _setCallbackInfo(PyObject* self, PyObject* _class); | |
630 | }; | |
631 | ||
632 | //--------------------------------------------------------------------------- | |
633 | ||
8e0ecdd9 RD |
634 | DocStr(wxBitmapDataObject, |
635 | "wx.BitmapDataObject is a specialization of wxDataObject for bitmap | |
636 | data. It can be used without change to paste data into the `wx.Clipboard` | |
637 | or a `wx.DropSource`. | |
638 | ", " | |
639 | :see: `wx.PyBitmapDataObject` if you wish to override `GetBitmap` to increase efficiency."); | |
640 | ||
d14a1e28 RD |
641 | class wxBitmapDataObject : public wxDataObjectSimple { |
642 | public: | |
8e0ecdd9 RD |
643 | DocCtorStr( |
644 | wxBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap), | |
645 | "Constructor, optionally passing a bitmap (otherwise use `SetBitmap` | |
646 | later).", ""); | |
d14a1e28 | 647 | |
8e0ecdd9 RD |
648 | DocDeclStr( |
649 | wxBitmap , GetBitmap() const, | |
650 | "Returns the bitmap associated with the data object. You may wish to | |
651 | override this method (by deriving from `wx.PyBitmapDataObject`) when | |
652 | offering data on-demand, but this is not required by wxWidgets' | |
653 | internals. Use this method to get data in bitmap form from the | |
654 | `wx.Clipboard`.", ""); | |
655 | ||
656 | DocDeclStr( | |
657 | void , SetBitmap(const wxBitmap& bitmap), | |
658 | "Sets the bitmap associated with the data object. This method is called | |
659 | when the data object receives data. Usually there will be no reason to | |
660 | override this function.", ""); | |
661 | ||
0eae5d09 | 662 | %property(Bitmap, GetBitmap, SetBitmap, doc="See `GetBitmap` and `SetBitmap`"); |
d14a1e28 RD |
663 | }; |
664 | ||
665 | ||
666 | ||
8e0ecdd9 RD |
667 | |
668 | ||
d14a1e28 RD |
669 | %{ // Create a new class for wxPython to use |
670 | class wxPyBitmapDataObject : public wxBitmapDataObject { | |
671 | public: | |
672 | wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap) | |
673 | : wxBitmapDataObject(bitmap) {} | |
674 | ||
675 | wxBitmap GetBitmap() const; | |
676 | void SetBitmap(const wxBitmap& bitmap); | |
677 | PYPRIVATE; | |
678 | }; | |
679 | ||
680 | wxBitmap wxPyBitmapDataObject::GetBitmap() const { | |
681 | wxBitmap* rval = &wxNullBitmap; | |
6e6b3557 | 682 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
d14a1e28 RD |
683 | if (wxPyCBH_findCallback(m_myInst, "GetBitmap")) { |
684 | PyObject* ro; | |
685 | wxBitmap* ptr; | |
686 | ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()")); | |
687 | if (ro) { | |
688 | if (wxPyConvertSwigPtr(ro, (void **)&ptr, wxT("wxBitmap"))) | |
689 | rval = ptr; | |
690 | Py_DECREF(ro); | |
691 | } | |
692 | } | |
da32eb53 | 693 | wxPyEndBlockThreads(blocked); |
d14a1e28 RD |
694 | return *rval; |
695 | } | |
8e0ecdd9 | 696 | |
d14a1e28 | 697 | void wxPyBitmapDataObject::SetBitmap(const wxBitmap& bitmap) { |
6e6b3557 | 698 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
d14a1e28 | 699 | if (wxPyCBH_findCallback(m_myInst, "SetBitmap")) { |
a72f4631 | 700 | PyObject* bo = wxPyConstructObject((void*)&bitmap, wxT("wxBitmap"), false); |
d14a1e28 RD |
701 | wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", bo)); |
702 | Py_DECREF(bo); | |
703 | } | |
da32eb53 | 704 | wxPyEndBlockThreads(blocked); |
d14a1e28 RD |
705 | } |
706 | %} | |
707 | ||
708 | ||
709 | ||
8e0ecdd9 RD |
710 | |
711 | DocStr(wxPyBitmapDataObject, | |
712 | "wx.PyBitmapDataObject is a version of `wx.BitmapDataObject` that is | |
713 | Python-aware and knows how to reflect calls to its C++ virtual methods | |
714 | to methods in the Python derived class. To be able to provide bitmap | |
715 | data on demand derive from this class and overload `GetBitmap`.", ""); | |
716 | ||
d14a1e28 RD |
717 | class wxPyBitmapDataObject : public wxBitmapDataObject { |
718 | public: | |
2b9048c5 | 719 | %pythonAppend wxPyBitmapDataObject "self._setCallbackInfo(self, PyBitmapDataObject)" |
d14a1e28 RD |
720 | |
721 | wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap); | |
722 | void _setCallbackInfo(PyObject* self, PyObject* _class); | |
723 | }; | |
724 | ||
725 | //--------------------------------------------------------------------------- | |
726 | ||
727 | ||
8e0ecdd9 RD |
728 | DocStr(wxFileDataObject, |
729 | "wx.FileDataObject is a specialization of `wx.DataObjectSimple` for | |
730 | file names. The program works with it just as if it were a list of | |
731 | absolute file names, but internally it uses the same format as | |
732 | Explorer and other compatible programs under Windows or GNOME/KDE | |
733 | filemanager under Unix which makes it possible to receive files from | |
23f8f985 | 734 | them using this class.", ""); |
8e0ecdd9 | 735 | |
d14a1e28 RD |
736 | class wxFileDataObject : public wxDataObjectSimple |
737 | { | |
738 | public: | |
8e0ecdd9 RD |
739 | DocCtorStr( |
740 | wxFileDataObject(), | |
741 | "", ""); | |
d14a1e28 | 742 | |
8e0ecdd9 RD |
743 | DocDeclAStr( |
744 | const wxArrayString& , GetFilenames(), | |
745 | "GetFilenames(self) -> [names]", | |
746 | "Returns a list of file names.", ""); | |
747 | ||
748 | DocDeclStr( | |
749 | void , AddFile(const wxString &filename), | |
750 | "Adds a file to the list of files represented by this data object.", ""); | |
751 | ||
42e2bbb9 | 752 | %property(Filenames, GetFilenames, doc="See `GetFilenames`"); |
d14a1e28 RD |
753 | }; |
754 | ||
d14a1e28 RD |
755 | //--------------------------------------------------------------------------- |
756 | ||
8e0ecdd9 RD |
757 | DocStr(wxCustomDataObject, |
758 | "wx.CustomDataObject is a specialization of `wx.DataObjectSimple` for | |
759 | some application-specific data in arbitrary format. Python strings | |
760 | are used for getting and setting data, but any picklable object can | |
761 | easily be transfered via strings. A copy of the data is stored in the | |
762 | data object.", ""); | |
763 | ||
d14a1e28 RD |
764 | class wxCustomDataObject : public wxDataObjectSimple { |
765 | public: | |
23f8f985 RD |
766 | %nokwargs wxCustomDataObject; |
767 | wxCustomDataObject(const wxDataFormat& format); | |
768 | %extend { | |
769 | wxCustomDataObject(const wxString& formatName) { | |
770 | return new wxCustomDataObject(wxDataFormat(formatName)); | |
771 | } | |
772 | } | |
773 | wxCustomDataObject(); | |
774 | ||
8e0ecdd9 | 775 | |
d14a1e28 | 776 | %extend { |
214c4fbe RD |
777 | DocAStr(SetData, |
778 | "SetData(self, String data) -> bool", | |
779 | "Copy the data value to the data object.", ""); | |
d14a1e28 | 780 | bool SetData(PyObject* data) { |
8e0ecdd9 | 781 | bool rval; |
6e6b3557 | 782 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
d14a1e28 | 783 | if (PyString_Check(data)) { |
8e0ecdd9 | 784 | rval = self->SetData(PyString_Size(data), PyString_AsString(data)); |
d14a1e28 RD |
785 | } |
786 | else { | |
787 | // raise a TypeError if not a string | |
788 | PyErr_SetString(PyExc_TypeError, "String expected."); | |
a72f4631 | 789 | rval = false; |
d14a1e28 | 790 | } |
8e0ecdd9 RD |
791 | wxPyEndBlockThreads(blocked); |
792 | return rval; | |
d14a1e28 RD |
793 | } |
794 | } | |
8e0ecdd9 RD |
795 | %pythoncode { TakeData = SetData } |
796 | ||
797 | DocDeclStr( | |
798 | size_t , GetSize(), | |
799 | "Get the size of the data.", ""); | |
800 | ||
d14a1e28 | 801 | |
d14a1e28 | 802 | %extend { |
214c4fbe RD |
803 | DocAStr(GetData, |
804 | "GetData(self) -> String", | |
805 | "Returns the data bytes from the data object as a string.", ""); | |
d14a1e28 | 806 | PyObject* GetData() { |
8e0ecdd9 | 807 | PyObject* obj; |
6e6b3557 | 808 | wxPyBlock_t blocked = wxPyBeginBlockThreads(); |
8e0ecdd9 | 809 | obj = PyString_FromStringAndSize((char*)self->GetData(), self->GetSize()); |
c61fb035 RD |
810 | wxPyEndBlockThreads(blocked); |
811 | return obj; | |
d14a1e28 RD |
812 | } |
813 | } | |
0eae5d09 RD |
814 | |
815 | %property(Data, GetData, SetData, doc="See `GetData` and `SetData`"); | |
816 | %property(Size, GetSize, doc="See `GetSize`"); | |
817 | ||
d14a1e28 RD |
818 | }; |
819 | ||
820 | ||
d14a1e28 RD |
821 | //--------------------------------------------------------------------------- |
822 | ||
8e0ecdd9 RD |
823 | DocStr(wxURLDataObject, |
824 | "This data object holds a URL in a format that is compatible with some | |
825 | browsers such that it is able to be dragged to or from them.", ""); | |
095315e2 | 826 | class wxURLDataObject : public wxDataObject/*Composite*/ { |
d14a1e28 | 827 | public: |
d4645514 | 828 | wxURLDataObject(const wxString& url = wxPyEmptyString); |
d14a1e28 | 829 | |
8e0ecdd9 RD |
830 | DocDeclStr( |
831 | wxString , GetURL(), | |
832 | "Returns a string containing the current URL.", ""); | |
833 | ||
834 | DocDeclStr( | |
835 | void , SetURL(const wxString& url), | |
836 | "Set the URL.", ""); | |
837 | ||
d14a1e28 RD |
838 | }; |
839 | ||
840 | //--------------------------------------------------------------------------- | |
841 | ||
c1201401 | 842 | #if defined(__WXMSW__) || defined(__WXMAC__) |
d14a1e28 RD |
843 | |
844 | %{ | |
845 | #include <wx/metafile.h> | |
846 | %} | |
847 | ||
848 | class wxMetafileDataObject : public wxDataObjectSimple | |
849 | { | |
850 | public: | |
851 | wxMetafileDataObject(); | |
852 | ||
853 | void SetMetafile(const wxMetafile& metafile); | |
854 | wxMetafile GetMetafile() const; | |
855 | }; | |
856 | ||
857 | ||
858 | #else | |
859 | %{ | |
860 | class wxMetafileDataObject : public wxDataObjectSimple | |
861 | { | |
862 | public: | |
81cfe5e1 | 863 | wxMetafileDataObject() { wxPyRaiseNotImplemented(); } |
d14a1e28 RD |
864 | }; |
865 | %} | |
866 | ||
867 | class wxMetafileDataObject : public wxDataObjectSimple | |
868 | { | |
869 | public: | |
870 | wxMetafileDataObject(); | |
871 | }; | |
872 | ||
873 | #endif | |
874 | ||
875 | //--------------------------------------------------------------------------- | |
876 | //--------------------------------------------------------------------------- | |
877 |