From 0613a218aad1482ff3003a7cc4755c0b9651f3c2 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Fri, 12 Apr 2013 22:12:47 +0200 Subject: [PATCH] Also put HTML on the clipboard when copying The HTML export is now mature enough so that it can be used to transfer formatted text to the clipboard. This enhances interoperability e.g. with office applications. --- src/CutAndPaste.cpp | 14 +++++++++----- src/frontends/Clipboard.h | 4 ++-- src/frontends/qt4/GuiClipboard.cpp | 12 +++++++----- src/frontends/qt4/GuiClipboard.h | 2 +- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index 6534475885..da5739d289 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -23,6 +23,7 @@ #include "BufferView.h" #include "Changes.h" #include "Cursor.h" +#include "Encoding.h" #include "ErrorList.h" #include "FuncCode.h" #include "FuncRequest.h" @@ -474,11 +475,14 @@ void putClipboard(ParagraphList const & paragraphs, buffer->paragraphs() = paragraphs; buffer->inset().setBuffer(*buffer); buffer->params().setDocumentClass(docclass); - ostringstream lyx; - if (buffer->write(lyx)) - theClipboard().put(lyx.str(), plaintext); - else - theClipboard().put(string(), plaintext); + string lyx; + ostringstream oslyx; + if (buffer->write(oslyx)) + lyx = oslyx.str(); + odocstringstream oshtml; + OutputParams runparams(encodings.fromLyXName("utf8")); + buffer->writeLyXHTMLSource(oshtml, runparams, Buffer::FullSource); + theClipboard().put(lyx, oshtml.str(), plaintext); // Save that memory buffer->paragraphs().clear(); } diff --git a/src/frontends/Clipboard.h b/src/frontends/Clipboard.h index 92e7167708..1e0d48685d 100644 --- a/src/frontends/Clipboard.h +++ b/src/frontends/Clipboard.h @@ -55,7 +55,7 @@ public: virtual docstring const getAsText() const = 0; /// Get the contents of the window system clipboard as graphics file. virtual FileName getAsGraphics(Cursor const & cur, GraphicsType type) const = 0; - + /** * Fill the system clipboard. The format of \p lyx is as written in * .lyx files, the format of \p text is plain text. @@ -65,7 +65,7 @@ public: * This should be called when the user requests to cut or copy to * the clipboard. */ - virtual void put(std::string const & lyx, docstring const & text) = 0; + virtual void put(std::string const & lyx, docstring const & html, docstring const & text) = 0; /// Does the clipboard contain LyX contents? virtual bool hasLyXContents() const = 0; diff --git a/src/frontends/qt4/GuiClipboard.cpp b/src/frontends/qt4/GuiClipboard.cpp index 438feac97b..b2a97d6077 100644 --- a/src/frontends/qt4/GuiClipboard.cpp +++ b/src/frontends/qt4/GuiClipboard.cpp @@ -296,9 +296,9 @@ FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) cons return FileName(); // data from ourself or some other LyX instance QByteArray const ar = cache_.data(mime); - LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.data() + LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.constData() << "length = " << ar.count()); - + QFile f(toqstr(filename.absFileName())); if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { LYXERR(Debug::ACTION, "Error opening file " @@ -341,10 +341,10 @@ docstring const GuiClipboard::getAsText() const } -void GuiClipboard::put(string const & lyx, docstring const & text) +void GuiClipboard::put(string const & lyx, docstring const & html, docstring const & text) { LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `" - << to_utf8(text) << "')"); + << to_utf8(html) << "' `" << to_utf8(text) << "')"); // We don't convert the encoding of lyx since the encoding of the // clipboard contents is specified in the data itself QMimeData * data = new QMimeData; @@ -363,6 +363,8 @@ void GuiClipboard::put(string const & lyx, docstring const & text) // clipboard. QString const qtext = toqstr(text); data->setText(qtext); + QString const qhtml = toqstr(html); + data->setHtml(qhtml); qApp->clipboard()->setMimeData(data, QClipboard::Clipboard); } @@ -470,7 +472,7 @@ void GuiClipboard::on_dataChanged() LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:"); for (int i = 0; i < l.count(); i++) LYXERR(Debug::ACTION, l.value(i)); - + text_clipboard_empty_ = qApp->clipboard()-> text(QClipboard::Clipboard).isEmpty(); diff --git a/src/frontends/qt4/GuiClipboard.h b/src/frontends/qt4/GuiClipboard.h index a6540f1ce8..5b452beae8 100644 --- a/src/frontends/qt4/GuiClipboard.h +++ b/src/frontends/qt4/GuiClipboard.h @@ -70,7 +70,7 @@ public: std::string const getAsLyX() const; FileName getAsGraphics(Cursor const & cur, GraphicsType type) const; docstring const getAsText() const; - void put(std::string const & lyx, docstring const & text); + void put(std::string const & lyx, docstring const & html, docstring const & text); bool hasLyXContents() const; bool hasGraphicsContents(GraphicsType type = AnyGraphicsType) const; bool hasTextContents() const; -- 2.39.2