]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiClipboard.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiClipboard.cpp
index 59aa11f4942ff7bc8a25eab99def70c7c6cebe54..ec6b916e4506c46e86be8620a63b8c1b98defd0f 100644 (file)
 #include "GuiClipboard.h"
 #include "qt_helpers.h"
 
-#include "frontends/alert.h"
-
 #include "Buffer.h"
 #include "BufferView.h"
 #include "Cursor.h"
 
+#include "support/lassert.h"
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/filetools.h"
-#include "support/FileFilterList.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
+#ifdef Q_WS_MACX
+#include "support/linkback/LinkBackProxy.h"
+#endif // Q_WS_MACX
+
+#include "frontends/alert.h"
+
 #include <QApplication>
 #include <QBuffer>
 #include <QClipboard>
 #include <QDataStream>
 #include <QFile>
 #include <QImage>
-#include <QMacPasteboardMime>
 #include <QMimeData>
 #include <QString>
 #include <QStringList>
 
-#ifdef Q_WS_WIN
-#include <QWindowsMime>
-#ifdef Q_CYGWIN_WIN
-#include <wtypes.h>
-#endif
-#include <objidl.h>
-#endif // Q_WS_WIN
-
-#include "boost/assert.hpp"
-
+#include <memory>
 #include <map>
 
-#ifdef Q_WS_MACX
-#include "support/linkback/LinkBackProxy.h"
-#endif // Q_WS_MACX
-
 using namespace std;
 using namespace lyx::support;
 
