| 1 |
#include <gtkmm.h> |
| 2 |
#include <iostream> |
| 3 |
|
| 4 |
namespace |
| 5 |
{ |
| 6 |
Gtk::Window* pDialog = nullptr; |
| 7 |
Glib::RefPtr<Gtk::Application> app; |
| 8 |
|
| 9 |
void on_button_clicked() |
| 10 |
{ |
| 11 |
// When the application's only window is removed with |
| 12 |
// unset_application(), Gtk::Application::run() will end. |
| 13 |
if (pDialog) |
| 14 |
pDialog->unset_application(); |
| 15 |
} |
| 16 |
|
| 17 |
void on_app_activate() |
| 18 |
{ |
| 19 |
// Load the GtkBuilder file and instantiate its widgets: |
| 20 |
auto refBuilder = Gtk::Builder::create(); |
| 21 |
try |
| 22 |
{ |
| 23 |
refBuilder->add_from_file("basic.ui"); |
| 24 |
} |
| 25 |
catch(const Glib::FileError& ex) |
| 26 |
{ |
| 27 |
std::cerr << "FileError: " << ex.what() << std::endl; |
| 28 |
return; |
| 29 |
} |
| 30 |
catch(const Glib::MarkupError& ex) |
| 31 |
{ |
| 32 |
std::cerr << "MarkupError: " << ex.what() << std::endl; |
| 33 |
return; |
| 34 |
} |
| 35 |
catch(const Gtk::BuilderError& ex) |
| 36 |
{ |
| 37 |
std::cerr << "BuilderError: " << ex.what() << std::endl; |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
// Get the GtkBuilder-instantiated dialog: |
| 42 |
pDialog = refBuilder->get_widget<Gtk::Window>("DialogBasic"); |
| 43 |
if (!pDialog) |
| 44 |
{ |
| 45 |
std::cerr << "Could not get the dialog" << std::endl; |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// Get the GtkBuilder-instantiated button, and connect a signal handler: |
| 50 |
auto pButton = refBuilder->get_widget<Gtk::Button>("quit_button"); |
| 51 |
if (pButton) |
| 52 |
pButton->signal_clicked().connect([] () { on_button_clicked(); }); |
| 53 |
|
| 54 |
// It's not possible to delete widgets after app->run() has returned. |
| 55 |
// Delete the dialog with its child widgets before app->run() returns. |
| 56 |
app->signal_window_removed().connect([](Gtk::Window* window) { delete window; }); |
| 57 |
|
| 58 |
app->add_window(*pDialog); |
| 59 |
pDialog->set_visible(true); |
| 60 |
} |
| 61 |
} // anonymous namespace |
| 62 |
|
| 63 |
int main(int argc, char** argv) |
| 64 |
{ |
| 65 |
app = Gtk::Application::create("org.gtkmm.example"); |
| 66 |
|
| 67 |
// Instantiate a dialog when the application has been activated. |
| 68 |
// This can only be done after the application has been registered. |
| 69 |
// It's possible to call app->register_application() explicitly, but |
| 70 |
// usually it's easier to let app->run() do it for you. |
| 71 |
app->signal_activate().connect([] () { on_app_activate(); }); |
| 72 |
|
| 73 |
return app->run(argc, argv); |
| 74 |
} |