[wxPython] Custom data in the clipboard

How can I copy/paste custom data to/from the clipboard?
Why wxDataFormat doesn't accept a string but only an integer? So the
id must be generated using os specific functions.

from wxPython.wx import *

try:
    import win32clipboard
    id = win32clipboard.RegisterClipboardFormat("my_data")
    print "id:", id
except:
    id = 1234
my_data_format = wxDataFormat(id)

class MyDataObject(wxPyDataObjectSimple):
    def __init__(self, data=""):
        wxPyDataObjectSimple.__init__(self, my_data_format)
        self._data = data

    def GetDataHere(self):
        return self._data

    def GetDataSize():
        return len(self._data)

    def SetData(self, data):
        self._data = data
        return 1

if __name__ == "__main__":
    app = wxPySimpleApp()

    if wxTheClipboard.Open():
        if not wxTheClipboard.SetData(MyDataObject("Some text")):
            print "Data can't be copied to clipboard."
        wxTheClipboard.Close()
    else:
        print "Clipboard can't be opened."

    if wxTheClipboard.Open():
        if wxTheClipboard.IsSupported(my_data_format):
            data = MyDataObject()
            if not wxTheClipboard.GetData(data):
                print "Data can't be copied from clipboard."
            wxMessageBox("->" + data.GetDataHere() + "<-")
        else:
            print "Format not supported."
        wxTheClipboard.Close()
    else:
        print "Clipboard can't be opened."

···

--
Marco Barisione (ICQ 84277092)

I translated the code yo C++ and it works correctly. Why? Is it a
wxPython bug or I missed sometihng?

class MyDataObject : public wxDataObjectSimple{
public:
    MyDataObject(wxDataFormat & data_format, char * data="")
        : wxDataObjectSimple(data_format){
        m_data = strdup(data);
    }

    bool GetDataHere(void *buf) const{
        strcpy((char*)buf, m_data);
        return true;
    }

    size_t GetDataSize() const{
        return strlen(m_data) + 1;
    }

    bool SetData(size_t len, const void *buf){
        strncpy(m_data, (char*)buf, len);
        return true;
    }

private:
    char * m_data;
};

void clipboard(){
    wxDataFormat my_data_format("name");
    //unsigned int id = RegisterClipboardFormat("my_data");
    //wxDataFormat my_data_format(id);

    if (wxTheClipboard->Open()){
        if (!wxTheClipboard->SetData(new MyDataObject(my_data_format,
"Some text")))
            wxMessageBox("Data can't be copied to clipboard.");
        wxTheClipboard->Close();
    }
    else
        wxMessageBox("Clipboard can't be opened.");

    if (wxTheClipboard->Open()){
        if (wxTheClipboard->IsSupported(my_data_format)){
            MyDataObject data(my_data_format);
            if (!wxTheClipboard->GetData(data))
                wxMessageBox("Data can't be copied from clipboard.");
            char * buff = new char[data.GetDataSize()];
            data.GetDataHere(buff);
            wxMessageBox(buff);
            delete buff;
        }
        else
            wxMessageBox("Format not supported.");
        wxTheClipboard->Close();
    }
    else{
        wxMessageBox("Clipboard can't be opened.");
    }
}

···

On Mon, 29 Jul 2002 12:01:32 +0200, Marco Barisione <marco.bari@vene.ws> wrote:

How can I copy/paste custom data to/from the clipboard?
[...]

--
Marco Barisione (ICQ 84277092)

How can I copy/paste custom data to/from the clipboard?
Why wxDataFormat doesn't accept a string but only an integer? So the
id must be generated using os specific functions.

Try using wxCustomDataFormat and wxCustomData. Like so:

from wxPython.wx import *

my_data_format = wxCustomDataFormat("my_data")

class MyDataObject(wxCustomDataObject):
    def __init__(self, data=""):
        wxCustomDataObject.__init__(self, my_data_format)
        self.SetData(data)

if __name__ == "__main__":
    app = wxPySimpleApp()

    if wxTheClipboard.Open():
        if not wxTheClipboard.SetData(MyDataObject("Some text")):
            print "Data can't be copied to clipboard."
        wxTheClipboard.Close()
    else:
        print "Clipboard can't be opened."

    if wxTheClipboard.Open():
        if wxTheClipboard.IsSupported(my_data_format):
            data = MyDataObject()
            if not wxTheClipboard.GetData(data):
                print "Data can't be copied from clipboard."
            wxMessageBox("->" + data.GetData() + "<-")
        else:
            print "Format not supported."
        wxTheClipboard.Close()
    else:
        print "Clipboard can't be opened."

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!