-static char const * const lyx_mime_type = "application/x-lyx";
-static char const * const pdf_mime_type = "application/pdf";
-static char const * const emf_mime_type = "image/x-emf";
-static char const * const wmf_mime_type = "image/x-wmf";
 
 namespace lyx {
 
 namespace frontend {
 
-#ifdef Q_WS_WIN
-
-static FORMATETC cfFromMime(QString const & mimetype)
-{
-       FORMATETC formatetc;
-       if (mimetype == emf_mime_type) {
-               formatetc.cfFormat = CF_ENHMETAFILE;
-               formatetc.tymed = TYMED_ENHMF;
-       }
-       else if (mimetype == wmf_mime_type) {
-               formatetc.cfFormat = CF_METAFILEPICT;
-               formatetc.tymed = TYMED_MFPICT;
-       }
-       else return formatetc;
-       formatetc.ptd = NULL;
-       formatetc.dwAspect = DVASPECT_CONTENT;
-       formatetc.lindex = -1;
-       return formatetc;
-}
-
-
-class QWindowsMimeMetafile : public QWindowsMime {
-public:
-       bool canConvertFromMime(FORMATETC const & formatetc, QMimeData const * mimedata) const;
-       bool canConvertToMime(QString const & mimetype, IDataObject * pDataObj) const;
-       bool convertFromMime(FORMATETC const & formatetc, const QMimeData * mimedata, STGMEDIUM * pmedium) const;
-       QVariant convertToMime(QString const & mimetype, IDataObject * pDataObj, QVariant::Type preferredType) const;
-       QVector<FORMATETC> formatsForMime(QString const & mimeType, QMimeData const * mimeData) const;
-       QString mimeForFormat(FORMATETC const &) const;
-};
-
-
-QString QWindowsMimeMetafile::mimeForFormat(FORMATETC const & formatetc) const
-{
-       QString f;
-       if (formatetc.cfFormat == CF_ENHMETAFILE)
-               f = emf_mime_type; 
-       else if (formatetc.cfFormat == CF_METAFILEPICT)
-               f = wmf_mime_type;
-       return f;
-}
-
-
-bool QWindowsMimeMetafile::canConvertFromMime(
-       FORMATETC const & formatetc, QMimeData const * mimedata) const
-{
-       return false;
-}
-
-
-bool QWindowsMimeMetafile::canConvertToMime(
-       QString const & mimetype, IDataObject * pDataObj) const
-{
-       FORMATETC formatetc = cfFromMime(mimetype);
-       return pDataObj->QueryGetData(&formatetc) == S_OK;
-}
-
-
-bool QWindowsMimeMetafile::convertFromMime(
-       FORMATETC const & formatetc, QMimeData const * mimedata, 
-       STGMEDIUM * pmedium) const
-{
-       return false;
-}
-
-
-QVariant QWindowsMimeMetafile::convertToMime(QString const & mimetype,
-       IDataObject * pDataObj, QVariant::Type preferredType) const
-{
-       QByteArray data;
-       if (!canConvertToMime(mimetype, pDataObj))
-               return data;
-
-       FORMATETC formatetc = cfFromMime(mimetype);
-       STGMEDIUM s;
-       if (pDataObj->GetData(&formatetc, &s) != S_OK)
-               return data;
-
-       int dataSize;
-       if (s.tymed == TYMED_ENHMF) {
-               dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, 0, NULL);
-               data.resize(dataSize);
-               dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, dataSize, (LPBYTE)data.data());
-       } else if (s.tymed == TYMED_MFPICT) {
-               dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, 0, NULL);
-               data.resize(dataSize);
-               dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, dataSize, (LPBYTE)data.data());
-       }
-       data.detach();
-       ReleaseStgMedium(&s);
-
-       return data;
-}
-
-
-QVector<FORMATETC> QWindowsMimeMetafile::formatsForMime(
-       QString const & mimetype, QMimeData const * mimedata) const
-{
-       QVector<FORMATETC> formats;
-       formats += cfFromMime(mimetype);
-       return formats;
-}
-
-static QWindowsMimeMetafile * metafileWindowsMime;
-
-#endif // Q_WS_WIN
-
-#ifdef Q_WS_MACX
-
-class QMacPasteboardMimeGraphics : public QMacPasteboardMime {
-public:
-       QMacPasteboardMimeGraphics()
-               : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL)
-       {}
-       ~QMacPasteboardMimeGraphics() {}
-       QString convertorName();
-       QString flavorFor(const QString & mime);
-       QString mimeFor(QString flav);
-       bool canConvert(const QString & mime, QString flav);
-       QVariant convertToMime(const QString & mime, QList<QByteArray> data, QString flav);
-       QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav);
-};
-
-
-QString QMacPasteboardMimeGraphics::convertorName()
-{
-       return "Graphics";
-}
-
-
-QString QMacPasteboardMimeGraphics::flavorFor(const QString & mime)
-{
-       LYXERR(Debug::ACTION, "flavorFor " << fromqstr(mime));
-       if (mime == QLatin1String(pdf_mime_type))
-               return QLatin1String("com.adobe.pdf");
-       return QString();
-}
-
-
-QString QMacPasteboardMimeGraphics::mimeFor(QString flav)
-{
-       LYXERR(Debug::ACTION, "mimeFor " << fromqstr(flav));
-       if (flav == QLatin1String("com.adobe.pdf"))
-               return QLatin1String(pdf_mime_type);
-       return QString();
-}
-
-
-bool QMacPasteboardMimeGraphics::canConvert(const QString & mime, QString flav)
-{
-       return mimeFor(flav) == mime;
-}
-
-
-QVariant QMacPasteboardMimeGraphics::convertToMime(const QString & mime, QList<QByteArray> data, QString)
-{
-       if(data.count() > 1)
-               qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
-       return data.first();
-}
-
-
-QList<QByteArray> QMacPasteboardMimeGraphics::convertFromMime(const QString &mime, QVariant data, QString)
-{
-       QList<QByteArray> ret;
-       ret.append(data.toByteArray());
-       return ret;
-}
 
