Implement cocoa clipboard support.
[profile/ivi/qtbase.git] / src / plugins / platforms / cocoa / qcocoaintegration.mm
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qcocoaintegration.h"
43
44 #include "qcocoawindow.h"
45 #include "qcocoabackingstore.h"
46 #include "qcocoanativeinterface.h"
47 #include "qcocoamenuloader.h"
48 #include "qcocoaeventdispatcher.h"
49 #include "qcocoahelpers.h"
50 #include "qcocoaapplication.h"
51 #include "qcocoaapplicationdelegate.h"
52 #include "qmenu_mac.h"
53 #include "qcocoafiledialoghelper.h"
54 #include "qcocoatheme.h"
55 #include "qcocoainputcontext.h"
56 #include "qmacmime.h"
57
58 #include <QtGui/qplatformaccessibility_qpa.h>
59 #include <QtCore/qcoreapplication.h>
60
61 #include <QtWidgets/QDialog>
62 #include <QtWidgets/QFileDialog>
63
64 #include <QtPlatformSupport/private/qcoretextfontdatabase_p.h>
65
66 QT_BEGIN_NAMESPACE
67
68 QCocoaScreen::QCocoaScreen(int screenIndex)
69     :QPlatformScreen()
70 {
71     m_screen = [[NSScreen screens] objectAtIndex:screenIndex];
72     NSRect rect = [m_screen frame];
73     m_geometry = QRect(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
74
75     m_format = QImage::Format_RGB32;
76
77     m_depth = NSBitsPerPixelFromDepth([m_screen depth]);
78
79     const int dpi = 72;
80     const qreal inch = 25.4;
81     m_physicalSize = QSizeF(m_geometry.size()) * inch / dpi;
82
83     m_cursor = new QCocoaCursor;
84 };
85
86 QCocoaScreen::~QCocoaScreen()
87 {
88     delete m_cursor;
89 }
90
91 QCocoaIntegration::QCocoaIntegration()
92     : mFontDb(new QCoreTextFontDatabase())
93     , mEventDispatcher(new QCocoaEventDispatcher())
94     , mInputContext(new QCocoaInputContext)
95     , mAccessibility(new QPlatformAccessibility)
96     , mCocoaClipboard(new QCocoaClipboard)
97     , mCocoaDrag(new QCocoaDrag)
98 {
99     QCocoaAutoReleasePool pool;
100
101     qApp->setAttribute(Qt::AA_DontUseNativeMenuBar, false);
102
103     NSApplication *cocoaApplication = [NSApplication sharedApplication];
104
105     if (qgetenv("QT_MAC_DISABLE_FOREGROUND_APPLICATION_TRANSFORM").isEmpty()) {
106         // Applications launched from plain executables (without an app
107         // bundle) are "background" applications that does not take keybaord
108         // focus or have a dock icon or task switcher entry. Qt Gui apps generally
109         // wants to be foreground applications so change the process type. (But
110         // see the function implementation for exceptions.)
111         qt_mac_transformProccessToForegroundApplication();
112
113         // Move the application window to front to avoid launching behind the terminal.
114         // Ignoring other apps is neccessary (we must ignore the terminal), but makes
115         // Qt apps play slightly less nice with other apps when lanching from Finder
116         // (See the activateIgnoringOtherApps docs.)
117         [cocoaApplication activateIgnoringOtherApps : YES];
118     }
119
120     // ### For AA_MacPluginApplication we don't want to load the menu nib.
121     // Qt 4 also does not set the application delegate, so that behavior
122     // is matched here.
123     if (!QCoreApplication::testAttribute(Qt::AA_MacPluginApplication)) {
124
125         // Set app delegate, link to the current delegate (if any)
126         QT_MANGLE_NAMESPACE(QCocoaApplicationDelegate) *newDelegate = [QT_MANGLE_NAMESPACE(QCocoaApplicationDelegate) sharedDelegate];
127         [newDelegate setReflectionDelegate:[cocoaApplication delegate]];
128         [cocoaApplication setDelegate:newDelegate];
129
130         // Load the application menu. This menu contains Preferences, Hide, Quit.
131         QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *qtMenuLoader = [[QT_MANGLE_NAMESPACE(QCocoaMenuLoader) alloc] init];
132         qt_mac_loadMenuNib(qtMenuLoader);
133         [cocoaApplication setMenu:[qtMenuLoader menu]];
134         [newDelegate setMenuLoader:qtMenuLoader];
135     }
136
137     NSArray *screens = [NSScreen screens];
138     for (uint i = 0; i < [screens count]; i++) {
139         QCocoaScreen *screen = new QCocoaScreen(i);
140         mScreens.append(screen);
141         screenAdded(screen);
142     }
143
144     QMacPasteboardMime::initializeMimeTypes();
145 }
146
147 QCocoaIntegration::~QCocoaIntegration()
148 {
149     [[NSApplication sharedApplication] setDelegate: 0];
150
151     // Delete the clipboard integration and destroy mime type converters.
152     // Deleting the clipboard integration flushes promised pastes using
153     // the mime converters - the ordering here is important.
154     delete mCocoaClipboard;
155     QMacPasteboardMime::destroyMimeTypes();
156
157     // Delete screens in reverse order to avoid crash in case of multiple screens
158     while (!mScreens.isEmpty()) {
159         delete mScreens.takeLast();
160     }
161 }
162
163 bool QCocoaIntegration::hasCapability(QPlatformIntegration::Capability cap) const
164 {
165     switch (cap) {
166     case ThreadedPixmaps: return true;
167     case OpenGL : return true;
168     case ThreadedOpenGL : return true;
169     case BufferQueueingOpenGL: return true;
170     default: return QPlatformIntegration::hasCapability(cap);
171     }
172 }
173
174
175
176 QPlatformWindow *QCocoaIntegration::createPlatformWindow(QWindow *window) const
177 {
178     return new QCocoaWindow(window);
179 }
180
181 QPlatformOpenGLContext *QCocoaIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
182 {
183     return new QCocoaGLContext(context->format(), context->shareHandle());
184 }
185
186 QPlatformBackingStore *QCocoaIntegration::createPlatformBackingStore(QWindow *window) const
187 {
188     return new QCocoaBackingStore(window);
189 }
190
191 QAbstractEventDispatcher *QCocoaIntegration::guiThreadEventDispatcher() const
192 {
193     return mEventDispatcher;
194 }
195
196 QPlatformFontDatabase *QCocoaIntegration::fontDatabase() const
197 {
198     return mFontDb.data();
199 }
200
201 QPlatformNativeInterface *QCocoaIntegration::nativeInterface() const
202 {
203     return new QCocoaNativeInterface();
204 }
205
206 QPlatformInputContext *QCocoaIntegration::inputContext() const
207 {
208     return mInputContext.data();
209 }
210
211 QPlatformAccessibility *QCocoaIntegration::accessibility() const
212 {
213     return mAccessibility.data();
214 }
215
216 QPlatformClipboard *QCocoaIntegration::clipboard() const
217 {
218     return mCocoaClipboard;
219 }
220
221 QPlatformDrag *QCocoaIntegration::drag() const
222 {
223     return mCocoaDrag.data();
224 }
225
226 QStringList QCocoaIntegration::themeNames() const
227 {
228     return QStringList(QLatin1String(QCocoaTheme::name));
229 }
230
231 QPlatformTheme *QCocoaIntegration::createPlatformTheme(const QString &name) const
232 {
233     if (name == QLatin1String(QCocoaTheme::name))
234         return new QCocoaTheme;
235     return QPlatformIntegration::createPlatformTheme(name);
236 }
237
238 QT_END_NAMESPACE