1 /////////////////////////////////////////////////////////////////////////////
 
   3 // Purpose:     SWIG definitions for the Clipboard and Drag-n-drop classes
 
   7 // Created:     31-October-1999
 
   9 // Copyright:   (c) 1999 by Total Control Software
 
  10 // Licence:     wxWindows license
 
  11 /////////////////////////////////////////////////////////////////////////////
 
  18 #include <wx/dataobj.h>
 
  19 #include <wx/clipbrd.h>
 
  23 //----------------------------------------------------------------------
 
  26 %include my_typemaps.i
 
  28 // Import some definitions of other classes, etc.
 
  34 %pragma(python) code = "import wx"
 
  36 //---------------------------------------------------------------------------
 
  38     // Put some wx default wxChar* values into wxStrings.
 
  39     static const wxString wxPyEmptyString(wxT(""));
 
  41 //----------------------------------------------------------------------
 
  68 //----------------------------------------------------------------------
 
  72     wxDataFormat( wxDataFormatId type );
 
  75     void SetType(wxDataFormatId format);
 
  76     wxDataFormatId GetType() const;
 
  78     wxString GetId() const;
 
  79     void SetId(const wxString& format);
 
  82 %new wxDataFormat* wxCustomDataFormat(const wxString &id);
 
  83 %{  // An alternate constructor...
 
  84     wxDataFormat* wxCustomDataFormat(const wxString &id) {
 
  85         return new wxDataFormat(id);
 
  91 wxDataFormat wxPyFormatInvalid;
 
  94 %name(wxFormatInvalid) wxDataFormat wxPyFormatInvalid;
 
  98 //----------------------------------------------------------------------
 
 102 class wxDataObject {      //  An abstract base class
 
 105         Get  = 0x01,    // format is supported by GetDataHere()
 
 106         Set  = 0x02,    // format is supported by SetData()
 
 107         Both = 0x03     // format is supported by both (unused currently)
 
 112     wxDataFormat GetPreferredFormat(Direction dir = wxDataObject::Get);
 
 113     size_t GetFormatCount(Direction dir = wxDataObject::Get);
 
 114     void GetAllFormats(wxDataFormat *formats,
 
 115                        Direction dir = wxDataObject::Get);
 
 116     size_t GetDataSize(const wxDataFormat& format);
 
 117     bool GetDataHere(const wxDataFormat& format, void *buf);
 
 118     bool SetData(const wxDataFormat& format,
 
 119                  size_t len, const void * buf);
 
 120     bool IsSupportedFormat(const wxDataFormat& format);
 
 123 //----------------------------------------------------------------------
 
 125 class wxDataObjectSimple : public wxDataObject {
 
 127     wxDataObjectSimple(const wxDataFormat& format = wxPyFormatInvalid);
 
 129     const wxDataFormat& GetFormat();
 
 130     void SetFormat(const wxDataFormat& format);
 
 135 %{  // Create a new class for wxPython to use
 
 136 class wxPyDataObjectSimple : public wxDataObjectSimple {
 
 138     wxPyDataObjectSimple(const wxDataFormat& format = wxPyFormatInvalid)
 
 139         : wxDataObjectSimple(format) {}
 
 141     DEC_PYCALLBACK_SIZET_(GetDataSize);
 
 142     bool GetDataHere(void *buf);
 
 143     bool SetData(size_t len, const void *buf);
 
 147 IMP_PYCALLBACK_SIZET_(wxPyDataObjectSimple, wxDataObjectSimple, GetDataSize);
 
 149 bool wxPyDataObjectSimple::GetDataHere(void *buf) {
 
 150     // We need to get the data for this object and write it to buf.  I think
 
 151     // the best way to do this for wxPython is to have the Python method
 
 152     // return either a string or None and then act appropriately with the
 
 156     wxPyBeginBlockThreads();
 
 157     if (m_myInst.findCallback("GetDataHere")) {
 
 159         ro = m_myInst.callCallbackObj(Py_BuildValue("()"));
 
 161             rval = (ro != Py_None && PyString_Check(ro));
 
 163                 memcpy(buf, PyString_AsString(ro), PyString_Size(ro));
 
 167     wxPyEndBlockThreads();
 
 171 bool wxPyDataObjectSimple::SetData(size_t len, const void *buf) {
 
 172     // For this one we simply need to make a string from buf and len
 
 173     // and send it to the Python method.
 
 175     wxPyBeginBlockThreads();
 
 176     if (m_myInst.findCallback("SetData")) {
 
 177         PyObject* data = PyString_FromStringAndSize((char*)buf, len);
 
 178         rval = m_myInst.callCallback(Py_BuildValue("(O)", data));
 
 181     wxPyEndBlockThreads();
 
 188 // Now define it for SWIG
 
 189 class wxPyDataObjectSimple : public wxDataObjectSimple {
 
 191     wxPyDataObjectSimple(const wxDataFormat& format = wxPyFormatInvalid);
 
 192     void _setCallbackInfo(PyObject* self, PyObject* _class);
 
 193     %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxPyDataObjectSimple)"
 
 196 //----------------------------------------------------------------------
 
 198 class wxDataObjectComposite : public wxDataObject {
 
 200     wxDataObjectComposite();
 
 202     void Add(wxDataObjectSimple *dataObject, int preferred = FALSE);
 
 203     %pragma(python) addtomethod = "Add:_args[0].thisown = 0"
 
 208 //----------------------------------------------------------------------
 
 210 class wxTextDataObject : public wxDataObjectSimple {
 
 212     wxTextDataObject(const wxString& text = wxPyEmptyString);
 
 214     size_t GetTextLength();
 
 216     void SetText(const wxString& text);
 
 221 %{  // Create a new class for wxPython to use
 
 222 class wxPyTextDataObject : public wxTextDataObject {
 
 224     wxPyTextDataObject(const wxString& text = wxPyEmptyString)
 
 225         : wxTextDataObject(text) {}
 
 227     DEC_PYCALLBACK_SIZET_(GetTextLength);
 
 228     DEC_PYCALLBACK_STRING_(GetText);
 
 229     DEC_PYCALLBACK__STRING(SetText);
 
 233 IMP_PYCALLBACK_SIZET_(wxPyTextDataObject, wxTextDataObject, GetTextLength);
 
 234 IMP_PYCALLBACK_STRING_(wxPyTextDataObject, wxTextDataObject, GetText);
 
 235 IMP_PYCALLBACK__STRING(wxPyTextDataObject, wxTextDataObject, SetText);
 
 240 // Now define it for SWIG
 
 241 class wxPyTextDataObject : public wxTextDataObject {
 
 243     wxPyTextDataObject(const wxString& text = wxPyEmptyString);
 
 244     void _setCallbackInfo(PyObject* self, PyObject* _class);
 
 245     %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxPyTextDataObject)"
 
 248 //----------------------------------------------------------------------
 
 250 class wxBitmapDataObject : public wxDataObjectSimple {
 
 252     wxBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
 
 254     wxBitmap GetBitmap();
 
 255     void SetBitmap(const wxBitmap& bitmap);
 
 260 %{  // Create a new class for wxPython to use
 
 261 class wxPyBitmapDataObject : public wxBitmapDataObject {
 
 263     wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap)
 
 264         : wxBitmapDataObject(bitmap) {}
 
 266     wxBitmap GetBitmap();
 
 267     void SetBitmap(const wxBitmap& bitmap);
 
 271 wxBitmap wxPyBitmapDataObject::GetBitmap() {
 
 272     wxBitmap* rval = &wxNullBitmap;
 
 273     wxPyBeginBlockThreads();
 
 274     if (m_myInst.findCallback("GetBitmap")) {
 
 277         ro = m_myInst.callCallbackObj(Py_BuildValue("()"));
 
 279             if (!SWIG_GetPtrObj(ro, (void **)&ptr, "_wxBitmap_p"))
 
 284     wxPyEndBlockThreads();
 
 288 void wxPyBitmapDataObject::SetBitmap(const wxBitmap& bitmap) {
 
 289     wxPyBeginBlockThreads();
 
 290     if (m_myInst.findCallback("SetBitmap")) {
 
 291         PyObject* bo = wxPyConstructObject((void*)&bitmap, "wxBitmap");
 
 292         m_myInst.callCallback(Py_BuildValue("(O)", bo));
 
 295     wxPyEndBlockThreads();
 
 301 // Now define it for SWIG
 
 302 class wxPyBitmapDataObject : public wxBitmapDataObject {
 
 304     wxPyBitmapDataObject(const wxBitmap& bitmap = wxNullBitmap);
 
 305     void _setCallbackInfo(PyObject* self, PyObject* _class);
 
 306     %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxPyBitmapDataObject)"
 
 310 //----------------------------------------------------------------------
 
 312 class wxFileDataObject : public wxDataObjectSimple
 
 317     //const wxArrayString& GetFilenames();
 
 319         PyObject* GetFilenames() {
 
 320             const wxArrayString& strings = self->GetFilenames();
 
 321             return wxArrayString2PyList_helper(strings);
 
 325     void AddFile(const wxString &filename);
 
 330 //----------------------------------------------------------------------
 
 332 class wxCustomDataObject : public wxDataObjectSimple {
 
 334     wxCustomDataObject(const wxDataFormat& format = wxPyFormatInvalid);
 
 336     //void TakeData(size_t size, void *data);
 
 337     //bool SetData(size_t size, const void *buf);
 
 339         void TakeData(PyObject* data) {
 
 340             if (PyString_Check(data)) {
 
 341                 self->SetData(PyString_Size(data), PyString_AsString(data));
 
 344         bool SetData(PyObject* data) {
 
 345             if (PyString_Check(data)) {
 
 346                 return self->SetData(PyString_Size(data), PyString_AsString(data));
 
 356         PyObject* GetData() {
 
 357             return PyString_FromStringAndSize((char*)self->GetData(), self->GetSize());
 
 365 //----------------------------------------------------------------------
 
 367 class wxURLDataObject : public wxDataObjectComposite {
 
 372     void SetURL(const wxString& url);
 
 375 //----------------------------------------------------------------------
 
 380 #include <wx/metafile.h>
 
 383 class wxMetafileDataObject : public wxDataObjectSimple
 
 386     wxMetafileDataObject();
 
 388     void SetMetafile(const wxMetafile& metafile);
 
 389     wxMetafile GetMetafile() const;
 
 394 //----------------------------------------------------------------------
 
 395 //----------------------------------------------------------------------
 
 396 //----------------------------------------------------------------------
 
 398 class wxClipboard : public wxObject {
 
 404     bool IsOpened() const;
 
 406     bool AddData( wxDataObject *data );
 
 407     %pragma(python) addtomethod = "AddData:_args[0].thisown = 0"
 
 408     bool SetData( wxDataObject *data );
 
 409     %pragma(python) addtomethod = "SetData:_args[0].thisown = 0"
 
 411     bool IsSupported( const wxDataFormat& format );
 
 412     bool GetData( wxDataObject& data );
 
 415     void UsePrimarySelection( int primary = FALSE );
 
 419     // See below in the init function...
 
 420     wxClipboard* wxPyTheClipboard;
 
 423 %name(wxTheClipboard) wxClipboard* wxPyTheClipboard;
 
 426 //----------------------------------------------------------------------
 
 427 //----------------------------------------------------------------------
 
 429 // flags for wxDropSource::DoDragDrop()
 
 433     wxDrag_CopyOnly    = 0, // allow only copying
 
 434     wxDrag_AllowMove   = 1, // allow moving (copying is always allowed)
 
 435     wxDrag_DefaultMove = 3  // the default operation is move, not copy
 
 440     wxDragError,    // error prevented the d&d operation from completing
 
 441     wxDragNone,     // drag target didn't accept the data
 
 442     wxDragCopy,     // the data was successfully copied
 
 443     wxDragMove,     // the data was successfully moved (MSW only)
 
 444     wxDragLink,     // operation is a drag-link
 
 445     wxDragCancel    // the operation was cancelled by user (not an error)
 
 448 bool wxIsDragResultOk(wxDragResult res);
 
 450 //----------------------------------------------------------------------
 
 452 class wxPyDropSource : public wxDropSource {
 
 455     wxPyDropSource(wxWindow *win = NULL,
 
 456                    const wxCursor &cursorCopy = wxNullCursor,
 
 457                    const wxCursor &cursorMove = wxNullCursor,
 
 458                    const wxCursor &cursorStop = wxNullCursor)
 
 459         : wxDropSource(win, cursorCopy, cursorMove, cursorStop) {}
 
 461     wxPyDropSource(wxWindow *win = NULL,
 
 462                    const wxIcon &go = wxNullIcon)
 
 463         : wxDropSource(win, go) {}
 
 465     ~wxPyDropSource() { }
 
 467     DEC_PYCALLBACK_BOOL_DR(GiveFeedback);
 
 471 IMP_PYCALLBACK_BOOL_DR(wxPyDropSource, wxDropSource, GiveFeedback);
 
 476 %name(wxDropSource) class wxPyDropSource {
 
 479     wxPyDropSource(wxWindow *win = NULL,
 
 480                  const wxCursor &cursorCopy = wxNullCursor,
 
 481                  const wxCursor &cursorMove = wxNullCursor,
 
 482                  const wxCursor &cursorStop = wxNullCursor);
 
 484     wxPyDropSource(wxWindow *win = NULL,
 
 485                    const wxIcon &go = wxNullIcon);
 
 488     void _setCallbackInfo(PyObject* self, PyObject* _class, int incref);
 
 489     %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxDropSource, 0)"
 
 492     void SetData(wxDataObject& data);
 
 493     wxDataObject *GetDataObject();
 
 494     void SetCursor(wxDragResult res, const wxCursor& cursor);
 
 495     wxDragResult DoDragDrop(int flags = wxDrag_CopyOnly);
 
 497     bool base_GiveFeedback(wxDragResult effect);
 
 500 //----------------------------------------------------------------------
 
 502 // Just a place holder for the type system.  The real base class for
 
 503 // wxPython is wxPyDropTarget
 
 510 class wxPyDropTarget : public wxDropTarget {
 
 512     wxPyDropTarget(wxDataObject *dataObject = NULL)
 
 513         : wxDropTarget(dataObject) {}
 
 515 //      DEC_PYCALLBACK_SIZET_(GetFormatCount);
 
 516 //      DEC_PYCALLBACK_DATAFMT_SIZET(GetFormat);
 
 518     DEC_PYCALLBACK__(OnLeave);
 
 519     DEC_PYCALLBACK_DR_2WXCDR(OnEnter);
 
 520     DEC_PYCALLBACK_DR_2WXCDR(OnDragOver);
 
 521     DEC_PYCALLBACK_DR_2WXCDR_pure(OnData);
 
 522     DEC_PYCALLBACK_BOOL_INTINT(OnDrop);
 
 527 //  IMP_PYCALLBACK_SIZET_(wxPyDropTarget, wxDropTarget, GetFormatCount);
 
 528 //  IMP__PYCALLBACK_DATAFMT_SIZET(wxPyDropTarget, wxDropTarget, GetFormat);
 
 530 IMP_PYCALLBACK__(wxPyDropTarget, wxDropTarget, OnLeave);
 
 531 IMP_PYCALLBACK_DR_2WXCDR(wxPyDropTarget, wxDropTarget, OnEnter);
 
 532 IMP_PYCALLBACK_DR_2WXCDR(wxPyDropTarget, wxDropTarget, OnDragOver);
 
 533 IMP_PYCALLBACK_DR_2WXCDR_pure(wxPyDropTarget, wxDropTarget, OnData);
 
 534 IMP_PYCALLBACK_BOOL_INTINT(wxPyDropTarget, wxDropTarget, OnDrop);
 
 539 class wxPyDropTarget : public wxDropTarget {
 
 541     wxPyDropTarget(wxDataObject *dataObject = NULL);
 
 542     %pragma(python) addtomethod = "__init__:if _args:_args[0].thisown = 0"
 
 543     void _setCallbackInfo(PyObject* self, PyObject* _class);
 
 544     %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxPyDropTarget)"
 
 548     wxDataObject *GetDataObject();
 
 549     void SetDataObject(wxDataObject *dataObject);
 
 550     %pragma(python) addtomethod = "SetDataObject:if _args:_args[0].thisown = 0"
 
 552 //      size_t base_GetFormatCount();
 
 553 //      wxDataFormat base_GetFormat(size_t n);
 
 555     wxDragResult base_OnEnter(wxCoord x, wxCoord y, wxDragResult def);
 
 556     wxDragResult base_OnDragOver(wxCoord x, wxCoord y, wxDragResult def);
 
 558     bool base_OnDrop(wxCoord x, wxCoord y);
 
 559     //wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def) = 0;
 
 561     // **** not sure about this one
 
 567 //----------------------------------------------------------------------
 
 570 class wxPyTextDropTarget : public wxTextDropTarget {
 
 572     wxPyTextDropTarget() {}
 
 574     DEC_PYCALLBACK_BOOL_INTINTSTR_pure(OnDropText);
 
 576     DEC_PYCALLBACK__(OnLeave);
 
 577     DEC_PYCALLBACK_DR_2WXCDR(OnEnter);
 
 578     DEC_PYCALLBACK_DR_2WXCDR(OnDragOver);
 
 579     DEC_PYCALLBACK_DR_2WXCDR(OnData);
 
 580     DEC_PYCALLBACK_BOOL_INTINT(OnDrop);
 
 585 IMP_PYCALLBACK_BOOL_INTINTSTR_pure(wxPyTextDropTarget, wxTextDropTarget, OnDropText);
 
 586 IMP_PYCALLBACK__(wxPyTextDropTarget, wxTextDropTarget, OnLeave);
 
 587 IMP_PYCALLBACK_DR_2WXCDR(wxPyTextDropTarget, wxTextDropTarget, OnEnter);
 
 588 IMP_PYCALLBACK_DR_2WXCDR(wxPyTextDropTarget, wxTextDropTarget, OnDragOver);
 
 589 IMP_PYCALLBACK_DR_2WXCDR(wxPyTextDropTarget, wxTextDropTarget, OnData);
 
 590 IMP_PYCALLBACK_BOOL_INTINT(wxPyTextDropTarget, wxTextDropTarget, OnDrop);
 
 594 %name(wxTextDropTarget) class wxPyTextDropTarget : public wxPyDropTarget {
 
 596     wxPyTextDropTarget();
 
 597     void _setCallbackInfo(PyObject* self, PyObject* _class);
 
 598     %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxTextDropTarget)"
 
 600     //bool OnDropText(wxCoord x, wxCoord y, const wxString& text) = 0;
 
 601     wxDragResult base_OnEnter(wxCoord x, wxCoord y, wxDragResult def);
 
 602     wxDragResult base_OnDragOver(wxCoord x, wxCoord y, wxDragResult def);
 
 604     bool base_OnDrop(wxCoord x, wxCoord y);
 
 605     wxDragResult base_OnData(wxCoord x, wxCoord y, wxDragResult def);
 
 608 //----------------------------------------------------------------------
 
 610 class wxPyFileDropTarget : public wxFileDropTarget {
 
 612     wxPyFileDropTarget() {}
 
 614     virtual bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString& filenames);
 
 616     DEC_PYCALLBACK__(OnLeave);
 
 617     DEC_PYCALLBACK_DR_2WXCDR(OnEnter);
 
 618     DEC_PYCALLBACK_DR_2WXCDR(OnDragOver);
 
 619     DEC_PYCALLBACK_DR_2WXCDR(OnData);
 
 620     DEC_PYCALLBACK_BOOL_INTINT(OnDrop);
 
 625 bool wxPyFileDropTarget::OnDropFiles(wxCoord x, wxCoord y,
 
 626                                      const wxArrayString& filenames) {
 
 628     wxPyBeginBlockThreads();
 
 629     if (m_myInst.findCallback("OnDropFiles")) {
 
 630         PyObject* list = wxArrayString2PyList_helper(filenames);
 
 631         rval = m_myInst.callCallback(Py_BuildValue("(iiO)",x,y,list));
 
 634     wxPyEndBlockThreads();
 
 640 IMP_PYCALLBACK__(wxPyFileDropTarget, wxFileDropTarget, OnLeave);
 
 641 IMP_PYCALLBACK_DR_2WXCDR(wxPyFileDropTarget, wxFileDropTarget, OnEnter);
 
 642 IMP_PYCALLBACK_DR_2WXCDR(wxPyFileDropTarget, wxFileDropTarget, OnDragOver);
 
 643 IMP_PYCALLBACK_DR_2WXCDR(wxPyFileDropTarget, wxFileDropTarget, OnData);
 
 644 IMP_PYCALLBACK_BOOL_INTINT(wxPyFileDropTarget, wxFileDropTarget, OnDrop);
 
 649 %name(wxFileDropTarget) class wxPyFileDropTarget : public wxPyDropTarget
 
 652     wxPyFileDropTarget();
 
 653     void _setCallbackInfo(PyObject* self, PyObject* _class);
 
 654     %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxFileDropTarget)"
 
 656 //    bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString& filenames) = 0;
 
 657     wxDragResult base_OnEnter(wxCoord x, wxCoord y, wxDragResult def);
 
 658     wxDragResult base_OnDragOver(wxCoord x, wxCoord y, wxDragResult def);
 
 660     bool base_OnDrop(wxCoord x, wxCoord y);
 
 661     wxDragResult base_OnData(wxCoord x, wxCoord y, wxDragResult def);
 
 664 //----------------------------------------------------------------------
 
 665 //----------------------------------------------------------------------
 
 666 //----------------------------------------------------------------------
 
 670     wxPyTheClipboard = wxTheClipboard;
 
 671     wxPyPtrTypeMap_Add("wxDropSource", "wxPyDropSource");
 
 672     wxPyPtrTypeMap_Add("wxTextDropTarget", "wxPyTextDropTarget");
 
 673     wxPyPtrTypeMap_Add("wxFileDropTarget", "wxPyFileDropTarget");
 
 676 //----------------------------------------------------------------------