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 | {
|
---|
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 | QT_WA({
|
---|
329 | SendMessage(d->nextClipboardViewer, m->message,
|
---|
330 | m->wParam, m->lParam);
|
---|
331 | } , {
|
---|
332 | SendMessageA(d->nextClipboardViewer, m->message,
|
---|
333 | m->wParam, m->lParam);
|
---|
334 | });
|
---|
335 | }
|
---|
336 |
|
---|
337 | return true;
|
---|
338 | }
|
---|
339 |
|
---|
340 | void QClipboard::connectNotify(const char *signal)
|
---|
341 | {
|
---|
342 | if (qstrcmp(signal,SIGNAL(dataChanged())) == 0) {
|
---|
343 | // ensure we are up and running but block signals so the dataChange signal
|
---|
344 | // is not emitted while being connected to.
|
---|
345 | bool blocked = blockSignals(true);
|
---|
346 | QClipboardData *d = clipboardData();
|
---|
347 | blockSignals(blocked);
|
---|
348 | Q_UNUSED(d);
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | const QMimeData *QClipboard::mimeData(Mode mode) const
|
---|
353 | {
|
---|
354 | if (mode != Clipboard)
|
---|
355 | return 0;
|
---|
356 |
|
---|
357 | QClipboardData *data = clipboardData();
|
---|
358 | // sort cut for local copy / paste
|
---|
359 | if (ownsClipboard() && data->iData->mimeData())
|
---|
360 | return data->iData->mimeData();
|
---|
361 | return &data->watcher;
|
---|
362 | }
|
---|
363 |
|
---|
364 | bool QClipboard::supportsMode(Mode mode) const
|
---|
365 | {
|
---|
366 | return (mode == Clipboard);
|
---|
367 | }
|
---|
368 |
|
---|
369 | bool QClipboard::ownsMode(Mode mode) const
|
---|
370 | {
|
---|
371 | if (mode == Clipboard) {
|
---|
372 | QClipboardData *d = clipboardData();
|
---|
373 | #if !defined(Q_OS_WINCE)
|
---|
374 | return d->iData && OleIsCurrentClipboard(d->iData) == S_OK;
|
---|
375 | #else
|
---|
376 | return d->iData && GetClipboardOwner() == d->clipBoardViewer->internalWinId();
|
---|
377 | #endif
|
---|
378 | } else {
|
---|
379 | return false;
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | void QClipboard::ownerDestroyed()
|
---|
384 | {
|
---|
385 | }
|
---|
386 |
|
---|
387 | QT_END_NAMESPACE
|
---|
388 |
|
---|
389 | #endif // QT_NO_CLIPBOARD
|
---|