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 , mCocoaClipboard(new QCocoaClipboard)
97 , mCocoaDrag(new QCocoaDrag)
99 QCocoaAutoReleasePool pool;
101 qApp->setAttribute(Qt::AA_DontUseNativeMenuBar, false);
103 NSApplication *cocoaApplication = [NSApplication sharedApplication];
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();
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];
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
123 if (!QCoreApplication::testAttribute(Qt::AA_MacPluginApplication)) {
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];
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];
137 NSArray *screens = [NSScreen screens];
138 for (uint i = 0; i < [screens count]; i++) {
139 QCocoaScreen *screen = new QCocoaScreen(i);
140 mScreens.append(screen);
144 QMacPasteboardMime::initializeMimeTypes();
147 QCocoaIntegration::~QCocoaIntegration()
149 [[NSApplication sharedApplication] setDelegate: 0];
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();
157 // Delete screens in reverse order to avoid crash in case of multiple screens
158 while (!mScreens.isEmpty()) {
159 delete mScreens.takeLast();
163 bool QCocoaIntegration::hasCapability(QPlatformIntegration::Capability cap) const
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);
176 QPlatformWindow *QCocoaIntegration::createPlatformWindow(QWindow *window) const
178 return new QCocoaWindow(window);
181 QPlatformOpenGLContext *QCocoaIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
183 return new QCocoaGLContext(context->format(), context->shareHandle());
186 QPlatformBackingStore *QCocoaIntegration::createPlatformBackingStore(QWindow *window) const
188 return new QCocoaBackingStore(window);
191 QAbstractEventDispatcher *QCocoaIntegration::guiThreadEventDispatcher() const
193 return mEventDispatcher;
196 QPlatformFontDatabase *QCocoaIntegration::fontDatabase() const
198 return mFontDb.data();
201 QPlatformNativeInterface *QCocoaIntegration::nativeInterface() const
203 return new QCocoaNativeInterface();
206 QPlatformInputContext *QCocoaIntegration::inputContext() const
208 return mInputContext.data();
211 QPlatformAccessibility *QCocoaIntegration::accessibility() const
213 return mAccessibility.data();
216 QPlatformClipboard *QCocoaIntegration::clipboard() const
218 return mCocoaClipboard;
221 QPlatformDrag *QCocoaIntegration::drag() const
223 return mCocoaDrag.data();
226 QStringList QCocoaIntegration::themeNames() const
228 return QStringList(QLatin1String(QCocoaTheme::name));
231 QPlatformTheme *QCocoaIntegration::createPlatformTheme(const QString &name) const
233 if (name == QLatin1String(QCocoaTheme::name))
234 return new QCocoaTheme;
235 return QPlatformIntegration::createPlatformTheme(name);