diff options
Diffstat (limited to 'src/plugins/platforms/cocoa')
21 files changed, 783 insertions, 155 deletions
diff --git a/src/plugins/platforms/cocoa/messages.cpp b/src/plugins/platforms/cocoa/messages.cpp index 3db1618a50..1fe80b28b1 100644 --- a/src/plugins/platforms/cocoa/messages.cpp +++ b/src/plugins/platforms/cocoa/messages.cpp @@ -93,4 +93,9 @@ QPlatformMenuItem::MenuRole detectMenuRole(const QString &caption) return QPlatformMenuItem::NoRole; } +QString msgDialogButtonDiscard() +{ + return QCoreApplication::translate("QCocoaTheme", "Don't Save"); +} + QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/messages.h b/src/plugins/platforms/cocoa/messages.h index 09705c1e21..97f3ea7009 100644 --- a/src/plugins/platforms/cocoa/messages.h +++ b/src/plugins/platforms/cocoa/messages.h @@ -53,6 +53,8 @@ QString qt_mac_applicationmenu_string(int type); QPlatformMenuItem::MenuRole detectMenuRole(const QString &caption); +QString msgDialogButtonDiscard(); + QT_END_NAMESPACE #endif // MESSAGES_H diff --git a/src/plugins/platforms/cocoa/qcocoaapplication.h b/src/plugins/platforms/cocoa/qcocoaapplication.h index ffb12ea846..bb218bcabe 100644 --- a/src/plugins/platforms/cocoa/qcocoaapplication.h +++ b/src/plugins/platforms/cocoa/qcocoaapplication.h @@ -86,7 +86,7 @@ // /* - Cocoa Application Categories + Cocoa Application Categories */ #include "qglobal.h" #include "private/qcore_mac_p.h" diff --git a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm index f9767ce716..327ca00ad6 100644 --- a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm +++ b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm @@ -114,7 +114,7 @@ static void cleanupCocoaApplicationDelegate() - (void)updateScreens:(NSNotification *)notification { Q_UNUSED(notification); - if (QCocoaIntegration *ci = dynamic_cast<QCocoaIntegration *>(QGuiApplicationPrivate::platformIntegration())) + if (QCocoaIntegration *ci = QCocoaIntegration::instance()) ci->updateScreens(); } diff --git a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm index ee69cd7d86..29eed73535 100644 --- a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm +++ b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm @@ -75,7 +75,9 @@ #include "qcocoaeventdispatcher.h" #include "qcocoaautoreleasepool.h" +#include "qcocoawindow.h" +#include "qcocoahelpers.h" #include "qguiapplication.h" #include "qevent.h" #include "qhash.h" @@ -95,11 +97,6 @@ QT_BEGIN_NAMESPACE QT_USE_NAMESPACE -enum { - QtCocoaEventSubTypeWakeup = SHRT_MAX, - QtCocoaEventSubTypePostMessage = SHRT_MAX-1 -}; - static inline CFRunLoopRef mainRunLoop() { return CFRunLoopGetMain(); @@ -625,7 +622,7 @@ NSModalSession QCocoaEventDispatcherPrivate::currentModalSession() if (!info.session) { QCocoaAutoReleasePool pool; - NSWindow *nswindow = static_cast<NSWindow *>(QGuiApplication::platformNativeInterface()->nativeResourceForWindow("nswindow", info.window)); + NSWindow *nswindow = static_cast<QCocoaWindow *>(info.window->handle())->nativeWindow(); if (!nswindow) continue; diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.mm b/src/plugins/platforms/cocoa/qcocoaglcontext.mm index 8f74a71b1e..6f76892d93 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.mm +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.mm @@ -42,6 +42,7 @@ #include "qcocoaglcontext.h" #include "qcocoawindow.h" #include "qcocoaautoreleasepool.h" +#include "qcocoahelpers.h" #include <qdebug.h> #include <QtCore/private/qcore_mac_p.h> #include <QtPlatformSupport/private/cglconvenience_p.h> @@ -143,7 +144,7 @@ QCocoaGLContext::QCocoaGLContext(const QSurfaceFormat &format, QPlatformOpenGLCo [pixelFormat release]; - const GLint interval = 1; + const GLint interval = format.swapInterval() >= 0 ? format.swapInterval() : 1; [m_context setValues:&interval forParameter:NSOpenGLCPSwapInterval]; if (format.alphaBufferSize() > 0) { @@ -151,6 +152,11 @@ QCocoaGLContext::QCocoaGLContext(const QSurfaceFormat &format, QPlatformOpenGLCo [m_context setValues:&zeroOpacity forParameter:NSOpenGLCPSurfaceOpacity]; } + + // OpenGL surfaces can be ordered either above(default) or below the NSWindow. + const GLint order = qt_mac_resolveOption(1, "QT_MAC_OPENGL_SURFACE_ORDER"); + [m_context setValues:&order forParameter:NSOpenGLCPSurfaceOrder]; + updateSurfaceFormat(); } diff --git a/src/plugins/platforms/cocoa/qcocoahelpers.h b/src/plugins/platforms/cocoa/qcocoahelpers.h index 3e402673f3..419bf631aa 100644 --- a/src/plugins/platforms/cocoa/qcocoahelpers.h +++ b/src/plugins/platforms/cocoa/qcocoahelpers.h @@ -116,6 +116,7 @@ inline NSPoint qt_mac_flipPoint(const QPoint &p) inline NSPoint qt_mac_flipPoint(const QPointF &p) { return NSMakePoint(p.x(), qt_mac_flipYCoordinate(p.y())); } +NSRect qt_mac_flipRect(const QRect &rect); NSRect qt_mac_flipRect(const QRect &rect, QWindow *window); Qt::MouseButton cocoaButton2QtButton(NSInteger buttonNum); @@ -161,6 +162,39 @@ CGContextRef qt_mac_cg_context(QPaintDevice *pdev); CGImageRef qt_mac_toCGImage(const QImage &qImage, bool isMask, uchar **dataCopy); QImage qt_mac_toQImage(CGImageRef image); +template<typename T> +T qt_mac_resolveOption(const T &fallback, const QByteArray &environment) +{ + // check for environment variable + if (!environment.isEmpty()) { + QByteArray env = qgetenv(environment); + if (!env.isEmpty()) + return T(env.toInt()); // works when T is bool, int. + } + + return fallback; +} + +template<typename T> +T qt_mac_resolveOption(const T &fallback, QWindow *window, const QByteArray &property, const QByteArray &environment) +{ + // check for environment variable + if (!environment.isEmpty()) { + QByteArray env = qgetenv(environment); + if (!env.isEmpty()) + return T(env.toInt()); // works when T is bool, int. + } + + // check for window property + if (window && !property.isNull()) { + QVariant windowProperty = window->property(property); + if (windowProperty.isValid()) + return windowProperty.value<T>(); + } + + // return default value. + return fallback; +} QT_END_NAMESPACE #endif //QCOCOAHELPERS_H diff --git a/src/plugins/platforms/cocoa/qcocoahelpers.mm b/src/plugins/platforms/cocoa/qcocoahelpers.mm index 4a5696b35e..d27c134fa3 100644 --- a/src/plugins/platforms/cocoa/qcocoahelpers.mm +++ b/src/plugins/platforms/cocoa/qcocoahelpers.mm @@ -585,6 +585,12 @@ QString qt_mac_applicationName() return appName; } +NSRect qt_mac_flipRect(const QRect &rect) +{ + int flippedY = qt_mac_flipYCoordinate(rect.y() + rect.height()); + return NSMakeRect(rect.x(), flippedY, rect.width(), rect.height()); +} + /* Mac window coordinates are in the first quadrant: 0, 0 is at the lower-left corner of the primary screen. This function converts the given rect to an diff --git a/src/plugins/platforms/cocoa/qcocoainputcontext.mm b/src/plugins/platforms/cocoa/qcocoainputcontext.mm index 79b1c0856f..f119699004 100644 --- a/src/plugins/platforms/cocoa/qcocoainputcontext.mm +++ b/src/plugins/platforms/cocoa/qcocoainputcontext.mm @@ -43,6 +43,7 @@ #include "qcocoainputcontext.h" #include "qcocoanativeinterface.h" #include "qcocoaautoreleasepool.h" +#include "qcocoawindow.h" #include <QtCore/QRect> #include <QtGui/QGuiApplication> @@ -98,13 +99,12 @@ void QCocoaInputContext::reset() { QPlatformInputContext::reset(); - if (!mWindow) return; + if (!mWindow) + return; - QCocoaNativeInterface *nativeInterface = qobject_cast<QCocoaNativeInterface *>(QGuiApplication::platformNativeInterface()); - if (!nativeInterface) return; - - QNSView *view = static_cast<QNSView *>(nativeInterface->nativeResourceForWindow("nsview", mWindow)); - if (!view) return; + QNSView *view = static_cast<QCocoaWindow *>(mWindow->handle())->qtView(); + if (!view) + return; QCocoaAutoReleasePool pool; NSInputManager *currentIManager = [NSInputManager currentInputManager]; diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.h b/src/plugins/platforms/cocoa/qcocoaintegration.h index 111329aaee..b1b73e5f08 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.h +++ b/src/plugins/platforms/cocoa/qcocoaintegration.h @@ -46,6 +46,10 @@ #include "qcocoaautoreleasepool.h" #include "qcocoacursor.h" +#include "qcocoawindow.h" +#include "qcocoanativeinterface.h" +#include "qcocoainputcontext.h" +#include "qcocoaaccessibility.h" #include "qcocoaclipboard.h" #include "qcocoadrag.h" #include "qcocoaservices.h" @@ -53,6 +57,7 @@ #include <QtCore/QScopedPointer> #include <qpa/qplatformintegration.h> +#include <QtPlatformSupport/private/qcoretextfontdatabase_p.h> QT_BEGIN_NAMESPACE @@ -103,23 +108,25 @@ public: QCocoaIntegration(); ~QCocoaIntegration(); + static QCocoaIntegration *instance(); + bool hasCapability(QPlatformIntegration::Capability cap) const; QPlatformWindow *createPlatformWindow(QWindow *window) const; QPlatformOpenGLContext *createPlatformOpenGLContext(QOpenGLContext *context) const; QPlatformBackingStore *createPlatformBackingStore(QWindow *widget) const; QAbstractEventDispatcher *createEventDispatcher() const; - QPlatformFontDatabase *fontDatabase() const; - QPlatformNativeInterface *nativeInterface() const; - QPlatformInputContext *inputContext() const; - QPlatformAccessibility *accessibility() const; - QPlatformClipboard *clipboard() const; - QPlatformDrag *drag() const; + QCoreTextFontDatabase *fontDatabase() const; + QCocoaNativeInterface *nativeInterface() const; + QCocoaInputContext *inputContext() const; + QCocoaAccessibility *accessibility() const; + QCocoaClipboard *clipboard() const; + QCocoaDrag *drag() const; QStringList themeNames() const; QPlatformTheme *createPlatformTheme(const QString &name) const; - QPlatformServices *services() const; + QCocoaServices *services() const; QVariant styleHint(StyleHint hint) const; QList<int> possibleKeys(const QKeyEvent *event) const; @@ -127,21 +134,27 @@ public: void updateScreens(); QCocoaScreen *screenAtIndex(int index); + void setToolbar(QWindow *window, NSToolbar *toolbar); + NSToolbar *toolbar(QWindow *window) const; + void clearToolbars(); private: + static QCocoaIntegration *mInstance; - QScopedPointer<QPlatformFontDatabase> mFontDb; + QScopedPointer<QCoreTextFontDatabase> mFontDb; - QScopedPointer<QPlatformInputContext> mInputContext; + QScopedPointer<QCocoaInputContext> mInputContext; #ifndef QT_NO_ACCESSIBILITY - QScopedPointer<QPlatformAccessibility> mAccessibility; + QScopedPointer<QCocoaAccessibility> mAccessibility; #endif QScopedPointer<QPlatformTheme> mPlatformTheme; QList<QCocoaScreen *> mScreens; QCocoaClipboard *mCocoaClipboard; QScopedPointer<QCocoaDrag> mCocoaDrag; - QScopedPointer<QPlatformNativeInterface> mNativeInterface; + QScopedPointer<QCocoaNativeInterface> mNativeInterface; QScopedPointer<QCocoaServices> mServices; QScopedPointer<QCocoaKeyMapper> mKeyboardMapper; + + QHash<QWindow *, NSToolbar *> mToolbars; }; QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.mm b/src/plugins/platforms/cocoa/qcocoaintegration.mm index 5f01274d98..0c1ddf9ad8 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.mm +++ b/src/plugins/platforms/cocoa/qcocoaintegration.mm @@ -58,7 +58,6 @@ #include <qpa/qplatformaccessibility.h> #include <QtCore/qcoreapplication.h> -#include <QtPlatformSupport/private/qcoretextfontdatabase_p.h> #include <IOKit/graphics/IOGraphicsLib.h> static void initResources() @@ -214,6 +213,8 @@ QPixmap QCocoaScreen::grabWindow(WId window, int x, int y, int width, int height return windowPixmap; } +QCocoaIntegration *QCocoaIntegration::mInstance = 0; + QCocoaIntegration::QCocoaIntegration() : mFontDb(new QCoreTextFontDatabase()) , mInputContext(new QCocoaInputContext) @@ -226,6 +227,10 @@ QCocoaIntegration::QCocoaIntegration() , mServices(new QCocoaServices) , mKeyboardMapper(new QCocoaKeyMapper) { + if (mInstance != 0) + qWarning("Creating multiple Cocoa platform integrations is not supported"); + mInstance = this; + initResources(); QCocoaAutoReleasePool pool; @@ -273,6 +278,8 @@ QCocoaIntegration::QCocoaIntegration() QCocoaIntegration::~QCocoaIntegration() { + mInstance = 0; + qt_resetNSApplicationSendEvent(); QCocoaAutoReleasePool pool; @@ -294,6 +301,13 @@ QCocoaIntegration::~QCocoaIntegration() while (!mScreens.isEmpty()) { delete mScreens.takeLast(); } + + clearToolbars(); +} + +QCocoaIntegration *QCocoaIntegration::instance() +{ + return mInstance; } /*! @@ -388,22 +402,22 @@ QAbstractEventDispatcher *QCocoaIntegration::createEventDispatcher() const return new QCocoaEventDispatcher; } -QPlatformFontDatabase *QCocoaIntegration::fontDatabase() const +QCoreTextFontDatabase *QCocoaIntegration::fontDatabase() const { return mFontDb.data(); } -QPlatformNativeInterface *QCocoaIntegration::nativeInterface() const +QCocoaNativeInterface *QCocoaIntegration::nativeInterface() const { return mNativeInterface.data(); } -QPlatformInputContext *QCocoaIntegration::inputContext() const +QCocoaInputContext *QCocoaIntegration::inputContext() const { return mInputContext.data(); } -QPlatformAccessibility *QCocoaIntegration::accessibility() const +QCocoaAccessibility *QCocoaIntegration::accessibility() const { #ifndef QT_NO_ACCESSIBILITY return mAccessibility.data(); @@ -412,12 +426,12 @@ QPlatformAccessibility *QCocoaIntegration::accessibility() const #endif } -QPlatformClipboard *QCocoaIntegration::clipboard() const +QCocoaClipboard *QCocoaIntegration::clipboard() const { return mCocoaClipboard; } -QPlatformDrag *QCocoaIntegration::drag() const +QCocoaDrag *QCocoaIntegration::drag() const { return mCocoaDrag.data(); } @@ -434,7 +448,7 @@ QPlatformTheme *QCocoaIntegration::createPlatformTheme(const QString &name) cons return QPlatformIntegration::createPlatformTheme(name); } -QPlatformServices *QCocoaIntegration::services() const +QCocoaServices *QCocoaIntegration::services() const { return mServices.data(); } @@ -454,4 +468,27 @@ QList<int> QCocoaIntegration::possibleKeys(const QKeyEvent *event) const return mKeyboardMapper->possibleKeys(event); } +void QCocoaIntegration::setToolbar(QWindow *window, NSToolbar *toolbar) +{ + if (NSToolbar *prevToolbar = mToolbars.value(window)) + [prevToolbar release]; + + [toolbar retain]; + mToolbars.insert(window, toolbar); +} + +NSToolbar *QCocoaIntegration::toolbar(QWindow *window) const +{ + return mToolbars.value(window); +} + +void QCocoaIntegration::clearToolbars() +{ + QHash<QWindow *, NSToolbar *>::const_iterator it = mToolbars.constBegin(); + while (it != mToolbars.constEnd()) { + [it.value() release]; + } + mToolbars.clear(); +} + QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoanativeinterface.h b/src/plugins/platforms/cocoa/qcocoanativeinterface.h index 5c59c73847..bf7e85619a 100644 --- a/src/plugins/platforms/cocoa/qcocoanativeinterface.h +++ b/src/plugins/platforms/cocoa/qcocoanativeinterface.h @@ -135,8 +135,14 @@ private: // Request a unified title and toolbar look for the window. static void setContentBorderThickness(QWindow *window, int topThickness, int bottomThickness); + + // Sets a NSToolbar instance for the given QWindow. The + // toolbar will be attached to the native NSWindow when + // that is created; + static void setNSToolbar(QWindow *window, void *nsToolbar); }; +QT_END_NAMESPACE + #endif // QCOCOANATIVEINTERFACE_H -QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoanativeinterface.mm b/src/plugins/platforms/cocoa/qcocoanativeinterface.mm index eb520b97c9..85ce96a8b6 100644 --- a/src/plugins/platforms/cocoa/qcocoanativeinterface.mm +++ b/src/plugins/platforms/cocoa/qcocoanativeinterface.mm @@ -47,6 +47,7 @@ #include "qmacmime.h" #include "qcocoahelpers.h" #include "qcocoaapplication.h" +#include "qcocoaintegration.h" #include <qbytearray.h> #include <qwindow.h> @@ -125,6 +126,8 @@ QPlatformNativeInterface::NativeResourceForIntegrationFunction QCocoaNativeInter return NativeResourceForIntegrationFunction(QCocoaNativeInterface::setEmbeddedInForeignView); if (resource.toLower() == "setcontentborderthickness") return NativeResourceForIntegrationFunction(QCocoaNativeInterface::setContentBorderThickness); + if (resource.toLower() == "setnstoolbar") + return NativeResourceForIntegrationFunction(QCocoaNativeInterface::setNSToolbar); return 0; } @@ -285,4 +288,16 @@ void QCocoaNativeInterface::setContentBorderThickness(QWindow *window, int topTh cocoaWindow->setContentBorderThickness(topThickness, bottomThickness); } +void QCocoaNativeInterface::setNSToolbar(QWindow *window, void *nsToolbar) +{ + if (!window) + return; + + QCocoaIntegration::instance()->setToolbar(window, static_cast<NSToolbar *>(nsToolbar)); + + QCocoaWindow *cocoaWindow = static_cast<QCocoaWindow *>(window->handle()); + if (cocoaWindow) + cocoaWindow->updateNSToolbar(); +} + QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoatheme.h b/src/plugins/platforms/cocoa/qcocoatheme.h index e4237c9b3e..d60cdf10d1 100644 --- a/src/plugins/platforms/cocoa/qcocoatheme.h +++ b/src/plugins/platforms/cocoa/qcocoatheme.h @@ -73,6 +73,7 @@ public: QPlatformTheme::IconOptions options = 0) const; QVariant themeHint(ThemeHint hint) const; + QString standardButtonText(int button) const Q_DECL_OVERRIDE; static const char *name; diff --git a/src/plugins/platforms/cocoa/qcocoatheme.mm b/src/plugins/platforms/cocoa/qcocoatheme.mm index d863861288..dce1671800 100644 --- a/src/plugins/platforms/cocoa/qcocoatheme.mm +++ b/src/plugins/platforms/cocoa/qcocoatheme.mm @@ -42,6 +42,7 @@ #import <Cocoa/Cocoa.h> #include "qcocoatheme.h" +#include "messages.h" #include <QtCore/QVariant> @@ -278,7 +279,7 @@ QVariant QCocoaTheme::themeHint(ThemeHint hint) const case QPlatformTheme::StyleNames: return QStringList(QStringLiteral("macintosh")); case QPlatformTheme::DialogButtonBoxLayout: - return QVariant(1); // QDialogButtonBox::MacLayout + return QVariant(QPlatformDialogHelper::MacLayout); case KeyboardScheme: return QVariant(int(MacKeyboardScheme)); case TabAllWidgets: @@ -300,6 +301,11 @@ QVariant QCocoaTheme::themeHint(ThemeHint hint) const return QPlatformTheme::themeHint(hint); } +QString QCocoaTheme::standardButtonText(int button) const +{ + return button == QPlatformDialogHelper::Discard ? msgDialogButtonDiscard() : QPlatformTheme::standardButtonText(button); +} + QPlatformMenuItem *QCocoaTheme::createPlatformMenuItem() const { return new QCocoaMenuItem(); diff --git a/src/plugins/platforms/cocoa/qcocoawindow.h b/src/plugins/platforms/cocoa/qcocoawindow.h index 452be90108..748280af6a 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.h +++ b/src/plugins/platforms/cocoa/qcocoawindow.h @@ -52,19 +52,27 @@ QT_FORWARD_DECLARE_CLASS(QCocoaWindow) -@interface QNSWindow : NSWindow { +@interface QNSWindow : NSWindow +{ @public QCocoaWindow *m_cocoaPlatformWindow; } +- (id)initWithContentRect:(NSRect)contentRect + styleMask:(NSUInteger)windowStyle + qPlatformWindow:(QCocoaWindow *)qpw; - (void)clearPlatformWindow; -- (BOOL)canBecomeKeyWindow; @end -@interface QNSPanel : NSPanel { +@interface QNSPanel : NSPanel +{ @public QCocoaWindow *m_cocoaPlatformWindow; } + +- (id)initWithContentRect:(NSRect)contentRect + styleMask:(NSUInteger)windowStyle + qPlatformWindow:(QCocoaWindow *)qpw; + - (void)clearPlatformWindow; -- (BOOL)canBecomeKeyWindow; @end @class QNSWindowDelegate; @@ -100,6 +108,10 @@ public: void setGeometry(const QRect &rect); void setCocoaGeometry(const QRect &rect); + void clipChildWindows(); + void clipWindow(const NSRect &clipRect); + void show(bool becauseOfAncestor = false); + void hide(bool becauseOfAncestor = false); void setVisible(bool visible); void setWindowFlags(Qt::WindowFlags flags); void setWindowState(Qt::WindowState state); @@ -127,6 +139,8 @@ public: NSView *contentView() const; void setContentView(NSView *contentView); + QNSView *qtView() const; + NSWindow *nativeWindow() const; void setEmbeddedInForeignView(bool subwindow); @@ -159,6 +173,7 @@ public: void registerTouch(bool enable); void setContentBorderThickness(int topThickness, int bottomThickness); void applyContentBorderThickness(NSWindow *window); + void updateNSToolbar(); qreal devicePixelRatio() const; bool isWindowExposable(); @@ -167,16 +182,18 @@ public: void updateExposedGeometry(); QWindow *childWindowAt(QPoint windowPoint); protected: - // NSWindow handling. The QCocoaWindow/QNSView can either be displayed - // in an existing NSWindow or in one created by Qt. void recreateWindow(const QPlatformWindow *parentWindow); NSWindow *createNSWindow(); void setNSWindow(NSWindow *window); void clearNSWindow(NSWindow *window); + bool shouldUseNSPanel(); + QRect windowGeometry() const; QCocoaWindow *parentCocoaWindow() const; void syncWindowState(Qt::WindowState newState); + void reinsertChildWindow(QCocoaWindow *child); + void removeChildWindow(QCocoaWindow *child); // private: public: // for QNSView @@ -186,11 +203,16 @@ public: // for QNSView NSView *m_contentView; QNSView *m_qtView; NSWindow *m_nsWindow; + QCocoaWindow *m_forwardWindow; // TODO merge to one variable if possible bool m_contentViewIsEmbedded; // true if the m_contentView is actually embedded in a "foreign" NSView hiearchy bool m_contentViewIsToBeEmbedded; // true if the m_contentView is intended to be embedded in a "foreign" NSView hiearchy + QCocoaWindow *m_parentCocoaWindow; + bool m_isNSWindowChild; // this window is a non-top level QWindow with a NSWindow. + QList<QCocoaWindow *> m_childWindows; + QNSWindowDelegate *m_nsWindowDelegate; Qt::WindowFlags m_windowFlags; Qt::WindowState m_synchedWindowState; @@ -211,6 +233,8 @@ public: // for QNSView QRect m_exposedGeometry; int m_registerTouchCount; bool m_resizableTransientParent; + bool m_hiddenByClipping; + bool m_hiddenByAncestor; static const int NoAlertRequest; NSInteger m_alertRequest; @@ -219,6 +243,11 @@ public: // for QNSView bool m_drawContentBorderGradient; int m_topContentBorderThickness; int m_bottomContentBorderThickness; + + // used by showFullScreen in fake mode + QRect m_normalGeometry; + Qt::WindowFlags m_oldWindowFlags; + NSApplicationPresentationOptions m_presentationOptions; }; QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index 211ecd60ab..d972782f31 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -45,6 +45,7 @@ #include "qcocoaeventdispatcher.h" #include "qcocoaglcontext.h" #include "qcocoahelpers.h" +#include "qcocoanativeinterface.h" #include "qnsview.h" #include <QtCore/qfileinfo.h> #include <QtCore/private/qcore_mac_p.h> @@ -104,8 +105,29 @@ static bool isMouseEvent(NSEvent *ev) @implementation QNSWindow +- (id)initWithContentRect:(NSRect)contentRect + styleMask:(NSUInteger)windowStyle + qPlatformWindow:(QCocoaWindow *)qpw +{ + self = [super initWithContentRect:contentRect + styleMask:windowStyle + backing:NSBackingStoreBuffered + defer:NO]; // Deferring window creation breaks OpenGL (the GL context is + // set up before the window is shown and needs a proper window) + + if (self) { + m_cocoaPlatformWindow = qpw; + } + return self; +} + - (BOOL)canBecomeKeyWindow { + // Prevent child NSWindows from becoming the key window in + // order keep the active apperance of the top-level window. + if (!m_cocoaPlatformWindow || m_cocoaPlatformWindow->m_isNSWindowChild) + return NO; + // The default implementation returns NO for title-bar less windows, // override and return yes here to make sure popup windows such as // the combobox popup can become the key window. @@ -118,7 +140,8 @@ static bool isMouseEvent(NSEvent *ev) // Windows with a transient parent (such as combobox popup windows) // cannot become the main window: - if (!m_cocoaPlatformWindow || m_cocoaPlatformWindow->window()->transientParent()) + if (!m_cocoaPlatformWindow || m_cocoaPlatformWindow->m_isNSWindowChild + || m_cocoaPlatformWindow->window()->transientParent()) canBecomeMain = NO; return canBecomeMain; @@ -126,6 +149,24 @@ static bool isMouseEvent(NSEvent *ev) - (void) sendEvent: (NSEvent*) theEvent { + if (m_cocoaPlatformWindow && m_cocoaPlatformWindow->m_forwardWindow) { + if (theEvent.type == NSLeftMouseUp || theEvent.type == NSLeftMouseDragged) { + QNSView *forwardView = m_cocoaPlatformWindow->m_qtView; + if (theEvent.type == NSLeftMouseUp) { + [forwardView mouseUp:theEvent]; + m_cocoaPlatformWindow->m_forwardWindow = 0; + } else { + [forwardView mouseDragged:theEvent]; + } + + return; + } + + if (theEvent.type == NSLeftMouseDown) { + m_cocoaPlatformWindow->m_forwardWindow = 0; + } + } + [super sendEvent: theEvent]; if (!m_cocoaPlatformWindow) @@ -153,6 +194,22 @@ static bool isMouseEvent(NSEvent *ev) @implementation QNSPanel +- (id)initWithContentRect:(NSRect)contentRect + styleMask:(NSUInteger)windowStyle + qPlatformWindow:(QCocoaWindow *)qpw +{ + self = [super initWithContentRect:contentRect + styleMask:windowStyle + backing:NSBackingStoreBuffered + defer:NO]; // Deferring window creation breaks OpenGL (the GL context is + // set up before the window is shown and needs a proper window) + + if (self) { + m_cocoaPlatformWindow = qpw; + } + return self; +} + - (BOOL)canBecomeKeyWindow { if (!m_cocoaPlatformWindow) @@ -160,7 +217,8 @@ static bool isMouseEvent(NSEvent *ev) // Only tool or dialog windows should become key: if (m_cocoaPlatformWindow - && (m_cocoaPlatformWindow->window()->type() == Qt::Tool || m_cocoaPlatformWindow->window()->type() == Qt::Dialog)) + && (m_cocoaPlatformWindow->window()->type() == Qt::Tool || + m_cocoaPlatformWindow->window()->type() == Qt::Dialog)) return YES; return NO; } @@ -199,8 +257,11 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw) , m_contentView(nil) , m_qtView(nil) , m_nsWindow(0) + , m_forwardWindow(0) , m_contentViewIsEmbedded(false) , m_contentViewIsToBeEmbedded(false) + , m_parentCocoaWindow(0) + , m_isNSWindowChild(false) , m_nsWindowDelegate(0) , m_synchedWindowState(Qt::WindowActive) , m_windowModality(Qt::NonModal) @@ -215,11 +276,14 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw) , m_isExposed(false) , m_registerTouchCount(0) , m_resizableTransientParent(false) + , m_hiddenByClipping(false) + , m_hiddenByAncestor(false) , m_alertRequest(NoAlertRequest) , monitor(nil) , m_drawContentBorderGradient(false) , m_topContentBorderThickness(0) , m_bottomContentBorderThickness(0) + , m_normalGeometry(QRect(0,0,-1,-1)) { #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG qDebug() << "QCocoaWindow::QCocoaWindow" << this; @@ -239,9 +303,15 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw) // problem, except if the appilcation wants to have a "custom" viewport. // (like the hellogl example) if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7 - && tlw->surfaceType() == QSurface::OpenGLSurface) - [m_contentView setWantsBestResolutionOpenGLSurface:YES]; + && tlw->surfaceType() == QSurface::OpenGLSurface) { + BOOL enable = qt_mac_resolveOption(YES, tlw, "_q_mac_wantsBestResolutionOpenGLSurface", + "QT_MAC_WANTS_BEST_RESOLUTION_OPENGL_SURFACE"); + [m_contentView setWantsBestResolutionOpenGLSurface:enable]; + } #endif + BOOL enable = qt_mac_resolveOption(NO, tlw, "_q_mac_wantsLayer", + "QT_MAC_WANTS_LAYER"); + [m_contentView setWantsLayer:enable]; } setGeometry(tlw->geometry()); recreateWindow(parent()); @@ -259,8 +329,21 @@ QCocoaWindow::~QCocoaWindow() QCocoaAutoReleasePool pool; clearNSWindow(m_nsWindow); - if (parent()) + if (m_isNSWindowChild) { + if (m_parentCocoaWindow) + m_parentCocoaWindow->removeChildWindow(this); + } else if (parent()) { [m_contentView removeFromSuperview]; + } else if (m_qtView) { + [[NSNotificationCenter defaultCenter] removeObserver:m_qtView + name:nil object:m_nsWindow]; + } + + foreach (QCocoaWindow *child, m_childWindows) { + [m_nsWindow removeChildWindow:child->m_nsWindow]; + child->m_parentCocoaWindow = 0; + } + [m_contentView release]; [m_nsWindow release]; [m_nsWindowDelegate release]; @@ -272,8 +355,16 @@ QSurfaceFormat QCocoaWindow::format() const return window()->requestedFormat(); } -void QCocoaWindow::setGeometry(const QRect &rect) +void QCocoaWindow::setGeometry(const QRect &rectIn) { + QRect rect = rectIn; + // This means it is a call from QWindow::setFramePosition() and + // the coordinates include the frame (size is still the contents rectangle). + if (qt_window_private(const_cast<QWindow *>(window()))->positionPolicy + == QWindowPrivate::WindowFrameInclusive) { + const QMargins margins = frameMargins(); + rect.moveTopLeft(rect.topLeft() + QPoint(margins.left(), margins.top())); + } if (geometry() == rect) return; #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG @@ -291,7 +382,16 @@ void QCocoaWindow::setCocoaGeometry(const QRect &rect) return; } - if (m_nsWindow) { + if (m_isNSWindowChild) { + QPlatformWindow::setGeometry(rect); + NSWindow *parentNSWindow = m_parentCocoaWindow->m_nsWindow; + NSRect parentWindowFrame = [parentNSWindow contentRectForFrameRect:parentNSWindow.frame]; + clipWindow(parentWindowFrame); + + // call this here: updateGeometry in qnsview.mm is a no-op for this case + QWindowSystemInterface::handleGeometryChange(window(), rect); + QWindowSystemInterface::handleExposeEvent(window(), rect); + } else if (m_nsWindow) { NSRect bounds = qt_mac_flipRect(rect, window()); [m_nsWindow setFrame:[m_nsWindow frameRectForContentRect:bounds] display:YES animate:NO]; } else { @@ -301,8 +401,99 @@ void QCocoaWindow::setCocoaGeometry(const QRect &rect) // will call QPlatformWindow::setGeometry(rect) during resize confirmation (see qnsview.mm) } +void QCocoaWindow::clipChildWindows() +{ + foreach (QCocoaWindow *childWindow, m_childWindows) { + childWindow->clipWindow(m_nsWindow.frame); + } +} + +void QCocoaWindow::clipWindow(const NSRect &clipRect) +{ + if (!m_isNSWindowChild) + return; + + NSRect clippedWindowRect = NSZeroRect; + if (!NSIsEmptyRect(clipRect)) { + NSRect windowFrame = qt_mac_flipRect(QRect(window()->mapToGlobal(QPoint(0, 0)), geometry().size()), window()); + clippedWindowRect = NSIntersectionRect(windowFrame, clipRect); + // Clipping top/left offsets the content. Move it back. + NSPoint contentViewOffset = NSMakePoint(qMax(CGFloat(0), NSMinX(clippedWindowRect) - NSMinX(windowFrame)), + qMax(CGFloat(0), NSMaxY(windowFrame) - NSMaxY(clippedWindowRect))); + [m_contentView setBoundsOrigin:contentViewOffset]; + } + + if (NSIsEmptyRect(clippedWindowRect)) { + if (!m_hiddenByClipping) { + // We dont call hide() here as we will recurse further down + [m_nsWindow orderOut:nil]; + m_hiddenByClipping = true; + } + } else { + [m_nsWindow setFrame:clippedWindowRect display:YES animate:NO]; + if (m_hiddenByClipping) { + m_hiddenByClipping = false; + if (!m_hiddenByAncestor) { + [m_nsWindow orderFront:nil]; + m_parentCocoaWindow->reinsertChildWindow(this); + } + } + } + + // recurse + foreach (QCocoaWindow *childWindow, m_childWindows) { + childWindow->clipWindow(clippedWindowRect); + } +} + +void QCocoaWindow::hide(bool becauseOfAncestor) +{ + bool visible = [m_nsWindow isVisible]; + + if (!m_hiddenByAncestor && !visible) // Already explicitly hidden + return; + if (m_hiddenByAncestor && becauseOfAncestor) // Trying to hide some child again + return; + + m_hiddenByAncestor = becauseOfAncestor; + + if (!visible) // Could have been clipped before + return; + + foreach (QCocoaWindow *childWindow, m_childWindows) + childWindow->hide(true); + + [m_nsWindow orderOut:nil]; +} + +void QCocoaWindow::show(bool becauseOfAncestor) +{ + if ([m_nsWindow isVisible]) + return; + + if (m_parentCocoaWindow && ![m_parentCocoaWindow->m_nsWindow isVisible]) { + m_hiddenByAncestor = true; // Parent still hidden, don't show now + } else if ((becauseOfAncestor == m_hiddenByAncestor) // Was NEITHER explicitly hidden + && !m_hiddenByClipping) { // ... NOR clipped + if (m_isNSWindowChild) { + m_hiddenByAncestor = false; + setCocoaGeometry(window()->geometry()); + } + if (!m_hiddenByClipping) { // setCocoaGeometry() can change the clipping status + [m_nsWindow orderFront:nil]; + if (m_isNSWindowChild) + m_parentCocoaWindow->reinsertChildWindow(this); + foreach (QCocoaWindow *childWindow, m_childWindows) + childWindow->show(true); + } + } +} + void QCocoaWindow::setVisible(bool visible) { + if (m_isNSWindowChild && m_hiddenByClipping) + return; + QCocoaAutoReleasePool pool; QCocoaWindow *parentCocoaWindow = 0; if (window()->transientParent()) @@ -367,8 +558,10 @@ void QCocoaWindow::setVisible(bool visible) m_hasModalSession = true; } else if ([m_nsWindow canBecomeKeyWindow]) { [m_nsWindow makeKeyAndOrderFront:nil]; + foreach (QCocoaWindow *childWindow, m_childWindows) + childWindow->show(true); } else { - [m_nsWindow orderFront: nil]; + show(); } // We want the events to properly reach the popup, dialog, and tool @@ -392,28 +585,30 @@ void QCocoaWindow::setVisible(bool visible) // qDebug() << "close" << this; if (m_glContext) m_glContext->windowWasHidden(); + QCocoaEventDispatcher *cocoaEventDispatcher = qobject_cast<QCocoaEventDispatcher *>(QGuiApplication::instance()->eventDispatcher()); + QCocoaEventDispatcherPrivate *cocoaEventDispatcherPrivate = 0; + if (cocoaEventDispatcher) + cocoaEventDispatcherPrivate = static_cast<QCocoaEventDispatcherPrivate *>(QObjectPrivate::get(cocoaEventDispatcher)); if (m_nsWindow) { if (m_hasModalSession) { - QCocoaEventDispatcher *cocoaEventDispatcher = qobject_cast<QCocoaEventDispatcher *>(QGuiApplication::instance()->eventDispatcher()); - Q_ASSERT(cocoaEventDispatcher != 0); - QCocoaEventDispatcherPrivate *cocoaEventDispatcherPrivate = static_cast<QCocoaEventDispatcherPrivate *>(QObjectPrivate::get(cocoaEventDispatcher)); - cocoaEventDispatcherPrivate->endModalSession(window()); + if (cocoaEventDispatcherPrivate) + cocoaEventDispatcherPrivate->endModalSession(window()); m_hasModalSession = false; - - [m_nsWindow orderOut:m_nsWindow]; - if (m_nsWindow == [NSApp keyWindow] && !cocoaEventDispatcherPrivate->currentModalSession()) { - // Probably because we call runModalSession: outside [NSApp run] in QCocoaEventDispatcher - // (e.g., when show()-ing a modal QDialog instead of exec()-ing it), it can happen that - // the current NSWindow is still key after being ordered out. Then, after checking we - // don't have any other modal session left, it's safe to make the main window key again. - NSWindow *mainWindow = [NSApp mainWindow]; - if (mainWindow && [mainWindow canBecomeKeyWindow]) - [mainWindow makeKeyWindow]; - } } else { if ([m_nsWindow isSheet]) [NSApp endSheet:m_nsWindow]; - [m_nsWindow orderOut:m_nsWindow]; + } + + hide(); + if (m_nsWindow == [NSApp keyWindow] + && !(cocoaEventDispatcherPrivate && cocoaEventDispatcherPrivate->currentModalSession())) { + // Probably because we call runModalSession: outside [NSApp run] in QCocoaEventDispatcher + // (e.g., when show()-ing a modal QDialog instead of exec()-ing it), it can happen that + // the current NSWindow is still key after being ordered out. Then, after checking we + // don't have any other modal session left, it's safe to make the main window key again. + NSWindow *mainWindow = [NSApp mainWindow]; + if (mainWindow && [mainWindow canBecomeKeyWindow]) + [mainWindow makeKeyWindow]; } } else { [m_contentView setHidden:YES]; @@ -520,7 +715,7 @@ void QCocoaWindow::setWindowShadow(Qt::WindowFlags flags) void QCocoaWindow::setWindowFlags(Qt::WindowFlags flags) { - if (m_nsWindow) { + if (m_nsWindow && !m_isNSWindowChild) { NSUInteger styleMask = windowStyleMask(flags); NSInteger level = this->windowLevel(flags); [m_nsWindow setStyleMask:styleMask]; @@ -618,16 +813,55 @@ void QCocoaWindow::raise() // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm) if (!m_nsWindow) return; - if ([m_nsWindow isVisible]) - [m_nsWindow orderFront: m_nsWindow]; + if (m_isNSWindowChild) { + QList<QCocoaWindow *> &siblings = m_parentCocoaWindow->m_childWindows; + siblings.removeOne(this); + siblings.append(this); + if (m_hiddenByClipping) + return; + } + if ([m_nsWindow isVisible]) { + if (m_isNSWindowChild) { + // -[NSWindow orderFront:] doesn't work with attached windows. + // The only solution is to remove and add the child window. + // This will place it on top of all the other NSWindows. + NSWindow *parentNSWindow = m_parentCocoaWindow->m_nsWindow; + [parentNSWindow removeChildWindow:m_nsWindow]; + [parentNSWindow addChildWindow:m_nsWindow ordered:NSWindowAbove]; + } else { + [m_nsWindow orderFront: m_nsWindow]; + } + } } void QCocoaWindow::lower() { if (!m_nsWindow) return; - if ([m_nsWindow isVisible]) - [m_nsWindow orderBack: m_nsWindow]; + if (m_isNSWindowChild) { + QList<QCocoaWindow *> &siblings = m_parentCocoaWindow->m_childWindows; + siblings.removeOne(this); + siblings.prepend(this); + if (m_hiddenByClipping) + return; + } + if ([m_nsWindow isVisible]) { + if (m_isNSWindowChild) { + // -[NSWindow orderBack:] doesn't work with attached windows. + // The only solution is to remove and add all the child windows except this one. + // This will keep the current window at the bottom while adding the others on top of it, + // hopefully in the same order (this is not documented anywhere in the Cocoa documentation). + NSWindow *parentNSWindow = m_parentCocoaWindow->m_nsWindow; + NSArray *children = [parentNSWindow.childWindows copy]; + for (NSWindow *child in children) + if (m_nsWindow != child) { + [parentNSWindow removeChildWindow:child]; + [parentNSWindow addChildWindow:child ordered:NSWindowAbove]; + } + } else { + [m_nsWindow orderBack: m_nsWindow]; + } + } } bool QCocoaWindow::isExposed() const @@ -755,6 +989,16 @@ void QCocoaWindow::setContentView(NSView *contentView) recreateWindow(parent()); // Adds the content view to parent NSView } +QNSView *QCocoaWindow::qtView() const +{ + return m_qtView; +} + +NSWindow *QCocoaWindow::nativeWindow() const +{ + return m_nsWindow; +} + void QCocoaWindow::setEmbeddedInForeignView(bool embedded) { m_contentViewIsToBeEmbedded = embedded; @@ -773,6 +1017,9 @@ void QCocoaWindow::windowWillMove() void QCocoaWindow::windowDidMove() { + if (m_isNSWindowChild) + return; + [m_qtView updateGeometry]; } @@ -781,6 +1028,10 @@ void QCocoaWindow::windowDidResize() if (!m_nsWindow) return; + if (m_isNSWindowChild) + return; + + clipChildWindows(); [m_qtView updateGeometry]; } @@ -820,8 +1071,22 @@ QCocoaGLContext *QCocoaWindow::currentContext() const void QCocoaWindow::recreateWindow(const QPlatformWindow *parentWindow) { + bool wasNSWindowChild = m_isNSWindowChild; + // TODO Set value for m_isNSWindowChild here + bool needsNSWindow = m_isNSWindowChild || !parentWindow; + + QCocoaWindow *oldParentCocoaWindow = m_parentCocoaWindow; + m_parentCocoaWindow = const_cast<QCocoaWindow *>(static_cast<const QCocoaWindow *>(parentWindow)); + + bool usesNSPanel = [m_nsWindow isKindOfClass:[QNSPanel class]]; + + // No child QNSWindow should notify its QNSView + if (m_nsWindow && m_qtView && m_parentCocoaWindow && !oldParentCocoaWindow) + [[NSNotificationCenter defaultCenter] removeObserver:m_qtView + name:nil object:m_nsWindow]; + // Remove current window (if any) - if (m_nsWindow) { + if ((m_nsWindow && !needsNSWindow) || (usesNSPanel != shouldUseNSPanel())) { clearNSWindow(m_nsWindow); [m_nsWindow close]; [m_nsWindow release]; @@ -830,22 +1095,63 @@ void QCocoaWindow::recreateWindow(const QPlatformWindow *parentWindow) m_nsWindowDelegate = 0; } + if (needsNSWindow) { + bool noPreviousWindow = m_nsWindow == 0; + if (noPreviousWindow) + m_nsWindow = createNSWindow(); + + // Only non-child QNSWindows should notify their QNSViews + // (but don't register more than once). + if (m_qtView && (noPreviousWindow || (wasNSWindowChild && !m_isNSWindowChild))) + [[NSNotificationCenter defaultCenter] addObserver:m_qtView + selector:@selector(windowNotification:) + name:nil // Get all notifications + object:m_nsWindow]; + + if (oldParentCocoaWindow) { + if (!m_isNSWindowChild || oldParentCocoaWindow != m_parentCocoaWindow) + oldParentCocoaWindow->removeChildWindow(this); + m_forwardWindow = oldParentCocoaWindow; + } + + setNSWindow(m_nsWindow); + } + if (m_contentViewIsToBeEmbedded) { // An embedded window doesn't have its own NSWindow. } else if (!parentWindow) { - // Create a new NSWindow if this is a top-level window. - m_nsWindow = createNSWindow(); - setNSWindow(m_nsWindow); - // QPlatformWindow subclasses must sync up with QWindow on creation: propagateSizeHints(); setWindowFlags(window()->flags()); setWindowTitle(window()->title()); setWindowState(window()->windowState()); + } else if (m_isNSWindowChild) { + m_nsWindow.styleMask = NSBorderlessWindowMask; + m_nsWindow.hasShadow = NO; + m_nsWindow.level = NSNormalWindowLevel; + NSWindowCollectionBehavior collectionBehavior = + NSWindowCollectionBehaviorManaged | NSWindowCollectionBehaviorIgnoresCycle; +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 + if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) { + collectionBehavior |= NSWindowCollectionBehaviorFullScreenAuxiliary; + m_nsWindow.animationBehavior = NSWindowAnimationBehaviorNone; + } +#endif + m_nsWindow.collectionBehavior = collectionBehavior; + setCocoaGeometry(window()->geometry()); + + QList<QCocoaWindow *> &siblings = m_parentCocoaWindow->m_childWindows; + if (siblings.contains(this)) { + if (!m_hiddenByClipping) + m_parentCocoaWindow->reinsertChildWindow(this); + } else { + if (!m_hiddenByClipping) + [m_parentCocoaWindow->m_nsWindow addChildWindow:m_nsWindow ordered:NSWindowAbove]; + siblings.append(this); + } } else { // Child windows have no NSWindow, link the NSViews instead. - const QCocoaWindow *parentCococaWindow = static_cast<const QCocoaWindow *>(parentWindow); - [parentCococaWindow->m_contentView addSubview : m_contentView]; + [m_parentCocoaWindow->m_contentView addSubview : m_contentView]; QRect rect = window()->geometry(); NSRect frame = NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height()); [m_contentView setFrame:frame]; @@ -855,6 +1161,24 @@ void QCocoaWindow::recreateWindow(const QPlatformWindow *parentWindow) const qreal opacity = qt_window_private(window())->opacity; if (!qFuzzyCompare(opacity, qreal(1.0))) setOpacity(opacity); + + // top-level QWindows may have an attached NSToolBar, call + // update function which will attach to the NSWindow. + if (!parentWindow) + updateNSToolbar(); +} + +void QCocoaWindow::reinsertChildWindow(QCocoaWindow *child) +{ + int childIndex = m_childWindows.indexOf(child); + Q_ASSERT(childIndex != -1); + + for (int i = childIndex; i < m_childWindows.size(); i++) { + NSWindow *nsChild = m_childWindows[i]->m_nsWindow; + if (i != childIndex) + [m_nsWindow removeChildWindow:nsChild]; + [m_nsWindow addChildWindow:nsChild ordered:NSWindowAbove]; + } } void QCocoaWindow::requestActivateWindow() @@ -864,6 +1188,14 @@ void QCocoaWindow::requestActivateWindow() [ window makeKeyWindow ]; } +bool QCocoaWindow::shouldUseNSPanel() +{ + Qt::WindowType type = window()->type(); + + return !m_isNSWindowChild && + ((type & Qt::Popup) == Qt::Popup || (type & Qt::Dialog) == Qt::Dialog); +} + NSWindow * QCocoaWindow::createNSWindow() { QCocoaAutoReleasePool pool; @@ -874,18 +1206,21 @@ NSWindow * QCocoaWindow::createNSWindow() Qt::WindowType type = window()->type(); Qt::WindowFlags flags = window()->flags(); - NSUInteger styleMask = windowStyleMask(flags); + NSUInteger styleMask; + if (m_isNSWindowChild) { + styleMask = NSBorderlessWindowMask; + } else { + styleMask = windowStyleMask(flags); + } NSWindow *createdWindow = 0; // Use NSPanel for popup-type windows. (Popup, Tool, ToolTip, SplashScreen) // and dialogs - if ((type & Qt::Popup) == Qt::Popup || (type & Qt::Dialog) == Qt::Dialog) { + if (shouldUseNSPanel()) { QNSPanel *window; window = [[QNSPanel alloc] initWithContentRect:frame - styleMask: styleMask - backing:NSBackingStoreBuffered - defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up - // before the window is shown and needs a proper window.). + styleMask: styleMask + qPlatformWindow:this]; if ((type & Qt::Popup) == Qt::Popup) [window setHasShadow:YES]; [window setHidesOnDeactivate: NO]; @@ -899,17 +1234,12 @@ NSWindow * QCocoaWindow::createNSWindow() [window setAnimationBehavior:NSWindowAnimationBehaviorUtilityWindow]; } #endif - window->m_cocoaPlatformWindow = this; createdWindow = window; } else { QNSWindow *window; window = [[QNSWindow alloc] initWithContentRect:frame - styleMask: styleMask - backing:NSBackingStoreBuffered - defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up - // before the window is shown and needs a proper window.). - window->m_cocoaPlatformWindow = this; - + styleMask: styleMask + qPlatformWindow:this]; createdWindow = window; } @@ -944,16 +1274,11 @@ void QCocoaWindow::setNSWindow(NSWindow *window) // QCocoaWindow is deleted by Qt. [window setReleasedWhenClosed : NO]; - - if (m_qtView) - [[NSNotificationCenter defaultCenter] addObserver:m_qtView - selector:@selector(windowNotification:) - name:nil // Get all notifications - object:m_nsWindow]; - - [m_contentView setPostsFrameChangedNotifications: NO]; - [window setContentView:m_contentView]; - [m_contentView setPostsFrameChangedNotifications: YES]; + if (window.contentView != m_contentView) { + [m_contentView setPostsFrameChangedNotifications: NO]; + [window setContentView:m_contentView]; + [m_contentView setPostsFrameChangedNotifications: YES]; + } } void QCocoaWindow::clearNSWindow(NSWindow *window) @@ -961,14 +1286,22 @@ void QCocoaWindow::clearNSWindow(NSWindow *window) [window setContentView:nil]; [window setDelegate:nil]; [window clearPlatformWindow]; - [[NSNotificationCenter defaultCenter] removeObserver:m_contentView - name:nil object:window]; + + if (m_isNSWindowChild) { + m_parentCocoaWindow->removeChildWindow(this); + } +} + +void QCocoaWindow::removeChildWindow(QCocoaWindow *child) +{ + m_childWindows.removeOne(child); + [m_nsWindow removeChildWindow:child->m_nsWindow]; } // Returns the current global screen geometry for the nswindow associated with this window. QRect QCocoaWindow::windowGeometry() const { - if (!m_nsWindow) + if (!m_nsWindow || m_isNSWindowChild) return geometry(); NSRect rect = [m_nsWindow frame]; @@ -1015,13 +1348,35 @@ void QCocoaWindow::syncWindowState(Qt::WindowState newState) } if ((m_synchedWindowState & Qt::WindowFullScreen) != (newState & Qt::WindowFullScreen)) { + bool fakeFullScreen = true; #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) { - [m_nsWindow toggleFullScreen : m_nsWindow]; - } else { - // TODO: "normal" fullscreen + if (window()->flags() & Qt::WindowFullscreenButtonHint) { + fakeFullScreen = false; + [m_nsWindow toggleFullScreen : m_nsWindow]; + } } #endif + if (fakeFullScreen) { + if (newState & Qt::WindowFullScreen) { + QScreen *screen = window()->screen(); + if (screen) { + if (m_normalGeometry.width() < 0) { + m_oldWindowFlags = m_windowFlags; + window()->setFlags(window()->flags() | Qt::FramelessWindowHint); + m_normalGeometry = windowGeometry(); + setGeometry(screen->geometry()); + m_presentationOptions = [NSApp presentationOptions]; + [NSApp setPresentationOptions : m_presentationOptions | NSApplicationPresentationAutoHideMenuBar | NSApplicationPresentationAutoHideDock]; + } + } + } else { + window()->setFlags(m_oldWindowFlags); + setGeometry(m_normalGeometry); + m_normalGeometry.setRect(0, 0, -1, -1); + [NSApp setPresentationOptions : m_presentationOptions]; + } + } } // New state is now the current synched state @@ -1054,16 +1409,21 @@ void QCocoaWindow::setWindowCursor(NSCursor *cursor) // for a popup window.) Qt expects the set cursor to "stick": // it should be accociated with the window until a different // cursor is set. - - // Cocoa has different abstractions. We can set the cursor *now*: - if (m_windowUnderMouse) - [cursor set]; - // or we can set the cursor on mouse enter/leave using tracking - // areas. This is done in QNSView, save the cursor: if (m_windowCursor != cursor) { [m_windowCursor release]; m_windowCursor = [cursor retain]; } + + // Use the built in cursor rect API if the QCocoaWindow has a NSWindow. + // Othervise, set the cursor if this window is under the mouse. In + // this case QNSView::cursorUpdate will set the cursor as the pointer + // moves. + if (m_nsWindow && m_qtView) { + [m_nsWindow invalidateCursorRectsForView : m_qtView]; + } else { + if (m_windowUnderMouse) + [cursor set]; + } } void QCocoaWindow::registerTouch(bool enable) @@ -1106,6 +1466,19 @@ void QCocoaWindow::applyContentBorderThickness(NSWindow *window) } } +void QCocoaWindow::updateNSToolbar() +{ + if (!m_nsWindow) + return; + + NSToolbar *toolbar = QCocoaIntegration::instance()->toolbar(window()); + + if ([m_nsWindow toolbar] == toolbar) + return; + + [m_nsWindow setToolbar: toolbar]; + [m_nsWindow setShowsToolbarButton:YES]; +} qreal QCocoaWindow::devicePixelRatio() const { diff --git a/src/plugins/platforms/cocoa/qnsview.mm b/src/plugins/platforms/cocoa/qnsview.mm index ed9aad1654..58c732de98 100644 --- a/src/plugins/platforms/cocoa/qnsview.mm +++ b/src/plugins/platforms/cocoa/qnsview.mm @@ -229,7 +229,21 @@ static QTouchDevice *touchDevice = 0; - (void)updateGeometry { QRect geometry; - if (m_platformWindow->m_nsWindow) { + + if (m_platformWindow->m_isNSWindowChild) { + return; +#if 0 + //geometry = qt_mac_toQRect([self frame]); + qDebug() << "nsview updateGeometry" << m_platformWindow->window(); + QRect screenRect = qt_mac_toQRect([m_platformWindow->m_nsWindow convertRectToScreen:[self frame]]); + qDebug() << "screenRect" << screenRect; + + screenRect.moveTop(qt_mac_flipYCoordinate(screenRect.y() + screenRect.height())); + geometry = QRect(m_platformWindow->window()->parent()->mapFromGlobal(screenRect.topLeft()), screenRect.size()); + qDebug() << "geometry" << geometry; +#endif + //geometry = QRect(screenRect.origin.x, qt_mac_flipYCoordinate(screenRect.origin.y + screenRect.size.height), screenRect.size.width, screenRect.size.height); + } else if (m_platformWindow->m_nsWindow) { // top level window, get window rect and flip y. NSRect rect = [self frame]; NSRect windowRect = [[self window] frame]; @@ -310,10 +324,9 @@ static QTouchDevice *touchDevice = 0; m_platformWindow->exposeWindow(); } else if (notificationName == NSWindowDidChangeScreenNotification) { if (m_window) { - QCocoaIntegration *ci = static_cast<QCocoaIntegration *>(QGuiApplicationPrivate::platformIntegration()); NSUInteger screenIndex = [[NSScreen screens] indexOfObject:self.window.screen]; if (screenIndex != NSNotFound) { - QCocoaScreen *cocoaScreen = ci->screenAtIndex(screenIndex); + QCocoaScreen *cocoaScreen = QCocoaIntegration::instance()->screenAtIndex(screenIndex); QWindowSystemInterface::handleWindowScreenChanged(m_window, cocoaScreen->screen()); } } @@ -551,14 +564,20 @@ static QTouchDevice *touchDevice = 0; QPointF qtWindowPoint; QPointF qtScreenPoint; - [self convertFromScreen:[NSEvent mouseLocation] toWindowPoint:&qtWindowPoint andScreenPoint:&qtScreenPoint]; + QNSView *targetView = self; + if (m_platformWindow && m_platformWindow->m_forwardWindow + && (theEvent.type == NSLeftMouseDragged || theEvent.type == NSLeftMouseUp)) { + targetView = m_platformWindow->m_forwardWindow->m_qtView; + } + + [targetView convertFromScreen:[NSEvent mouseLocation] toWindowPoint:&qtWindowPoint andScreenPoint:&qtScreenPoint]; ulong timestamp = [theEvent timestamp] * 1000; - QCocoaDrag* nativeDrag = static_cast<QCocoaDrag *>(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); nativeDrag->setLastMouseEvent(theEvent, self); Qt::KeyboardModifiers keyboardModifiers = [QNSView convertKeyModifiers:[theEvent modifierFlags]]; - QWindowSystemInterface::handleMouseEvent(m_window, timestamp, qtWindowPoint, qtScreenPoint, m_buttons, keyboardModifiers); + QWindowSystemInterface::handleMouseEvent(targetView->m_window, timestamp, qtWindowPoint, qtScreenPoint, m_buttons, keyboardModifiers); } - (void)handleFrameStrutMouseEvent:(NSEvent *)theEvent @@ -689,8 +708,18 @@ static QTouchDevice *touchDevice = 0; -(void)cursorUpdate:(NSEvent *)theEvent { Q_UNUSED(theEvent) - if (m_platformWindow->m_windowCursor) + // Set the cursor manually if there is no NSWindow. + if (!m_platformWindow->m_nsWindow && m_platformWindow->m_windowCursor) [m_platformWindow->m_windowCursor set]; + else + [super cursorUpdate:theEvent]; +} + +-(void)resetCursorRects +{ + // Use the cursor rect API if there is a NSWindow + if (m_platformWindow->m_nsWindow && m_platformWindow->m_windowCursor) + [self addCursorRect:[self visibleRect] cursor:m_platformWindow->m_windowCursor]; } - (void)mouseMoved:(NSEvent *)theEvent @@ -1619,7 +1648,7 @@ static QTabletEvent::TabletDevice wacomTabletDevice(NSEvent *theEvent) - (NSDragOperation) draggingSourceOperationMaskForLocal:(BOOL)isLocal { Q_UNUSED(isLocal); - QCocoaDrag* nativeDrag = static_cast<QCocoaDrag *>(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); return qt_mac_mapDropActions(nativeDrag->currentDrag()->supportedActions()); } @@ -1650,7 +1679,7 @@ static QTabletEvent::TabletDevice wacomTabletDevice(NSEvent *theEvent) QPlatformDragQtResponse response(false, Qt::IgnoreAction, QRect()); if ([sender draggingSource] != nil) { - QCocoaDrag* nativeDrag = static_cast<QCocoaDrag *>(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); response = QWindowSystemInterface::handleDrag(m_window, nativeDrag->platformDropData(), qt_windowPoint, qtAllowed); } else { QCocoaDropData mimeData([sender draggingPasteboard]); @@ -1678,14 +1707,14 @@ static QTabletEvent::TabletDevice wacomTabletDevice(NSEvent *theEvent) QPlatformDropQtResponse response(false, Qt::IgnoreAction); if ([sender draggingSource] != nil) { - QCocoaDrag* nativeDrag = static_cast<QCocoaDrag *>(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); response = QWindowSystemInterface::handleDrop(m_window, nativeDrag->platformDropData(), qt_windowPoint, qtAllowed); } else { QCocoaDropData mimeData([sender draggingPasteboard]); response = QWindowSystemInterface::handleDrop(m_window, &mimeData, qt_windowPoint, qtAllowed); } if (response.isAccepted()) { - QCocoaDrag* nativeDrag = static_cast<QCocoaDrag *>(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); nativeDrag->setAcceptedAction(response.acceptedAction()); } return response.isAccepted(); diff --git a/src/plugins/platforms/cocoa/qnsviewaccessibility.mm b/src/plugins/platforms/cocoa/qnsviewaccessibility.mm index e8f26aa8c4..a438950a55 100644 --- a/src/plugins/platforms/cocoa/qnsviewaccessibility.mm +++ b/src/plugins/platforms/cocoa/qnsviewaccessibility.mm @@ -45,7 +45,7 @@ #include "qcocoahelpers.h" #include "qcocoaaccessibility.h" #include "qcocoaaccessibilityelement.h" -#include <qpa/qplatformintegration.h> +#include "qcocoaintegration.h" #include <QtGui/qaccessible.h> #include <QtCore/QDebug> @@ -63,7 +63,7 @@ - (id)accessibilityAttributeValue:(NSString *)attribute { // activate accessibility updates - QGuiApplicationPrivate::platformIntegration()->accessibility()->setActive(true); + QCocoaIntegration::instance()->accessibility()->setActive(true); if ([attribute isEqualToString:NSAccessibilityRoleAttribute]) { if (m_window->accessibleRoot()) diff --git a/src/plugins/platforms/cocoa/qprintengine_mac.mm b/src/plugins/platforms/cocoa/qprintengine_mac.mm index f363b1772f..3e92a45a62 100644 --- a/src/plugins/platforms/cocoa/qprintengine_mac.mm +++ b/src/plugins/platforms/cocoa/qprintengine_mac.mm @@ -591,20 +591,43 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va return; switch (key) { - case PPK_CollateCopies: + + // The following keys are properties or derived values and so cannot be set + case PPK_PageRect: + break; + case PPK_PaperRect: + break; + case PPK_PaperSources: + break; + case PPK_SupportsMultipleCopies: + break; + case PPK_SupportedResolutions: break; + + // The following keys are settings that are unsupported by the Mac PrintEngine case PPK_ColorMode: break; - case PPK_Creator: + case PPK_CustomBase: break; - case PPK_DocumentName: + case PPK_Duplex: + // TODO Add support using PMSetDuplex / PMGetDuplex + break; + case PPK_FontEmbedding: break; case PPK_PageOrder: + // TODO Check if can be supported via Cups Options break; case PPK_PaperSource: + // TODO Check if can be supported via Cups Options + break; + case PPK_PrinterProgram: break; case PPK_SelectionOption: break; + case PPK_WindowsPageSize: + break; + + // The following keys are properties and settings that are supported by the Mac PrintEngine case PPK_Resolution: { PMPrinter printer; UInt32 count; @@ -633,7 +656,15 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va PMSessionValidatePageFormat(d->session(), d->format(), kPMDontWantBoolean); break; } - + case PPK_CollateCopies: + PMSetCollate(d->settings(), value.toBool()); + break; + case PPK_Creator: + d->m_creator = value.toString(); + break; + case PPK_DocumentName: + PMPrintSettingsSetJobName(d->settings(), QCFString(value.toString())); + break; case PPK_FullPage: d->fullPage = value.toBool(); break; @@ -642,18 +673,15 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va PMSetCopies(d->settings(), value.toInt(), false); break; case PPK_Orientation: { - if (d->state == QPrinter::Active) { - qWarning("QMacPrintEngine::setOrientation: Orientation cannot be changed during a print job, ignoring change"); - } else { - QPrinter::Orientation newOrientation = QPrinter::Orientation(value.toInt()); - if (d->hasCustomPaperSize && (d->orient != newOrientation)) - d->customSize = QSizeF(d->customSize.height(), d->customSize.width()); - d->orient = newOrientation; - PMOrientation o = d->orient == QPrinter::Portrait ? kPMPortrait : kPMLandscape; - PMSetOrientation(d->format(), o, false); - PMSessionValidatePageFormat(d->session(), d->format(), kPMDontWantBoolean); - } - break; } + QPrinter::Orientation newOrientation = QPrinter::Orientation(value.toInt()); + if (d->hasCustomPaperSize && (d->orient != newOrientation)) + d->customSize = QSizeF(d->customSize.height(), d->customSize.width()); + d->orient = newOrientation; + PMOrientation o = d->orient == QPrinter::Portrait ? kPMPortrait : kPMLandscape; + PMSetOrientation(d->format(), o, false); + PMSessionValidatePageFormat(d->session(), d->format(), kPMDontWantBoolean); + break; + } case PPK_OutputFileName: d->outputFilename = value.toString(); break; @@ -709,9 +737,7 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va d->hasCustomPageMargins = true; break; } - - default: - break; + // No default so that compiler will complain if new keys added and not handled in this engine } } @@ -724,16 +750,63 @@ QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const return *d->valueCache.find(key); switch (key) { - case PPK_CollateCopies: - ret = false; - break; + + // The following keys are settings that are unsupported by the Mac PrintEngine + // Return sensible default values to ensure consistent behavior across platforms case PPK_ColorMode: ret = QPrinter::Color; break; + case PPK_CustomBase: + // Special case, leave null + break; + case PPK_Duplex: + // TODO Add support using PMSetDuplex / PMGetDuplex + ret = QPrinter::DuplexNone; + break; + case PPK_FontEmbedding: + ret = false; + break; + case PPK_PageOrder: + // TODO Check if can be supported via Cups Options + ret = QPrinter::FirstPageFirst; + break; + case PPK_PaperSource: + // TODO Check if can be supported via Cups Options + ret = QPrinter::Auto; + break; + case PPK_PaperSources: { + // TODO Check if can be supported via Cups Options + QList<QVariant> out; + out << int(QPrinter::Auto); + ret = out; + break; + } + case PPK_PrinterProgram: + ret = QString(); + break; + case PPK_SelectionOption: + ret = QString(); + break; + case PPK_WindowsPageSize: + // Special case, leave null + break; + + // The following keys are properties and settings that are supported by the Mac PrintEngine + case PPK_CollateCopies: { + Boolean status; + PMGetCollate(d->settings(), &status); + ret = bool(status); + break; + } case PPK_Creator: + ret = d->m_creator; break; - case PPK_DocumentName: + case PPK_DocumentName: { + CFStringRef name; + PMPrintSettingsGetJobName(d->settings(), &name); + ret = QCFString::toQString(name); break; + } case PPK_FullPage: ret = d->fullPage; break; @@ -757,10 +830,6 @@ QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const case PPK_OutputFileName: ret = d->outputFilename; break; - case PPK_PageOrder: - break; - case PPK_PaperSource: - break; case PPK_PageRect: { // PageRect is returned in device pixels QRect r; @@ -855,8 +924,7 @@ QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const ret = margins; break; } - default: - break; + // No default so that compiler will complain if new keys added and not handled in this engine } return ret; } diff --git a/src/plugins/platforms/cocoa/qprintengine_mac_p.h b/src/plugins/platforms/cocoa/qprintengine_mac_p.h index 644a07184f..e3a8520811 100644 --- a/src/plugins/platforms/cocoa/qprintengine_mac_p.h +++ b/src/plugins/platforms/cocoa/qprintengine_mac_p.h @@ -125,6 +125,7 @@ public: NSPrintInfo *printInfo; PMResolution resolution; QString outputFilename; + QString m_creator; bool fullPage; QPaintEngine *paintEngine; bool hasCustomPaperSize; |