Question

Because I´m writting a "generic" application behaving completely different when facing other configurations, I´m forced to show gtk windows even if I dont yet know them at startup. There might also be the requirement that multiple windows need to be visisble (not modal dialogs but standalone windows) at the same time. But, it would be great if one can simply start one gtk event loop at startup.

Is is somehow possible to add windows to that loop after it has been started? While I found the Gtk::Application class which seems to support exactly the indented behaviour I´m restricted to use the Gtk::Main class.

Was it helpful?

Solution

There's only a single Gtk::Main object allowed. Widgets should be created in the same thread the main event loop is being run in. To work around this limitation you need to develop a way to pass your window creation commands to the gtk thread.

The simplest way is to use Glib::Dispatcher

struct WindowBuilder {
    /**/
    Glib::Dispatcher* signal_create;

    void create_window() {
        //From main thread...
        signal_create->emit();
    }
}

void create_mainWnd() {
    new Ui::MainWnd();
}

//From Gtk thread...
builder->signal_create->connect(sigc::ptr_fun(create_mainWnd));

Gtk::Main::run();

Glib::Dispatcher doesn't take any arguments with it, so next step is to figure out how to pass arguments around between threads.

For different types of windows you can just use different disptachers.

boost::asio::io_service can help you pass messages around.

while(!exit) {
    io_service.reset();
    io_service.poll();
    while(Gtk::Main::events_pending())
        Gtk::Main::iteration();
    Sleep(0);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top