Cocoa: Deliver key event to top-level QWidgetWindow

Having several QWidgetWindow in our hierarchy translates as as many NSViews.
Clicking will make the NSView under the mouse cursor key, meaning it will
receive all the Cocoa key events. In order to make sure the QWidgets hierarchy
sees the key event "as usual," we climb the QWindow hierarchy in search for the
top-level QWidgetWindow. (Something similar is already being done in -[QNSView
becomeFirstResponder]).

Task-number: QTBUG-32914
Change-Id: Idc700309d202820de326d4e2990fad24d7b692ae
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
This commit is contained in:
Gabriel de Dietrich 2013-09-30 16:40:55 +02:00 committed by The Qt Project
parent 5b88f7b0ac
commit 42f0a4f2c9

View File

@ -208,6 +208,22 @@ static QTouchDevice *touchDevice = 0;
if ([self window])
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:[self window]];
}
- (QWindow *)topLevelWindow
{
QWindow *focusWindow = m_window;
// For widgets we need to do a bit of trickery as the window
// to activate is the window of the top-level widget.
if (m_window->metaObject()->className() == QStringLiteral("QWidgetWindow")) {
while (focusWindow->parent()) {
focusWindow = focusWindow->parent();
}
}
return focusWindow;
}
- (void)updateGeometry
{
QRect geometry;
@ -457,16 +473,7 @@ static QTouchDevice *touchDevice = 0;
{
if (m_window->flags() & Qt::WindowTransparentForInput)
return NO;
QWindow *focusWindow = m_window;
// For widgets we need to do a bit of trickery as the window
// to activate is the window of the top-level widget.
if (m_window->metaObject()->className() == QStringLiteral("QWidgetWindow")) {
while (focusWindow->parent()) {
focusWindow = focusWindow->parent();
}
}
QWindowSystemInterface::handleWindowActivated(focusWindow);
QWindowSystemInterface::handleWindowActivated([self topLevelWindow]);
return YES;
}
@ -1124,10 +1131,12 @@ static QTabletEvent::TabletDevice wacomTabletDevice(NSEvent *theEvent)
if (ch.unicode() < 0xf700 || ch.unicode() > 0xf8ff)
text = QCFString::toQString(characters);
QWindow *focusWindow = [self topLevelWindow];
if (eventType == QEvent::KeyPress) {
if (m_composingText.isEmpty())
m_sendKeyEvent = !QWindowSystemInterface::tryHandleShortcutEvent(m_window, timestamp, keyCode, modifiers, text);
m_sendKeyEvent = !QWindowSystemInterface::tryHandleShortcutEvent(focusWindow, timestamp, keyCode, modifiers, text);
QObject *fo = QGuiApplication::focusObject();
if (m_sendKeyEvent && fo) {
@ -1144,7 +1153,7 @@ static QTabletEvent::TabletDevice wacomTabletDevice(NSEvent *theEvent)
}
if (m_sendKeyEvent && m_composingText.isEmpty())
QWindowSystemInterface::handleExtendedKeyEvent(m_window, timestamp, QEvent::Type(eventType), keyCode, modifiers,
QWindowSystemInterface::handleExtendedKeyEvent(focusWindow, timestamp, QEvent::Type(eventType), keyCode, modifiers,
nativeScanCode, nativeVirtualKey, nativeModifiers, text, [nsevent isARepeat]);
m_sendKeyEvent = false;