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"
21 #include "BufferView.h"
24 #include "support/lassert.h"
25 #include "support/convert.h"
26 #include "support/debug.h"
27 #include "support/filetools.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
31 #include "support/linkback/LinkBackProxy.h"
34 #include "frontends/alert.h"
36 #include <QApplication>
39 #include <QDataStream>
42 #include <QMacPasteboardMime>
45 #include <QStringList>
49 #include <QWindowsMime>
50 #if defined(Q_CYGWIN_WIN) || defined(Q_CC_MINGW)
60 using namespace lyx::support;
62 static char const * const lyx_mime_type = "application/x-lyx";
63 static char const * const pdf_mime_type = "application/pdf";
64 static char const * const emf_mime_type = "image/x-emf";
65 static char const * const wmf_mime_type = "image/x-wmf";
73 static FORMATETC cfFromMime(QString const & mimetype)
76 if (mimetype == emf_mime_type) {
77 formatetc.cfFormat = CF_ENHMETAFILE;
78 formatetc.tymed = TYMED_ENHMF;
79 } else if (mimetype == wmf_mime_type) {
80 formatetc.cfFormat = CF_METAFILEPICT;
81 formatetc.tymed = TYMED_MFPICT;
84 formatetc.dwAspect = DVASPECT_CONTENT;
85 formatetc.lindex = -1;
90 class QWindowsMimeMetafile : public QWindowsMime {
92 bool canConvertFromMime(FORMATETC const & formatetc, QMimeData const * mimedata) const;
93 bool canConvertToMime(QString const & mimetype, IDataObject * pDataObj) const;
94 bool convertFromMime(FORMATETC const & formatetc, const QMimeData * mimedata, STGMEDIUM * pmedium) const;
95 QVariant convertToMime(QString const & mimetype, IDataObject * pDataObj, QVariant::Type preferredType) const;
96 QVector<FORMATETC> formatsForMime(QString const & mimeType, QMimeData const * mimeData) const;
97 QString mimeForFormat(FORMATETC const &) const;
101 QString QWindowsMimeMetafile::mimeForFormat(FORMATETC const & formatetc) const
104 if (formatetc.cfFormat == CF_ENHMETAFILE)
106 else if (formatetc.cfFormat == CF_METAFILEPICT)
112 bool QWindowsMimeMetafile::canConvertFromMime(FORMATETC const & formatetc,
113 QMimeData const * mimedata) const
119 bool QWindowsMimeMetafile::canConvertToMime(QString const & mimetype,
120 IDataObject * pDataObj) const
122 if (mimetype != emf_mime_type && mimetype != wmf_mime_type)
124 FORMATETC formatetc = cfFromMime(mimetype);
125 return pDataObj->QueryGetData(&formatetc) == S_OK;
129 bool QWindowsMimeMetafile::convertFromMime(FORMATETC const & formatetc,
130 QMimeData const * mimedata, STGMEDIUM * pmedium) const
136 QVariant QWindowsMimeMetafile::convertToMime(QString const & mimetype,
137 IDataObject * pDataObj, QVariant::Type preferredType) const
140 if (!canConvertToMime(mimetype, pDataObj))
143 FORMATETC formatetc = cfFromMime(mimetype);
145 if (pDataObj->GetData(&formatetc, &s) != S_OK)
149 if (s.tymed == TYMED_ENHMF) {
150 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, 0, 0);
151 data.resize(dataSize);
152 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, dataSize, (LPBYTE)data.data());
153 } else if (s.tymed == TYMED_MFPICT) {
154 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, 0, 0);
155 data.resize(dataSize);
156 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, dataSize, (LPBYTE)data.data());
159 ReleaseStgMedium(&s);
165 QVector<FORMATETC> QWindowsMimeMetafile::formatsForMime(
166 QString const & mimetype, QMimeData const * mimedata) const
168 QVector<FORMATETC> formats;
169 formats += cfFromMime(mimetype);
173 static QWindowsMimeMetafile * metafileWindowsMime;
179 class QMacPasteboardMimeGraphics : public QMacPasteboardMime {
181 QMacPasteboardMimeGraphics()
182 : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL)
184 QString convertorName();
185 QString flavorFor(QString const & mime);
186 QString mimeFor(QString flav);
187 bool canConvert(QString const & mime, QString flav);
188 QVariant convertToMime(QString const & mime, QList<QByteArray> data, QString flav);
189 QList<QByteArray> convertFromMime(QString const & mime, QVariant data, QString flav);
193 QString QMacPasteboardMimeGraphics::convertorName()
199 QString QMacPasteboardMimeGraphics::flavorFor(QString const & mime)
201 LYXERR(Debug::ACTION, "flavorFor " << mime);
202 if (mime == QLatin1String(pdf_mime_type))
203 return QLatin1String("com.adobe.pdf");
208 QString QMacPasteboardMimeGraphics::mimeFor(QString flav)
210 LYXERR(Debug::ACTION, "mimeFor " << flav);
211 if (flav == QLatin1String("com.adobe.pdf"))
212 return QLatin1String(pdf_mime_type);
217 bool QMacPasteboardMimeGraphics::canConvert(QString const & mime, QString flav)
219 return mimeFor(flav) == mime;
223 QVariant QMacPasteboardMimeGraphics::convertToMime(QString const & mime, QList<QByteArray> data, QString)
226 qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
231 QList<QByteArray> QMacPasteboardMimeGraphics::convertFromMime(QString const & mime, QVariant data, QString)
233 QList<QByteArray> ret;
234 ret.append(data.toByteArray());
238 static QMacPasteboardMimeGraphics * graphicsPasteboardMime;
243 GuiClipboard::GuiClipboard()
245 connect(qApp->clipboard(), SIGNAL(dataChanged()),
246 this, SLOT(on_dataChanged()));
247 // initialize clipboard status.
251 if (!graphicsPasteboardMime)
252 graphicsPasteboardMime = new QMacPasteboardMimeGraphics();
256 if (!metafileWindowsMime)
257 metafileWindowsMime = new QWindowsMimeMetafile();
262 GuiClipboard::~GuiClipboard()
265 if (metafileWindowsMime) {
266 delete metafileWindowsMime;
267 metafileWindowsMime = 0;
271 closeAllLinkBackLinks();
272 if (graphicsPasteboardMime) {
273 delete graphicsPasteboardMime;
274 graphicsPasteboardMime = 0;
280 string const GuiClipboard::getAsLyX() const
282 LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
283 // We don't convert encodings here since the encoding of the
284 // clipboard contents is specified in the data itself
285 QMimeData const * source =
286 qApp->clipboard()->mimeData(QClipboard::Clipboard);
288 LYXERR(Debug::ACTION, "' (no QMimeData)");
292 if (source->hasFormat(lyx_mime_type)) {
293 // data from ourself or some other LyX instance
294 QByteArray const ar = source->data(lyx_mime_type);
295 string const s(ar.data(), ar.count());
296 LYXERR(Debug::ACTION, s << "'");
299 LYXERR(Debug::ACTION, "'");
304 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
305 Clipboard::GraphicsType & type) const
307 // create file dialog filter according to the existing types in the clipboard
308 vector<Clipboard::GraphicsType> types;
309 if (hasGraphicsContents(Clipboard::EmfGraphicsType))
310 types.push_back(Clipboard::EmfGraphicsType);
311 if (hasGraphicsContents(Clipboard::WmfGraphicsType))
312 types.push_back(Clipboard::WmfGraphicsType);
313 if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
314 types.push_back(Clipboard::LinkBackGraphicsType);
315 if (hasGraphicsContents(Clipboard::PdfGraphicsType))
316 types.push_back(Clipboard::PdfGraphicsType);
317 if (hasGraphicsContents(Clipboard::PngGraphicsType))
318 types.push_back(Clipboard::PngGraphicsType);
319 if (hasGraphicsContents(Clipboard::JpegGraphicsType))
320 types.push_back(Clipboard::JpegGraphicsType);
322 LASSERT(!types.empty(), /**/);
324 // select prefered type if AnyGraphicsType was passed
325 if (type == Clipboard::AnyGraphicsType)
326 type = types.front();
329 map<Clipboard::GraphicsType, string> extensions;
330 map<Clipboard::GraphicsType, docstring> typeNames;
332 extensions[Clipboard::EmfGraphicsType] = "emf";
333 extensions[Clipboard::WmfGraphicsType] = "wmf";
334 extensions[Clipboard::LinkBackGraphicsType] = "linkback";
335 extensions[Clipboard::PdfGraphicsType] = "pdf";
336 extensions[Clipboard::PngGraphicsType] = "png";
337 extensions[Clipboard::JpegGraphicsType] = "jpeg";
339 typeNames[Clipboard::EmfGraphicsType] = _("Enhanced Metafile");
340 typeNames[Clipboard::WmfGraphicsType] = _("Windows Metafile");
341 typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
342 typeNames[Clipboard::PdfGraphicsType] = _("PDF");
343 typeNames[Clipboard::PngGraphicsType] = _("PNG");
344 typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
346 // find unused filename with primary extension
347 string document_path = cur.buffer().fileName().onlyPath().absFilename();
348 unsigned newfile_number = 0;
352 filename = FileName(addName(document_path,
354 + convert<string>(newfile_number) + "."
355 + extensions[type]));
356 } while (filename.isReadableFile());
359 // create file type filter, putting the prefered on to the front
361 for (size_t i = 0; i != types.size(); ++i) {
362 docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
363 + " (*." + from_ascii(extensions[types[i]]) + ")";
364 if (types[i] == type)
365 filter.prepend(toqstr(s));
367 filter.append(toqstr(s));
369 filter = fileFilters(filter.join(";;"));
371 // show save dialog for the graphic
372 FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
373 FileDialog::Result result =
374 dlg.save(toqstr(filename.onlyPath().absFilename()), filter,
375 toqstr(filename.onlyFileName()));
377 if (result.first == FileDialog::Later)
380 string newFilename = fromqstr(result.second);
381 if (newFilename.empty()) {
382 cur.bv().message(_("Canceled."));
385 filename.set(newFilename);
387 // check the extension (the user could have changed it)
388 if (!suffixIs(ascii_lowercase(filename.absFilename()),
389 "." + extensions[type])) {
390 // the user changed the extension. Check if the type is available
392 for (i = 1; i != types.size(); ++i) {
393 if (suffixIs(ascii_lowercase(filename.absFilename()),
394 "." + extensions[types[i]])) {
400 // invalid extension found, or none at all. In the latter
401 // case set the default extensions.
402 if (i == types.size()
403 && filename.onlyFileName().find('.') == string::npos) {
404 filename.changeExtension("." + extensions[type]);
408 // check whether the file exists and warn the user
409 if (!filename.exists())
411 int ret = frontend::Alert::prompt(
412 _("Overwrite external file?"),
413 bformat(_("File %1$s already exists, do you want to overwrite it?"),
414 from_utf8(filename.absFilename())), 1, 1, _("&Overwrite"), _("&Cancel"));
416 // overwrite, hence break the dialog loop
419 // not overwrite, hence show the dialog again (i.e. loop)
426 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
428 // get the filename from the user
429 FileName filename = getPastedGraphicsFileName(cur, type);
430 if (filename.empty())
433 // handle image cases first
434 if (type == PngGraphicsType || type == JpegGraphicsType) {
435 // get image from QImage from clipboard
436 QImage image = qApp->clipboard()->image();
437 if (image.isNull()) {
438 LYXERR(Debug::ACTION, "No image in clipboard");
442 // convert into graphics format
445 buffer.open(QIODevice::WriteOnly);
446 if (type == PngGraphicsType)
447 image.save(toqstr(filename.absFilename()), "PNG");
448 else if (type == JpegGraphicsType)
449 image.save(toqstr(filename.absFilename()), "JPEG");
451 LASSERT(false, /**/);
457 QMimeData const * source =
458 qApp->clipboard()->mimeData(QClipboard::Clipboard);
460 LYXERR(Debug::ACTION, "0 bytes (no QMimeData)");
467 case PdfGraphicsType: mime = pdf_mime_type; break;
468 case LinkBackGraphicsType: mime = pdf_mime_type; break;
469 case EmfGraphicsType: mime = emf_mime_type; break;
470 case WmfGraphicsType: mime = wmf_mime_type; break;
471 default: LASSERT(false, /**/);
475 if (!source->hasFormat(mime))
477 // data from ourself or some other LyX instance
478 QByteArray const ar = source->data(mime);
479 LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.data()
480 << "length = " << ar.count());
482 QFile f(toqstr(filename.absFilename()));
483 if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
484 LYXERR(Debug::ACTION, "Error opening file "
485 << filename.absFilename() << " for writing");
489 // write the (LinkBack) PDF data
491 if (type == LinkBackGraphicsType) {
493 void const * linkBackData;
494 unsigned linkBackLen;
495 getLinkBackData(&linkBackData, &linkBackLen);
496 f.write((char *)linkBackData, linkBackLen);
497 quint32 pdfLen = ar.size();
499 ds << pdfLen; // big endian by default
501 // only non-Mac this should never happen
502 LASSERT(false, /**/);
511 docstring const GuiClipboard::getAsText() const
513 // text data from other applications
514 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
515 .normalized(QString::NormalizationForm_C);
516 LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << str << "'");
520 return internalLineEnding(qstring_to_ucs4(str));
524 void GuiClipboard::put(string const & lyx, docstring const & text)
526 LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
527 << to_utf8(text) << "')");
528 // We don't convert the encoding of lyx since the encoding of the
529 // clipboard contents is specified in the data itself
530 QMimeData * data = new QMimeData;
532 QByteArray const qlyx(lyx.c_str(), lyx.size());
533 data->setData(lyx_mime_type, qlyx);
535 // Don't test for text.empty() since we want to be able to clear the
537 QString const qtext = toqstr(text);
538 data->setText(qtext);
539 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
543 bool GuiClipboard::hasLyXContents() const
545 QMimeData const * const source =
546 qApp->clipboard()->mimeData(QClipboard::Clipboard);
547 return source && source->hasFormat(lyx_mime_type);
551 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
553 if (type == AnyGraphicsType) {
554 return hasGraphicsContents(PdfGraphicsType)
555 || hasGraphicsContents(PngGraphicsType)
556 || hasGraphicsContents(JpegGraphicsType)
557 || hasGraphicsContents(EmfGraphicsType)
558 || hasGraphicsContents(WmfGraphicsType)
559 || hasGraphicsContents(LinkBackGraphicsType);
562 QMimeData const * const source =
563 qApp->clipboard()->mimeData(QClipboard::Clipboard);
565 // handle image cases first
566 if (type == PngGraphicsType || type == JpegGraphicsType)
567 return source->hasImage();
569 // handle LinkBack for Mac
571 if (type == LinkBackGraphicsType)
572 return isLinkBackDataInPasteboard();
574 if (type == LinkBackGraphicsType)
579 QStringList const & formats = source->formats();
580 LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
581 for (int i = 0; i < formats.size(); ++i) {
582 LYXERR(Debug::ACTION, "Found format " << formats[i]);
585 // compute mime for type
588 case EmfGraphicsType: mime = emf_mime_type; break;
589 case WmfGraphicsType: mime = wmf_mime_type; break;
590 case PdfGraphicsType: mime = pdf_mime_type; break;
591 default: LASSERT(false, /**/);
594 return source && source->hasFormat(mime);
598 bool GuiClipboard::isInternal() const
600 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
601 // the preamble dialog
602 // FIXME: This does only work on X11, since ownsClipboard() is
603 // hardwired to return false on Windows and OS X.
604 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
608 bool GuiClipboard::hasInternal() const
610 // Windows and Mac OS X does not have the concept of ownership;
611 // the clipboard is a fully global resource so all applications
612 // are notified of changes.
613 #if (defined(Q_WS_X11))
621 void GuiClipboard::on_dataChanged()
623 QMimeData const * const source =
624 qApp->clipboard()->mimeData(QClipboard::Clipboard);
625 QStringList l = source->formats();
626 LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
627 for (int i = 0; i < l.count(); i++)
628 LYXERR(Debug::ACTION, l.value(i));
630 text_clipboard_empty_ = qApp->clipboard()->
631 text(QClipboard::Clipboard).isEmpty();
633 has_lyx_contents_ = hasLyXContents();
634 has_graphics_contents_ = hasGraphicsContents();
638 bool GuiClipboard::empty() const
640 // We need to check both the plaintext and the LyX version of the
641 // clipboard. The plaintext version is empty if the LyX version
642 // contains only one inset, and the LyX version is empty if the
643 // clipboard does not come from LyX.
644 if (!text_clipboard_empty_)
646 return !has_lyx_contents_ && !has_graphics_contents_;
649 } // namespace frontend
652 #include "GuiClipboard_moc.cpp"