Joel
Joel

Reputation: 2035

Gtk::Window shows and exits while porting my gtkmm2 to gtkmm3 application

I'm porting my gtkmm2 to gtkmm3 application, this is what I have so far:

// The main.cxx:
#include "alarmui.hxx"

int main (int argc, char *argv[]) {
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm." PACKAGE_ID);
    alarm_ui win(app);
    app->run ();
    return 0;
}

Header:

// The alarmui.hxx
#ifndef ALARMUI_HXX_INC
#define ALARMUI_HXX_INC

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <gtkmm/statusicon.h>
#include <iostream>
#include <memory>
#include <functional>

class alarm_ui : public Gtk::Window
{
    private:
        Glib::RefPtr<Gtk::Application> _refApp;
        Glib::RefPtr<Gtk::StatusIcon> m_status_icon;
    public:
        alarm_ui (Glib::RefPtr<Gtk::Application>&);
        virtual ~alarm_ui ();
    protected:
        virtual bool delete_event (GdkEventAny*);
        void status_icon_activate_cb ();
};

#endif

source code:

#include "alarmui.hxx"

alarm_ui::alarm_ui (Glib::RefPtr<Gtk::Application>& refApp) : _refApp(refApp)
{
    std::cout << "init" << std::endl;
    set_icon_from_file (ICON_PNG_PATH);
    m_status_icon = Gtk::StatusIcon::create_from_file (ICON_PNG_PATH);
    m_status_icon->signal_activate().connect (std::bind(&alarm_ui::status_icon_activate_cb, this));
    show_all ();
}

alarm_ui::~alarm_ui () {
    std::cout << "done" << std::endl;
}

bool alarm_ui::delete_event (GdkEventAny* event) {
    return false;
}

void alarm_ui::status_icon_activate_cb () {
    if (get_visible ()) {
        iconify ();
        hide ();
    } else {
        deiconify ();
        show();
    }
}

I'm trying to show my window with a status icon. Toggle the window visibility, while clicking in the status icon. The code compiles fine but seems that when I execute the binary the constructor and destructor are invoked. If I use something like this:

Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm." PACKAGE_ID);
    alarm_ui win(app);
    app->run (win);

The windows shows, but...as expected, exits on hide() command...any ideas? Are hold() and release() my only options?

Upvotes: 0

Views: 181

Answers (1)

murrayc
murrayc

Reputation: 2123

By default, Gtk::Application::run() returns when all the Application's windows have been closed (hidden). Your window (win) will then be destroyed when it goes out of scope when your main() ends.

Gtk::Application::hold() and release() might indeed be what you need. Or maybe you can just do whatever you need to do after run() returns. I guess it depends on what you want to do and when.

Upvotes: 1

Related Questions