3 * \file qt4/GuiClipboard.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiClipboard.h"
16 #include "qt_helpers.h"
20 #include <QApplication>
25 #include "support/lstrings.h"
27 using lyx::support::internalLineEnding;
28 using lyx::support::externalLineEnding;
33 static char const * const mime_type = "application/x-lyx";
39 string const GuiClipboard::getAsLyX() const
41 LYXERR(Debug::ACTION) << "GuiClipboard::getAsLyX(): `";
42 // We don't convert encodings here since the encoding of the
43 // clipboard contents is specified in the data itself
44 QMimeData const * source =
45 qApp->clipboard()->mimeData(QClipboard::Clipboard);
47 LYXERR(Debug::ACTION) << "' (no QMimeData)" << endl;
50 if (source->hasFormat(mime_type)) {
51 // data from ourself or some other LyX instance
52 QByteArray const ar = source->data(mime_type);
53 string const s(ar.data(), ar.count());
54 LYXERR(Debug::ACTION) << s << "'" << endl;
57 LYXERR(Debug::ACTION) << "'" << endl;
62 docstring const GuiClipboard::getAsText() const
64 // text data from other applications
65 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
66 .normalized(QString::NormalizationForm_C);
67 LYXERR(Debug::ACTION) << "GuiClipboard::getAsText(): `"
68 << fromqstr(str) << "'" << endl;
72 return internalLineEnding(qstring_to_ucs4(str));
76 void GuiClipboard::put(string const & lyx, docstring const & text)
78 LYXERR(Debug::ACTION) << "GuiClipboard::put(`" << lyx << "' `"
79 << to_utf8(text) << "')" << endl;
80 // We don't convert the encoding of lyx since the encoding of the
81 // clipboard contents is specified in the data itself
82 QMimeData * data = new QMimeData;
84 QByteArray const qlyx(lyx.c_str(), lyx.size());
85 data->setData(mime_type, qlyx);
87 // Don't test for text.empty() since we want to be able to clear the
89 QString const qtext = toqstr(text);
91 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
95 bool GuiClipboard::hasLyXContents() const
97 QMimeData const * const source =
98 qApp->clipboard()->mimeData(QClipboard::Clipboard);
99 return source && source->hasFormat(mime_type);
103 bool GuiClipboard::isInternal() const
105 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
106 // the preamble dialog
107 // FIXME: This does only work on X11, since ownsClipboard() is
108 // hardwired to return false on Windows and OS X.
109 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
113 bool GuiClipboard::empty() const
115 // We need to check both the plaintext and the LyX version of the
116 // clipboard. The plaintext version is empty if the LyX version
117 // contains only one inset, and the LyX version is empry if the
118 // clipboard does not come from LyX.
119 if (!qApp->clipboard()->text(QClipboard::Clipboard).isEmpty())
121 return !hasLyXContents();
124 } // namespace frontend