From 8b53e5a2261d192bce49d40f0019f52f73b1557a Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Mon, 14 Dec 1998 16:13:49 +0000 Subject: [PATCH] New wxDataObject, DnD and Clipboard code A few more minor fixes git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1194 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- configure | 2 +- configure.in | 2 +- include/wx/dataobj.h | 19 ++ include/wx/defs.h | 3 +- include/wx/gtk/clipbrd.h | 30 +- include/wx/gtk/dataobj.h | 178 ++++++++++++ include/wx/gtk/dnd.h | 80 +----- include/wx/gtk1/clipbrd.h | 30 +- include/wx/gtk1/dataobj.h | 178 ++++++++++++ include/wx/gtk1/dnd.h | 80 +----- samples/controls/controls.cpp | 68 ++++- samples/image/image.cpp | 8 +- src/common/variant.cpp | 12 +- src/generic/listctrl.cpp | 10 +- src/gtk.inc | 1 + src/gtk/clipbrd.cpp | 509 +++++++++++++++++++++------------- src/gtk/dataobj.cpp | 56 ++++ src/gtk/dnd.cpp | 49 +++- src/gtk1/clipbrd.cpp | 509 +++++++++++++++++++++------------- src/gtk1/dataobj.cpp | 56 ++++ src/gtk1/dnd.cpp | 49 +++- 21 files changed, 1305 insertions(+), 624 deletions(-) create mode 100644 include/wx/dataobj.h create mode 100644 include/wx/gtk/dataobj.h create mode 100644 include/wx/gtk1/dataobj.h create mode 100644 src/gtk/dataobj.cpp create mode 100644 src/gtk1/dataobj.cpp diff --git a/configure b/configure index 5bd21457fe..7aca4c2ffa 100755 --- a/configure +++ b/configure @@ -4570,7 +4570,7 @@ DEFAULT_wxUSE_POSTSCRIPT=1 DEFAULT_wxUSE_IPC=1 DEFAULT_wxUSE_RESOURCES=1 DEFAULT_wxUSE_CONSTRAINTS=1 -DEFAULT_wxUSE_CLIPBOARD=0 +DEFAULT_wxUSE_CLIPBOARD=1 DEFAULT_wxUSE_DND=1 DEFAULT_wxUSE_MDI_ARCHITECTURE=1 diff --git a/configure.in b/configure.in index 5ac599ba21..6baea894c1 100644 --- a/configure.in +++ b/configure.in @@ -717,7 +717,7 @@ DEFAULT_wxUSE_POSTSCRIPT=1 DEFAULT_wxUSE_IPC=1 DEFAULT_wxUSE_RESOURCES=1 DEFAULT_wxUSE_CONSTRAINTS=1 -DEFAULT_wxUSE_CLIPBOARD=0 +DEFAULT_wxUSE_CLIPBOARD=1 DEFAULT_wxUSE_DND=1 DEFAULT_wxUSE_MDI_ARCHITECTURE=1 diff --git a/include/wx/dataobj.h b/include/wx/dataobj.h new file mode 100644 index 0000000000..ef2a97920d --- /dev/null +++ b/include/wx/dataobj.h @@ -0,0 +1,19 @@ +#ifndef _WX_DATAOBJ_H_BASE_ +#define _WX_DATAOBJ_H_BASE_ + +#if defined(__WXMSW__) +#include "wx/msw/ole/dataobj.h" +#elif defined(__WXMOTIF__) +#include "wx/motif/dnd.h" +#elif defined(__WXGTK__) +#include "wx/gtk/dataobj.h" +#elif defined(__WXQT__) +#include "wx/qt/dnd.h" +#elif defined(__WXMAC__) +#include "wx/mac/dnd.h" +#elif defined(__WXSTUBS__) +#include "wx/stubs/dnd.h" +#endif + +#endif + // _WX_DATAOBJ_H_BASE_ diff --git a/include/wx/defs.h b/include/wx/defs.h index cc731991c3..41987323ee 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -721,7 +721,8 @@ enum wxDataFormat wxDF_METAFILE = 3, /* CF_METAFILEPICT */ wxDF_DIB = 8, /* CF_DIB */ wxDF_OEMTEXT = 7, /* CF_OEMTEXT */ - wxDF_FILENAME = 15 /* CF_HDROP */ + wxDF_FILENAME = 15, /* CF_HDROP */ + wxDF_PRIVATE = 20 }; // Virtual keycodes diff --git a/include/wx/gtk/clipbrd.h b/include/wx/gtk/clipbrd.h index b8d488a747..5f7a92eead 100644 --- a/include/wx/gtk/clipbrd.h +++ b/include/wx/gtk/clipbrd.h @@ -18,8 +18,8 @@ #include "wx/defs.h" #include "wx/object.h" #include "wx/list.h" +#include "wx/dataobj.h" #include "wx/control.h" -#include "wx/dnd.h" // for wxDataObject #include "wx/module.h" //----------------------------------------------------------------------------- @@ -48,21 +48,32 @@ public: wxClipboard(); ~wxClipboard(); - virtual void SetData( wxDataObject *data ); + // open the clipboard before SetData() and GetData() + virtual bool Open(); - virtual bool IsSupportedFormat( wxDataFormat format ); - virtual bool ObtainData( wxDataFormat format ); + // close the clipboard after SetData() and GetData() + virtual void Close(); - // call these after ObtainData() - virtual size_t GetDataSize() const; - virtual void GetDataHere( void *data ) const; + // can be called several times + virtual bool SetData( wxDataObject *data ); + + // format available on the clipboard ? + // supply ID if private format, the same as wxPrivateDataObject::SetId() + virtual bool IsSupportedFormat( wxDataFormat format, const wxString &id = "" ); + + // fill data with data on the clipboard (if available) + virtual bool GetData( wxDataObject *data ); // clears wxTheClipboard and the system's clipboard if possible virtual void Clear(); // implementation + + GdkAtom GetTargetAtom( wxDataFormat format, const wxString &id = "" ); + + bool m_open; - wxDataObject *m_data; + wxList m_dataObjects; char *m_sentString, *m_receivedString; void *m_receivedTargets; @@ -71,8 +82,7 @@ public: bool m_formatSupported; GdkAtom m_targetRequested; - size_t m_receivedSize; - char *m_receivedData; + wxDataObject *m_receivedData; }; //----------------------------------------------------------------------------- diff --git a/include/wx/gtk/dataobj.h b/include/wx/gtk/dataobj.h new file mode 100644 index 0000000000..8128475f1b --- /dev/null +++ b/include/wx/gtk/dataobj.h @@ -0,0 +1,178 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: dataobj.h +// Purpose: declaration of the wxDataObject class +// Author: Robert Roebling +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin, Robert Roebling +// Licence: wxWindows license +/////////////////////////////////////////////////////////////////////////////// + +#ifndef __GTKDATAOBJECTH__ +#define __GTKDATAOBJECTH__ + +#ifdef __GNUG__ +#pragma interface +#endif + +#include "wx/defs.h" +#include "wx/object.h" +#include "wx/string.h" +#include "wx/bitmap.h" + +//------------------------------------------------------------------------- +// classes +//------------------------------------------------------------------------- + +class wxDataObject; +class wxTextDataObject; +class wxBitmapDataObject; +class wxPrivateDataObject; +class wxFileDataObject; + +//------------------------------------------------------------------------- +// wxDataObject +//------------------------------------------------------------------------- + +class wxDataObject: public wxObject +{ + DECLARE_ABSTRACT_CLASS( wxDataObject ) + +public: + + wxDataObject() {} + ~wxDataObject() {} + + virtual wxDataFormat GetFormat() const = 0; + + // implementation + + GdkAtom m_formatAtom; +}; + +// ---------------------------------------------------------------------------- +// wxTextDataObject is a specialization of wxDataObject for text data +// ---------------------------------------------------------------------------- + +class wxTextDataObject : public wxDataObject +{ + DECLARE_DYNAMIC_CLASS( wxTextDataObject ) + +public: + + wxTextDataObject() {} + wxTextDataObject( const wxString& strText ) + : m_strText(strText) { } + + virtual wxDataFormat GetFormat() const + { return wxDF_TEXT; } + + void SetText( const wxString& strText) + { m_strText = strText; } + + wxString GetText() + { return m_strText; } + +private: + wxString m_strText; + +}; + +// ---------------------------------------------------------------------------- +// wxFileDataObject is a specialization of wxDataObject for file names +// ---------------------------------------------------------------------------- + +class wxFileDataObject : public wxDataObject +{ + DECLARE_DYNAMIC_CLASS( wxFileDataObject ) + +public: + + wxFileDataObject(void) {} + + virtual wxDataFormat GetFormat() const + { return wxDF_FILENAME; } + + void AddFile( const wxString &file ) + { m_files += file; m_files += (char)0; } + + wxString GetFiles() + { return m_files; } + +private: + wxString m_files; + +}; + +// ---------------------------------------------------------------------------- +// wxBitmapDataObject is a specialization of wxDataObject for bitmaps +// ---------------------------------------------------------------------------- + +class wxBitmapDataObject : public wxDataObject +{ + DECLARE_DYNAMIC_CLASS( wxBitmapDataObject ) + +public: + + wxBitmapDataObject(void) {} + + virtual wxDataFormat GetFormat() const + { return wxDF_BITMAP; } + + void SetBitmap( const wxBitmap &bitmap ) + { m_bitmap = bitmap; } + + wxBitmap GetBitmap() + { return m_bitmap; } + +private: + wxBitmap m_bitmap; +}; + +// ---------------------------------------------------------------------------- +// wxPrivateDataObject is a specialization of wxDataObject for app specific data +// ---------------------------------------------------------------------------- + +class wxPrivateDataObject : public wxDataObject +{ + DECLARE_DYNAMIC_CLASS( wxPrivateDataObject ) + +public: + + wxPrivateDataObject() + { m_size = 0; m_data = (char*) NULL; } + + ~wxPrivateDataObject() + { if (m_data) delete[] m_data; } + + virtual wxDataFormat GetFormat() const + { return wxDF_PRIVATE; } + + // the string ID identifies the format of clipboard or DnD data. a word + // processor would e.g. add a wxTextDataObject and a wxPrivateDataObject + // to the clipboard - the latter with the Id "WXWORD_FORMAT". + + void SetId( const wxString& id ) + { m_id = id; } + + wxString GetId() + { return m_id; } + + // will make internal copy + void SetData( const char *data, size_t size ); + + size_t GetDataSize() + { return m_size; } + + char* GetData() + { return m_data; } + +private: + size_t m_size; + char* m_data; + wxString m_id; +}; + + +#endif + //__GTKDNDH__ + diff --git a/include/wx/gtk/dnd.h b/include/wx/gtk/dnd.h index 937fe1aa89..e319f0d415 100644 --- a/include/wx/gtk/dnd.h +++ b/include/wx/gtk/dnd.h @@ -18,6 +18,7 @@ #include "wx/defs.h" #include "wx/object.h" #include "wx/string.h" +#include "wx/dataobj.h" #include "wx/cursor.h" //------------------------------------------------------------------------- @@ -36,91 +37,12 @@ class wxWindow; -class wxDataObject; -class wxTextDataObject; -class wxFileDataObject; - class wxDropTarget; class wxTextDropTarget; class wxFileDropTarget; class wxDropSource; -//------------------------------------------------------------------------- -// wxDataObject -//------------------------------------------------------------------------- - -class wxDataObject: public wxObject -{ -public: - - wxDataObject() {}; - ~wxDataObject() {}; - - virtual wxDataFormat GetPreferredFormat() const = 0; - virtual bool IsSupportedFormat( wxDataFormat format ) const = 0; - virtual size_t GetDataSize() const = 0; - virtual void GetDataHere( void *data ) const = 0; - -}; - -// ---------------------------------------------------------------------------- -// wxTextDataObject is a specialization of wxDataObject for text data -// ---------------------------------------------------------------------------- - -class wxTextDataObject : public wxDataObject -{ -public: - - wxTextDataObject() { } - wxTextDataObject(const wxString& strText) : m_strText(strText) { } - void Init(const wxString& strText) { m_strText = strText; } - - virtual wxDataFormat GetPreferredFormat() const - { return wxDF_TEXT; } - - virtual bool IsSupportedFormat(wxDataFormat format) const - { return format == wxDF_TEXT; } - - virtual size_t GetDataSize() const - { return m_strText.Len() + 1; } // +1 for trailing '\0' - - virtual void GetDataHere( void *data ) const - { memcpy(data, m_strText.c_str(), GetDataSize()); } - -private: - wxString m_strText; - -}; - -// ---------------------------------------------------------------------------- -// wxFileDataObject is a specialization of wxDataObject for file names -// ---------------------------------------------------------------------------- - -class wxFileDataObject : public wxDataObject -{ -public: - - wxFileDataObject(void) { } - void AddFile( const wxString &file ) - { m_files += file; m_files += '\0'; } - - virtual wxDataFormat GetPreferredFormat() const - { return wxDF_FILENAME; } - - virtual bool IsSupportedFormat( wxDataFormat format ) const - { return format == wxDF_FILENAME; } - - virtual size_t GetDataSize() const - { return m_files.Len(); } // no trailing '\0' - - virtual void GetDataHere( void *data ) const - { memcpy(data, m_files.c_str(), GetDataSize()); } - -private: - wxString m_files; - -}; //------------------------------------------------------------------------- // wxDropTarget //------------------------------------------------------------------------- diff --git a/include/wx/gtk1/clipbrd.h b/include/wx/gtk1/clipbrd.h index b8d488a747..5f7a92eead 100644 --- a/include/wx/gtk1/clipbrd.h +++ b/include/wx/gtk1/clipbrd.h @@ -18,8 +18,8 @@ #include "wx/defs.h" #include "wx/object.h" #include "wx/list.h" +#include "wx/dataobj.h" #include "wx/control.h" -#include "wx/dnd.h" // for wxDataObject #include "wx/module.h" //----------------------------------------------------------------------------- @@ -48,21 +48,32 @@ public: wxClipboard(); ~wxClipboard(); - virtual void SetData( wxDataObject *data ); + // open the clipboard before SetData() and GetData() + virtual bool Open(); - virtual bool IsSupportedFormat( wxDataFormat format ); - virtual bool ObtainData( wxDataFormat format ); + // close the clipboard after SetData() and GetData() + virtual void Close(); - // call these after ObtainData() - virtual size_t GetDataSize() const; - virtual void GetDataHere( void *data ) const; + // can be called several times + virtual bool SetData( wxDataObject *data ); + + // format available on the clipboard ? + // supply ID if private format, the same as wxPrivateDataObject::SetId() + virtual bool IsSupportedFormat( wxDataFormat format, const wxString &id = "" ); + + // fill data with data on the clipboard (if available) + virtual bool GetData( wxDataObject *data ); // clears wxTheClipboard and the system's clipboard if possible virtual void Clear(); // implementation + + GdkAtom GetTargetAtom( wxDataFormat format, const wxString &id = "" ); + + bool m_open; - wxDataObject *m_data; + wxList m_dataObjects; char *m_sentString, *m_receivedString; void *m_receivedTargets; @@ -71,8 +82,7 @@ public: bool m_formatSupported; GdkAtom m_targetRequested; - size_t m_receivedSize; - char *m_receivedData; + wxDataObject *m_receivedData; }; //----------------------------------------------------------------------------- diff --git a/include/wx/gtk1/dataobj.h b/include/wx/gtk1/dataobj.h new file mode 100644 index 0000000000..8128475f1b --- /dev/null +++ b/include/wx/gtk1/dataobj.h @@ -0,0 +1,178 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: dataobj.h +// Purpose: declaration of the wxDataObject class +// Author: Robert Roebling +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin, Robert Roebling +// Licence: wxWindows license +/////////////////////////////////////////////////////////////////////////////// + +#ifndef __GTKDATAOBJECTH__ +#define __GTKDATAOBJECTH__ + +#ifdef __GNUG__ +#pragma interface +#endif + +#include "wx/defs.h" +#include "wx/object.h" +#include "wx/string.h" +#include "wx/bitmap.h" + +//------------------------------------------------------------------------- +// classes +//------------------------------------------------------------------------- + +class wxDataObject; +class wxTextDataObject; +class wxBitmapDataObject; +class wxPrivateDataObject; +class wxFileDataObject; + +//------------------------------------------------------------------------- +// wxDataObject +//------------------------------------------------------------------------- + +class wxDataObject: public wxObject +{ + DECLARE_ABSTRACT_CLASS( wxDataObject ) + +public: + + wxDataObject() {} + ~wxDataObject() {} + + virtual wxDataFormat GetFormat() const = 0; + + // implementation + + GdkAtom m_formatAtom; +}; + +// ---------------------------------------------------------------------------- +// wxTextDataObject is a specialization of wxDataObject for text data +// ---------------------------------------------------------------------------- + +class wxTextDataObject : public wxDataObject +{ + DECLARE_DYNAMIC_CLASS( wxTextDataObject ) + +public: + + wxTextDataObject() {} + wxTextDataObject( const wxString& strText ) + : m_strText(strText) { } + + virtual wxDataFormat GetFormat() const + { return wxDF_TEXT; } + + void SetText( const wxString& strText) + { m_strText = strText; } + + wxString GetText() + { return m_strText; } + +private: + wxString m_strText; + +}; + +// ---------------------------------------------------------------------------- +// wxFileDataObject is a specialization of wxDataObject for file names +// ---------------------------------------------------------------------------- + +class wxFileDataObject : public wxDataObject +{ + DECLARE_DYNAMIC_CLASS( wxFileDataObject ) + +public: + + wxFileDataObject(void) {} + + virtual wxDataFormat GetFormat() const + { return wxDF_FILENAME; } + + void AddFile( const wxString &file ) + { m_files += file; m_files += (char)0; } + + wxString GetFiles() + { return m_files; } + +private: + wxString m_files; + +}; + +// ---------------------------------------------------------------------------- +// wxBitmapDataObject is a specialization of wxDataObject for bitmaps +// ---------------------------------------------------------------------------- + +class wxBitmapDataObject : public wxDataObject +{ + DECLARE_DYNAMIC_CLASS( wxBitmapDataObject ) + +public: + + wxBitmapDataObject(void) {} + + virtual wxDataFormat GetFormat() const + { return wxDF_BITMAP; } + + void SetBitmap( const wxBitmap &bitmap ) + { m_bitmap = bitmap; } + + wxBitmap GetBitmap() + { return m_bitmap; } + +private: + wxBitmap m_bitmap; +}; + +// ---------------------------------------------------------------------------- +// wxPrivateDataObject is a specialization of wxDataObject for app specific data +// ---------------------------------------------------------------------------- + +class wxPrivateDataObject : public wxDataObject +{ + DECLARE_DYNAMIC_CLASS( wxPrivateDataObject ) + +public: + + wxPrivateDataObject() + { m_size = 0; m_data = (char*) NULL; } + + ~wxPrivateDataObject() + { if (m_data) delete[] m_data; } + + virtual wxDataFormat GetFormat() const + { return wxDF_PRIVATE; } + + // the string ID identifies the format of clipboard or DnD data. a word + // processor would e.g. add a wxTextDataObject and a wxPrivateDataObject + // to the clipboard - the latter with the Id "WXWORD_FORMAT". + + void SetId( const wxString& id ) + { m_id = id; } + + wxString GetId() + { return m_id; } + + // will make internal copy + void SetData( const char *data, size_t size ); + + size_t GetDataSize() + { return m_size; } + + char* GetData() + { return m_data; } + +private: + size_t m_size; + char* m_data; + wxString m_id; +}; + + +#endif + //__GTKDNDH__ + diff --git a/include/wx/gtk1/dnd.h b/include/wx/gtk1/dnd.h index 937fe1aa89..e319f0d415 100644 --- a/include/wx/gtk1/dnd.h +++ b/include/wx/gtk1/dnd.h @@ -18,6 +18,7 @@ #include "wx/defs.h" #include "wx/object.h" #include "wx/string.h" +#include "wx/dataobj.h" #include "wx/cursor.h" //------------------------------------------------------------------------- @@ -36,91 +37,12 @@ class wxWindow; -class wxDataObject; -class wxTextDataObject; -class wxFileDataObject; - class wxDropTarget; class wxTextDropTarget; class wxFileDropTarget; class wxDropSource; -//------------------------------------------------------------------------- -// wxDataObject -//------------------------------------------------------------------------- - -class wxDataObject: public wxObject -{ -public: - - wxDataObject() {}; - ~wxDataObject() {}; - - virtual wxDataFormat GetPreferredFormat() const = 0; - virtual bool IsSupportedFormat( wxDataFormat format ) const = 0; - virtual size_t GetDataSize() const = 0; - virtual void GetDataHere( void *data ) const = 0; - -}; - -// ---------------------------------------------------------------------------- -// wxTextDataObject is a specialization of wxDataObject for text data -// ---------------------------------------------------------------------------- - -class wxTextDataObject : public wxDataObject -{ -public: - - wxTextDataObject() { } - wxTextDataObject(const wxString& strText) : m_strText(strText) { } - void Init(const wxString& strText) { m_strText = strText; } - - virtual wxDataFormat GetPreferredFormat() const - { return wxDF_TEXT; } - - virtual bool IsSupportedFormat(wxDataFormat format) const - { return format == wxDF_TEXT; } - - virtual size_t GetDataSize() const - { return m_strText.Len() + 1; } // +1 for trailing '\0' - - virtual void GetDataHere( void *data ) const - { memcpy(data, m_strText.c_str(), GetDataSize()); } - -private: - wxString m_strText; - -}; - -// ---------------------------------------------------------------------------- -// wxFileDataObject is a specialization of wxDataObject for file names -// ---------------------------------------------------------------------------- - -class wxFileDataObject : public wxDataObject -{ -public: - - wxFileDataObject(void) { } - void AddFile( const wxString &file ) - { m_files += file; m_files += '\0'; } - - virtual wxDataFormat GetPreferredFormat() const - { return wxDF_FILENAME; } - - virtual bool IsSupportedFormat( wxDataFormat format ) const - { return format == wxDF_FILENAME; } - - virtual size_t GetDataSize() const - { return m_files.Len(); } // no trailing '\0' - - virtual void GetDataHere( void *data ) const - { memcpy(data, m_files.c_str(), GetDataSize()); } - -private: - wxString m_files; - -}; //------------------------------------------------------------------------- // wxDropTarget //------------------------------------------------------------------------- diff --git a/samples/controls/controls.cpp b/samples/controls/controls.cpp index 371df8a09e..52ef792d97 100644 --- a/samples/controls/controls.cpp +++ b/samples/controls/controls.cpp @@ -408,21 +408,41 @@ void MyPanel::OnPasteFromClipboard( wxCommandEvent &WXUNUSED(event) ) { #ifdef __WXGTK__ - if (!wxTheClipboard->IsSupportedFormat( wxDF_TEXT )) return; - - if (!wxTheClipboard->ObtainData( wxDF_TEXT )) return; - - int size = wxTheClipboard->GetDataSize()+1; - - char *data = new char[size]; - - data[size-1] = 0; + if (!wxTheClipboard->IsSupportedFormat( wxDF_TEXT )) + { + *m_text << "The clipboard doesn't contain any data in the requested format." << "\n"; + + return; + } + + if (!wxTheClipboard->Open()) + { + *m_text << "Error opening the clipboard." << "\n"; + + return; + } + else + { + *m_text << "Successfully opened the clipboard." << "\n"; + } + + wxTextDataObject *data = new wxTextDataObject(); - wxTheClipboard->GetDataHere( data ); + if (wxTheClipboard->GetData( data )) + { + *m_text << "Successfully retrieved data from the clipboard." << "\n"; + *m_multitext << data->GetText() << "\n"; + } + else + { + *m_text << "Error getting data from the clipboard." << "\n"; + } + + wxTheClipboard->Close(); - *m_multitext << data << "\n"; + *m_text << "Closed the clipboard." << "\n"; - delete[] data; + delete data; #endif } @@ -437,8 +457,30 @@ void MyPanel::OnCopyToClipboard( wxCommandEvent &WXUNUSED(event) ) wxTextDataObject *data = new wxTextDataObject( text ); - wxTheClipboard->SetData( data ); + if (!wxTheClipboard->Open()) + { + *m_text << "Error opening the clipboard." << "\n"; + + return; + } + else + { + *m_text << "Successfully opened the clipboard." << "\n"; + } + if (!wxTheClipboard->SetData( data )) + { + *m_text << "Error while copying to the clipboard." << "\n"; + } + else + { + *m_text << "Successfully copied data to the clipboard." << "\n"; + } + + wxTheClipboard->Close(); + + *m_text << "Closed the clipboard." << "\n"; + #endif } diff --git a/samples/image/image.cpp b/samples/image/image.cpp index 4e51460807..1c64b7aedc 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -77,8 +77,6 @@ END_EVENT_TABLE() MyCanvas::MyCanvas( wxWindow *parent, const wxWindowID id, const wxPoint &pos, const wxSize &size ) : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER ) { - wxImage image; - wxBitmap bitmap( 100, 100 ); wxMemoryDC dc; @@ -88,14 +86,14 @@ MyCanvas::MyCanvas( wxWindow *parent, const wxWindowID id, const wxPoint &pos, c dc.DrawRectangle( 0, 0, 100, 100 ); dc.SelectObject( wxNullBitmap ); - image = bitmap.ConvertToImage(); + wxImage image( bitmap ); image.SaveFile( "../test.png", wxBITMAP_TYPE_PNG ); image.LoadFile( "../horse.png", wxBITMAP_TYPE_PNG ); - my_horse = new wxBitmap( image ); + my_horse = new wxBitmap( image.ConvertToBitmap() ); image.LoadFile( "../test.png", wxBITMAP_TYPE_PNG ); - my_square = new wxBitmap( image ); + my_square = new wxBitmap( image.ConvertToBitmap() ); } MyCanvas::~MyCanvas(void) diff --git a/src/common/variant.cpp b/src/common/variant.cpp index 90e577e68e..86d5c83175 100644 --- a/src/common/variant.cpp +++ b/src/common/variant.cpp @@ -771,13 +771,13 @@ bool wxVariantDataTime::Write(wxString& str) const return TRUE; } -bool wxVariantDataTime::Read(istream& str) +bool wxVariantDataTime::Read(istream& WXUNUSED(str)) { // Not implemented return FALSE; } -bool wxVariantDataTime::Read(wxString& str) +bool wxVariantDataTime::Read(wxString& WXUNUSED(str)) { // Not implemented return FALSE; @@ -844,13 +844,13 @@ bool wxVariantDataDate::Write(wxString& str) const return TRUE; } -bool wxVariantDataDate::Read(istream& str) +bool wxVariantDataDate::Read(istream& WXUNUSED(str)) { // Not implemented return FALSE; } -bool wxVariantDataDate::Read(wxString& str) +bool wxVariantDataDate::Read(wxString& WXUNUSED(str)) { // Not implemented return FALSE; @@ -921,13 +921,13 @@ bool wxVariantDataVoidPtr::Write(wxString& str) const return TRUE; } -bool wxVariantDataVoidPtr::Read(istream& str) +bool wxVariantDataVoidPtr::Read(istream& WXUNUSED(str)) { // Not implemented return FALSE; } -bool wxVariantDataVoidPtr::Read(wxString& str) +bool wxVariantDataVoidPtr::Read(wxString& WXUNUSED(str)) { // Not implemented return FALSE; diff --git a/src/generic/listctrl.cpp b/src/generic/listctrl.cpp index 7b63d0044a..6db8a65314 100644 --- a/src/generic/listctrl.cpp +++ b/src/generic/listctrl.cpp @@ -1913,7 +1913,7 @@ void wxListMainWindow::CalculatePositions( void ) int y = 1; int entireHeight = m_lines.Number() * lineSpacing + 2; int scroll_pos = GetScrollPos( wxVERTICAL ); - SetScrollbars( m_xScroll, m_yScroll, 0, (entireHeight+15-6) / m_yScroll, 0, scroll_pos, TRUE ); + SetScrollbars( m_xScroll, m_yScroll, 0, (entireHeight+15) / m_yScroll, 0, scroll_pos, TRUE ); GetClientSize( &clientWidth, &clientHeight ); wxNode* node = m_lines.First(); @@ -1959,11 +1959,11 @@ void wxListMainWindow::CalculatePositions( void ) line->GetSize( lineWidth, lineHeight ); if (lineWidth > maxWidth) maxWidth = lineWidth; y += lineSpacing; - if (y+lineSpacing-7 >= clientHeight) // -7 for earlier "line breaking" + if (y+lineSpacing-6 >= clientHeight) // -6 for earlier "line breaking" { y = 5; - x += maxWidth+5; - entireWidth += maxWidth+5; + x += maxWidth+6; + entireWidth += maxWidth+6; maxWidth = 0; } node = node->Next(); @@ -1976,7 +1976,7 @@ void wxListMainWindow::CalculatePositions( void ) if (!node) tries = 1; // everything fits, no second try required } } - m_visibleLines = (clientHeight+7) / (lineSpacing); // +7 for earlier "line breaking" + m_visibleLines = (clientHeight+6) / (lineSpacing); // +6 for earlier "line breaking" int scroll_pos = GetScrollPos( wxHORIZONTAL ); SetScrollbars( m_xScroll, m_yScroll, (entireWidth+15) / m_xScroll, 0, scroll_pos, 0, TRUE ); diff --git a/src/gtk.inc b/src/gtk.inc index 3e3457f3ee..edf6038a34 100644 --- a/src/gtk.inc +++ b/src/gtk.inc @@ -80,6 +80,7 @@ LIB_CPP_SRC=\ gtk/combobox.cpp \ gtk/cursor.cpp \ gtk/data.cpp \ + gtk/dataobj.cpp \ gtk/dc.cpp \ gtk/dcclient.cpp \ gtk/dcmemory.cpp \ diff --git a/src/gtk/clipbrd.cpp b/src/gtk/clipbrd.cpp index d7812a880f..253a7157a7 100644 --- a/src/gtk/clipbrd.cpp +++ b/src/gtk/clipbrd.cpp @@ -57,26 +57,26 @@ targets_selection_received( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, wxClipboard *clipboard ) { - if (!wxTheClipboard) return; + if (!wxTheClipboard) return; - if (selection_data->length <= 0) return; + if (selection_data->length <= 0) return; - // make sure we got the data in the correct form - if (selection_data->type != GDK_SELECTION_TYPE_ATOM) return; + // make sure we got the data in the correct form + if (selection_data->type != GDK_SELECTION_TYPE_ATOM) return; - // the atoms we received, holding a list of targets (= formats) - GdkAtom *atoms = (GdkAtom *)selection_data->data; + // the atoms we received, holding a list of targets (= formats) + GdkAtom *atoms = (GdkAtom *)selection_data->data; - for (unsigned int i=0; ilength/sizeof(GdkAtom); i++) - { - if (atoms[i] == clipboard->m_targetRequested) - { - clipboard->m_formatSupported = TRUE; - return; - } - } + for (unsigned int i=0; ilength/sizeof(GdkAtom); i++) + { + if (atoms[i] == clipboard->m_targetRequested) + { + clipboard->m_formatSupported = TRUE; + return; + } + } - return; + return; } //----------------------------------------------------------------------------- @@ -88,20 +88,63 @@ selection_received( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, wxClipboard *clipboard ) { - if (!wxTheClipboard) return; - - if (selection_data->length <= 0) return; - - size_t size = (size_t) selection_data->length; + if (!wxTheClipboard) return; - // make sure we got the data in the correct form - if (selection_data->type != GDK_SELECTION_TYPE_STRING) return; - - clipboard->m_receivedSize = size; - - clipboard->m_receivedData = new char[size+1]; + wxDataObject *data_object = clipboard->m_receivedData; + + if (!data_object) return; + + if (selection_data->length <= 0) return; - memcpy( clipboard->m_receivedData, selection_data->data, size); + // make sure we got the data in the correct format + + if (data_object->m_formatAtom != selection_data->target) return; + + // make sure we got the data in the correct form (selection type). + // if so, copy data to target object + + switch (data_object->GetFormat()) + { + case wxDF_TEXT: + { + if (selection_data->type != GDK_SELECTION_TYPE_STRING) return; + + wxTextDataObject *text_object = (wxTextDataObject *) data_object; + + wxString text = (const char*) selection_data->data; + + text_object->SetText( text ); + + break; + } + + case wxDF_BITMAP: + { + if (selection_data->type != GDK_SELECTION_TYPE_BITMAP) return; + + return; + + break; + } + + case wxDF_PRIVATE: + { + if (selection_data->type != GDK_SELECTION_TYPE_STRING) return; + + wxPrivateDataObject *private_object = (wxPrivateDataObject *) data_object; + + private_object->SetData( (const char*) selection_data->data, (size_t) selection_data->length ); + + break; + } + + default: + { + return; + } + } + + wxTheClipboard->m_formatSupported = TRUE; } //----------------------------------------------------------------------------- @@ -111,14 +154,14 @@ selection_received( GtkWidget *WXUNUSED(widget), static gint selection_clear( GtkWidget *WXUNUSED(widget), GdkEventSelection *WXUNUSED(event) ) { - if (!wxTheClipboard) return TRUE; + if (!wxTheClipboard) return TRUE; - /* the clipboard is no longer in our hands. we can delete the - * clipboard data. I hope I got that one right... */ + // the clipboard is no longer in our hands. we have to delete the + // clipboard data. - wxTheClipboard->SetData( (wxDataObject*) NULL ); + wxTheClipboard->m_dataObjects.Clear(); - return TRUE; + return TRUE; } //----------------------------------------------------------------------------- @@ -128,33 +171,70 @@ selection_clear( GtkWidget *WXUNUSED(widget), GdkEventSelection *WXUNUSED(event) static void selection_handler( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, gpointer WXUNUSED(data) ) { - if (!wxTheClipboard) return; - - wxDataObject *data_object = wxTheClipboard->m_data; + if (!wxTheClipboard) return; - if (!data_object) return; - - if (data_object->GetDataSize() == 0) return; - - - - gint len = data_object->GetDataSize(); - guchar *bin_data = (guchar*) malloc( len ); - data_object->GetDataHere( (void*)bin_data ); - - if (selection_data->target == GDK_TARGET_STRING) - { - gtk_selection_data_set( - selection_data, GDK_SELECTION_TYPE_STRING, 8*sizeof(gchar), bin_data, len ); - } -/* - else if (selection_data->target == g_textAtom) - { - gtk_selection_data_set( - selection_data, g_textAtom, 8*sizeof(gchar), bin_data, len ); - } -*/ - free( bin_data ); + wxNode *node = wxTheClipboard->m_dataObjects.First(); + + while (node) + { + wxDataObject *data_object = (wxDataObject *)node->Data(); + + if (data_object->m_formatAtom != selection_data->target) + { + node = node->Next(); + break; + } + + switch (data_object->GetFormat()) + { + case wxDF_TEXT: + { + wxTextDataObject *text_object = (wxTextDataObject*) data_object; + + wxString text = text_object->GetText(); + + char *s = WXSTRINGCAST text; + int len = (int) text.Length(); + + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) s, + len ); + + break; + } + + case wxDF_BITMAP: + { + // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object; + + // how do we do that ? + + break; + } + + case wxDF_PRIVATE: + { + wxPrivateDataObject *private_object = (wxPrivateDataObject*) data_object; + + if (private_object->GetDataSize() == 0) return; + + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) private_object->GetData(), + (int) private_object->GetDataSize() ); + } + + default: + break; + } + + node = node->Next(); + } } //----------------------------------------------------------------------------- @@ -165,185 +245,222 @@ IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject) wxClipboard::wxClipboard() { - m_data = (wxDataObject*) NULL; + m_open = FALSE; + + m_dataObjects.DeleteContents( TRUE ); - m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP ); - gtk_widget_realize( m_clipboardWidget ); + m_receivedData = (wxDataObject*) NULL; - gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), - "selection_clear_event", - GTK_SIGNAL_FUNC( selection_clear ), - (gpointer) NULL ); + m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP ); + gtk_widget_realize( m_clipboardWidget ); + + gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), + "selection_clear_event", + GTK_SIGNAL_FUNC( selection_clear ), + (gpointer) NULL ); - if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE ); - if (!g_textAtom) g_textAtom = gdk_atom_intern( "TEXT", FALSE ); - if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE); - - m_receivedData = (char*)NULL; - m_receivedSize = 0; - m_formatSupported = FALSE; - m_targetRequested = 0; + if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE ); + if (!g_textAtom) g_textAtom = gdk_atom_intern( "TEXT", FALSE ); + if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE); + + m_formatSupported = FALSE; + m_targetRequested = 0; } wxClipboard::~wxClipboard() { - Clear(); + Clear(); - if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget ); + if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget ); } void wxClipboard::Clear() { - /* As we have data we also own the clipboard. Once we no longer own - it, clear_selection is called which will set m_data to zero */ + if (m_dataObjects.GetCount()) + { + /* As we have data we also own the clipboard. Once we no longer own + it, clear_selection is called which will set m_data to zero */ - if (m_data) - { - if (gdk_selection_owner_get( g_clipboardAtom) == m_clipboardWidget->window) - { - gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, GDK_CURRENT_TIME ); - } + if (gdk_selection_owner_get( g_clipboardAtom) == m_clipboardWidget->window) + { + gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, GDK_CURRENT_TIME ); + } - delete m_data; - m_data = (wxDataObject*) NULL; - } + m_dataObjects.Clear(); + } - m_receivedSize = 0; + m_targetRequested = 0; - if (m_receivedData) - { - delete[] m_receivedData; - m_receivedData = (char*) NULL; - } + m_formatSupported = FALSE; +} + +bool wxClipboard::Open() +{ + wxCHECK_MSG( !m_open, FALSE, "clipboard already open" ); - m_targetRequested = 0; + m_open = TRUE; - m_formatSupported = FALSE; + return TRUE; } -void wxClipboard::SetData( wxDataObject *data ) +bool wxClipboard::SetData( wxDataObject *data ) { - Clear(); + wxCHECK_MSG( data, FALSE, "data is invalid" ); -/* - GTK 1.0.X cannot remove a target from a widget so if a widget - at first offers text and then a bitmap (and no longer text) to - the clipboard, we seem too have to delete it. -*/ + m_dataObjects.Append( data ); + + wxCHECK_MSG( m_open, FALSE, "clipboard not open" ); + + if (data->GetFormat() == wxDF_PRIVATE) + { + wxPrivateDataObject* pd = (wxPrivateDataObject*) data; + + wxCHECK_MSG( !pd->GetId().IsEmpty(), FALSE, "private clipboard format requires ID string" ); + + data->m_formatAtom = GetTargetAtom( data->GetFormat(), pd->GetId() ); + } + else + { + data->m_formatAtom = GetTargetAtom( data->GetFormat() ); + } + + // Add handlers if someone requests data + + gtk_selection_add_handler( m_clipboardWidget, + g_clipboardAtom, + data->m_formatAtom, + selection_handler, + NULL ); + + // Tell the world we offer clipboard data + + if (!gtk_selection_owner_set( m_clipboardWidget, + g_clipboardAtom, + GDK_CURRENT_TIME )) + { + return FALSE; + } + + return TRUE; +} - if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget ); - - m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP ); - gtk_widget_realize( m_clipboardWidget ); - - if (m_data) delete m_data; - m_data = data; - if (!m_data) return; - - if (!gtk_selection_owner_set( m_clipboardWidget, - g_clipboardAtom, - GDK_CURRENT_TIME)) - { - delete m_data; - m_data = (wxDataObject*) NULL; - return; - } - - switch (m_data->GetPreferredFormat()) - { - case wxDF_TEXT: - gtk_selection_add_handler( m_clipboardWidget, - g_clipboardAtom, - // g_textAtom, - GDK_TARGET_STRING, - selection_handler, - NULL ); - break; - default: - break; - } +void wxClipboard::Close() +{ + wxCHECK_RET( m_open, "clipboard not open" ); + + m_open = FALSE; } -bool wxClipboard::IsSupportedFormat( wxDataFormat format ) +bool wxClipboard::IsSupportedFormat( wxDataFormat format, const wxString &id ) { - m_targetRequested = 0; - - if (format == wxDF_TEXT) - { -// m_targetRequested = g_textAtom; - m_targetRequested = GDK_TARGET_STRING; - } + m_targetRequested = GetTargetAtom( format, id ); - if (m_targetRequested == 0) return FALSE; + if (m_targetRequested == 0) return FALSE; + + // add handler for target (= format) query - gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), - "selection_received", - GTK_SIGNAL_FUNC( targets_selection_received ), - (gpointer) this ); + gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), + "selection_received", + GTK_SIGNAL_FUNC( targets_selection_received ), + (gpointer) this ); - m_formatSupported = FALSE; + m_formatSupported = FALSE; - gtk_selection_convert( m_clipboardWidget, - g_clipboardAtom, - g_targetsAtom, - GDK_CURRENT_TIME ); + // perform query. this will set m_formatSupported to + // TRUE if m_targetRequested is supported + + gtk_selection_convert( m_clipboardWidget, + g_clipboardAtom, + g_targetsAtom, + GDK_CURRENT_TIME ); - gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget), - GTK_SIGNAL_FUNC( targets_selection_received ), - (gpointer) this ); + gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget), + GTK_SIGNAL_FUNC( targets_selection_received ), + (gpointer) this ); - if (!m_formatSupported) return FALSE; + if (!m_formatSupported) return FALSE; - return TRUE; + return TRUE; } -bool wxClipboard::ObtainData( wxDataFormat format ) +bool wxClipboard::GetData( wxDataObject *data ) { - m_receivedSize = 0; - - if (m_receivedData) - { - delete[] m_receivedData; - m_receivedData = (char*) NULL; - } + wxCHECK_MSG( m_open, FALSE, "clipboard not open" ); + + m_receivedData = data; + + wxCHECK_MSG( m_receivedData, FALSE, "invalid data object" ); + + if (m_receivedData->GetFormat() == wxDF_PRIVATE) + { + wxPrivateDataObject* pd = (wxPrivateDataObject*) m_receivedData; + + wxCHECK_MSG( !pd->GetId().IsEmpty(), FALSE, "private clipboard format requires ID string" ); + + m_targetRequested = GetTargetAtom( m_receivedData->GetFormat(), pd->GetId() ); + } + else + { + m_targetRequested = GetTargetAtom( m_receivedData->GetFormat() ); + } + + data->m_formatAtom = m_targetRequested; - m_targetRequested = 0; + wxCHECK_MSG( m_targetRequested, FALSE, "unsupported clipboard format" ); - if (format == wxDF_TEXT) - { -// m_targetRequested = g_textAtom; - m_targetRequested = GDK_TARGET_STRING; - } + m_formatSupported = FALSE; - if (m_targetRequested == 0) return FALSE; + gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), + "selection_received", + GTK_SIGNAL_FUNC( selection_received ), + (gpointer) this ); - gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), - "selection_received", - GTK_SIGNAL_FUNC( selection_received ), - (gpointer) this ); - - gtk_selection_convert( m_clipboardWidget, - g_clipboardAtom, - m_targetRequested, - GDK_CURRENT_TIME ); + gtk_selection_convert( m_clipboardWidget, + g_clipboardAtom, + m_targetRequested, + GDK_CURRENT_TIME ); - gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget), - GTK_SIGNAL_FUNC( selection_received ), - (gpointer) this ); - - if (m_receivedSize == 0) return FALSE; - - return TRUE; -} + gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget), + GTK_SIGNAL_FUNC( selection_received ), + (gpointer) this ); -size_t wxClipboard::GetDataSize() const -{ - return m_receivedSize; + wxCHECK_MSG( m_formatSupported, FALSE, "error retrieving data from clipboard" ); + + return TRUE; } -void wxClipboard::GetDataHere( void *data ) const +GdkAtom wxClipboard::GetTargetAtom( wxDataFormat format, const wxString &id ) { - memcpy(data, m_receivedData, m_receivedSize ); + // What is X representation of that format? + + switch (format) + { + case wxDF_TEXT: + { + return GDK_TARGET_STRING; + // g_textAtom + } + + case wxDF_BITMAP: + { + return GDK_TARGET_BITMAP; + break; + } + + case wxDF_PRIVATE: + { + // we create our own X representation + + return gdk_atom_intern( WXSTRINGCAST( id ), FALSE ); + } + + default: + { + return (GdkAtom) 0; + } + } + + return (GdkAtom) 0; } //----------------------------------------------------------------------------- @@ -354,13 +471,13 @@ IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule) bool wxClipboardModule::OnInit() { - wxTheClipboard = new wxClipboard(); + wxTheClipboard = new wxClipboard(); - return TRUE; + return TRUE; } void wxClipboardModule::OnExit() { - if (wxTheClipboard) delete wxTheClipboard; - wxTheClipboard = (wxClipboard*) NULL; + if (wxTheClipboard) delete wxTheClipboard; + wxTheClipboard = (wxClipboard*) NULL; } diff --git a/src/gtk/dataobj.cpp b/src/gtk/dataobj.cpp new file mode 100644 index 0000000000..e8c256e4a8 --- /dev/null +++ b/src/gtk/dataobj.cpp @@ -0,0 +1,56 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: dataobj.cpp +// Purpose: wxDataObject class +// Author: Robert Roebling +// Id: $Id$ +// Copyright: (c) 1998 Robert Roebling +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ +#pragma implementation "dataobj.h" +#endif + +#include "wx/dataobj.h" + +//------------------------------------------------------------------------- +// wxDataObject +//------------------------------------------------------------------------- + +IMPLEMENT_ABSTRACT_CLASS( wxDataObject, wxObject ) + +// ---------------------------------------------------------------------------- +// wxTextDataObject +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject, wxDataObject ) + +// ---------------------------------------------------------------------------- +// wxFileDataObject +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject, wxDataObject ) + +// ---------------------------------------------------------------------------- +// wxBitmapDataObject +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject, wxDataObject ) + +// ---------------------------------------------------------------------------- +// wxPrivateDataObject +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS( wxPrivateDataObject, wxDataObject ) + +void wxPrivateDataObject::SetData( const char *data, size_t size ) +{ + m_size = size; + + if (m_data) delete[] m_data; + + m_data = new char[size]; + + memcpy( m_data, data, size ); +} + diff --git a/src/gtk/dnd.cpp b/src/gtk/dnd.cpp index e36ee810cb..180e6ba727 100644 --- a/src/gtk/dnd.cpp +++ b/src/gtk/dnd.cpp @@ -632,15 +632,43 @@ shape_motion (GtkWidget *widget, void gtk_drag_callback( GtkWidget *widget, GdkEvent *event, wxDropSource *source ) { - wxDataObject *data = source->m_data; - - size_t size = data->GetDataSize(); - char *ptr = new char[size]; - data->GetDataHere( ptr ); - - gtk_widget_dnd_data_set( widget, event, ptr, size ); - - delete ptr; + wxDataObject *data = source->m_data; + + switch (data->GetFormat()) + { + case wxDF_TEXT: + { + wxTextDataObject *text_object = (wxTextDataObject*) data; + + wxString text = text_object->GetText(); + + gtk_widget_dnd_data_set( widget, + event, + (unsigned char*) text.c_str, + (int) text.Length() ); + + break; + } + + case wxDF_FILENAME: + { + wxFileDataObject *file_object = (wxFileDataObject*) data; + + wxString text = file_object->GetFiles(); + + gtk_widget_dnd_data_set( widget, + event, + (unsigned char*) text.c_str, + (int) text.Length() ); + + break; + } + + default: + { + return; + } + } source->m_retValue = wxDragCopy; } @@ -695,7 +723,6 @@ wxDragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) ) wxASSERT_MSG( m_data, "wxDragSource: no data" ); if (!m_data) return (wxDragResult) wxDragNone; - if (m_data->GetDataSize() == 0) return (wxDragResult) wxDragNone; static GtkWidget *drag_icon = NULL; static GtkWidget *drop_icon = NULL; @@ -801,7 +828,7 @@ void wxDropSource::RegisterWindow(void) wxString formats; - wxDataFormat df = m_data->GetPreferredFormat(); + wxDataFormat df = m_data->GetFormat(); switch (df) { diff --git a/src/gtk1/clipbrd.cpp b/src/gtk1/clipbrd.cpp index d7812a880f..253a7157a7 100644 --- a/src/gtk1/clipbrd.cpp +++ b/src/gtk1/clipbrd.cpp @@ -57,26 +57,26 @@ targets_selection_received( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, wxClipboard *clipboard ) { - if (!wxTheClipboard) return; + if (!wxTheClipboard) return; - if (selection_data->length <= 0) return; + if (selection_data->length <= 0) return; - // make sure we got the data in the correct form - if (selection_data->type != GDK_SELECTION_TYPE_ATOM) return; + // make sure we got the data in the correct form + if (selection_data->type != GDK_SELECTION_TYPE_ATOM) return; - // the atoms we received, holding a list of targets (= formats) - GdkAtom *atoms = (GdkAtom *)selection_data->data; + // the atoms we received, holding a list of targets (= formats) + GdkAtom *atoms = (GdkAtom *)selection_data->data; - for (unsigned int i=0; ilength/sizeof(GdkAtom); i++) - { - if (atoms[i] == clipboard->m_targetRequested) - { - clipboard->m_formatSupported = TRUE; - return; - } - } + for (unsigned int i=0; ilength/sizeof(GdkAtom); i++) + { + if (atoms[i] == clipboard->m_targetRequested) + { + clipboard->m_formatSupported = TRUE; + return; + } + } - return; + return; } //----------------------------------------------------------------------------- @@ -88,20 +88,63 @@ selection_received( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, wxClipboard *clipboard ) { - if (!wxTheClipboard) return; - - if (selection_data->length <= 0) return; - - size_t size = (size_t) selection_data->length; + if (!wxTheClipboard) return; - // make sure we got the data in the correct form - if (selection_data->type != GDK_SELECTION_TYPE_STRING) return; - - clipboard->m_receivedSize = size; - - clipboard->m_receivedData = new char[size+1]; + wxDataObject *data_object = clipboard->m_receivedData; + + if (!data_object) return; + + if (selection_data->length <= 0) return; - memcpy( clipboard->m_receivedData, selection_data->data, size); + // make sure we got the data in the correct format + + if (data_object->m_formatAtom != selection_data->target) return; + + // make sure we got the data in the correct form (selection type). + // if so, copy data to target object + + switch (data_object->GetFormat()) + { + case wxDF_TEXT: + { + if (selection_data->type != GDK_SELECTION_TYPE_STRING) return; + + wxTextDataObject *text_object = (wxTextDataObject *) data_object; + + wxString text = (const char*) selection_data->data; + + text_object->SetText( text ); + + break; + } + + case wxDF_BITMAP: + { + if (selection_data->type != GDK_SELECTION_TYPE_BITMAP) return; + + return; + + break; + } + + case wxDF_PRIVATE: + { + if (selection_data->type != GDK_SELECTION_TYPE_STRING) return; + + wxPrivateDataObject *private_object = (wxPrivateDataObject *) data_object; + + private_object->SetData( (const char*) selection_data->data, (size_t) selection_data->length ); + + break; + } + + default: + { + return; + } + } + + wxTheClipboard->m_formatSupported = TRUE; } //----------------------------------------------------------------------------- @@ -111,14 +154,14 @@ selection_received( GtkWidget *WXUNUSED(widget), static gint selection_clear( GtkWidget *WXUNUSED(widget), GdkEventSelection *WXUNUSED(event) ) { - if (!wxTheClipboard) return TRUE; + if (!wxTheClipboard) return TRUE; - /* the clipboard is no longer in our hands. we can delete the - * clipboard data. I hope I got that one right... */ + // the clipboard is no longer in our hands. we have to delete the + // clipboard data. - wxTheClipboard->SetData( (wxDataObject*) NULL ); + wxTheClipboard->m_dataObjects.Clear(); - return TRUE; + return TRUE; } //----------------------------------------------------------------------------- @@ -128,33 +171,70 @@ selection_clear( GtkWidget *WXUNUSED(widget), GdkEventSelection *WXUNUSED(event) static void selection_handler( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, gpointer WXUNUSED(data) ) { - if (!wxTheClipboard) return; - - wxDataObject *data_object = wxTheClipboard->m_data; + if (!wxTheClipboard) return; - if (!data_object) return; - - if (data_object->GetDataSize() == 0) return; - - - - gint len = data_object->GetDataSize(); - guchar *bin_data = (guchar*) malloc( len ); - data_object->GetDataHere( (void*)bin_data ); - - if (selection_data->target == GDK_TARGET_STRING) - { - gtk_selection_data_set( - selection_data, GDK_SELECTION_TYPE_STRING, 8*sizeof(gchar), bin_data, len ); - } -/* - else if (selection_data->target == g_textAtom) - { - gtk_selection_data_set( - selection_data, g_textAtom, 8*sizeof(gchar), bin_data, len ); - } -*/ - free( bin_data ); + wxNode *node = wxTheClipboard->m_dataObjects.First(); + + while (node) + { + wxDataObject *data_object = (wxDataObject *)node->Data(); + + if (data_object->m_formatAtom != selection_data->target) + { + node = node->Next(); + break; + } + + switch (data_object->GetFormat()) + { + case wxDF_TEXT: + { + wxTextDataObject *text_object = (wxTextDataObject*) data_object; + + wxString text = text_object->GetText(); + + char *s = WXSTRINGCAST text; + int len = (int) text.Length(); + + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) s, + len ); + + break; + } + + case wxDF_BITMAP: + { + // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object; + + // how do we do that ? + + break; + } + + case wxDF_PRIVATE: + { + wxPrivateDataObject *private_object = (wxPrivateDataObject*) data_object; + + if (private_object->GetDataSize() == 0) return; + + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) private_object->GetData(), + (int) private_object->GetDataSize() ); + } + + default: + break; + } + + node = node->Next(); + } } //----------------------------------------------------------------------------- @@ -165,185 +245,222 @@ IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject) wxClipboard::wxClipboard() { - m_data = (wxDataObject*) NULL; + m_open = FALSE; + + m_dataObjects.DeleteContents( TRUE ); - m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP ); - gtk_widget_realize( m_clipboardWidget ); + m_receivedData = (wxDataObject*) NULL; - gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), - "selection_clear_event", - GTK_SIGNAL_FUNC( selection_clear ), - (gpointer) NULL ); + m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP ); + gtk_widget_realize( m_clipboardWidget ); + + gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), + "selection_clear_event", + GTK_SIGNAL_FUNC( selection_clear ), + (gpointer) NULL ); - if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE ); - if (!g_textAtom) g_textAtom = gdk_atom_intern( "TEXT", FALSE ); - if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE); - - m_receivedData = (char*)NULL; - m_receivedSize = 0; - m_formatSupported = FALSE; - m_targetRequested = 0; + if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE ); + if (!g_textAtom) g_textAtom = gdk_atom_intern( "TEXT", FALSE ); + if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE); + + m_formatSupported = FALSE; + m_targetRequested = 0; } wxClipboard::~wxClipboard() { - Clear(); + Clear(); - if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget ); + if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget ); } void wxClipboard::Clear() { - /* As we have data we also own the clipboard. Once we no longer own - it, clear_selection is called which will set m_data to zero */ + if (m_dataObjects.GetCount()) + { + /* As we have data we also own the clipboard. Once we no longer own + it, clear_selection is called which will set m_data to zero */ - if (m_data) - { - if (gdk_selection_owner_get( g_clipboardAtom) == m_clipboardWidget->window) - { - gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, GDK_CURRENT_TIME ); - } + if (gdk_selection_owner_get( g_clipboardAtom) == m_clipboardWidget->window) + { + gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, GDK_CURRENT_TIME ); + } - delete m_data; - m_data = (wxDataObject*) NULL; - } + m_dataObjects.Clear(); + } - m_receivedSize = 0; + m_targetRequested = 0; - if (m_receivedData) - { - delete[] m_receivedData; - m_receivedData = (char*) NULL; - } + m_formatSupported = FALSE; +} + +bool wxClipboard::Open() +{ + wxCHECK_MSG( !m_open, FALSE, "clipboard already open" ); - m_targetRequested = 0; + m_open = TRUE; - m_formatSupported = FALSE; + return TRUE; } -void wxClipboard::SetData( wxDataObject *data ) +bool wxClipboard::SetData( wxDataObject *data ) { - Clear(); + wxCHECK_MSG( data, FALSE, "data is invalid" ); -/* - GTK 1.0.X cannot remove a target from a widget so if a widget - at first offers text and then a bitmap (and no longer text) to - the clipboard, we seem too have to delete it. -*/ + m_dataObjects.Append( data ); + + wxCHECK_MSG( m_open, FALSE, "clipboard not open" ); + + if (data->GetFormat() == wxDF_PRIVATE) + { + wxPrivateDataObject* pd = (wxPrivateDataObject*) data; + + wxCHECK_MSG( !pd->GetId().IsEmpty(), FALSE, "private clipboard format requires ID string" ); + + data->m_formatAtom = GetTargetAtom( data->GetFormat(), pd->GetId() ); + } + else + { + data->m_formatAtom = GetTargetAtom( data->GetFormat() ); + } + + // Add handlers if someone requests data + + gtk_selection_add_handler( m_clipboardWidget, + g_clipboardAtom, + data->m_formatAtom, + selection_handler, + NULL ); + + // Tell the world we offer clipboard data + + if (!gtk_selection_owner_set( m_clipboardWidget, + g_clipboardAtom, + GDK_CURRENT_TIME )) + { + return FALSE; + } + + return TRUE; +} - if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget ); - - m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP ); - gtk_widget_realize( m_clipboardWidget ); - - if (m_data) delete m_data; - m_data = data; - if (!m_data) return; - - if (!gtk_selection_owner_set( m_clipboardWidget, - g_clipboardAtom, - GDK_CURRENT_TIME)) - { - delete m_data; - m_data = (wxDataObject*) NULL; - return; - } - - switch (m_data->GetPreferredFormat()) - { - case wxDF_TEXT: - gtk_selection_add_handler( m_clipboardWidget, - g_clipboardAtom, - // g_textAtom, - GDK_TARGET_STRING, - selection_handler, - NULL ); - break; - default: - break; - } +void wxClipboard::Close() +{ + wxCHECK_RET( m_open, "clipboard not open" ); + + m_open = FALSE; } -bool wxClipboard::IsSupportedFormat( wxDataFormat format ) +bool wxClipboard::IsSupportedFormat( wxDataFormat format, const wxString &id ) { - m_targetRequested = 0; - - if (format == wxDF_TEXT) - { -// m_targetRequested = g_textAtom; - m_targetRequested = GDK_TARGET_STRING; - } + m_targetRequested = GetTargetAtom( format, id ); - if (m_targetRequested == 0) return FALSE; + if (m_targetRequested == 0) return FALSE; + + // add handler for target (= format) query - gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), - "selection_received", - GTK_SIGNAL_FUNC( targets_selection_received ), - (gpointer) this ); + gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), + "selection_received", + GTK_SIGNAL_FUNC( targets_selection_received ), + (gpointer) this ); - m_formatSupported = FALSE; + m_formatSupported = FALSE; - gtk_selection_convert( m_clipboardWidget, - g_clipboardAtom, - g_targetsAtom, - GDK_CURRENT_TIME ); + // perform query. this will set m_formatSupported to + // TRUE if m_targetRequested is supported + + gtk_selection_convert( m_clipboardWidget, + g_clipboardAtom, + g_targetsAtom, + GDK_CURRENT_TIME ); - gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget), - GTK_SIGNAL_FUNC( targets_selection_received ), - (gpointer) this ); + gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget), + GTK_SIGNAL_FUNC( targets_selection_received ), + (gpointer) this ); - if (!m_formatSupported) return FALSE; + if (!m_formatSupported) return FALSE; - return TRUE; + return TRUE; } -bool wxClipboard::ObtainData( wxDataFormat format ) +bool wxClipboard::GetData( wxDataObject *data ) { - m_receivedSize = 0; - - if (m_receivedData) - { - delete[] m_receivedData; - m_receivedData = (char*) NULL; - } + wxCHECK_MSG( m_open, FALSE, "clipboard not open" ); + + m_receivedData = data; + + wxCHECK_MSG( m_receivedData, FALSE, "invalid data object" ); + + if (m_receivedData->GetFormat() == wxDF_PRIVATE) + { + wxPrivateDataObject* pd = (wxPrivateDataObject*) m_receivedData; + + wxCHECK_MSG( !pd->GetId().IsEmpty(), FALSE, "private clipboard format requires ID string" ); + + m_targetRequested = GetTargetAtom( m_receivedData->GetFormat(), pd->GetId() ); + } + else + { + m_targetRequested = GetTargetAtom( m_receivedData->GetFormat() ); + } + + data->m_formatAtom = m_targetRequested; - m_targetRequested = 0; + wxCHECK_MSG( m_targetRequested, FALSE, "unsupported clipboard format" ); - if (format == wxDF_TEXT) - { -// m_targetRequested = g_textAtom; - m_targetRequested = GDK_TARGET_STRING; - } + m_formatSupported = FALSE; - if (m_targetRequested == 0) return FALSE; + gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), + "selection_received", + GTK_SIGNAL_FUNC( selection_received ), + (gpointer) this ); - gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), - "selection_received", - GTK_SIGNAL_FUNC( selection_received ), - (gpointer) this ); - - gtk_selection_convert( m_clipboardWidget, - g_clipboardAtom, - m_targetRequested, - GDK_CURRENT_TIME ); + gtk_selection_convert( m_clipboardWidget, + g_clipboardAtom, + m_targetRequested, + GDK_CURRENT_TIME ); - gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget), - GTK_SIGNAL_FUNC( selection_received ), - (gpointer) this ); - - if (m_receivedSize == 0) return FALSE; - - return TRUE; -} + gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget), + GTK_SIGNAL_FUNC( selection_received ), + (gpointer) this ); -size_t wxClipboard::GetDataSize() const -{ - return m_receivedSize; + wxCHECK_MSG( m_formatSupported, FALSE, "error retrieving data from clipboard" ); + + return TRUE; } -void wxClipboard::GetDataHere( void *data ) const +GdkAtom wxClipboard::GetTargetAtom( wxDataFormat format, const wxString &id ) { - memcpy(data, m_receivedData, m_receivedSize ); + // What is X representation of that format? + + switch (format) + { + case wxDF_TEXT: + { + return GDK_TARGET_STRING; + // g_textAtom + } + + case wxDF_BITMAP: + { + return GDK_TARGET_BITMAP; + break; + } + + case wxDF_PRIVATE: + { + // we create our own X representation + + return gdk_atom_intern( WXSTRINGCAST( id ), FALSE ); + } + + default: + { + return (GdkAtom) 0; + } + } + + return (GdkAtom) 0; } //----------------------------------------------------------------------------- @@ -354,13 +471,13 @@ IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule) bool wxClipboardModule::OnInit() { - wxTheClipboard = new wxClipboard(); + wxTheClipboard = new wxClipboard(); - return TRUE; + return TRUE; } void wxClipboardModule::OnExit() { - if (wxTheClipboard) delete wxTheClipboard; - wxTheClipboard = (wxClipboard*) NULL; + if (wxTheClipboard) delete wxTheClipboard; + wxTheClipboard = (wxClipboard*) NULL; } diff --git a/src/gtk1/dataobj.cpp b/src/gtk1/dataobj.cpp new file mode 100644 index 0000000000..e8c256e4a8 --- /dev/null +++ b/src/gtk1/dataobj.cpp @@ -0,0 +1,56 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: dataobj.cpp +// Purpose: wxDataObject class +// Author: Robert Roebling +// Id: $Id$ +// Copyright: (c) 1998 Robert Roebling +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ +#pragma implementation "dataobj.h" +#endif + +#include "wx/dataobj.h" + +//------------------------------------------------------------------------- +// wxDataObject +//------------------------------------------------------------------------- + +IMPLEMENT_ABSTRACT_CLASS( wxDataObject, wxObject ) + +// ---------------------------------------------------------------------------- +// wxTextDataObject +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject, wxDataObject ) + +// ---------------------------------------------------------------------------- +// wxFileDataObject +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject, wxDataObject ) + +// ---------------------------------------------------------------------------- +// wxBitmapDataObject +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject, wxDataObject ) + +// ---------------------------------------------------------------------------- +// wxPrivateDataObject +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS( wxPrivateDataObject, wxDataObject ) + +void wxPrivateDataObject::SetData( const char *data, size_t size ) +{ + m_size = size; + + if (m_data) delete[] m_data; + + m_data = new char[size]; + + memcpy( m_data, data, size ); +} + diff --git a/src/gtk1/dnd.cpp b/src/gtk1/dnd.cpp index e36ee810cb..180e6ba727 100644 --- a/src/gtk1/dnd.cpp +++ b/src/gtk1/dnd.cpp @@ -632,15 +632,43 @@ shape_motion (GtkWidget *widget, void gtk_drag_callback( GtkWidget *widget, GdkEvent *event, wxDropSource *source ) { - wxDataObject *data = source->m_data; - - size_t size = data->GetDataSize(); - char *ptr = new char[size]; - data->GetDataHere( ptr ); - - gtk_widget_dnd_data_set( widget, event, ptr, size ); - - delete ptr; + wxDataObject *data = source->m_data; + + switch (data->GetFormat()) + { + case wxDF_TEXT: + { + wxTextDataObject *text_object = (wxTextDataObject*) data; + + wxString text = text_object->GetText(); + + gtk_widget_dnd_data_set( widget, + event, + (unsigned char*) text.c_str, + (int) text.Length() ); + + break; + } + + case wxDF_FILENAME: + { + wxFileDataObject *file_object = (wxFileDataObject*) data; + + wxString text = file_object->GetFiles(); + + gtk_widget_dnd_data_set( widget, + event, + (unsigned char*) text.c_str, + (int) text.Length() ); + + break; + } + + default: + { + return; + } + } source->m_retValue = wxDragCopy; } @@ -695,7 +723,6 @@ wxDragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) ) wxASSERT_MSG( m_data, "wxDragSource: no data" ); if (!m_data) return (wxDragResult) wxDragNone; - if (m_data->GetDataSize() == 0) return (wxDragResult) wxDragNone; static GtkWidget *drag_icon = NULL; static GtkWidget *drop_icon = NULL; @@ -801,7 +828,7 @@ void wxDropSource::RegisterWindow(void) wxString formats; - wxDataFormat df = m_data->GetPreferredFormat(); + wxDataFormat df = m_data->GetFormat(); switch (df) { -- 2.45.2