2024-10-29 17:44:21 +01:00
|
|
|
#include "app_win_2_back.hpp"
|
|
|
|
#include "hello_world.hpp"
|
|
|
|
#include <iostream>
|
2024-10-29 18:00:04 +01:00
|
|
|
#include <glibmm/miscutils.h>
|
2024-10-29 20:23:15 +01:00
|
|
|
#include <gtkmm/switch.h>
|
2024-10-29 17:44:21 +01:00
|
|
|
|
|
|
|
namespace gui_to_app {
|
|
|
|
AppWin2Back::AppWin2Back() : Gtk::Application("org.billy_adventures.character_sheet", Flags::HANDLES_OPEN) {
|
|
|
|
Glib::set_application_name("Billy's character sheet");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWin2Back::on_startup() {
|
|
|
|
Application::on_startup();
|
|
|
|
const auto menu_bar = []() -> Glib::RefPtr<Gio::Menu> {
|
|
|
|
try {
|
|
|
|
const auto builder = Gtk::Builder::create_from_file("menu_bar.ui");
|
|
|
|
return builder->get_object<Gio::Menu>("menu");
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
std::cerr << e.what() << '\n';
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (!menu_bar) {
|
|
|
|
std::cerr << "Error occured while loading menu bar's.\n";
|
|
|
|
} else {
|
|
|
|
set_menubar(menu_bar);
|
2024-10-29 18:00:04 +01:00
|
|
|
add_action("quit", sigc::mem_fun(*this, &AppWin2Back::on_quit));
|
2024-10-29 17:44:21 +01:00
|
|
|
set_accel_for_action("app.quit", "<Ctrl>q");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Glib::RefPtr<AppWin2Back> AppWin2Back::create() {
|
|
|
|
return Glib::make_refptr_for_instance(new AppWin2Back());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWin2Back::on_activate() {
|
|
|
|
Application::on_activate();
|
|
|
|
|
|
|
|
const auto app_builder = [this]() -> Glib::RefPtr<Gtk::Builder> {
|
|
|
|
try {
|
|
|
|
return Gtk::Builder::create_from_file("window_ui.ui");
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
quit();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (!app_builder) {
|
|
|
|
std::cerr << "Error while loading UI.\n";
|
|
|
|
quit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
main_window = Gtk::Builder::get_widget_derived<learn_gtkmm4::HelloWorld>(app_builder, "main_window");
|
|
|
|
if (main_window != nullptr) {
|
|
|
|
add_window(*main_window);
|
|
|
|
main_window->set_show_menubar(true);
|
|
|
|
main_window->set_visible(true);
|
|
|
|
} else {
|
2024-10-29 18:00:04 +01:00
|
|
|
on_quit();
|
2024-10-29 20:23:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sword_switch = app_builder->get_widget<Gtk::Switch>("sword_switch");
|
|
|
|
lance_switch = app_builder->get_widget<Gtk::Switch>("lance_switch");
|
|
|
|
morgen_switch = app_builder->get_widget<Gtk::Switch>("morgenstern_switch");
|
|
|
|
bow_switch = app_builder->get_widget<Gtk::Switch>("arc_switch");
|
|
|
|
chainmail_switch = app_builder->get_widget<Gtk::Switch>("chainmail_switch");
|
|
|
|
cookpot_switch = app_builder->get_widget<Gtk::Switch>("cookingpot_switch");
|
|
|
|
pamphlet_switch = app_builder->get_widget<Gtk::Switch>("pamphlet_tourist_switch");
|
|
|
|
medkit_switch = app_builder->get_widget<Gtk::Switch>("medkit_switch");
|
|
|
|
fourche_switch = app_builder->get_widget<Gtk::Switch>("fourche_switch");
|
|
|
|
dagger_switch = app_builder->get_widget<Gtk::Switch>("dagger_switch");
|
|
|
|
rock_kit_switch = app_builder->get_widget<Gtk::Switch>("rock_kit_switch");
|
|
|
|
sack_switch = app_builder->get_widget<Gtk::Switch>("sack_switch");
|
|
|
|
|
|
|
|
const auto test_result = [this]() -> std::vector<const char *> {
|
|
|
|
const std::array test_ptrs{
|
|
|
|
std::pair{ "sword_switch", sword_switch == nullptr },
|
|
|
|
std::pair{ "lance_switch", lance_switch == nullptr },
|
|
|
|
std::pair{ "morgen_switch", morgen_switch == nullptr },
|
|
|
|
std::pair{ "bow_switch", bow_switch == nullptr },
|
|
|
|
std::pair{ "chainmail_switch", chainmail_switch == nullptr },
|
|
|
|
std::pair{ "cookpot_switch", cookpot_switch == nullptr },
|
|
|
|
std::pair{ "pamphlet_switch", pamphlet_switch == nullptr },
|
|
|
|
std::pair{ "medkit_switch", medkit_switch == nullptr },
|
|
|
|
std::pair{ "fourche_switch", fourche_switch == nullptr },
|
|
|
|
std::pair{ "dagger_switch", dagger_switch == nullptr },
|
|
|
|
std::pair{ "rock_kit_switch", rock_kit_switch == nullptr },
|
|
|
|
std::pair{ "sack_switch", sack_switch == nullptr },
|
|
|
|
};
|
|
|
|
std::vector<const char *> test_result;
|
|
|
|
test_result.reserve(test_ptrs.size());
|
|
|
|
std::for_each(test_ptrs.cbegin(), test_ptrs.cend(), [&test_result](const auto &a) {
|
|
|
|
if (a.second) {
|
|
|
|
test_result.emplace_back(a.first);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return test_result;
|
|
|
|
}();
|
|
|
|
if (!test_result.empty()) {
|
|
|
|
std::cerr << "Error occurred, at least one switch is not available:\n";
|
|
|
|
for (const auto result: test_result) {
|
|
|
|
std::cerr << result << '\n';
|
|
|
|
}
|
|
|
|
on_quit();
|
|
|
|
return;
|
2024-10-29 18:00:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWin2Back::on_quit() {
|
|
|
|
auto windows = get_windows();
|
|
|
|
for (auto *window: windows) {
|
|
|
|
window->set_visible(false);
|
2024-10-29 20:22:28 +01:00
|
|
|
delete window;
|
2024-10-29 17:44:21 +01:00
|
|
|
}
|
2024-10-29 18:00:04 +01:00
|
|
|
quit();
|
2024-10-29 17:44:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // gui_to_app
|