[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
---|
| 5 | **
|
---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
| 7 | **
|
---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 9 | ** Commercial Usage
|
---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 13 | ** a written agreement between you and Nokia.
|
---|
| 14 | **
|
---|
| 15 | ** GNU Lesser General Public License Usage
|
---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 19 | ** packaging of this file. Please review the following information to
|
---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 22 | **
|
---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
| 26 | ** package.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
---|
| 37 | ** contact the sales department at [email protected].
|
---|
| 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::fromUtf16((unsigned short *)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 | {
|
---|
[325] | 253 | if (mode != Clipboard) {
|
---|
| 254 | delete src;
|
---|
[2] | 255 | return;
|
---|
[325] | 256 | }
|
---|
| 257 |
|
---|
[2] | 258 | QClipboardData *d = clipboardData();
|
---|
| 259 |
|
---|
| 260 | if (!(d->iData && d->iData->mimeData() == src)) {
|
---|
| 261 | d->releaseIData();
|
---|
| 262 | d->iData = new QOleDataObject(src);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | if (OleSetClipboard(d->iData) != S_OK) {
|
---|
| 266 | d->releaseIData();
|
---|
| 267 | qErrnoWarning("QClipboard::setMimeData: Failed to set data on clipboard");
|
---|
| 268 | return;
|
---|
| 269 | }
|
---|
| 270 | #if defined(Q_OS_WINCE)
|
---|
| 271 | // As WinCE does not support notifications we send the signal here
|
---|
| 272 | // We will get no event when the clipboard changes outside...
|
---|
| 273 | emit dataChanged();
|
---|
| 274 | emit changed(Clipboard);
|
---|
| 275 | #endif
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | void QClipboard::clear(Mode mode)
|
---|
| 279 | {
|
---|
| 280 | if (mode != Clipboard) return;
|
---|
| 281 |
|
---|
| 282 | QClipboardData *d = clipboardData();
|
---|
| 283 |
|
---|
| 284 | d->releaseIData();
|
---|
| 285 |
|
---|
| 286 | if (OleSetClipboard(0) != S_OK) {
|
---|
| 287 | qErrnoWarning("QClipboard::clear: Failed to clear data on clipboard");
|
---|
| 288 | return;
|
---|
| 289 | }
|
---|
| 290 | #if defined(Q_OS_WINCE)
|
---|
| 291 | // As WinCE does not support notifications we send the signal here
|
---|
| 292 | // We will get no event when the clipboard changes outside...
|
---|
| 293 | emit dataChanged();
|
---|
| 294 | emit changed(Clipboard);
|
---|
| 295 | #endif
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | bool QClipboard::event(QEvent *e)
|
---|
| 299 | {
|
---|
| 300 | if (e->type() != QEvent::Clipboard)
|
---|
| 301 | return QObject::event(e);
|
---|
| 302 |
|
---|
| 303 | QClipboardData *d = clipboardData();
|
---|
| 304 |
|
---|
| 305 | MSG *m = (MSG *)((QClipboardEvent*)e)->data();
|
---|
| 306 | if (!m) {
|
---|
| 307 | // this is sent to render all formats at app shut down
|
---|
| 308 | if (ownsClipboard()) {
|
---|
| 309 | OleFlushClipboard();
|
---|
| 310 | d->releaseIData();
|
---|
| 311 | }
|
---|
| 312 | return true;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | bool propagate = false;
|
---|
| 316 |
|
---|
| 317 | if (m->message == WM_CHANGECBCHAIN) {
|
---|
| 318 | if ((HWND)m->wParam == d->nextClipboardViewer)
|
---|
| 319 | d->nextClipboardViewer = (HWND)m->lParam;
|
---|
| 320 | else
|
---|
| 321 | propagate = true;
|
---|
| 322 | } else if (m->message == WM_DRAWCLIPBOARD) {
|
---|
| 323 | emitChanged(QClipboard::Clipboard);
|
---|
| 324 | if (!ownsClipboard() && d->iData)
|
---|
| 325 | // clean up the clipboard object if we no longer own the clipboard
|
---|
| 326 | d->releaseIData();
|
---|
| 327 | propagate = true;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | if (propagate && d->nextClipboardViewer) {
|
---|
| 331 | QT_WA({
|
---|
| 332 | SendMessage(d->nextClipboardViewer, m->message,
|
---|
| 333 | m->wParam, m->lParam);
|
---|
| 334 | } , {
|
---|
| 335 | SendMessageA(d->nextClipboardViewer, m->message,
|
---|
| 336 | m->wParam, m->lParam);
|
---|
| 337 | });
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | return true;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | void QClipboard::connectNotify(const char *signal)
|
---|
| 344 | {
|
---|
| 345 | if (qstrcmp(signal,SIGNAL(dataChanged())) == 0) {
|
---|
| 346 | // ensure we are up and running but block signals so the dataChange signal
|
---|
| 347 | // is not emitted while being connected to.
|
---|
| 348 | bool blocked = blockSignals(true);
|
---|
| 349 | QClipboardData *d = clipboardData();
|
---|
| 350 | blockSignals(blocked);
|
---|
| 351 | Q_UNUSED(d);
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | const QMimeData *QClipboard::mimeData(Mode mode) const
|
---|
| 356 | {
|
---|
| 357 | if (mode != Clipboard)
|
---|
| 358 | return 0;
|
---|
| 359 |
|
---|
| 360 | QClipboardData *data = clipboardData();
|
---|
| 361 | // sort cut for local copy / paste
|
---|
| 362 | if (ownsClipboard() && data->iData->mimeData())
|
---|
| 363 | return data->iData->mimeData();
|
---|
| 364 | return &data->watcher;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | bool QClipboard::supportsMode(Mode mode) const
|
---|
| 368 | {
|
---|
| 369 | return (mode == Clipboard);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | bool QClipboard::ownsMode(Mode mode) const
|
---|
| 373 | {
|
---|
| 374 | if (mode == Clipboard) {
|
---|
| 375 | QClipboardData *d = clipboardData();
|
---|
| 376 | #if !defined(Q_OS_WINCE)
|
---|
| 377 | return d->iData && OleIsCurrentClipboard(d->iData) == S_OK;
|
---|
| 378 | #else
|
---|
| 379 | return d->iData && GetClipboardOwner() == d->clipBoardViewer->internalWinId();
|
---|
| 380 | #endif
|
---|
| 381 | } else {
|
---|
| 382 | return false;
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | void QClipboard::ownerDestroyed()
|
---|
| 387 | {
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | QT_END_NAMESPACE
|
---|
| 391 |
|
---|
| 392 | #endif // QT_NO_CLIPBOARD
|
---|