-static QMacPasteboardMimeGraphics * graphicsPasteboardMime;
-
-#endif // Q_WS_MACX
+QString const lyxMimeType(){ return "application/x-lyx"; }
+QString const pdfMimeType(){ return "application/pdf"; }
+QString const emfMimeType(){ return "image/x-emf"; }
+QString const wmfMimeType(){ return "image/x-wmf"; }
 
 
 GuiClipboard::GuiClipboard()
@@ -249,24 +68,6 @@ GuiClipboard::GuiClipboard()
                this, SLOT(on_dataChanged()));
        // initialize clipboard status.
        on_dataChanged();
-       
-#ifdef Q_WS_MACX
-       if (!graphicsPasteboardMime)
-               graphicsPasteboardMime = new QMacPasteboardMimeGraphics();
-#endif // Q_WS_MACX
-
-#ifdef Q_WS_WIN
-       if (!metafileWindowsMime)
-               metafileWindowsMime = new QWindowsMimeMetafile();
-#endif // Q_WS_WIN
-}
-
-
-GuiClipboard::~GuiClipboard()
-{
-#ifdef Q_WS_MACX
-       closeAllLinkBackLinks();
-#endif // Q_WS_MACX
 }
 
 
@@ -282,9 +83,9 @@ string const GuiClipboard::getAsLyX() const
                return string();
        }
 
-       if (source->hasFormat(lyx_mime_type)) {
+       if (source->hasFormat(lyxMimeType())) {
                // data from ourself or some other LyX instance
-               QByteArray const ar = source->data(lyx_mime_type);
+               QByteArray const ar = source->data(lyxMimeType());
                string const s(ar.data(), ar.count());
                LYXERR(Debug::ACTION, s << "'");
                return s;
@@ -312,7 +113,7 @@ FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
        if (hasGraphicsContents(Clipboard::JpegGraphicsType))
                types.push_back(Clipboard::JpegGraphicsType);
        
-       BOOST_ASSERT(!types.empty());
+       LASSERT(!types.empty(), /**/);
        
        // select prefered type if AnyGraphicsType was passed
        if (type == Clipboard::AnyGraphicsType)
@@ -337,7 +138,7 @@ FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
        typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
        
        // find unused filename with primary extension
-       string document_path = cur.buffer().fileName().onlyPath().absFilename();
+       string document_path = cur.buffer()->fileName().onlyPath().absFilename();
        unsigned newfile_number = 0;
        FileName filename;
        do {
@@ -350,16 +151,16 @@ FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
        
        while (true) {
                // create file type filter, putting the prefered on to the front
-               docstring filterSpec;
+               QStringList filter;
                for (size_t i = 0; i != types.size(); ++i) {
                        docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
                                + " (*." + from_ascii(extensions[types[i]]) + ")";
                        if (types[i] == type)
-                               filterSpec = s + filterSpec;
+                               filter.prepend(toqstr(s));
                        else
-                               filterSpec += ";;" + s;
+                               filter.append(toqstr(s));
                }
-               FileFilterList const filter(filterSpec);
+               filter = fileFilters(filter.join(";;"));
                
                // show save dialog for the graphic
                FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
@@ -441,7 +242,7 @@ FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) cons
                else if (type == JpegGraphicsType)
                        image.save(toqstr(filename.absFilename()), "JPEG");
                else
-                       BOOST_ASSERT(false);
+                       LASSERT(false, /**/);
                
                return filename;
        }
@@ -457,11 +258,11 @@ FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) cons
        // get mime for type
        QString mime;
        switch (type) {
-       case PdfGraphicsType: mime = pdf_mime_type; break;
-       case LinkBackGraphicsType: mime = pdf_mime_type; break;
-       case EmfGraphicsType: mime = emf_mime_type; break;
-       case WmfGraphicsType: mime = wmf_mime_type; break;
-       default: BOOST_ASSERT(false);
+       case PdfGraphicsType: mime = pdfMimeType(); break;
+       case LinkBackGraphicsType: mime = pdfMimeType(); break;
+       case EmfGraphicsType: mime = emfMimeType(); break;
+       case WmfGraphicsType: mime = wmfMimeType(); break;
+       default: LASSERT(false, /**/);
        }
        
        // get data
@@ -492,7 +293,7 @@ FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) cons
                ds << pdfLen; // big endian by default
 #else
                // only non-Mac this should never happen
