/*! \internal
    Returns true if the widget \a w is a logical sub window of the current
    top-level widget.
*/
bool qWidgetShortcutContextMatcher(QObject *object, Qt::ShortcutContext context)
{
    Q_ASSERT_X(object, "QShortcutMap", "Shortcut has no owner. Illegal map state!");

    QWidget *active_window = QApplication::activeWindow();

    // popups do not become the active window,
    // so we fake it here to get the correct context
    // for the shortcut system.
    if (QApplication::activePopupWidget())
        active_window = QApplication::activePopupWidget();

    if (!active_window) {
        QWindow *qwindow = QGuiApplication::focusWindow();
        if (qwindow && qwindow->isActive()) {
            while (qwindow) {
                QWidgetWindow *widgetWindow = qobject_cast<QWidgetWindow *>(qwindow);
                if (widgetWindow) {
                    active_window = widgetWindow->widget();
                    break;
                }
                qwindow = qwindow->parent();
            }
        }
    }

    if (!active_window)
        return false;

#ifndef QT_NO_ACTION
    if (QAction *a = qobject_cast<QAction *>(object))
        return correctActionContext(context, a, active_window);
#endif

#ifndef QT_NO_GRAPHICSVIEW
    if (QGraphicsWidget *gw = qobject_cast<QGraphicsWidget *>(object))
        return correctGraphicsWidgetContext(context, gw, active_window);
#endif

    QWidget *w = qobject_cast<QWidget *>(object);
    if (!w) {
        QShortcut *s = qobject_cast<QShortcut *>(object);
        if (s)
            w = s->parentWidget();
    }

    if (!w) {
        QWindow *qwindow = qobject_cast<QWindow *>(object);
        while (qwindow) {
            QWidgetWindow *widget_window = qobject_cast<QWidgetWindow *>(qwindow);
            if (widget_window) {
                w = widget_window->widget();
                break;
            }
            qwindow = qwindow->parent();
        }
    }

    if (!w)
        return false;

    return correctWidgetContext(context, w, active_window);
}