How to detect when a window's position is changed in GTKmm 3?

I want to receive a signal every time a window has been moved either physically by the user or programmatically (e.g. Gtk::window::move) in X11.
I tried doing it by calling add_events(Gdk::STRUCTURE_MASK) for the Gtk::Window object and then connecting to its signal_configure_event, but the callback is not being called. I also tried connecting to signal_configure_event on a widget inside the window, but that did not work either.

Hello @Snowdrop!

This sample is working here on GNOME 43 (X11):

#include <gtkmm.h>

class MyAppWindow
 : public Gtk::ApplicationWindow
{
public:
  explicit MyAppWindow();

private:
  bool on_configure_event(GdkEventConfigure *e);
};

MyAppWindow::MyAppWindow()
{
  add_events(Gdk::STRUCTURE_MASK | Gdk::SUBSTRUCTURE_MASK);
  signal_configure_event().connect(
    sigc::mem_fun(*this, &MyAppWindow::on_configure_event));
}

bool
MyAppWindow::on_configure_event(GdkEventConfigure *e)
{
  g_print ("Configure event\n");

  return false; // propagate event
}

int main(int argc, char *argv[])
{
  auto app =
    Gtk::Application::create(argc, argv,
      "org.gtkmm.examples.base");

  MyAppWindow window;
  window.set_default_size(200, 200);

  return app->run(window);
}
1 Like

Note that Gtkmm defaults to a connect after behavior. I.e, by default connect() uses g_signal_connect_after. That’s quite surprising in general. To make it use g_signal_connect you can specify the second argument:

signal_configure_event().connect(
    sigc::mem_fun(*this, &MyAppWindow::on_configure_event),
    false /* do not connect after */);

That may help in case an handler blocks propagation

1 Like

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