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"
26 using lyx::support::internalLineEnding;
27 using lyx::support::externalLineEnding;
32 static char const * const mime_type = "application/x-lyx";
38 string const GuiClipboard::getAsLyX() const
40 LYXERR(Debug::ACTION) << "GuiClipboard::getAsLyX(): `";
41 // We don't convert encodings here since the encoding of the
42 // clipboard contents is specified in the data itself
43 QMimeData const * source =
44 qApp->clipboard()->mimeData(QClipboard::Clipboard);
46 LYXERR(Debug::ACTION) << "' (no QMimeData)" << endl;
49 if (source->hasFormat(mime_type)) {
50 // data from ourself or some other LyX instance
51 QByteArray const ar = source->data(mime_type);
52 string const s(ar.data(), ar.count());
53 LYXERR(Debug::ACTION) << s << "'" << endl;
56 LYXERR(Debug::ACTION) << "'" << endl;
61 docstring const GuiClipboard::getAsText() const
63 // text data from other applications
64 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
65 .normalized(QString::NormalizationForm_C);
66 LYXERR(Debug::ACTION) << "GuiClipboard::getAsText(): `"
67 << fromqstr(str) << "'" << endl;
71 return internalLineEnding(qstring_to_ucs4(str));
75 void GuiClipboard::put(string const & lyx, docstring const & text)
77 LYXERR(Debug::ACTION) << "GuiClipboard::put(`" << lyx << "' `"
78 << to_utf8(text) << "')" << endl;
79 // We don't convert the encoding of lyx since the encoding of the
80 // clipboard contents is specified in the data itself
81 QMimeData * data = new QMimeData;
83 QByteArray const qlyx(lyx.c_str(), lyx.size());
84 data->setData(mime_type, qlyx);
86 // Don't test for text.empty() since we want to be able to clear the
88 QString const qtext = toqstr(text);
90 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
94 bool GuiClipboard::hasLyXContents() const
96 QMimeData const * const source =
97 qApp->clipboard()->mimeData(QClipboard::Clipboard);
98 return source && source->hasFormat(mime_type);
102 bool GuiClipboard::isInternal() const
104 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
105 // the preamble dialog
106 // FIXME: This does only work on X11, since ownsClipboard() is
107 // hardwired to return false on Windows and OS X.
108 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
112 bool GuiClipboard::empty() const
114 // We need to check both the plaintext and the LyX version of the
115 // clipboard. The plaintext version is empty if the LyX version
116 // contains only one inset, and the LyX version is empry if the
117 // clipboard does not come from LyX.
118 if (!qApp->clipboard()->text(QClipboard::Clipboard).isEmpty())
120 return !hasLyXContents();
123 } // namespace frontend