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 "FileDialog.h"
17 #include "GuiClipboard.h"
18 #include "qt_helpers.h"
20 #include "frontends/alert.h"
23 #include "BufferView.h"
26 #include "support/convert.h"
27 #include "support/debug.h"
28 #include "support/filetools.h"
29 #include "support/FileFilterList.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
33 #include <QApplication>
36 #include <QDataStream>
39 #include <QMacPasteboardMime>
42 #include <QStringList>
45 #include <QWindowsMime>
52 #include "boost/assert.hpp"
57 #include "support/linkback/LinkBackProxy.h"
61 using namespace lyx::support;
63 static char const * const lyx_mime_type = "application/x-lyx";
64 static char const * const pdf_mime_type = "application/pdf";
65 static char const * const emf_mime_type = "image/x-emf";
66 static char const * const wmf_mime_type = "image/x-wmf";
74 static FORMATETC cfFromMime(QString const & mimetype)
77 if (mimetype == emf_mime_type) {
78 formatetc.cfFormat = CF_ENHMETAFILE;
79 formatetc.tymed = TYMED_ENHMF;
81 else if (mimetype == wmf_mime_type) {
82 formatetc.cfFormat = CF_METAFILEPICT;
83 formatetc.tymed = TYMED_MFPICT;
85 else return formatetc;
87 formatetc.dwAspect = DVASPECT_CONTENT;
88 formatetc.lindex = -1;
93 class QWindowsMimeMetafile : public QWindowsMime {
95 bool canConvertFromMime(FORMATETC const & formatetc, QMimeData const * mimedata) const;
96 bool canConvertToMime(QString const & mimetype, IDataObject * pDataObj) const;
97 bool convertFromMime(FORMATETC const & formatetc, const QMimeData * mimedata, STGMEDIUM * pmedium) const;
98 QVariant convertToMime(QString const & mimetype, IDataObject * pDataObj, QVariant::Type preferredType) const;
99 QVector<FORMATETC> formatsForMime(QString const & mimeType, QMimeData const * mimeData) const;
100 QString mimeForFormat(FORMATETC const &) const;
104 QString QWindowsMimeMetafile::mimeForFormat(FORMATETC const & formatetc) const
107 if (formatetc.cfFormat == CF_ENHMETAFILE)
109 else if (formatetc.cfFormat == CF_METAFILEPICT)
115 bool QWindowsMimeMetafile::canConvertFromMime(
116 FORMATETC const & formatetc, QMimeData const * mimedata) const
122 bool QWindowsMimeMetafile::canConvertToMime(
123 QString const & mimetype, IDataObject * pDataObj) const
125 FORMATETC formatetc = cfFromMime(mimetype);
126 return pDataObj->QueryGetData(&formatetc) == S_OK;
130 bool QWindowsMimeMetafile::convertFromMime(
131 FORMATETC const & formatetc, QMimeData const * mimedata,
132 STGMEDIUM * pmedium) const
138 QVariant QWindowsMimeMetafile::convertToMime(QString const & mimetype,
139 IDataObject * pDataObj, QVariant::Type preferredType) const
142 if (!canConvertToMime(mimetype, pDataObj))
145 FORMATETC formatetc = cfFromMime(mimetype);
147 if (pDataObj->GetData(&formatetc, &s) != S_OK)
151 if (s.tymed == TYMED_ENHMF) {
152 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, 0, NULL);
153 data.resize(dataSize);
154 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, dataSize, (LPBYTE)data.data());
155 } else if (s.tymed == TYMED_MFPICT) {
156 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, 0, NULL);
157 data.resize(dataSize);
158 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, dataSize, (LPBYTE)data.data());
161 ReleaseStgMedium(&s);
167 QVector<FORMATETC> QWindowsMimeMetafile::formatsForMime(
168 QString const & mimetype, QMimeData const * mimedata) const
170 QVector<FORMATETC> formats;
171 formats += cfFromMime(mimetype);
175 static QWindowsMimeMetafile * metafileWindowsMime;
181 class QMacPasteboardMimeGraphics : public QMacPasteboardMime {
183 QMacPasteboardMimeGraphics()
184 : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL)
186 ~QMacPasteboardMimeGraphics() {}
187 QString convertorName();
188 QString flavorFor(const QString & mime);
189 QString mimeFor(QString flav);
190 bool canConvert(const QString & mime, QString flav);
191 QVariant convertToMime(const QString & mime, QList<QByteArray> data, QString flav);
192 QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav);
196 QString QMacPasteboardMimeGraphics::convertorName()
202 QString QMacPasteboardMimeGraphics::flavorFor(const QString & mime)
204 LYXERR(Debug::ACTION, "flavorFor " << fromqstr(mime));
205 if (mime == QLatin1String(pdf_mime_type))
206 return QLatin1String("com.adobe.pdf");
211 QString QMacPasteboardMimeGraphics::mimeFor(QString flav)
213 LYXERR(Debug::ACTION, "mimeFor " << fromqstr(flav));
214 if (flav == QLatin1String("com.adobe.pdf"))
215 return QLatin1String(pdf_mime_type);
220 bool QMacPasteboardMimeGraphics::canConvert(const QString & mime, QString flav)
222 return mimeFor(flav) == mime;
226 QVariant QMacPasteboardMimeGraphics::convertToMime(const QString & mime, QList<QByteArray> data, QString)
229 qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
234 QList<QByteArray> QMacPasteboardMimeGraphics::convertFromMime(const QString &mime, QVariant data, QString)
236 QList<QByteArray> ret;
237 ret.append(data.toByteArray());
241 static QMacPasteboardMimeGraphics * graphicsPasteboardMime;
246 GuiClipboard::GuiClipboard()
248 connect(qApp->clipboard(), SIGNAL(dataChanged()),
249 this, SLOT(on_dataChanged()));
250 // initialize clipboard status.
254 if (!graphicsPasteboardMime)
255 graphicsPasteboardMime = new QMacPasteboardMimeGraphics();
259 if (!metafileWindowsMime)
260 metafileWindowsMime = new QWindowsMimeMetafile();
265 GuiClipboard::~GuiClipboard()
268 closeAllLinkBackLinks();
273 string const GuiClipboard::getAsLyX() const
275 LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
276 // We don't convert encodings here since the encoding of the
277 // clipboard contents is specified in the data itself
278 QMimeData const * source =
279 qApp->clipboard()->mimeData(QClipboard::Clipboard);
281 LYXERR(Debug::ACTION, "' (no QMimeData)");
285 if (source->hasFormat(lyx_mime_type)) {
286 // data from ourself or some other LyX instance
287 QByteArray const ar = source->data(lyx_mime_type);
288 string const s(ar.data(), ar.count());
289 LYXERR(Debug::ACTION, s << "'");
292 LYXERR(Debug::ACTION, "'");
297 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
298 Clipboard::GraphicsType & type) const
300 // create file dialog filter according to the existing types in the clipboard
301 vector<Clipboard::GraphicsType> types;
302 if (hasGraphicsContents(Clipboard::EmfGraphicsType))
303 types.push_back(Clipboard::EmfGraphicsType);
304 if (hasGraphicsContents(Clipboard::WmfGraphicsType))
305 types.push_back(Clipboard::WmfGraphicsType);
306 if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
307 types.push_back(Clipboard::LinkBackGraphicsType);
308 if (hasGraphicsContents(Clipboard::PdfGraphicsType))
309 types.push_back(Clipboard::PdfGraphicsType);
310 if (hasGraphicsContents(Clipboard::PngGraphicsType))
311 types.push_back(Clipboard::PngGraphicsType);
312 if (hasGraphicsContents(Clipboard::JpegGraphicsType))
313 types.push_back(Clipboard::JpegGraphicsType);
315 BOOST_ASSERT(!types.empty());
317 // select prefered type if AnyGraphicsType was passed
318 if (type == Clipboard::AnyGraphicsType)
319 type = types.front();
322 map<Clipboard::GraphicsType, string> extensions;
323 map<Clipboard::GraphicsType, docstring> typeNames;
325 extensions[Clipboard::EmfGraphicsType] = "emf";
326 extensions[Clipboard::WmfGraphicsType] = "wmf";
327 extensions[Clipboard::LinkBackGraphicsType] = "linkback";
328 extensions[Clipboard::PdfGraphicsType] = "pdf";
329 extensions[Clipboard::PngGraphicsType] = "png";
330 extensions[Clipboard::JpegGraphicsType] = "jpeg";
332 typeNames[Clipboard::EmfGraphicsType] = _("Enhanced Metafile");
333 typeNames[Clipboard::WmfGraphicsType] = _("Windows Metafile");
334 typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
335 typeNames[Clipboard::PdfGraphicsType] = _("PDF");
336 typeNames[Clipboard::PngGraphicsType] = _("PNG");
337 typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
339 // find unused filename with primary extension
340 string document_path = cur.buffer().fileName().onlyPath().absFilename();
341 unsigned newfile_number = 0;
345 filename = FileName(addName(document_path,
347 + convert<string>(newfile_number) + "."
348 + extensions[type]));
349 } while (filename.isReadableFile());
352 // create file type filter, putting the prefered on to the front
353 docstring filterSpec;
354 for (size_t i = 0; i != types.size(); ++i) {
355 docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
356 + " (*." + from_ascii(extensions[types[i]]) + ")";
357 if (types[i] == type)
358 filterSpec = s + filterSpec;
360 filterSpec += ";;" + s;
362 FileFilterList const filter(filterSpec);
364 // show save dialog for the graphic
365 FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
366 FileDialog::Result result =
367 dlg.save(toqstr(filename.onlyPath().absFilename()), filter,
368 toqstr(filename.onlyFileName()));
370 if (result.first == FileDialog::Later)
373 string newFilename = fromqstr(result.second);
374 if (newFilename.empty()) {
375 cur.bv().message(_("Canceled."));
378 filename.set(newFilename);
380 // check the extension (the user could have changed it)
381 if (!suffixIs(ascii_lowercase(filename.absFilename()),
382 "." + extensions[type])) {
383 // the user changed the extension. Check if the type is available
385 for (i = 1; i != types.size(); ++i) {
386 if (suffixIs(ascii_lowercase(filename.absFilename()),
387 "." + extensions[types[i]])) {
393 // invalid extension found, or none at all. In the latter
394 // case set the default extensions.
395 if (i == types.size()
396 && filename.onlyFileName().find('.') == string::npos) {
397 filename.changeExtension("." + extensions[type]);
401 // check whether the file exists and warn the user
402 if (!filename.exists())
404 int ret = frontend::Alert::prompt(
405 _("Overwrite external file?"),
406 bformat(_("File %1$s already exists, do you want to overwrite it?"),
407 from_utf8(filename.absFilename())), 1, 1, _("&Overwrite"), _("&Cancel"));
409 // overwrite, hence break the dialog loop
412 // not overwrite, hence show the dialog again (i.e. loop)
419 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
421 // get the filename from the user
422 FileName filename = getPastedGraphicsFileName(cur, type);
423 if (filename.empty())
426 // handle image cases first
427 if (type == PngGraphicsType || type == JpegGraphicsType) {
428 // get image from QImage from clipboard
429 QImage image = qApp->clipboard()->image();
430 if (image.isNull()) {
431 LYXERR(Debug::ACTION, "No image in clipboard");
435 // convert into graphics format
438 buffer.open(QIODevice::WriteOnly);
439 if (type == PngGraphicsType)
440 image.save(toqstr(filename.absFilename()), "PNG");
441 else if (type == JpegGraphicsType)
442 image.save(toqstr(filename.absFilename()), "JPEG");
450 QMimeData const * source =
451 qApp->clipboard()->mimeData(QClipboard::Clipboard);
453 LYXERR(Debug::ACTION, "0 bytes (no QMimeData)");
460 case PdfGraphicsType: mime = pdf_mime_type; break;
461 case LinkBackGraphicsType: mime = pdf_mime_type; break;
462 case EmfGraphicsType: mime = emf_mime_type; break;
463 case WmfGraphicsType: mime = wmf_mime_type; break;
464 default: BOOST_ASSERT(false);
468 if (!source->hasFormat(mime))
470 // data from ourself or some other LyX instance
471 QByteArray const ar = source->data(mime);
472 LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.data()
473 << "length = " << ar.count());
475 QFile f(toqstr(filename.absFilename()));
476 if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
477 LYXERR(Debug::ACTION, "Error opening file "
478 << filename.absFilename() << " for writing");
482 // write the (LinkBack) PDF data
484 if (type == LinkBackGraphicsType) {
486 void const * linkBackData;
487 unsigned linkBackLen;
488 getLinkBackData(&linkBackData, &linkBackLen);
489 f.write((char *)linkBackData, linkBackLen);
490 quint32 pdfLen = ar.size();
492 ds << pdfLen; // big endian by default
494 // only non-Mac this should never happen
504 docstring const GuiClipboard::getAsText() const
506 // text data from other applications
507 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
508 .normalized(QString::NormalizationForm_C);
509 LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
513 return internalLineEnding(qstring_to_ucs4(str));
517 void GuiClipboard::put(string const & lyx, docstring const & text)
519 LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
520 << to_utf8(text) << "')");
521 // We don't convert the encoding of lyx since the encoding of the
522 // clipboard contents is specified in the data itself
523 QMimeData * data = new QMimeData;
525 QByteArray const qlyx(lyx.c_str(), lyx.size());
526 data->setData(lyx_mime_type, qlyx);
528 // Don't test for text.empty() since we want to be able to clear the
530 QString const qtext = toqstr(text);
531 data->setText(qtext);
532 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
536 bool GuiClipboard::hasLyXContents() const
538 QMimeData const * const source =
539 qApp->clipboard()->mimeData(QClipboard::Clipboard);
540 return source && source->hasFormat(lyx_mime_type);
544 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
546 if (type == AnyGraphicsType) {
547 return hasGraphicsContents(PdfGraphicsType)
548 || hasGraphicsContents(PngGraphicsType)
549 || hasGraphicsContents(JpegGraphicsType)
550 || hasGraphicsContents(EmfGraphicsType)
551 || hasGraphicsContents(WmfGraphicsType)
552 || hasGraphicsContents(LinkBackGraphicsType);
555 QMimeData const * const source =
556 qApp->clipboard()->mimeData(QClipboard::Clipboard);
558 // handle image cases first
559 if (type == PngGraphicsType || type == JpegGraphicsType)
560 return source->hasImage();
562 // handle LinkBack for Mac
564 if (type == LinkBackGraphicsType)
565 return isLinkBackDataInPasteboard();
567 if (type == LinkBackGraphicsType)
572 QStringList const & formats = source->formats();
573 LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
574 for (int i = 0; i < formats.size(); ++i) {
575 LYXERR(Debug::ACTION, "Found format " << fromqstr(formats[i]));
578 // compute mime for type
581 case EmfGraphicsType: mime = emf_mime_type; break;
582 case WmfGraphicsType: mime = wmf_mime_type; break;
583 case PdfGraphicsType: mime = pdf_mime_type; break;
584 default: BOOST_ASSERT(false);
587 return source && source->hasFormat(mime);
591 bool GuiClipboard::isInternal() const
593 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
594 // the preamble dialog
595 // FIXME: This does only work on X11, since ownsClipboard() is
596 // hardwired to return false on Windows and OS X.
597 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
601 bool GuiClipboard::hasInternal() const
603 // Windows and Mac OS X does not have the concept of ownership;
604 // the clipboard is a fully global resource so all applications
605 // are notified of changes.
606 #if (defined(Q_WS_X11))
614 void GuiClipboard::on_dataChanged()
616 QMimeData const * const source =
617 qApp->clipboard()->mimeData(QClipboard::Clipboard);
618 QStringList l = source->formats();
619 LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
620 for (int i = 0; i < l.count(); i++) {
621 LYXERR(Debug::ACTION, fromqstr(l.value(i)));
624 text_clipboard_empty_ = qApp->clipboard()->
625 text(QClipboard::Clipboard).isEmpty();
627 has_lyx_contents_ = hasLyXContents();
628 has_graphics_contents_ = hasGraphicsContents();
632 bool GuiClipboard::empty() const
634 // We need to check both the plaintext and the LyX version of the
635 // clipboard. The plaintext version is empty if the LyX version
636 // contains only one inset, and the LyX version is empty if the
637 // clipboard does not come from LyX.
638 if (!text_clipboard_empty_)
640 return !has_lyx_contents_ && !has_graphics_contents_;
643 } // namespace frontend
646 #include "GuiClipboard_moc.cpp"