diff --git a/include/wx/dataobj.h b/include/wx/dataobj.h index 4091f7b766..ef0c385df5 100644 --- a/include/wx/dataobj.h +++ b/include/wx/dataobj.h @@ -36,7 +36,9 @@ wxTextDataObject | wxBitmapDataObject | wxCustomDataObject - + | + | + wxImageDataObject */ // ============================================================================ @@ -545,6 +547,22 @@ private: wxDECLARE_NO_COPY_CLASS(wxCustomDataObject); }; +// ---------------------------------------------------------------------------- +// wxImageDataObject - data object for wxImage +// ---------------------------------------------------------------------------- + +class WXDLLIMPEXP_CORE wxImageDataObject : public wxCustomDataObject +{ +public: + explicit wxImageDataObject(const wxImage& image = wxNullImage); + + void SetImage(const wxImage& image); + wxImage GetImage() const; + +private: + wxDECLARE_NO_COPY_CLASS(wxImageDataObject); +}; + // ---------------------------------------------------------------------------- // include platform-specific declarations of wxXXXBase classes // ---------------------------------------------------------------------------- diff --git a/interface/wx/dataobj.h b/interface/wx/dataobj.h index 3b64f30169..90a6b7766f 100644 --- a/interface/wx/dataobj.h +++ b/interface/wx/dataobj.h @@ -35,7 +35,8 @@ @itemdef{wxDF_HTML, An HTML string. This is currently only valid on Mac and MSW.} @itemdef{wxDF_PNG, - A PNG file. This is valid only on MSW.} + A PNG file. This is valid only on MSW. This constant is available + since wxWidgets 3.1.5.} @endDefList As mentioned above, these standard formats may be passed to any function @@ -622,6 +623,42 @@ public: +/** + @class wxImageDataObject + + wxImageDataObject is a specialization of wxDataObject for image data. + It can be used e.g. when you need to put on and retrieve from the clipboard + a wxImage with its metadata (like image resolution). + + @since 3.1.5 + + @library{wxcore} + @category{dnd} + + @see @ref overview_dnd, wxDataObject, wxCustomDataObject, wxBitmapDataObject +*/ +class wxImageDataObject : public wxCustomDataObject +{ +public: + /** + Constructor, optionally passing an image (otherwise use SetImage() + later). + */ + explicit wxImageDataObject(const wxImage& image = wxNullImage); + + /** + Returns the image associated with the data object. + */ + wxImage GetImage() const; + + /** + Sets the image stored by the data object. + */ + void SetImage(const wxImage& image); +}; + + + /** @class wxURLDataObject diff --git a/src/common/dobjcmn.cpp b/src/common/dobjcmn.cpp index d963ac4cbe..20ce23217d 100644 --- a/src/common/dobjcmn.cpp +++ b/src/common/dobjcmn.cpp @@ -20,6 +20,7 @@ #include "wx/app.h" #endif +#include "wx/mstream.h" #include "wx/textbuf.h" // ---------------------------------------------------------------------------- @@ -620,6 +621,59 @@ bool wxCustomDataObject::SetData(size_t size, const void *buf) return true; } +// ---------------------------------------------------------------------------- +// wxImageDataObject +// ---------------------------------------------------------------------------- + +#if defined(__WXMSW__) +#define wxIMAGE_FORMAT_DATA wxDF_PNG +#define wxIMAGE_FORMAT_BITMAP_TYPE wxBITMAP_TYPE_PNG +#define wxIMAGE_FORMAT_NAME "PNG" +#elif defined(__WXGTK__) +#define wxIMAGE_FORMAT_DATA wxDF_BITMAP +#define wxIMAGE_FORMAT_BITMAP_TYPE wxBITMAP_TYPE_PNG +#define wxIMAGE_FORMAT_NAME "PNG" +#elif defined(__WXOSX__) +#define wxIMAGE_FORMAT_DATA wxDF_BITMAP +#define wxIMAGE_FORMAT_BITMAP_TYPE wxBITMAP_TYPE_TIFF +#define wxIMAGE_FORMAT_NAME "TIFF" +#else +#define wxIMAGE_FORMAT_DATA wxDF_BITMAP +#define wxIMAGE_FORMAT_BITMAP_TYPE wxBITMAP_TYPE_PNG +#define wxIMAGE_FORMAT_NAME "PNG" +#endif + +wxImageDataObject::wxImageDataObject(const wxImage& image) + : wxCustomDataObject(wxIMAGE_FORMAT_DATA) +{ + if ( image.IsOk() ) + { + SetImage(image); + } +} + +void wxImageDataObject::SetImage(const wxImage& image) +{ + wxCHECK_RET(wxImage::FindHandler(wxIMAGE_FORMAT_BITMAP_TYPE) != NULL, + wxIMAGE_FORMAT_NAME " image handler must be installed to use clipboard with image"); + + wxMemoryOutputStream mem; + image.SaveFile(mem, wxIMAGE_FORMAT_BITMAP_TYPE); + + SetData(mem.GetLength(), mem.GetOutputStreamBuffer()->GetBufferStart()); +} + +wxImage wxImageDataObject::GetImage() const +{ + wxCHECK_MSG(wxImage::FindHandler(wxIMAGE_FORMAT_BITMAP_TYPE) != NULL, wxNullImage, + wxIMAGE_FORMAT_NAME " image handler must be installed to use clipboard with image"); + + wxMemoryInputStream mem(GetData(), GetSize()); + wxImage image; + image.LoadFile(mem, wxIMAGE_FORMAT_BITMAP_TYPE); + return image; +} + // ============================================================================ // some common dnd related code // ============================================================================