source: trunk/src/gui/kernel/qclipboard_mac.cpp@ 451

Last change on this file since 451 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 19.0 KB
Line 
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#include "qapplication.h"
44#include "qbitmap.h"
45#include "qdatetime.h"
46#include "qdebug.h"
47#include "qapplication_p.h"
48#include <private/qt_mac_p.h>
49#include "qevent.h"
50#include "qurl.h"
51#include <stdlib.h>
52#include <string.h>
53
54QT_BEGIN_NAMESPACE
55
56QT_USE_NAMESPACE
57
58/*****************************************************************************
59 QClipboard debug facilities
60 *****************************************************************************/
61//#define DEBUG_PASTEBOARD
62
63#ifndef QT_NO_CLIPBOARD
64
65/*****************************************************************************
66 QClipboard member functions for mac.
67 *****************************************************************************/
68
69static QMacPasteboard *qt_mac_pasteboards[2] = {0, 0};
70
71static inline QMacPasteboard *qt_mac_pasteboard(QClipboard::Mode mode)
72{
73 Q_ASSERT(mode == QClipboard::Clipboard || mode == QClipboard::FindBuffer);
74 if (mode == QClipboard::Clipboard)
75 return qt_mac_pasteboards[0];
76 else
77 return qt_mac_pasteboards[1];
78}
79
80static void qt_mac_cleanupPasteboard() {
81 delete qt_mac_pasteboards[0];
82 delete qt_mac_pasteboards[1];
83 qt_mac_pasteboards[0] = 0;
84 qt_mac_pasteboards[1] = 0;
85}
86
87static bool qt_mac_updateScrap(QClipboard::Mode mode)
88{
89 if(!qt_mac_pasteboards[0]) {
90 qt_mac_pasteboards[0] = new QMacPasteboard(kPasteboardClipboard, QMacPasteboardMime::MIME_CLIP);
91 qt_mac_pasteboards[1] = new QMacPasteboard(kPasteboardFind, QMacPasteboardMime::MIME_CLIP);
92 qAddPostRoutine(qt_mac_cleanupPasteboard);
93 return true;
94 }
95 return qt_mac_pasteboard(mode)->sync();
96}
97
98void QClipboard::clear(Mode mode)
99{
100 if (!supportsMode(mode))
101 return;
102 qt_mac_updateScrap(mode);
103 qt_mac_pasteboard(mode)->clear();
104 setMimeData(0, mode);
105}
106
107void QClipboard::ownerDestroyed()
108{
109}
110
111
112void QClipboard::connectNotify(const char *signal)
113{
114 Q_UNUSED(signal);
115}
116
117bool QClipboard::event(QEvent *e)
118{
119 if(e->type() != QEvent::Clipboard)
120 return QObject::event(e);
121
122 if (qt_mac_updateScrap(QClipboard::Clipboard)) {
123 emitChanged(QClipboard::Clipboard);
124 }
125
126 if (qt_mac_updateScrap(QClipboard::FindBuffer)) {
127 emitChanged(QClipboard::FindBuffer);
128 }
129
130 return QObject::event(e);
131}
132
133const QMimeData *QClipboard::mimeData(Mode mode) const
134{
135 if (!supportsMode(mode))
136 return 0;
137 qt_mac_updateScrap(mode);
138 return qt_mac_pasteboard(mode)->mimeData();
139}
140
141void QClipboard::setMimeData(QMimeData *src, Mode mode)
142{
143 if (!supportsMode(mode))
144 return;
145 qt_mac_updateScrap(mode);
146 qt_mac_pasteboard(mode)->setMimeData(src);
147 emitChanged(mode);
148}
149
150bool QClipboard::supportsMode(Mode mode) const
151{
152 return (mode == Clipboard || mode == FindBuffer);
153}
154
155bool QClipboard::ownsMode(Mode mode) const
156{
157 Q_UNUSED(mode);
158 return false;
159}
160
161#endif // QT_NO_CLIPBOARD
162
163/*****************************************************************************
164 QMacPasteboard code
165*****************************************************************************/
166
167QMacPasteboard::QMacPasteboard(PasteboardRef p, uchar mt)
168{
169 mac_mime_source = false;
170 mime_type = mt ? mt : uchar(QMacPasteboardMime::MIME_ALL);
171 paste = p;
172 CFRetain(paste);
173}
174
175QMacPasteboard::QMacPasteboard(uchar mt)
176{
177 mac_mime_source = false;
178 mime_type = mt ? mt : uchar(QMacPasteboardMime::MIME_ALL);
179 paste = 0;
180 OSStatus err = PasteboardCreate(0, &paste);
181 if(err == noErr) {
182 PasteboardSetPromiseKeeper(paste, promiseKeeper, this);
183 } else {
184 qDebug("PasteBoard: Error creating pasteboard: [%d]", (int)err);
185 }
186}
187
188QMacPasteboard::QMacPasteboard(CFStringRef name, uchar mt)
189{
190 mac_mime_source = false;
191 mime_type = mt ? mt : uchar(QMacPasteboardMime::MIME_ALL);
192 paste = 0;
193 OSStatus err = PasteboardCreate(name, &paste);
194 if(err == noErr) {
195 PasteboardSetPromiseKeeper(paste, promiseKeeper, this);