return 0;
}
+bool QPlatformWindow::setKeyboardGrabEnabled(bool grab)
+{
+ Q_UNUSED(grab);
+ qWarning("This plugin does not support grabbing the keyboard");
+ return false;
+}
+
+bool QPlatformWindow::setMouseGrabEnabled(bool grab)
+{
+ Q_UNUSED(grab);
+ qWarning("This plugin does not support grabbing the mouse");
+ return false;
+}
+
/*!
\class QPlatformWindow
\since 4.8
virtual void requestActivateWindow();
virtual QPlatformGLContext *glContext() const;
+
+ virtual bool setKeyboardGrabEnabled(bool grab);
+ virtual bool setMouseGrabEnabled(bool grab);
+
protected:
QScopedPointer<QPlatformWindowPrivate> d_ptr;
private:
return d->surface;
}
+bool QWindow::setKeyboardGrabEnabled(bool grab)
+{
+ Q_D(QWindow);
+ if (d->platformWindow)
+ return d->platformWindow->setKeyboardGrabEnabled(grab);
+ return false;
+}
+
+bool QWindow::setMouseGrabEnabled(bool grab)
+{
+ Q_D(QWindow);
+ if (d->platformWindow)
+ return d->platformWindow->setMouseGrabEnabled(grab);
+ return false;
+}
+
void QWindow::showMinimized()
{
qDebug() << "unimplemented:" << __FILE__ << __LINE__;
QPlatformWindow *handle() const;
QWindowSurface *surface() const;
+ bool setKeyboardGrabEnabled(bool grab);
+ bool setMouseGrabEnabled(bool grab);
+
public Q_SLOTS:
inline void show() { setVisible(true); }
inline void hide() { setVisible(false); }
m_syncValue.hi = 0;
}
}
+
+bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
+{
+ if (!grab) {
+ xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
+ return true;
+ }
+ xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
+ m_window, XCB_TIME_CURRENT_TIME,
+ GrabModeAsync, GrabModeAsync);
+ xcb_generic_error_t *err;
+ xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, &err);
+ bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
+ free(reply);
+ free(err);
+ return result;
+}
+
+bool QXcbWindow::setMouseGrabEnabled(bool grab)
+{
+ if (!grab) {
+ xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
+ return true;
+ }
+ xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
+ (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
+ | EnterWindowMask | LeaveWindowMask | PointerMotionMask),
+ GrabModeAsync, GrabModeAsync,
+ XCB_WINDOW_NONE, XCB_CURSOR_NONE,
+ XCB_TIME_CURRENT_TIME);
+ xcb_generic_error_t *err;
+ xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, &err);
+ bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
+ free(reply);
+ free(err);
+ return result;
+}
QPlatformGLContext *glContext() const;
+ bool setKeyboardGrabEnabled(bool grab);
+ bool setMouseGrabEnabled(bool grab);
+
xcb_window_t xcb_window() const { return m_window; }
uint depth() const { return m_depth; }
QImage::Format format() const { return m_format; }
static QString appName;
static QString appFont;
+static bool popupGrabOk;
extern bool app_do_modal;
extern QWidgetList *qt_modal_stack;
+extern QWidget *qt_button_down;
+extern QWidget *qt_popup_down;
+extern bool qt_replay_popup_mouse_event;
+int openPopupCount = 0;
QString QApplicationPrivate::appName() const
{
q->setActiveWindow(tlw);
}
+static void ungrabKeyboardForPopup(QWidget *popup)
+{
+ if (QWidget::keyboardGrabber())
+ qt_widget_private(QWidget::keyboardGrabber())->stealKeyboardGrab(true);
+ else
+ qt_widget_private(popup)->stealKeyboardGrab(false);
+}
+
+static void ungrabMouseForPopup(QWidget *popup)
+{
+ if (QWidget::mouseGrabber())
+ qt_widget_private(QWidget::mouseGrabber())->stealMouseGrab(true);
+ else
+ qt_widget_private(popup)->stealMouseGrab(false);
+}
+
+static void grabForPopup(QWidget *popup)
+{
+ Q_ASSERT(popup->testAttribute(Qt::WA_WState_Created));
+ popupGrabOk = qt_widget_private(popup)->stealKeyboardGrab(true);
+ if (popupGrabOk) {
+ popupGrabOk = qt_widget_private(popup)->stealMouseGrab(true);
+ if (!popupGrabOk) {
+ // transfer grab back to the keyboard grabber if any
+ ungrabKeyboardForPopup(popup);
+ }
+ }
+}
+
void QApplicationPrivate::closePopup(QWidget *popup)
{
Q_Q(QApplication);
return;
popupWidgets->removeAll(popup);
-//###
-// if (popup == qt_popup_down) {
-// qt_button_down = 0;
-// qt_popup_down = 0;
-// }
+ if (popup == qt_popup_down) {
+ qt_button_down = 0;
+ qt_popup_down = 0;
+ }
- if (QApplicationPrivate::popupWidgets->count() == 0) { // this was the last popup
+ if (QApplicationPrivate::popupWidgets->count() == 0) { // this was the last popup
delete QApplicationPrivate::popupWidgets;
QApplicationPrivate::popupWidgets = 0;
- //### replay mouse event?
-
- //### transfer/release mouse grab
+ if (popupGrabOk) {
+ popupGrabOk = false;
+
+ if (popup->geometry().contains(QPoint(QGuiApplicationPrivate::mousePressX,
+ QGuiApplicationPrivate::mousePressY))
+ || popup->testAttribute(Qt::WA_NoMouseReplay)) {
+ // mouse release event or inside
+ qt_replay_popup_mouse_event = false;
+ } else { // mouse press event
+ QGuiApplicationPrivate::mousePressTime -= 10000; // avoid double click
+ qt_replay_popup_mouse_event = true;
+ }
- //### transfer/release keyboard grab
+ // transfer grab back to mouse grabber if any, otherwise release the grab
+ ungrabMouseForPopup(popup);
- //give back focus
+ // transfer grab back to keyboard grabber if any, otherwise release the grab
+ ungrabKeyboardForPopup(popup);
+ }
if (active_window) {
if (QWidget *fw = active_window->focusWidget()) {
} else {
// A popup was closed, so the previous popup gets the focus.
-
QWidget* aw = QApplicationPrivate::popupWidgets->last();
if (QWidget *fw = aw->focusWidget())
fw->setFocus(Qt::PopupFocusReason);
- //### regrab the keyboard and mouse in case 'popup' lost the grab
-
-
+ if (QApplicationPrivate::popupWidgets->count() == 1) // grab mouse/keyboard
+ grabForPopup(aw);
}
}
-int openPopupCount = 0;
void QApplicationPrivate::openPopup(QWidget *popup)
{
openPopupCount++;
- if (!popupWidgets) { // create list
+ if (!popupWidgets) // create list
popupWidgets = new QWidgetList;
+ popupWidgets->append(popup); // add to end of list
- /* only grab if you are the first/parent popup */
- //#### ->grabMouse(popup,true);
- //#### ->grabKeyboard(popup,true);
- //### popupGrabOk = true;
- }
- popupWidgets->append(popup); // add to end of list
+ if (QApplicationPrivate::popupWidgets->count() == 1) // grab mouse/keyboard
+ grabForPopup(popup);
// popups are not focus-handled by the window system (the first
// popup grabbed the keyboard), so we have to do that manually: A
#elif defined(Q_WS_QPA) // <--------------------------------------------------------- QPA
void setMaxWindowState_helper();
void setFullScreenSize_helper();
+ bool stealKeyboardGrab(bool grab);
+ bool stealMouseGrab(bool grab);
#ifndef QT_NO_CURSOR
void updateCursor() const;
#endif
if (qt_mouseGrb)
qt_mouseGrb->releaseMouse();
- // XXX
- //qwsDisplay()->grabMouse(this,true);
+ if (windowHandle())
+ windowHandle()->setMouseGrabEnabled(true);
qt_mouseGrb = this;
qt_pressGrab = 0;
if (qt_mouseGrb)
qt_mouseGrb->releaseMouse();
- // XXX
- //qwsDisplay()->grabMouse(this,true);
- //qwsDisplay()->selectCursor(this, cursor.handle());
+ if (windowHandle())
+ windowHandle()->setMouseGrabEnabled(true);
+
qt_mouseGrb = this;
qt_pressGrab = 0;
}
#endif
+bool QWidgetPrivate::stealMouseGrab(bool grab)
+{
+ // This is like a combination of grab/releaseMouse() but with error checking
+ // and it has no effect on the result of mouseGrabber().
+ Q_Q(QWidget);
+ return q->windowHandle() ? q->windowHandle()->setMouseGrabEnabled(grab) : false;
+}
+
void QWidget::releaseMouse()
{
if (qt_mouseGrb == this) {
- // XXX
- //qwsDisplay()->grabMouse(this,false);
+ if (windowHandle())
+ windowHandle()->setMouseGrabEnabled(false);
qt_mouseGrb = 0;
}
}
{
if (keyboardGrb)
keyboardGrb->releaseKeyboard();
- // XXX
- //qwsDisplay()->grabKeyboard(this, true);
+ if (windowHandle())
+ windowHandle()->setKeyboardGrabEnabled(true);
keyboardGrb = this;
}
+bool QWidgetPrivate::stealKeyboardGrab(bool grab)
+{
+ // This is like a combination of grab/releaseKeyboard() but with error
+ // checking and it has no effect on the result of keyboardGrabber().
+ Q_Q(QWidget);
+ return q->windowHandle() ? q->windowHandle()->setKeyboardGrabEnabled(grab) : false;
+}
+
void QWidget::releaseKeyboard()
{
if (keyboardGrb == this) {
- // XXX
- //qwsDisplay()->grabKeyboard(this, false);
+ if (windowHandle())
+ windowHandle()->setKeyboardGrabEnabled(false);
keyboardGrb = 0;
}
}
QWidget *qt_button_down = 0; // widget got last button-down
// popup control
-static QWidget *qt_popup_down = 0; // popup that contains the pressed widget
+QWidget *qt_popup_down = 0; // popup that contains the pressed widget
extern int openPopupCount;
-static bool replayPopupMouseEvent = false;
+bool qt_replay_popup_mouse_event = false;
extern bool qt_try_modal(QWidget *widget, QEvent::Type type);
QWidgetWindow::QWidgetWindow(QWidget *widget)
if (popup->isEnabled()) {
// deliver event
- replayPopupMouseEvent = false;
+ qt_replay_popup_mouse_event = false;
QWidget *receiver = popup;
QPoint widgetPos = mapped;
if (qt_button_down)
}
if (qApp->activePopupWidget() != activePopupWidget
- && replayPopupMouseEvent) {
+ && qt_replay_popup_mouse_event) {
if (m_widget->windowType() != Qt::Popup)
qt_button_down = 0;
- replayPopupMouseEvent = false;
+ qt_replay_popup_mouse_event = false;
} else if (event->type() == QEvent::MouseButtonPress
&& event->button() == Qt::RightButton
&& (openPopupCount == oldOpenPopupCount)) {