'Gtk - Intercept Mouse Button Events in Top Level Window

Keyboard events in Gtk propagates bottom up, starting from the top level window and then up the Z order. However mouse button events are first delivered to the widget at the top of the Z order and it is propagated down only if it is not handled there.

I am trying to implementing a UI lock feature where mouse button / touch events are to be ignored unless the user is clicking on the unlock button.

Is there any way in which I can intercept button presses at the top level window itself? I tried connecting to the signal_button_press_event with the after argument set to false. Even then the events are delivered to the child widget first.

Earlier I was using gdk event filters (Gdk::Window::add_filter) to do this however that works only on X11 backend and does not work for Wayland.

Updated after comment for adding minimal code

CTestWindow is a top level window derived from Gtk::Window. The button press signal is intercepted as below in the class constructor.

  signal_button_press_event().connect(sigc::mem_fun(*this,
                                                    &CTestWindow::HandleButtonPress),
                                      false);

This is how the handler is implemented

bool CTestWindow::HandleButtonPress(GdkEventButton *pEvent)
{
  bool bRet = false;

  /*
   * If the button press coordinate is outside the allocated area of the unlock button,
   * discard the event (by returning as processed).
   */
  if (!CCommon::IsWithinBounds(m_oUnlockButton.get_allocation(),
                               (int)pEvent->x,
                               (int)pEvent->y))
  {
    bRet = true;
  }

  return bRet;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source