util/src/gui/kernel/qclipboard_win.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qclipboard.h"
       
    43 
       
    44 #ifndef QT_NO_CLIPBOARD
       
    45 
       
    46 #include "qapplication.h"
       
    47 #include "qapplication_p.h"
       
    48 #include "qeventloop.h"
       
    49 #include "qwidget.h"
       
    50 #include "qevent.h"
       
    51 #include "qmime.h"
       
    52 #include "qt_windows.h"
       
    53 #include "qdnd_p.h"
       
    54 #include <private/qwidget_p.h>
       
    55 
       
    56 QT_BEGIN_NAMESPACE
       
    57 
       
    58 #if defined(Q_OS_WINCE)
       
    59 QT_BEGIN_INCLUDE_NAMESPACE
       
    60 #include "qguifunctions_wince.h"
       
    61 QT_END_INCLUDE_NAMESPACE
       
    62 
       
    63 HRESULT QtCeGetClipboard(IDataObject** obj);
       
    64 HRESULT QtCeSetClipboard(IDataObject* obj);
       
    65 void QtCeFlushClipboard();
       
    66 
       
    67 #define OleGetClipboard QtCeGetClipboard
       
    68 #define OleSetClipboard QtCeSetClipboard
       
    69 #define OleFlushClipboard QtCeFlushClipboard
       
    70 
       
    71 #endif
       
    72 
       
    73 
       
    74 class QClipboardWatcher : public QInternalMimeData {
       
    75 public:
       
    76     QClipboardWatcher()
       
    77         : QInternalMimeData()
       
    78     {
       
    79     }
       
    80 
       
    81     bool hasFormat_sys(const QString &mimetype) const;
       
    82     QStringList formats_sys() const;
       
    83     QVariant retrieveData_sys(const QString &mimetype, QVariant::Type preferredType) const;
       
    84 };
       
    85 
       
    86 
       
    87 bool QClipboardWatcher::hasFormat_sys(const QString &mime) const
       
    88 {
       
    89     IDataObject * pDataObj = 0;
       
    90 
       
    91     if (OleGetClipboard(&pDataObj) != S_OK && !pDataObj) // Sanity
       
    92         return false;
       
    93 
       
    94     bool has = QWindowsMime::converterToMime(mime, pDataObj) != 0;
       
    95 
       
    96     pDataObj->Release();
       
    97 
       
    98     return has;
       
    99 }
       
   100 
       
   101 QStringList QClipboardWatcher::formats_sys() const
       
   102 {
       
   103     QStringList fmts;
       
   104     IDataObject * pDataObj = 0;
       
   105 
       
   106     if (OleGetClipboard(&pDataObj) != S_OK && !pDataObj) // Sanity
       
   107         return QStringList();
       
   108 
       
   109     fmts = QWindowsMime::allMimesForFormats(pDataObj);
       
   110 
       
   111     pDataObj->Release();
       
   112 
       
   113     return fmts;
       
   114 }
       
   115 
       
   116 QVariant QClipboardWatcher::retrieveData_sys(const QString &mimeType, QVariant::Type type) const
       
   117 {
       
   118     QVariant result;
       
   119     IDataObject * pDataObj = 0;
       
   120 
       
   121     if (OleGetClipboard(&pDataObj) != S_OK && !pDataObj) // Sanity
       
   122         return result;
       
   123 
       
   124     QWindowsMime *converter = QWindowsMime::converterToMime(mimeType, pDataObj);
       
   125 
       
   126     if (converter)
       
   127         result = converter->convertToMime(mimeType, pDataObj, type);
       
   128 
       
   129     pDataObj->Release();
       
   130 
       
   131     return result;
       
   132 }
       
   133 
       
   134 class QClipboardData
       
   135 {
       
   136 public:
       
   137     QClipboardData()
       
   138         : iData(0)
       
   139         , nextClipboardViewer(0)
       
   140     {
       
   141         clipBoardViewer = new QWidget();
       
   142         clipBoardViewer->createWinId();
       
   143         clipBoardViewer->setObjectName(QLatin1String("internal clipboard owner"));
       
   144         // We dont need this internal widget to appear in QApplication::topLevelWidgets()
       
   145         if (QWidgetPrivate::allWidgets)
       
   146             QWidgetPrivate::allWidgets->remove(clipBoardViewer);
       
   147     }
       
   148 
       
   149     ~QClipboardData()
       
   150     {
       
   151         Q_ASSERT(clipBoardViewer->testAttribute(Qt::WA_WState_Created));
       
   152         ChangeClipboardChain(clipBoardViewer->internalWinId(), nextClipboardViewer);
       
   153         delete clipBoardViewer;
       
   154         releaseIData();
       
   155     }
       
   156 
       
   157     void releaseIData()
       
   158     {
       
   159         if (iData) {
       
   160             delete iData->mimeData();
       
   161             iData->releaseQt();
       
   162             iData->Release();
       
   163             iData = 0;
       
   164         }
       
   165     }
       
   166 
       
   167     QOleDataObject * iData;
       
   168     QWidget *clipBoardViewer;
       
   169     HWND nextClipboardViewer;
       
   170     QClipboardWatcher watcher;
       
   171 };
       
   172 
       
   173 static QClipboardData *ptrClipboardData = 0;
       
   174 
       
   175 static QClipboardData *clipboardData()
       
   176 {
       
   177     if (ptrClipboardData == 0) {
       
   178         ptrClipboardData = new QClipboardData;
       
   179         // this needs to be done here to avoid recursion
       
   180         Q_ASSERT(ptrClipboardData->clipBoardViewer->testAttribute(Qt::WA_WState_Created));
       
   181         ptrClipboardData->nextClipboardViewer = SetClipboardViewer(ptrClipboardData->clipBoardViewer->internalWinId());
       
   182     }
       
   183     return ptrClipboardData;
       
   184 }
       
   185 
       
   186 static void cleanupClipboardData()
       
   187 {
       
   188     delete ptrClipboardData;
       
   189     ptrClipboardData = 0;
       
   190 }
       
   191 
       
   192 #if defined(Q_OS_WINCE)
       
   193 HRESULT QtCeGetClipboard(IDataObject** obj)
       
   194 {
       
   195     HWND owner = ptrClipboardData->clipBoardViewer->internalWinId();
       
   196     if (!OpenClipboard(owner))
       
   197         return !S_OK;
       
   198 
       
   199     if (!IsClipboardFormatAvailable(CF_TEXT) && !IsClipboardFormatAvailable(CF_UNICODETEXT))
       
   200         return !S_OK;
       
   201 
       
   202     HANDLE clipData = GetClipboardData(CF_TEXT);
       
   203     QString clipText;
       
   204     if (clipData == 0) {
       
   205         clipData = GetClipboardData(CF_UNICODETEXT);
       
   206         if (clipData != 0)
       
   207             clipText = QString::fromWCharArray((wchar_t *)clipData);
       
   208     } else {
       
   209         clipText = QString::fromLatin1((const char*)clipData);
       
   210     }
       
   211 
       
   212     QMimeData *mimeData = new QMimeData();
       
   213     mimeData->setText(clipText);
       
   214     QOleDataObject* data = new QOleDataObject(mimeData);
       
   215     *obj = data;
       
   216     CloseClipboard();
       
   217     return S_OK;
       
   218 }
       
   219 
       
   220 HRESULT QtCeSetClipboard(IDataObject* obj)
       
   221 {
       
   222     HWND owner = ptrClipboardData->clipBoardViewer->internalWinId();
       
   223     if (!OpenClipboard(owner))
       
   224         return !S_OK;
       
   225 
       
   226     bool result = false;
       
   227     if (obj == 0) {
       
   228         result = true;
       
   229         EmptyClipboard();
       
   230         CloseClipboard();
       
   231     } else {
       
   232         QOleDataObject* qobj = static_cast<QOleDataObject*>(obj);
       
   233 
       
   234         const QMimeData* data = qobj->mimeData();
       
   235         if (data->hasText()) {
       
   236             EmptyClipboard();
       
   237             result = SetClipboardData(CF_UNICODETEXT, wcsdup(reinterpret_cast<const wchar_t *> (data->text().utf16()))) != NULL;
       
   238             CloseClipboard();
       
   239             result = true;
       
   240         }
       
   241     }
       
   242     return result ? S_OK : !S_OK;
       
   243 }
       
   244 
       
   245 void QtCeFlushClipboard() { }
       
   246 #endif
       
   247 
       
   248 
       
   249 
       
   250 QClipboard::~QClipboard()
       
   251 {
       
   252     cleanupClipboardData();
       
   253 }
       
   254 
       
   255 void QClipboard::setMimeData(QMimeData *src, Mode mode)
       
   256 {
       
   257     if (mode != Clipboard)
       
   258         return;
       
   259     QClipboardData *d = clipboardData();
       
   260 
       
   261     if (!(d->iData && d->iData->mimeData() == src)) {
       
   262         d->releaseIData();
       
   263         d->iData = new QOleDataObject(src);
       
   264     }
       
   265 
       
   266     if (OleSetClipboard(d->iData) != S_OK) {
       
   267         d->releaseIData();
       
   268         qErrnoWarning("QClipboard::setMimeData: Failed to set data on clipboard");
       
   269         return;
       
   270     }
       
   271 #if defined(Q_OS_WINCE)
       
   272     // As WinCE does not support notifications we send the signal here
       
   273     // We will get no event when the clipboard changes outside...
       
   274     emit dataChanged();
       
   275     emit changed(Clipboard);
       
   276 #endif
       
   277 }
       
   278 
       
   279 void QClipboard::clear(Mode mode)
       
   280 {
       
   281     if (mode != Clipboard) return;
       
   282 
       
   283     QClipboardData *d = clipboardData();
       
   284 
       
   285     d->releaseIData();
       
   286 
       
   287     if (OleSetClipboard(0) != S_OK) {
       
   288         qErrnoWarning("QClipboard::clear: Failed to clear data on clipboard");
       
   289         return;
       
   290     }
       
   291 #if defined(Q_OS_WINCE)
       
   292     // As WinCE does not support notifications we send the signal here
       
   293     // We will get no event when the clipboard changes outside...
       
   294     emit dataChanged();
       
   295     emit changed(Clipboard);
       
   296 #endif
       
   297 }
       
   298 
       
   299 bool QClipboard::event(QEvent *e)
       
   300 {
       
   301     if (e->type() != QEvent::Clipboard)
       
   302         return QObject::event(e);
       
   303 
       
   304     QClipboardData *d = clipboardData();
       
   305 
       
   306     MSG *m = (MSG *)((QClipboardEvent*)e)->data();
       
   307     if (!m) {
       
   308         // this is sent to render all formats at app shut down
       
   309         if (ownsClipboard()) {
       
   310             OleFlushClipboard();
       
   311             d->releaseIData();
       
   312         }
       
   313         return true;
       
   314     }
       
   315 
       
   316     bool propagate = false;
       
   317 
       
   318     if (m->message == WM_CHANGECBCHAIN) {
       
   319         if ((HWND)m->wParam == d->nextClipboardViewer)
       
   320             d->nextClipboardViewer = (HWND)m->lParam;
       
   321         else
       
   322             propagate = true;
       
   323     } else if (m->message == WM_DRAWCLIPBOARD) {
       
   324         emitChanged(QClipboard::Clipboard);
       
   325         if (!ownsClipboard() && d->iData)
       
   326             // clean up the clipboard object if we no longer own the clipboard
       
   327             d->releaseIData();
       
   328         propagate = true;
       
   329     }
       
   330 
       
   331     if (propagate && d->nextClipboardViewer) {
       
   332         SendMessage(d->nextClipboardViewer, m->message, m->wParam, m->lParam);
       
   333     }
       
   334 
       
   335     return true;
       
   336 }
       
   337 
       
   338 void QClipboard::connectNotify(const char *signal)
       
   339 {
       
   340     if (qstrcmp(signal,SIGNAL(dataChanged())) == 0) {
       
   341         // ensure we are up and running but block signals so the dataChange signal
       
   342         // is not emitted while being connected to.
       
   343         bool blocked = blockSignals(true);
       
   344         QClipboardData *d = clipboardData();
       
   345         blockSignals(blocked);
       
   346         Q_UNUSED(d);
       
   347     }
       
   348 }
       
   349 
       
   350 const QMimeData *QClipboard::mimeData(Mode mode) const
       
   351 {
       
   352     if (mode != Clipboard)
       
   353         return 0;
       
   354 
       
   355     QClipboardData *data = clipboardData();
       
   356     // sort cut for local copy / paste
       
   357     if (ownsClipboard() && data->iData->mimeData())
       
   358         return data->iData->mimeData();
       
   359     return &data->watcher;
       
   360 }
       
   361 
       
   362 bool QClipboard::supportsMode(Mode mode) const
       
   363 {
       
   364     return (mode == Clipboard);
       
   365 }
       
   366 
       
   367 bool QClipboard::ownsMode(Mode mode) const
       
   368 {
       
   369     if (mode == Clipboard) {
       
   370         QClipboardData *d = clipboardData();
       
   371 #if !defined(Q_OS_WINCE)
       
   372         return d->iData && OleIsCurrentClipboard(d->iData) == S_OK;
       
   373 #else
       
   374         return d->iData && GetClipboardOwner() == d->clipBoardViewer->internalWinId();
       
   375 #endif
       
   376     } else {
       
   377         return false;
       
   378     }
       
   379 }
       
   380 
       
   381 void QClipboard::ownerDestroyed()
       
   382 {
       
   383 }
       
   384 
       
   385 QT_END_NAMESPACE
       
   386 
       
   387 #endif // QT_NO_CLIPBOARD