Skip to content

Commit

Permalink
Copy image if only image is in cell, small fixes (#303)
Browse files Browse the repository at this point in the history
* Copy image if only image is in cell

Copy text if otherwise

* Support wxDF_PNG format for pasted images

As we copy PNG images directly to the clipboard,
also allow to paste PNG images from the clipboard.

Provide this functionality only on Windows.

* Fix wrong indent

* Copy original PNG does not work on Windows

Make it Linux only for now
The others platform will deal with the bitmap
  • Loading branch information
tobiolo committed Dec 12, 2022
1 parent 1e09aa7 commit 5562b45
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,23 @@ struct Document {
return nullptr;
}

const wxChar* CopyImageToClipboard(Cell *cell) {
if (wxTheClipboard->Open()) {
#ifdef __WXGTK__
if (!cell->text.image->png_data.empty()) {
wxCustomDataObject *pngimage = new wxCustomDataObject(wxDF_BITMAP);
pngimage->SetData(cell->text.image->png_data.size(), cell->text.image->png_data.data());
wxTheClipboard->SetData(pngimage);
} else
#endif
{
wxTheClipboard->SetData(new wxBitmapDataObject(cell->text.image->bm_orig));
}
wxTheClipboard->Close();
}
return _(L"Image copied to clipboard");
}

const wxChar *Action(wxDC &dc, int k) {
ShiftToCenter(dc);

Expand Down Expand Up @@ -1183,16 +1200,22 @@ struct Document {
sys->cellclipboard = c ? c->Clone(nullptr) : selected.g->CloneSel(selected);
}

if (wxTheClipboard->Open()) {
wxString s;
if (k == A_COPYCT) {
loopallcellssel(c, true) if (c->text.t.Len()) s += c->text.t + " ";
} else {
s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this);
const wxChar *returnmessage;
if(c and c->text.image and !c->text.t) {
returnmessage = CopyImageToClipboard(c);
} else {
if (wxTheClipboard->Open()) {
wxString s;
if (k == A_COPYCT) {
loopallcellssel(c, true) if (c->text.t.Len()) s += c->text.t + " ";
} else {
s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this);
}
sys->clipboardcopy = s;
wxTheClipboard->SetData(new wxTextDataObject(s));
wxTheClipboard->Close();
returnmessage = _(L"Text copied to clipboard");
}
sys->clipboardcopy = s;
wxTheClipboard->SetData(new wxTextDataObject(s));
wxTheClipboard->Close();
}

if (k == A_CUT) {
Expand All @@ -1206,7 +1229,7 @@ struct Document {
Refresh();
}
ZoomOutIfNoGrid(dc);
return nullptr;
return returnmessage;

case A_SELALL:
selected.SelAll();
Expand Down Expand Up @@ -1549,29 +1572,9 @@ struct Document {
}

case A_IMAGECPY: {
if(!c->text.image) return _(L"No image in this cell.");
if (selected.Thin()) return NoThin();

if (wxTheClipboard->Open()) {
#if defined(__WXMSW__) || defined(__WXGTK__)
if (!c->text.image->png_data.empty()) {
wxCustomDataObject *pngimage = new wxCustomDataObject(
#ifdef __WXMSW__
wxDF_PNG
#else
wxDF_BITMAP
#endif
);
pngimage->SetData(c->text.image->png_data.size(), c->text.image->png_data.data());
wxTheClipboard->SetData(pngimage);
} else
#endif
{
wxTheClipboard->SetData(new wxBitmapDataObject(c->text.image->bm_orig));
}
wxTheClipboard->Close();
}
return _(L"Image copied to clipboard");
if (!c->text.image) return _(L"No image in this cell.");
return CopyImageToClipboard(c);
}

case A_IMAGESCP:
Expand Down Expand Up @@ -1748,6 +1751,9 @@ struct Document {
case wxDF_BITMAP:
case wxDF_DIB:
case wxDF_TIFF:
#ifdef __WXMSW__
case wxDF_PNG:
#endif
if (dataobji->GetBitmap().GetRefData() != wxNullBitmap.GetRefData()) {
c->AddUndo(this);
SetImageBM(c, dataobji->GetBitmap().ConvertToImage(), sys->frame->csf);
Expand Down

0 comments on commit 5562b45

Please sign in to comment.