From 99b8b647e3623325a8bfe39bea66c71c5467cdbc Mon Sep 17 00:00:00 2001 From: Morten Johan Sorvig Date: Mon, 12 Mar 2012 12:07:51 +0100 Subject: [PATCH] Implement cocoa clipboard support. Add QCoccoaClipboard which wraps the existing QMacPasteboard implementation. Remove unused QClipboard integration code from qmacclipboard.mm Change mime type cleanup from using qAddPostRoutine to using an explicit call to destroyMimieTypes in the cocoa platform integration destructor. This is necessary to ensure cleanup happens in the correct order on app shutdown. Change-Id: Ief0e0d996b04c8e84e9fd2cd3a17fb5bd73bb761 Reviewed-by: Bradley T. Hughes --- src/plugins/platforms/cocoa/cocoa.pro | 2 + src/plugins/platforms/cocoa/qcocoaclipboard.h | 70 ++++++++++++ .../platforms/cocoa/qcocoaclipboard.mm | 93 ++++++++++++++++ .../platforms/cocoa/qcocoaintegration.h | 3 + .../platforms/cocoa/qcocoaintegration.mm | 14 ++- src/plugins/platforms/cocoa/qmacclipboard.mm | 100 ------------------ src/plugins/platforms/cocoa/qmacmime.h | 3 +- src/plugins/platforms/cocoa/qmacmime.mm | 22 ++-- 8 files changed, 194 insertions(+), 113 deletions(-) create mode 100644 src/plugins/platforms/cocoa/qcocoaclipboard.h create mode 100644 src/plugins/platforms/cocoa/qcocoaclipboard.mm diff --git a/src/plugins/platforms/cocoa/cocoa.pro b/src/plugins/platforms/cocoa/cocoa.pro index c42932520f..e861f48e7b 100644 --- a/src/plugins/platforms/cocoa/cocoa.pro +++ b/src/plugins/platforms/cocoa/cocoa.pro @@ -27,6 +27,7 @@ OBJECTIVE_SOURCES += main.mm \ qcocoafiledialoghelper.mm \ qcocoafontdialoghelper.mm \ qcocoacursor.mm \ + qcocoaclipboard.mm \ qcocoadrag.mm \ qmacclipboard.mm \ qmacmime.mm \ @@ -59,6 +60,7 @@ HEADERS += qcocoaintegration.h \ qcocoafiledialoghelper.h \ qcocoafontdialoghelper.h \ qcocoacursor.h \ + qcocoaclipboard.h \ qcocoadrag.h \ qmacclipboard.h \ qmacmime.h \ diff --git a/src/plugins/platforms/cocoa/qcocoaclipboard.h b/src/plugins/platforms/cocoa/qcocoaclipboard.h new file mode 100644 index 0000000000..27505058bf --- /dev/null +++ b/src/plugins/platforms/cocoa/qcocoaclipboard.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QCOCOACLIPBOARD_H +#define QCOCOACLIPBOARD_H + +#include +#include "qmacclipboard.h" +#include + +QT_BEGIN_NAMESPACE + +class QCocoaClipboard : public QPlatformClipboard +{ +public: + QCocoaClipboard(); + + QMimeData *mimeData(QClipboard::Mode mode = QClipboard::Clipboard); + void setMimeData(QMimeData *data, QClipboard::Mode mode = QClipboard::Clipboard); + bool supportsMode(QClipboard::Mode mode) const; + bool ownsMode(QClipboard::Mode mode) const; +protected: + QMacPasteboard *pasteboardForMode(QClipboard::Mode mode) const; + +private: + QScopedPointer m_clipboard; + QScopedPointer m_find; +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/plugins/platforms/cocoa/qcocoaclipboard.mm b/src/plugins/platforms/cocoa/qcocoaclipboard.mm new file mode 100644 index 0000000000..799fb85183 --- /dev/null +++ b/src/plugins/platforms/cocoa/qcocoaclipboard.mm @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "QCocoaclipboard.h" +#include "qmacmime.h" +#include "qmacclipboard.h" + +QT_BEGIN_NAMESPACE + +QCocoaClipboard::QCocoaClipboard() + :m_clipboard(new QMacPasteboard(kPasteboardClipboard, QMacPasteboardMime::MIME_CLIP)) + ,m_find(new QMacPasteboard(kPasteboardFind, QMacPasteboardMime::MIME_CLIP)) +{ + +} + +QMimeData *QCocoaClipboard::mimeData(QClipboard::Mode mode) +{ + if (QMacPasteboard *pasteBoard = pasteboardForMode(mode)) { + pasteBoard->sync(); + return pasteBoard->mimeData(); + } + return 0; +} + +void QCocoaClipboard::setMimeData(QMimeData *data, QClipboard::Mode mode) +{ + if (QMacPasteboard *pasteBoard = pasteboardForMode(mode)) { + pasteBoard->sync(); + pasteBoard->setMimeData(data); + emitChanged(mode); + } +} + +bool QCocoaClipboard::supportsMode(QClipboard::Mode mode) const +{ + return (mode == QClipboard::Clipboard || mode == QClipboard::FindBuffer); +} + +bool QCocoaClipboard::ownsMode(QClipboard::Mode mode) const +{ + return false; +} + +QMacPasteboard *QCocoaClipboard::pasteboardForMode(QClipboard::Mode mode) const +{ + if (mode == QClipboard::Clipboard) + return m_clipboard.data(); + else if (mode == QClipboard::FindBuffer) + return m_find.data(); + else + return 0; +} + +QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.h b/src/plugins/platforms/cocoa/qcocoaintegration.h index 9248df0d37..97e7a7ffde 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.h +++ b/src/plugins/platforms/cocoa/qcocoaintegration.h @@ -46,6 +46,7 @@ #include "qcocoaautoreleasepool.h" #include "qcocoacursor.h" +#include "qcocoaclipboard.h" #include "qcocoadrag.h" #include @@ -91,6 +92,7 @@ public: QPlatformNativeInterface *nativeInterface() const; QPlatformInputContext *inputContext() const; QPlatformAccessibility *accessibility() const; + QPlatformClipboard *clipboard() const; QPlatformDrag *drag() const; QStringList themeNames() const; @@ -105,6 +107,7 @@ private: QScopedPointer mAccessibility; QScopedPointer mPlatformTheme; QList mScreens; + QCocoaClipboard *mCocoaClipboard; QScopedPointer mCocoaDrag; }; diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.mm b/src/plugins/platforms/cocoa/qcocoaintegration.mm index 96027e0925..f5febd4a16 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.mm +++ b/src/plugins/platforms/cocoa/qcocoaintegration.mm @@ -93,6 +93,7 @@ QCocoaIntegration::QCocoaIntegration() , mEventDispatcher(new QCocoaEventDispatcher()) , mInputContext(new QCocoaInputContext) , mAccessibility(new QPlatformAccessibility) + , mCocoaClipboard(new QCocoaClipboard) , mCocoaDrag(new QCocoaDrag) { QCocoaAutoReleasePool pool; @@ -140,13 +141,19 @@ QCocoaIntegration::QCocoaIntegration() screenAdded(screen); } - QMacPasteboardMime::initialize(); + QMacPasteboardMime::initializeMimeTypes(); } QCocoaIntegration::~QCocoaIntegration() { [[NSApplication sharedApplication] setDelegate: 0]; + // Delete the clipboard integration and destroy mime type converters. + // Deleting the clipboard integration flushes promised pastes using + // the mime converters - the ordering here is important. + delete mCocoaClipboard; + QMacPasteboardMime::destroyMimeTypes(); + // Delete screens in reverse order to avoid crash in case of multiple screens while (!mScreens.isEmpty()) { delete mScreens.takeLast(); @@ -206,6 +213,11 @@ QPlatformAccessibility *QCocoaIntegration::accessibility() const return mAccessibility.data(); } +QPlatformClipboard *QCocoaIntegration::clipboard() const +{ + return mCocoaClipboard; +} + QPlatformDrag *QCocoaIntegration::drag() const { return mCocoaDrag.data(); diff --git a/src/plugins/platforms/cocoa/qmacclipboard.mm b/src/plugins/platforms/cocoa/qmacclipboard.mm index d5af6de69c..eff2c5b07a 100644 --- a/src/plugins/platforms/cocoa/qmacclipboard.mm +++ b/src/plugins/platforms/cocoa/qmacclipboard.mm @@ -63,106 +63,6 @@ QT_USE_NAMESPACE *****************************************************************************/ //#define DEBUG_PASTEBOARD -#ifndef QT_NO_CLIPBOARD - -/***************************************************************************** - QClipboard member functions for mac. - *****************************************************************************/ - -static QMacPasteboard *qt_mac_pasteboards[2] = {0, 0}; - -static inline QMacPasteboard *qt_mac_pasteboard(QClipboard::Mode mode) -{ - Q_ASSERT(mode == QClipboard::Clipboard || mode == QClipboard::FindBuffer); - if (mode == QClipboard::Clipboard) - return qt_mac_pasteboards[0]; - else - return qt_mac_pasteboards[1]; -} - -static void qt_mac_cleanupPasteboard() { - delete qt_mac_pasteboards[0]; - delete qt_mac_pasteboards[1]; - qt_mac_pasteboards[0] = 0; - qt_mac_pasteboards[1] = 0; -} - -static bool qt_mac_updateScrap(QClipboard::Mode mode) -{ - if (!qt_mac_pasteboards[0]) { - qt_mac_pasteboards[0] = new QMacPasteboard(kPasteboardClipboard, QMacPasteboardMime::MIME_CLIP); - qt_mac_pasteboards[1] = new QMacPasteboard(kPasteboardFind, QMacPasteboardMime::MIME_CLIP); - qAddPostRoutine(qt_mac_cleanupPasteboard); - return true; - } - return qt_mac_pasteboard(mode)->sync(); -} - -void QClipboard::clear(Mode mode) -{ - if (!supportsMode(mode)) - return; - qt_mac_updateScrap(mode); - qt_mac_pasteboard(mode)->clear(); - setMimeData(0, mode); -} - -void QClipboard::ownerDestroyed() -{ -} - - -void QClipboard::connectNotify(const char *signal) -{ - Q_UNUSED(signal); -} - -bool QClipboard::event(QEvent *e) -{ - if (e->type() != QEvent::Clipboard) - return QObject::event(e); - - if (qt_mac_updateScrap(QClipboard::Clipboard)) { - emitChanged(QClipboard::Clipboard); - } - - if (qt_mac_updateScrap(QClipboard::FindBuffer)) { - emitChanged(QClipboard::FindBuffer); - } - - return QObject::event(e); -} - -const QMimeData *QClipboard::mimeData(Mode mode) const -{ - if (!supportsMode(mode)) - return 0; - qt_mac_updateScrap(mode); - return qt_mac_pasteboard(mode)->mimeData(); -} - -void QClipboard::setMimeData(QMimeData *src, Mode mode) -{ - if (!supportsMode(mode)) - return; - qt_mac_updateScrap(mode); - qt_mac_pasteboard(mode)->setMimeData(src); - emitChanged(mode); -} - -bool QClipboard::supportsMode(Mode mode) const -{ - return (mode == Clipboard || mode == FindBuffer); -} - -bool QClipboard::ownsMode(Mode mode) const -{ - Q_UNUSED(mode); - return false; -} - -#endif // QT_NO_CLIPBOARD - /***************************************************************************** QMacPasteboard code *****************************************************************************/ diff --git a/src/plugins/platforms/cocoa/qmacmime.h b/src/plugins/platforms/cocoa/qmacmime.h index 842caa5f2f..7226caef28 100644 --- a/src/plugins/platforms/cocoa/qmacmime.h +++ b/src/plugins/platforms/cocoa/qmacmime.h @@ -58,7 +58,8 @@ public: explicit QMacPasteboardMime(char); virtual ~QMacPasteboardMime(); - static void initialize(); + static void initializeMimeTypes(); + static void destroyMimeTypes(); static QList all(uchar); static QMacPasteboardMime *convertor(uchar, const QString &mime, QString flav); diff --git a/src/plugins/platforms/cocoa/qmacmime.mm b/src/plugins/platforms/cocoa/qmacmime.mm index db86deb91c..8cb684fbea 100644 --- a/src/plugins/platforms/cocoa/qmacmime.mm +++ b/src/plugins/platforms/cocoa/qmacmime.mm @@ -64,14 +64,6 @@ extern CGImageRef qt_mac_createCGImageFromQImage(const QImage &img, const QImage typedef QList MimeList; Q_GLOBAL_STATIC(MimeList, globalMimeList) - -static void cleanup_mimes() -{ - MimeList *mimes = globalMimeList(); - while (!mimes->isEmpty()) - delete mimes->takeFirst(); -} - Q_GLOBAL_STATIC(QStringList, globalDraggedTypesList) /*! @@ -791,11 +783,9 @@ QList QMacPasteboardMimeVCard::convertFromMime(const QString &mime, This is an internal function. */ -void QMacPasteboardMime::initialize() +void QMacPasteboardMime::initializeMimeTypes() { if (globalMimeList()->isEmpty()) { - qAddPostRoutine(cleanup_mimes); - //standard types that we wrap new QMacPasteboardMimeTiff; new QMacPasteboardMimeUnicodeText; @@ -810,6 +800,16 @@ void QMacPasteboardMime::initialize() } } +/*! + \internal +*/ +void QMacPasteboardMime::destroyMimeTypes() +{ + MimeList *mimes = globalMimeList(); + while (!mimes->isEmpty()) + delete mimes->takeFirst(); +} + /*! Returns the most-recently created QMacPasteboardMime of type \a t that can convert between the \a mime and \a flav formats. Returns 0 if no such convertor