From 2fd3dbb00cc69c71d9d32d1103f095134ae9ce9d Mon Sep 17 00:00:00 2001 From: Morten Sorvig Date: Wed, 9 Nov 2011 10:35:11 +0100 Subject: [PATCH] Cocoa: Implement mouse and keyboard grab. Change-Id: Id53ff19d43213d79aaf8f1a6617a06ef9d0cfb4b Reviewed-by: Richard Moe Gustavsen --- src/plugins/platforms/cocoa/qcocoawindow.h | 17 ++++++++++++- src/plugins/platforms/cocoa/qcocoawindow.mm | 39 +++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoawindow.h b/src/plugins/platforms/cocoa/qcocoawindow.h index 4b9232d..87cd171 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.h +++ b/src/plugins/platforms/cocoa/qcocoawindow.h @@ -52,6 +52,20 @@ QT_BEGIN_NAMESPACE +@interface QNSWindow : NSWindow { + +} + +- (BOOL)canBecomeKeyWindow; + +@end + +@interface QNSPanel : QNSWindow { + +} + +@end + class QCocoaWindow : public QPlatformWindow { public: @@ -63,8 +77,9 @@ public: void setWindowTitle(const QString &title); void raise(); void lower(); - void propagateSizeHints(); + bool setKeyboardGrabEnabled(bool grab); + bool setMouseGrabEnabled(bool grab); WId winId() const; NSView *contentView() const; diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index 091c265..3c0c730 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -54,6 +54,23 @@ #include +@implementation QNSWindow + +- (BOOL)canBecomeKeyWindow +{ + + // 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. + return YES; +} + +@end + +@implementation QNSPanel + +@end + QCocoaWindow::QCocoaWindow(QWindow *tlw) : QPlatformWindow(tlw) , m_windowAttributes(0) @@ -163,6 +180,24 @@ void QCocoaWindow::propagateSizeHints() } } +bool QCocoaWindow::setKeyboardGrabEnabled(bool grab) +{ + if (grab && ![m_nsWindow isKeyWindow]) + [m_nsWindow makeKeyWindow]; + else if (!grab && [m_nsWindow isKeyWindow]) + [m_nsWindow resignKeyWindow]; + return true; +} + +bool QCocoaWindow::setMouseGrabEnabled(bool grab) +{ + if (grab && ![m_nsWindow isKeyWindow]) + [m_nsWindow makeKeyWindow]; + else if (!grab && [m_nsWindow isKeyWindow]) + [m_nsWindow resignKeyWindow]; + return true; +} + WId QCocoaWindow::winId() const { return WId(m_nsWindow); @@ -343,7 +378,7 @@ NSWindow * QCocoaWindow::createWindow() break; } - panel = [[NSPanel alloc] initWithContentRect:frame + panel = [[QNSPanel alloc] initWithContentRect:frame styleMask:m_windowAttributes backing:NSBackingStoreBuffered defer:NO]; // see window case below @@ -354,7 +389,7 @@ NSWindow * QCocoaWindow::createWindow() break; } default: - window = [[NSWindow alloc] initWithContentRect:frame + window = [[QNSWindow alloc] initWithContentRect:frame styleMask:(NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask) backing:NSBackingStoreBuffered defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up -- 2.7.4