It completely works

This commit is contained in:
Pcornat 2024-10-29 17:44:21 +01:00
parent ad879dd7a3
commit cea2c1296f
Signed by: Pcornat
GPG Key ID: E0326CC678A00BDD
8 changed files with 698 additions and 58 deletions

View File

@ -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

61
app_win_2_back.cpp Normal file
View File

@ -0,0 +1,61 @@
#include "app_win_2_back.hpp"
#include "hello_world.hpp"
#include <iostream>
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);
add_action("quit", sigc::mem_fun(*this, &AppWin2Back::quit));
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 {
quit();
}
}
} // gui_to_app

31
app_win_2_back.hpp Normal file
View File

@ -0,0 +1,31 @@
#ifndef LEARNGTK4_APP_WIN_2_BACK_HPP
#define LEARNGTK4_APP_WIN_2_BACK_HPP
#include <gtkmm.h>
namespace learn_gtkmm4 {
class HelloWorld;
}
namespace gui_to_app {
class AppWin2Back final : public Gtk::Application {
public:
~AppWin2Back() noexcept final = default;
static Glib::RefPtr<AppWin2Back> 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

View File

@ -1,23 +1,8 @@
//
// Created by postaron on 27/09/24.
//
#include "hello_world.hpp"
#include <iostream>
#include <gtkmm/box.h>
namespace learn_gtkmm4 {
HelloWorld::HelloWorld(const Glib::RefPtr<Gio::Menu>& 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<Gtk::Builder> &builder) :
Gtk::ApplicationWindow(cobject), m_builder(builder) {
}
} // learn_gtkmm4

View File

@ -1,31 +1,26 @@
//
// Created by postaron on 27/09/24.
//
#ifndef LEARNGTK4_HELLO_WORLD_HPP
#define LEARNGTK4_HELLO_WORLD_HPP
#include <gtkmm/button.h>
#include <gtkmm/glarea.h>
#include <gtkmm/box.h>
#include <gtkmm/applicationwindow.h>
#include <gtkmm/popovermenubar.h>
#include <gtkmm/popovermenu.h>
#include <gtkmm/builder.h>
namespace Gtk {
class Box;
}
namespace learn_gtkmm4 {
class HelloWorld : public Gtk::ApplicationWindow {
class HelloWorld final : public Gtk::ApplicationWindow {
public:
explicit HelloWorld(const Glib::RefPtr<Gio::Menu>& menuModel);
~HelloWorld() override = default;
using Gtk::ApplicationWindow::BaseObjectType;
explicit HelloWorld(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &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<Gtk::Builder> m_builder;
Gtk::Box *m_ui_box{ nullptr };
};
} // learn_gtkmm4

View File

@ -1,24 +1,6 @@
#include "hello_world.hpp"
#include <gtkmm/application.h>
#include <gtkmm/builder.h>
#include <iostream>
#include <memory>
#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<Gio::Menu> {
try {
const auto builder = Gtk::Builder::create_from_file("menu_bar.ui");
return std::dynamic_pointer_cast<Gio::Menu>(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<learn_gtkmm4::HelloWorld>(argc, argv, menu_model);
auto app = gui_to_app::AppWin2Back::create();
return app->run(argc, argv);
}

View File

@ -28,7 +28,7 @@
</section>
<section>
<item>
<attribute name="action">file.quit</attribute>
<attribute name="action">app.quit</attribute>
<attribute name="icon">application-exit</attribute>
<attribute name="label" translatable="yes" context="It means Quit">Quitter</attribute>
</item>

584
window_ui.ui Normal file
View File

@ -0,0 +1,584 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.6"/>
<object class="GtkApplicationWindow" id="main_window">
<property name="default-height">500</property>
<property name="default-width">720</property>
<property name="child">
<object class="GtkBox" id="main_box">
<property name="orientation">vertical</property>
<child>
<object class="GtkBox">
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="justify">center</property>
<property name="label" translatable="1">HABILETÉ</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkGrid">
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label">Base</property>
<property name="valign">center</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="habilete_label_base">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Carac.</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="habilete_label_carac">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Matériel</property>
<layout>
<property name="column">0</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="habilete_label_materiel">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">2</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label">Total</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="habilete_label_total">
<layout>
<property name="column">2</property>
<property name="row">0</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
</object>
</child>
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="justify">center</property>
<property name="label" translatable="1">ENDURANCE</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkGrid">
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label">Base</property>
<property name="valign">center</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="endurance_label_base">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Carac.</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="endurance_label_carac">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Matériel</property>
<layout>
<property name="column">0</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="endurance_label_materiel">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">2</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label">Total</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="endurance_label_total">
<layout>
<property name="column">2</property>
<property name="row">0</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
</object>
</child>
<child>
<object class="GtkGrid">
<property name="hexpand">1</property>
<child>
<object class="GtkLabel">
<property name="hexpand">1</property>
<property name="justify">center</property>
<property name="label" translatable="1">MATÉRIEL</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkListBox" id="list_available_objects">
<property name="selection-mode">multiple</property>
<property name="show-separators">1</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="justify">center</property>
<property name="label" translatable="1">ADRESSE</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkGrid">
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label">Base</property>
<property name="valign">center</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="adresse_label_base">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Carac.</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="adresse_label_carac">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Matériel</property>
<layout>
<property name="column">0</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="adresse_label_materiel">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">2</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label">Total</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="adresse_label_total">
<layout>
<property name="column">2</property>
<property name="row">0</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
</object>
</child>
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="justify">center</property>
<property name="label" translatable="1">CHANCE</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkGrid">
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label">Base</property>
<property name="valign">center</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="chance_label_base">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Carac.</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="chance_label_carac">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Matériel</property>
<layout>
<property name="column">0</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="chance_label_materiel">
<property name="halign">end</property>
<layout>
<property name="column">1</property>
<property name="row">2</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label">Total</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="chance_label_total">
<layout>
<property name="column">2</property>
<property name="row">0</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkGrid">
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="hexpand">1</property>
<property name="justify">center</property>
<property name="label" translatable="1">STAT. SECONDAIRES</property>
<property name="valign">center</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkGrid">
<property name="column-spacing">5</property>
<child>
<object class="GtkLabel">
<property name="halign">center</property>
<property name="label" translatable="1">Dégâts</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="label_degat">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="justify">right</property>
<property name="lines">1</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="halign">center</property>
<property name="label" translatable="1">Armure</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="label_armure">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="justify">right</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Critique</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="label_critique">
<property name="halign">end</property>
<property name="hexpand">1</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<layout>
<property name="column">0</property>
<property name="row">3</property>
</layout>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</interface>