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

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

View File

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

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