Search code examples
gtkgtk3gtkmmgtkmm3

gtkmm catch widget destruction event


I would like to add a sigc callback to the destruction of a widget. How can I connect so that when the widget is destroyed, my callback is called? There is no such signal on Gtk::Widget. Can I do it using C API?

Note : I use gtkmm 3.24.5.


Solution

  • I have not found documentation specific to 3.24.5. However, in 3.24.4, Gtk::Widgets inherits from sigc::trackable which exposes a sigc::trackable::add_destroy_notify_callback method. It seems to be what you are looking after. Here is a quick example to show how it works (Build with Gtkmm 3.24.20):

    #include <iostream>
    #include <gtkmm.h>
    
    void* DestructorCallback(void* p_in)
    {
        std::cout << "Button destruction!" << std::endl;
        return nullptr;
    }
    
    class MainWindow : public Gtk::ApplicationWindow
    {
    
    public:
    
        MainWindow();
    
    private:
    
        Gtk::Button m_button;
    
    };
    
    MainWindow::MainWindow()
    : m_button{"Hello World!"}
    {
        m_button.add_destroy_notify_callback(nullptr, DestructorCallback);
    }
    
    int main(int argc, char *argv[])
    {
        auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
      
        MainWindow window;
        window.show_all();
      
        return app->run(window);
    }
    

    The documentation does not say much (in fact, it says nothing) about what is supposed to be p_in as well as the void* return value from the callback though...

    Hope this works in Gtkmm 3.24.5.