Gtk4::entry - catch mouse events

Good day,

how can I catch mouse button pressed/released events on a Gtk::Entry widget? I tried this:

...
Gtk::Entry *entry = Gtk::make_managed<Gtk::Entry>();
Glib::RefPtr<Gtk::GestureClick> m_first_button_click_controller;
sigc::connection m_first_button_click_connection;

m_VBox.append(*entry);

m_first_button_click_controller = Gtk::GestureClick::create();
m_first_button_click_controller->set_button(GDK_BUTTON_PRIMARY);
m_first_button_click_connection = m_first_button_click_controller->signal_pressed().connect(
		sigc::mem_fun(*this, &ExampleWindow::on_first_button_mouse_pressed));

entry->add_controller(m_first_button_click_controller);
...

void
ExampleWindow::on_first_button_mouse_pressed(int n_pos, double x, double y)
{
	std::cout << __PRETTY_FUNCTION__ << std::endl;
}

Unfortunately, unlike with other Widgets, this does not work with a Gtk::Entry. Does someone have a hint what I’m missing here?

Thanks and best regards,
Christian

Hi,

The GtkEntry already catches and consumes click events, so your controller won’t be notified.

You have to set the propagation phase of m_first_button_click_controller to GTK_PHASE_CAPTURE to be able to catch the events before the GtkEntry default controller does.

More info about event propagation here: Gtk – 4.0: Overview of GTK input and event handling

1 Like

See also the example at examples/book/events/mouse/examplewindow.cc · master · GNOME / gtkmm-documentation · GitLab
where the controller is added to a Gtk::Button.

  mouse_click = Gtk::GestureClick::create();
  // This mouse event must be handled in the capture phase.
  // The GtkButton C class handles the event in the capture phase and
  // does not let it propagate to the bubble phase where events are
  // handled by default.
  mouse_click->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
1 Like

Thank you!

Best regards,
Christian

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.