when we can delete a class derived from Gtk::Window or others



I want to create some windows on demand with "new". If the user closes
the window, when I can call delete on that class derived from
Gtk::Dialog or Gtk::Window?

Example:
#include <iostream>
#include <string>
#include <gtkmm.h>


class ExampleWindow : public Gtk::Dialog
{
    protected:
        Gtk::Box m_VBox;
        Gtk::Button m_button;
        std::string mytext;
        Gtk::Frame frame;
        Gtk::Grid grid;
        Gtk::ScrolledWindow sw;

    public:
        ExampleWindow(const std::string& text_):
            m_VBox{ Gtk::ORIENTATION_VERTICAL }
        ,m_button{ text_ }
        ,mytext{ text_ }
        {
            set_title("Example");
            set_border_width(10);
            set_default_size(400, 200);

            get_vbox()->add(sw);
            sw.add(m_VBox);
            m_VBox.add( frame );
            frame.add( m_button );


m_button.signal_clicked().connect(sigc::mem_fun(this,&ExampleWindow::my_clicked));

            show_all_children();
        }

        void my_clicked()
        {
            ExampleWindow* win2 = new ExampleWindow("Stand Alone Win");
            win2->show();
        }

        virtual ~ExampleWindow() override
        {
            // Not called for the stand alone win while closing it. How
to achieve that?
            std::cout << "Destructor called for " << mytext << std::endl;
        }

        bool on_delete_event( GdkEventAny* ) override
        {
            std::cout << "sighandler on_delete called"  << mytext <<
std::endl;
            //delete this; // that did not work, the window did not hide!

            return false;
        }

        void on_hide() override
        {
            std::cout << "Hide event" << std::endl;
            delete this;    // is it safe to delete our self here?????
        }

};


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

    // Create win with new
    ExampleWindow* window = new ExampleWindow{ "Press to create new win" };

    //Shows the window and returns when it is closed.
    return app->run(*window);
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]