Implement wxImageDataObject

Using this object we can put an wxImage on or retrieve it from the clipboard. wxImage is stored internally as a blob with either a PNG file (wxMSW, wxGTK) or a TIFF file (wxOSX) and therefore some its metadata (like resolution) is stored on the clipboard too (what is not the case for wxBitmap stored with wxBitmapDataObject). wxImages stored with wxImageDataObject can be used by native applications.

Closes #17631.
This commit is contained in:
Artur Wieczorek
2020-12-30 10:04:48 +01:00
parent a27a7656ea
commit e09c35efb5
3 changed files with 111 additions and 2 deletions

View File

@@ -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
// ============================================================================