96027e0925504690a9742c9e3b98e9489150ab7c
[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     , mCocoaDrag(new QCocoaDrag)
97 {
98     QCocoaAutoReleasePool pool;
99
100     qApp->setAttribute(Qt::AA_DontUseNativeMenuBar, false);
101
102     NSApplication *cocoaApplication = [NSApplication sharedApplication];
103
104     if (qgetenv("QT_MAC_DISABLE_FOREGROUND_APPLICATION_TRANSFORM").isEmpty()) {
105         // Applications launched from plain executables (without an app
106         // bundle) are "background" applications that does not take keybaord
107         // focus or have a dock icon or task switcher entry. Qt Gui apps generally
108         // wants to be foreground applications so change the process type. (But
109         // see the function implementation for exceptions.)
110         qt_mac_transformProccessToForegroundApplication();
111
112         // Move the application window to front to avoid launching behind the terminal.
113         // Ignoring other apps is neccessary (we must ignore the terminal), but makes
114         // Qt apps play slightly less nice with other apps when lanching from Finder
115         // (See the activateIgnoringOtherApps docs.)
116         [cocoaApplication activateIgnoringOtherApps : YES];
117     }
118
119     // ### For AA_MacPluginApplication we don't want to load the menu nib.
120     // Qt 4 also does not set the application delegate, so that behavior
121     // is matched here.
122     if (!QCoreApplication::testAttribute(Qt::AA_MacPluginApplication)) {
123
124         // Set app delegate, link to the current delegate (if any)
125         QT_MANGLE_NAMESPACE(QCocoaApplicationDelegate) *newDelegate = [QT_MANGLE_NAMESPACE(QCocoaApplicationDelegate) sharedDelegate];
126         [newDelegate setReflectionDelegate:[cocoaApplication delegate]];
127         [cocoaApplication setDelegate:newDelegate];
128
129         // Load the application menu. This menu contains Preferences, Hide, Quit.
130         QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *qtMenuLoader = [[QT_MANGLE_NAMESPACE(QCocoaMenuLoader) alloc] init];
131         qt_mac_loadMenuNib(qtMenuLoader);
132         [cocoaApplication setMenu:[qtMenuLoader menu]];
133         [newDelegate setMenuLoader:qtMenuLoader];
134     }
135
136     NSArray *screens = [NSScreen screens];
137     for (uint i = 0; i < [screens count]; i++) {
138         QCocoaScreen *screen = new QCocoaScreen(i);
139         mScreens.append(screen);
140         screenAdded(screen);
141     }
142
143     QMacPasteboardMime::initialize();
144 }
145
146 QCocoaIntegration::~QCocoaIntegration()
147 {
148     [[NSApplication sharedApplication] setDelegate: 0];
149
150     // Delete screens in reverse order to avoid crash in case of multiple screens
151     while (!mScreens.isEmpty()) {
152         delete mScreens.takeLast();
153     }
154 }
155
156 bool QCocoaIntegration::hasCapability(QPlatformIntegration::Capability cap) const
157 {
158     switch (cap) {
159     case ThreadedPixmaps: return true;
160     case OpenGL : return true;
161     case ThreadedOpenGL : return true;
162     case BufferQueueingOpenGL: return true;
163     default: return QPlatformIntegration::hasCapability(cap);
164     }
165 }
166
167
168
169 QPlatformWindow *QCocoaIntegration::createPlatformWindow(QWindow *window) const
170 {
171     return new QCocoaWindow(window);
172 }
173
174 QPlatformOpenGLContext *QCocoaIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
175 {
176     return new QCocoaGLContext(context->format(), context->shareHandle());
177 }
178
179 QPlatformBackingStore *QCocoaIntegration::createPlatformBackingStore(QWindow *window) const
180 {
181     return new QCocoaBackingStore(window);
182 }
183
184 QAbstractEventDispatcher *QCocoaIntegration::guiThreadEventDispatcher() const
185 {
186     return mEventDispatcher;
187 }
188
189 QPlatformFontDatabase *QCocoaIntegration::fontDatabase() const
190 {
191     return mFontDb.data();
192 }
193
194 QPlatformNativeInterface *QCocoaIntegration::nativeInterface() const
195 {
196     return new QCocoaNativeInterface();
197 }
198
199 QPlatformInputContext *QCocoaIntegration::inputContext() const
200 {
201     return mInputContext.data();
202 }
203
204 QPlatformAccessibility *QCocoaIntegration::accessibility() const
205 {
206     return mAccessibility.data();
207 }
208
209 QPlatformDrag *QCocoaIntegration::drag() const
210 {
211     return mCocoaDrag.data();
212 }
213
214 QStringList QCocoaIntegration::themeNames() const
215 {
216     return QStringList(QLatin1String(QCocoaTheme::name));
217 }
218
219 QPlatformTheme *QCocoaIntegration::createPlatformTheme(const QString &name) const
220 {
221     if (name == QLatin1String(QCocoaTheme::name))
222         return new QCocoaTheme;
223     return QPlatformIntegration::createPlatformTheme(name);
224 }
225
226 QT_END_NAMESPACE