1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the plugins of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qcocoaintegration.h"
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"
58 #include <QtGui/qplatformaccessibility_qpa.h>
59 #include <QtCore/qcoreapplication.h>
61 #include <QtWidgets/QDialog>
62 #include <QtWidgets/QFileDialog>
64 #include <QtPlatformSupport/private/qcoretextfontdatabase_p.h>
68 QCocoaScreen::QCocoaScreen(int screenIndex)
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);
75 m_format = QImage::Format_RGB32;
77 m_depth = NSBitsPerPixelFromDepth([m_screen depth]);
80 const qreal inch = 25.4;
81 m_physicalSize = QSizeF(m_geometry.size()) * inch / dpi;
83 m_cursor = new QCocoaCursor;
86 QCocoaScreen::~QCocoaScreen()
91 QCocoaIntegration::QCocoaIntegration()
92 : mFontDb(new QCoreTextFontDatabase())
93 , mEventDispatcher(new QCocoaEventDispatcher())
94 , mInputContext(new QCocoaInputContext)
95 , mAccessibility(new QPlatformAccessibility)
96 , mCocoaDrag(new QCocoaDrag)
98 QCocoaAutoReleasePool pool;
100 qApp->setAttribute(Qt::AA_DontUseNativeMenuBar, false);
102 NSApplication *cocoaApplication = [NSApplication sharedApplication];
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();
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];
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
122 if (!QCoreApplication::testAttribute(Qt::AA_MacPluginApplication)) {
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];
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];
136 NSArray *screens = [NSScreen screens];
137 for (uint i = 0; i < [screens count]; i++) {
138 QCocoaScreen *screen = new QCocoaScreen(i);
139 mScreens.append(screen);
143 QMacPasteboardMime::initialize();
146 QCocoaIntegration::~QCocoaIntegration()
148 [[NSApplication sharedApplication] setDelegate: 0];
150 // Delete screens in reverse order to avoid crash in case of multiple screens
151 while (!mScreens.isEmpty()) {
152 delete mScreens.takeLast();
156 bool QCocoaIntegration::hasCapability(QPlatformIntegration::Capability cap) const
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);
169 QPlatformWindow *QCocoaIntegration::createPlatformWindow(QWindow *window) const
171 return new QCocoaWindow(window);
174 QPlatformOpenGLContext *QCocoaIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
176 return new QCocoaGLContext(context->format(), context->shareHandle());
179 QPlatformBackingStore *QCocoaIntegration::createPlatformBackingStore(QWindow *window) const
181 return new QCocoaBackingStore(window);
184 QAbstractEventDispatcher *QCocoaIntegration::guiThreadEventDispatcher() const
186 return mEventDispatcher;
189 QPlatformFontDatabase *QCocoaIntegration::fontDatabase() const
191 return mFontDb.data();
194 QPlatformNativeInterface *QCocoaIntegration::nativeInterface() const
196 return new QCocoaNativeInterface();
199 QPlatformInputContext *QCocoaIntegration::inputContext() const
201 return mInputContext.data();
204 QPlatformAccessibility *QCocoaIntegration::accessibility() const
206 return mAccessibility.data();
209 QPlatformDrag *QCocoaIntegration::drag() const
211 return mCocoaDrag.data();
214 QStringList QCocoaIntegration::themeNames() const
216 return QStringList(QLatin1String(QCocoaTheme::name));
219 QPlatformTheme *QCocoaIntegration::createPlatformTheme(const QString &name) const
221 if (name == QLatin1String(QCocoaTheme::name))
222 return new QCocoaTheme;
223 return QPlatformIntegration::createPlatformTheme(name);