-               BOOST_ASSERT(false);
+               LASSERT(false, /**/);
 #endif // Q_WS_MACX
        }
 
@@ -506,11 +307,11 @@ docstring const GuiClipboard::getAsText() const
        // text data from other applications
        QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
                                .normalized(QString::NormalizationForm_C);
-       LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
+       LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << str << "'");
        if (str.isNull())
                return docstring();
 
-       return internalLineEnding(qstring_to_ucs4(str));
+       return internalLineEnding(str);
 }
 
 
@@ -523,7 +324,7 @@ void GuiClipboard::put(string const & lyx, docstring const & text)
        QMimeData * data = new QMimeData;
        if (!lyx.empty()) {
                QByteArray const qlyx(lyx.c_str(), lyx.size());
-               data->setData(lyx_mime_type, qlyx);
+               data->setData(lyxMimeType(), qlyx);
        }
        // Don't test for text.empty() since we want to be able to clear the
        // clipboard.
@@ -537,7 +338,15 @@ bool GuiClipboard::hasLyXContents() const
 {
        QMimeData const * const source =
                qApp->clipboard()->mimeData(QClipboard::Clipboard);
-       return source && source->hasFormat(lyx_mime_type);
+       return source && source->hasFormat(lyxMimeType());
+}
+
+
+bool GuiClipboard::hasTextContents() const
+{
+       QMimeData const * const source =
+               qApp->clipboard()->mimeData(QClipboard::Clipboard);
+       return source && source->hasText();     
 }
 
 
@@ -560,28 +369,26 @@ bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
                return source->hasImage();
 
        // handle LinkBack for Mac
-#ifdef Q_WS_MACX
        if (type == LinkBackGraphicsType)
+#ifdef Q_WS_MACX
                return isLinkBackDataInPasteboard();
 #else
-       if (type == LinkBackGraphicsType)
                return false;
 #endif // Q_WS_MACX
        
        // get mime data
        QStringList const & formats = source->formats();
        LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
-       for (int i = 0; i < formats.size(); ++i) {
-               LYXERR(Debug::ACTION, "Found format " << fromqstr(formats[i]));
-       }
+       for (int i = 0; i < formats.size(); ++i)
+               LYXERR(Debug::ACTION, "Found format " << formats[i]);
 
        // compute mime for type
        QString mime;
        switch (type) {
-       case EmfGraphicsType: mime = emf_mime_type; break;
-       case WmfGraphicsType: mime = wmf_mime_type; break;
-       case PdfGraphicsType: mime = pdf_mime_type; break;
-       default: BOOST_ASSERT(false);
+       case EmfGraphicsType: mime = emfMimeType(); break;
+       case WmfGraphicsType: mime = wmfMimeType(); break;
+       case PdfGraphicsType: mime = pdfMimeType(); break;
+       default: LASSERT(false, /**/);
        }
        
        return source && source->hasFormat(mime);
@@ -617,9 +424,8 @@ void GuiClipboard::on_dataChanged()
        qApp->clipboard()->mimeData(QClipboard::Clipboard);
        QStringList l = source->formats();
        LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
-       for (int i = 0; i < l.count(); i++) {
-               LYXERR(Debug::ACTION, fromqstr(l.value(i)));
-       }
+       for (int i = 0; i < l.count(); i++)
+               LYXERR(Debug::ACTION, l.value(i));
        
        text_clipboard_empty_ = qApp->clipboard()->
                text(QClipboard::Clipboard).isEmpty();
@@ -643,4 +449,4 @@ bool GuiClipboard::empty() const
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiClipboard_moc.cpp"
+#include "moc_GuiClipboard.cpp"