From cea2c1296ffa85b11e9beecdd3551e5512fe705e Mon Sep 17 00:00:00 2001 From: Pcornat Date: Tue, 29 Oct 2024 17:44:21 +0100 Subject: [PATCH] It completely works --- CMakeLists.txt | 2 + app_win_2_back.cpp | 61 +++++ app_win_2_back.hpp | 31 +++ hello_world.cpp | 21 +- hello_world.hpp | 31 +-- main.cpp | 24 +- menu_bar.ui | 2 +- window_ui.ui | 584 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 698 insertions(+), 58 deletions(-) create mode 100644 app_win_2_back.cpp create mode 100644 app_win_2_back.hpp create mode 100644 window_ui.ui diff --git a/CMakeLists.txt b/CMakeLists.txt index c7d1696..f258d06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,8 @@ add_subdirectory(external/assimp EXCLUDE_FROM_ALL) add_executable(LearnGtk4 main.cpp hello_world.cpp hello_world.hpp + app_win_2_back.cpp + app_win_2_back.hpp ) set_target_properties(LearnGtk4 assimp PROPERTIES diff --git a/app_win_2_back.cpp b/app_win_2_back.cpp new file mode 100644 index 0000000..640c650 --- /dev/null +++ b/app_win_2_back.cpp @@ -0,0 +1,61 @@ +#include "app_win_2_back.hpp" +#include "hello_world.hpp" +#include + +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 { + try { + const auto builder = Gtk::Builder::create_from_file("menu_bar.ui"); + return builder->get_object("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); + add_action("quit", sigc::mem_fun(*this, &AppWin2Back::quit)); + set_accel_for_action("app.quit", "q"); + } + } + + Glib::RefPtr AppWin2Back::create() { + return Glib::make_refptr_for_instance(new AppWin2Back()); + } + + void AppWin2Back::on_activate() { + Application::on_activate(); + + const auto app_builder = [this]() -> Glib::RefPtr { + 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(app_builder, "main_window"); + if (main_window != nullptr) { + add_window(*main_window); + main_window->set_show_menubar(true); + main_window->set_visible(true); + } else { + quit(); + } + } + +} // gui_to_app \ No newline at end of file diff --git a/app_win_2_back.hpp b/app_win_2_back.hpp new file mode 100644 index 0000000..bd743c5 --- /dev/null +++ b/app_win_2_back.hpp @@ -0,0 +1,31 @@ +#ifndef LEARNGTK4_APP_WIN_2_BACK_HPP +#define LEARNGTK4_APP_WIN_2_BACK_HPP + +#include + +namespace learn_gtkmm4 { + class HelloWorld; +} + +namespace gui_to_app { + + class AppWin2Back final : public Gtk::Application { + public: + ~AppWin2Back() noexcept final = default; + + static Glib::RefPtr create(); + + protected: + AppWin2Back(); + + void on_startup() final; + + void on_activate() final; + + private: + learn_gtkmm4::HelloWorld *main_window{ nullptr }; + }; + +} // gui_to_app + +#endif //LEARNGTK4_APP_WIN_2_BACK_HPP diff --git a/hello_world.cpp b/hello_world.cpp index 52fb608..804d80d 100644 --- a/hello_world.cpp +++ b/hello_world.cpp @@ -1,23 +1,8 @@ -// -// Created by postaron on 27/09/24. -// - #include "hello_world.hpp" -#include +#include namespace learn_gtkmm4 { - HelloWorld::HelloWorld(const Glib::RefPtr& menuModel) : - m_VBox(Gtk::Orientation::VERTICAL), m_menu_bar(menuModel), m_button("Hello world!") { - set_title("Hello world!"); - set_default_size(1280, 720); - m_button.set_margin(10); - m_button.signal_clicked().connect(sigc::mem_fun(*this, &HelloWorld::on_button_clicked)); - m_VBox.append(m_menu_bar); - m_VBox.append(m_button); - set_child(m_VBox); - } - - void HelloWorld::on_button_clicked() { - std::cout << "Hello World" << std::endl; + HelloWorld::HelloWorld(BaseObjectType *cobject, const Glib::RefPtr &builder) : + Gtk::ApplicationWindow(cobject), m_builder(builder) { } } // learn_gtkmm4 \ No newline at end of file diff --git a/hello_world.hpp b/hello_world.hpp index 301a9c3..771a7d0 100644 --- a/hello_world.hpp +++ b/hello_world.hpp @@ -1,31 +1,26 @@ -// -// Created by postaron on 27/09/24. -// - #ifndef LEARNGTK4_HELLO_WORLD_HPP #define LEARNGTK4_HELLO_WORLD_HPP -#include -#include -#include #include -#include -#include +#include + +namespace Gtk { + class Box; +} namespace learn_gtkmm4 { - class HelloWorld : public Gtk::ApplicationWindow { + class HelloWorld final : public Gtk::ApplicationWindow { public: - explicit HelloWorld(const Glib::RefPtr& menuModel); - ~HelloWorld() override = default; + using Gtk::ApplicationWindow::BaseObjectType; + + explicit HelloWorld(BaseObjectType *cobject, const Glib::RefPtr &builder); + + ~HelloWorld() final = default; protected: - void on_button_clicked(); - - Gtk::Box m_VBox; - Gtk::PopoverMenuBar m_menu_bar; - Gtk::Button m_button; - Gtk::GLArea gl_area; + Glib::RefPtr m_builder; + Gtk::Box *m_ui_box{ nullptr }; }; } // learn_gtkmm4 diff --git a/main.cpp b/main.cpp index 1d35092..fe0d3eb 100644 --- a/main.cpp +++ b/main.cpp @@ -1,24 +1,6 @@ -#include "hello_world.hpp" -#include -#include -#include -#include +#include "app_win_2_back.hpp" int main(int argc, char *argv[]) { - auto app = Gtk::Application::create("org.gtkmm.example"); - - const auto menu_model = []() -> Glib::RefPtr { - try { - const auto builder = Gtk::Builder::create_from_file("menu_bar.ui"); - return std::dynamic_pointer_cast(builder->get_object("menu")); - } catch (const std::exception &e) { - std::cerr << e.what() << '\n'; - return nullptr; - } - }(); - if (menu_model == nullptr) { - std::cerr << "Menu model object is null. Down-cast failed.\n"; - return EXIT_FAILURE; - } - return app->make_window_and_run(argc, argv, menu_model); + auto app = gui_to_app::AppWin2Back::create(); + return app->run(argc, argv); } diff --git a/menu_bar.ui b/menu_bar.ui index ee6743d..b2aa985 100644 --- a/menu_bar.ui +++ b/menu_bar.ui @@ -28,7 +28,7 @@
- file.quit + app.quit application-exit Quitter diff --git a/window_ui.ui b/window_ui.ui new file mode 100644 index 0000000..649e932 --- /dev/null +++ b/window_ui.ui @@ -0,0 +1,584 @@ + + + + + 500 + 720 + + + vertical + + + + + vertical + + + + + center + HABILETÉ + + 0 + 0 + + + + + + + + + + start + Base + center + + 0 + 0 + + + + + + end + + 1 + 0 + + + + + + start + Carac. + + 0 + 1 + + + + + + end + + 1 + 1 + + + + + + start + Matériel + + 0 + 2 + + + + + + end + + 1 + 2 + + + + + 0 + 0 + + + + + + Total + + 1 + 0 + + + + + + + 2 + 0 + + + + + 0 + 1 + + + + + + + + + + center + ENDURANCE + + 0 + 0 + + + + + + + + + + start + Base + center + + 0 + 0 + + + + + + end + + 1 + 0 + + + + + + start + Carac. + + 0 + 1 + + + + + + end + + 1 + 1 + + + + + + start + Matériel + + 0 + 2 + + + + + + end + + 1 + 2 + + + + + 0 + 0 + + + + + + Total + + 1 + 0 + + + + + + + 2 + 0 + + + + + 0 + 1 + + + + + + + + 1 + + + 1 + center + MATÉRIEL + + 0 + 0 + + + + + + multiple + 1 + + 0 + 1 + + + + + + + + + + vertical + + + + + center + ADRESSE + + 0 + 0 + + + + + + + + + + start + Base + center + + 0 + 0 + + + + + + end + + 1 + 0 + + + + + + start + Carac. + + 0 + 1 + + + + + + end + + 1 + 1 + + + + + + start + Matériel + + 0 + 2 + + + + + + end + + 1 + 2 + + + + + 0 + 0 + + + + + + Total + + 1 + 0 + + + + + + + 2 + 0 + + + + + 0 + 1 + + + + + + + + + + center + CHANCE + + 0 + 0 + + + + + + + + + + start + Base + center + + 0 + 0 + + + + + + end + + 1 + 0 + + + + + + start + Carac. + + 0 + 1 + + + + + + end + + 1 + 1 + + + + + + start + Matériel + + 0 + 2 + + + + + + end + + 1 + 2 + + + + + 0 + 0 + + + + + + Total + + 1 + 0 + + + + + + + 2 + 0 + + + + + 0 + 1 + + + + + + + + + + + + + + 1 + center + STAT. SECONDAIRES + center + + 0 + 0 + + + + + 0 + 0 + + + + + + 5 + + + center + Dégâts + + 0 + 0 + + + + + + end + 1 + right + 1 + + 1 + 0 + + + + + 0 + 1 + + + + + + + + center + Armure + + 0 + 0 + + + + + + end + 1 + right + + 1 + 0 + + + + + 0 + 2 + + + + + + + + Critique + + 0 + 0 + + + + + + end + 1 + + 1 + 0 + + + + + 0 + 3 + + + + + + + + + + +