Demonstrate storing wxImage to clipboard in image sample
This commit is contained in:
@@ -91,6 +91,8 @@ public:
|
||||
#if wxUSE_CLIPBOARD
|
||||
void OnCopy(wxCommandEvent& event);
|
||||
void OnPaste(wxCommandEvent& event);
|
||||
void OnCopyImage(wxCommandEvent& evt);
|
||||
void OnPasteImage(wxCommandEvent& evt);
|
||||
#endif // wxUSE_CLIPBOARD
|
||||
|
||||
MyCanvas *m_canvas;
|
||||
@@ -124,8 +126,39 @@ class MyImageFrame : public wxFrame
|
||||
public:
|
||||
MyImageFrame(wxFrame *parent, const wxString& desc, const wxImage& image, double scale = 1.0)
|
||||
{
|
||||
Create(parent, desc, wxBitmap(image, wxBITMAP_SCREEN_DEPTH, scale),
|
||||
image.GetImageCount(desc));
|
||||
// Retrieve image info
|
||||
wxString info;
|
||||
int xres, yres;
|
||||
switch ( GetResolutionFromOptions(image, &xres, &yres) )
|
||||
{
|
||||
case wxIMAGE_RESOLUTION_NONE:
|
||||
break;
|
||||
|
||||
case wxIMAGE_RESOLUTION_CM:
|
||||
// convert to DPI
|
||||
xres = wxRound(xres / 10.0 * inches2mm);
|
||||
yres = wxRound(yres / 10.0 * inches2mm);
|
||||
wxFALLTHROUGH;
|
||||
|
||||
case wxIMAGE_RESOLUTION_INCHES:
|
||||
info = wxString::Format("DPI %i x %i", xres, yres);
|
||||
break;
|
||||
|
||||
default:
|
||||
wxFAIL_MSG("unexpected image resolution units");
|
||||
break;
|
||||
}
|
||||
|
||||
int numImages = desc.StartsWith("Clipboard") ? 1 : image.GetImageCount(desc);
|
||||
if ( numImages > 1 )
|
||||
{
|
||||
if ( !info.empty() )
|
||||
info += ", ";
|
||||
|
||||
info += wxString::Format("%d images", numImages);
|
||||
}
|
||||
|
||||
Create(parent, desc, wxBitmap(image, wxBITMAP_SCREEN_DEPTH, scale), info);
|
||||
}
|
||||
|
||||
MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
|
||||
@@ -137,7 +170,7 @@ private:
|
||||
bool Create(wxFrame *parent,
|
||||
const wxString& desc,
|
||||
const wxBitmap& bitmap,
|
||||
int numImages = 1)
|
||||
wxString info = wxString())
|
||||
{
|
||||
if ( !wxFrame::Create(parent, wxID_ANY,
|
||||
wxString::Format("Image from %s", desc),
|
||||
@@ -169,8 +202,7 @@ private:
|
||||
mbar->Check(ID_PAINT_BG, true);
|
||||
|
||||
CreateStatusBar(2);
|
||||
if ( numImages != 1 )
|
||||
SetStatusText(wxString::Format("%d images", numImages), 1);
|
||||
SetStatusText(info, 1);
|
||||
|
||||
SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
|
||||
|
||||
@@ -439,6 +471,41 @@ private:
|
||||
Refresh();
|
||||
}
|
||||
|
||||
// This is a copy of protected wxImageHandler::GetResolutionFromOptions()
|
||||
static wxImageResolution GetResolutionFromOptions(const wxImage& image, int* x, int* y)
|
||||
{
|
||||
wxCHECK_MSG(x && y, wxIMAGE_RESOLUTION_NONE, wxT("NULL pointer"));
|
||||
|
||||
if ( image.HasOption(wxIMAGE_OPTION_RESOLUTIONX) &&
|
||||
image.HasOption(wxIMAGE_OPTION_RESOLUTIONY) )
|
||||
{
|
||||
*x = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX);
|
||||
*y = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
|
||||
}
|
||||
else if ( image.HasOption(wxIMAGE_OPTION_RESOLUTION) )
|
||||
{
|
||||
*x =
|
||||
*y = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
|
||||
}
|
||||
else // no resolution options specified
|
||||
{
|
||||
*x =
|
||||
*y = 0;
|
||||
|
||||
return wxIMAGE_RESOLUTION_NONE;
|
||||
}
|
||||
|
||||
// get the resolution unit too
|
||||
int resUnit = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT);
|
||||
if ( !resUnit )
|
||||
{
|
||||
// this is the default
|
||||
resUnit = wxIMAGE_RESOLUTION_INCHES;
|
||||
}
|
||||
|
||||
return (wxImageResolution)resUnit;
|
||||
}
|
||||
|
||||
wxBitmap m_bitmap;
|
||||
double m_zoom;
|
||||
|
||||
@@ -631,7 +698,9 @@ enum
|
||||
ID_INFO,
|
||||
ID_SHOWRAW,
|
||||
ID_GRAPHICS,
|
||||
ID_SHOWTHUMBNAIL
|
||||
ID_SHOWTHUMBNAIL,
|
||||
ID_COPY_IMAGE,
|
||||
ID_PASTE_IMAGE
|
||||
};
|
||||
|
||||
wxIMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame );
|
||||
@@ -651,6 +720,8 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
||||
#if wxUSE_CLIPBOARD
|
||||
EVT_MENU(wxID_COPY, MyFrame::OnCopy)
|
||||
EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
|
||||
EVT_MENU(ID_COPY_IMAGE, MyFrame::OnCopyImage)
|
||||
EVT_MENU(ID_PASTE_IMAGE, MyFrame::OnPasteImage)
|
||||
#endif // wxUSE_CLIPBOARD
|
||||
EVT_UPDATE_UI(ID_NEW_HIDPI, MyFrame::OnUpdateNewFrameHiDPI)
|
||||
wxEND_EVENT_TABLE()
|
||||
@@ -686,8 +757,11 @@ MyFrame::MyFrame()
|
||||
|
||||
#if wxUSE_CLIPBOARD
|
||||
wxMenu *menuClipboard = new wxMenu;
|
||||
menuClipboard->Append(wxID_COPY, "&Copy test image\tCtrl-C");
|
||||
menuClipboard->Append(wxID_PASTE, "&Paste image\tCtrl-V");
|
||||
menuClipboard->Append(wxID_COPY, "&Copy test image as wxBitmap\tCtrl-C");
|
||||
menuClipboard->Append(wxID_PASTE, "&Paste image as wxBitmap\tCtrl-V");
|
||||
menuClipboard->AppendSeparator();
|
||||
menuClipboard->Append(ID_COPY_IMAGE, "Copy image as wxImage");
|
||||
menuClipboard->Append(ID_PASTE_IMAGE, "Paste image as wxImage");
|
||||
menu_bar->Append(menuClipboard, "&Clipboard");
|
||||
#endif // wxUSE_CLIPBOARD
|
||||
|
||||
@@ -945,6 +1019,38 @@ void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
|
||||
wxTheClipboard->Close();
|
||||
}
|
||||
|
||||
void MyFrame::OnCopyImage(wxCommandEvent& WXUNUSED(evt))
|
||||
{
|
||||
wxImage img;
|
||||
wxString filename = LoadUserImage(img);
|
||||
if ( filename.empty() )
|
||||
return;
|
||||
|
||||
wxImageDataObject* dobjImage = new wxImageDataObject;
|
||||
dobjImage->SetImage(img);
|
||||
|
||||
wxClipboardLocker clipOpener;
|
||||
if ( !wxTheClipboard->SetData(dobjImage) )
|
||||
{
|
||||
wxLogError("Failed to copy wxImage to clipboard");
|
||||
}
|
||||
}
|
||||
|
||||
void MyFrame::OnPasteImage(wxCommandEvent& WXUNUSED(evt))
|
||||
{
|
||||
wxImageDataObject dobjImage;
|
||||
|
||||
wxClipboardLocker clipOpener;
|
||||
if ( !wxTheClipboard->GetData(dobjImage) )
|
||||
{
|
||||
wxLogMessage("No wxImage data in the clipboard");
|
||||
}
|
||||
else
|
||||
{
|
||||
new MyImageFrame(this, "Clipboard (wxImage)", dobjImage.GetImage());
|
||||
}
|
||||
}
|
||||
|
||||
#endif // wxUSE_CLIPBOARD
|
||||
|
||||
void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
|
||||
|
Reference in New Issue
Block a user