src/gui/kernel/qclipboard_win.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 #if defined(Q_OS_WINCE)
       
    58 QT_BEGIN_INCLUDE_NAMESPACE
       
    59 #include "qguifunctions_wince.h"
       
    60 QT_END_INCLUDE_NAMESPACE
       
    61 
       
    62 HRESULT QtCeGetClipboard(IDataObject** obj);
       
    63 HRESULT QtCeSetClipboard(IDataObject* obj);
       
    64 void QtCeFlushClipboard();
       
    65 
       
    66 #define OleGetClipboard QtCeGetClipboard
       
    67 #define OleSetClipboard QtCeSetClipboard
       
    68 #define OleFlushClipboard QtCeFlushClipboard
       
    69 
       
    70 #endif
       
    71 
       
    72 
       
    73 class QClipboardWatcher : public QInternalMimeData {
       
    74 public:
       
    75     QClipboardWatcher()
       
    76         : QInternalMimeData()
       
    77     {
       
    78     }
       
    79 
       
    80     bool hasFormat_sys(const QString &mimetype) const;
       
    81     QStringList formats_sys() const;
       
    82     QVariant retrieveData_sys(const QString &mimetype, QVariant::Type preferredType) const;
       
    83 };
       
    84 
       
    85 
       
    86 bool QClipboardWatcher::hasFormat_sys(const QString &mime) const
       
    87 {
       
    88     IDataObject * pDataObj = 0;
       
    89 
       
    90     if (OleGetClipboard(&pDataObj) != S_OK && !pDataObj) // Sanity
       
    91         return false;
       
    92 
       
    93     bool has = QWindowsMime::converterToMime(mime, pDataObj) != 0;
       
    94 
       
    95     pDataObj->Release();
       
    96 
       
    97     return has;
       
    98 }
       
    99 
       
   100 QStringList QClipboardWatcher::formats_sys() const
       
   101 {
       
   102     QStringList fmts;
       
   103     IDataObject * pDataObj = 0;
       
   104 
       
   105     if (OleGetClipboard(&pDataObj) != S_OK && !pDataObj) // Sanity
       
   106         return QStringList();
       
   107 
       
   108     fmts = QWindowsMime::allMimesForFormats(pDataObj);
       
   109 
       
   110     pDataObj->Release();
       
   111 
       
   112     return fmts;
       
   113 }
       
   114 
       
   115 QVariant QClipboardWatcher::retrieveData_sys(const QString &mimeType, QVariant::Type type) const
       
   116 {
       
   117     QVariant result;
       
   118     IDataObject * pDataObj = 0;
       
   119 
       
   120     if (OleGetClipboard(&pDataObj) != S_OK && !pDataObj) // Sanity
       
   121         return result;
       
   122 
       
   123     QWindowsMime *converter = QWindowsMime::converterToMime(mimeType, pDataObj);
       
   124 
       
   125     if (converter)
       
   126         result = converter->convertToMime(mimeType, pDataObj, type);
       
   127 
       
   128     pDataObj->Release();
       
   129 
       
   130     return result;
       
   131 }
       
   132 
       
   133 class QClipboardData
       
   134 {
       
   135 public:
       
   136     QClipboardData()
       
   137         : iData(0)
       
   138         , nextClipboardViewer(0)
       
   139     {
       
   140         clipBoardViewer = new QWidget();
       
   141         clipBoardViewer->createWinId();
       
   142         clipBoardViewer->setObjectName(QLatin1String("internal clipboard owner"));
       
   143     }
       
   144 
       
   145     ~QClipboardData()
       
   146     {
       
   147         Q_ASSERT(clipBoardViewer->testAttribute(Qt::WA_WState_Created));
       
   148         ChangeClipboardChain(clipBoardViewer->internalWinId(), nextClipboardViewer);
       
   149         delete clipBoardViewer;
       
   150         releaseIData();
       
   151     }
       
   152 
       
   153     void releaseIData()
       
   154     {
       
   155         if (iData) {
       
   156             delete iData->mimeData();
       
   157             iData->releaseQt();
       
   158             iData->Release();
       
   159             iData = 0;
       
   160         }
       
   161     }
       
   162 
       
   163     QOleDataObject * iData;
       
   164     QWidget *clipBoardViewer;
       
   165     HWND nextClipboardViewer;
       
   166     QClipboardWatcher watcher;
       
   167 };
       
   168 
       
   169 static QClipboardData *ptrClipboardData = 0;
       
   170 
       
   171 static QClipboardData *clipboardData()
       
   172 {
       
   173     if (ptrClipboardData == 0) {
       
   174         ptrClipboardData = new QClipboardData;
       
   175         // this needs to be done here to avoid recursion
       
   176         Q_ASSERT(ptrClipboardData->clipBoardViewer->testAttribute(Qt::WA_WState_Created));
       
   177         ptrClipboardData->nextClipboardViewer = SetClipboardViewer(ptrClipboardData->clipBoardViewer->internalWinId());
       
   178     }
       
   179     return ptrClipboardData;
       
   180 }
       
   181 
       
   182 static void cleanupClipboardData()
       
   183 {
       
   184     delete ptrClipboardData;
       
   185     ptrClipboardData = 0;
       
   186 }
       
   187 
       
   188 #if defined(Q_OS_WINCE)
       
   189 HRESULT QtCeGetClipboard(IDataObject** obj)
       
   190 {
       
   191     HWND owner = ptrClipboardData->clipBoardViewer->internalWinId();
       
   192     if (!OpenClipboard(owner))
       
   193         return !S_OK;
       
   194 
       
   195     if (!IsClipboardFormatAvailable(CF_TEXT) && !IsClipboardFormatAvailable(CF_UNICODETEXT))
       
   196         return !S_OK;
       
   197 
       
   198     HANDLE clipData = GetClipboardData(CF_TEXT);
       
   199     QString clipText;
       
   200     if (clipData == 0) {
       
   201         clipData = GetClipboardData(CF_UNICODETEXT);
       
   202         if (clipData != 0)
       
   203             clipText = QString::fromWCharArray((wchar_t *)clipData);
       
   204     } else {
       
   205         clipText = QString::fromLatin1((const char*)clipData);
       
   206     }
       
   207 
       
   208     QMimeData *mimeData = new QMimeData();
       
   209     mimeData->setText(clipText);
       
   210     QOleDataObject* data = new QOleDataObject(mimeData);
       
   211     *obj = data;
       
   212     CloseClipboard();
       
   213     return S_OK;
       
   214 }
       
   215 
       
   216 HRESULT QtCeSetClipboard(IDataObject* obj)
       
   217 {
       
   218     HWND owner = ptrClipboardData->clipBoardViewer->internalWinId();
       
   219     if (!OpenClipboard(owner))
       
   220         return !S_OK;
       
   221 
       
   222     bool result = false;
       
   223     if (obj == 0) {
       
   224         result = true;
       
   225         EmptyClipboard();
       
   226         CloseClipboard();
       
   227     } else {
       
   228         QOleDataObject* qobj = static_cast<QOleDataObject*>(obj);
       
   229 
       
   230         const QMimeData* data = qobj->mimeData();
       
   231         if (data->hasText()) {
       
   232             EmptyClipboard();
       
   233             result = SetClipboardData(CF_UNICODETEXT, wcsdup(reinterpret_cast<const wchar_t *> (data->text().utf16()))) != NULL;
       
   234             CloseClipboard();
       
   235             result = true;
       
   236         }
       
   237     }
       
   238     return result ? S_OK : !S_OK;
       
   239 }
       
   240 
       
   241 void QtCeFlushClipboard() { }
       
   242 #endif
       
   243 
       
   244 
       
   245 
       
   246 QClipboard::~QClipboard()
       
   247 {
       
   248     cleanupClipboardData();
       
   249 }
       
   250 
       
   251 void QClipboard::setMimeData(QMimeData *src, Mode mode)
       
   252 {
       
   253     if (mode != Clipboard)
       
   254         return;
       
   255     QClipboardData *d = clipboardData();
       
   256 
       
   257     if (!(d->iData && d->iData->mimeData() == src)) {
       
   258         d->releaseIData();
       
   259         d->iData = new QOleDataObject(src);
       
   260     }
       
   261 
       
   262     if (OleSetClipboard(d->iData) != S_OK) {
       
   263         d->releaseIData();
       
   264         qErrnoWarning("QClipboard::setMimeData: Failed to set data on clipboard");
       
   265         return;
       
   266     }
       
   267 #if defined(Q_OS_WINCE)
       
   268     // As WinCE does not support notifications we send the signal here
       
   269     // We will get no event when the clipboard changes outside...
       
   270     emit dataChanged();
       
   271     emit changed(Clipboard);
       
   272 #endif
       
   273 }
       
   274 
       
   275 void QClipboard::clear(Mode mode)
       
   276 {
       
   277     if (mode != Clipboard) return;
       
   278 
       
   279     QClipboardData *d = clipboardData();
       
   280 
       
   281     d->releaseIData();
       
   282 
       
   283     if (OleSetClipboard(0) != S_OK) {
       
   284         qErrnoWarning("QClipboard::clear: Failed to clear data on clipboard");
       
   285         return;
       
   286     }
       
   287 #if defined(Q_OS_WINCE)
       
   288     // As WinCE does not support notifications we send the signal here
       
   289     // We will get no event when the clipboard changes outside...
       
   290     emit dataChanged();
       
   291     emit changed(Clipboard);
       
   292 #endif
       
   293 }
       
   294 
       
   295 bool QClipboard::event(QEvent *e)
       
   296 {
       
   297     if (e->type() != QEvent::Clipboard)
       
   298         return QObject::event(e);
       
   299 
       
   300     QClipboardData *d = clipboardData();
       
   301 
       
   302     MSG *m = (MSG *)((QClipboardEvent*)e)->data();
       
   303     if (!m) {
       
   304         // this is sent to render all formats at app shut down
       
   305         if (ownsClipboard()) {
       
   306             OleFlushClipboard();
       
   307             d->releaseIData();
       
   308         }
       
   309         return true;
       
   310     }
       
   311 
       
   312     bool propagate = false;
       
   313 
       
   314     if (m->message == WM_CHANGECBCHAIN) {
       
   315         if ((HWND)m->wParam == d->nextClipboardViewer)
       
   316             d->nextClipboardViewer = (HWND)m->lParam;
       
   317         else
       
   318             propagate = true;
       
   319     } else if (m->message == WM_DRAWCLIPBOARD) {
       
   320         emitChanged(QClipboard::Clipboard);
       
   321         if (!ownsClipboard() && d->iData)
       
   322             // clean up the clipboard object if we no longer own the clipboard
       
   323             d->releaseIData();
       
   324         propagate = true;
       
   325     }
       
   326 
       
   327     if (propagate && d->nextClipboardViewer) {
       
   328         SendMessage(d->nextClipboardViewer, m->message, m->wParam, m->lParam);
       
   329     }
       
   330 
       
   331     return true;
       
   332 }
       
   333 
       
   334 void QClipboard::connectNotify(const char *signal)
       
   335 {
       
   336     if (qstrcmp(signal,SIGNAL(dataChanged())) == 0) {
       
   337         // ensure we are up and running but block signals so the dataChange signal
       
   338         // is not emitted while being connected to.
       
   339         bool blocked = blockSignals(true);
       
   340         QClipboardData *d = clipboardData();
       
   341         blockSignals(blocked);
       
   342         Q_UNUSED(d);
       
   343     }
       
   344 }
       
   345 
       
   346 const QMimeData *QClipboard::mimeData(Mode mode) const
       
   347 {
       
   348     if (mode != Clipboard)
       
   349         return 0;
       
   350 
       
   351     QClipboardData *data = clipboardData();
       
   352     // sort cut for local copy / paste
       
   353     if (ownsClipboard() && data->iData->mimeData())
       
   354         return data->iData->mimeData();
       
   355     return &data->watcher;
       
   356 }
       
   357 
       
   358 bool QClipboard::supportsMode(Mode mode) const
       
   359 {
       
   360     return (mode == Clipboard);
       
   361 }
       
   362 
       
   363 bool QClipboard::ownsMode(Mode mode) const
       
   364 {
       
   365     if (mode == Clipboard) {
       
   366         QClipboardData *d = clipboardData();
       
   367 #if !defined(Q_OS_WINCE)
       
   368         return d->iData && OleIsCurrentClipboard(d->iData) == S_OK;
       
   369 #else
       
   370         return d->iData && GetClipboardOwner() == d->clipBoardViewer->internalWinId();
       
   371 #endif
       
   372     } else {
       
   373         return false;
       
   374     }
       
   375 }
       
   376 
       
   377 void QClipboard::ownerDestroyed()
       
   378 {
       
   379 }
       
   380 
       
   381 QT_END_NAMESPACE
       
   382 
       
   383 #endif // QT_NO_CLIPBOARD