[gnome-builder] meson-templates: improve gtkmm template
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] meson-templates: improve gtkmm template
- Date: Sun, 18 Jul 2021 18:35:40 +0000 (UTC)
commit 9e90becb03b2fb7b66e2110fdaf4e7c8cf0f349a
Author: Elnee <hsoftdima gmail com>
Date: Fri May 3 15:06:11 2019 +0300
meson-templates: improve gtkmm template
src/plugins/meson-templates/resources/src/main.cpp | 4 +--
.../meson-templates/resources/src/window.cpp | 41 +++++++++++++++-------
.../meson-templates/resources/src/window.hpp | 16 +++++----
.../meson-templates/resources/src/window.ui | 20 +++--------
4 files changed, 44 insertions(+), 37 deletions(-)
---
diff --git a/src/plugins/meson-templates/resources/src/main.cpp
b/src/plugins/meson-templates/resources/src/main.cpp
index f17ff4d89..58f8e5b9b 100644
--- a/src/plugins/meson-templates/resources/src/main.cpp
+++ b/src/plugins/meson-templates/resources/src/main.cpp
@@ -10,10 +10,8 @@ on_activate (Glib::RefPtr<Gtk::Application> app)
static std::unique_ptr<Gtk::Window> window;
if (!window) {
- window = std::make_unique<{{Prefix}}Window>();
+ window = {{Prefix}}Window::create();
window->property_application() = app;
- window->property_default_width() = 600;
- window->property_default_height() = 300;
app->add_window(*window);
}
diff --git a/src/plugins/meson-templates/resources/src/window.cpp
b/src/plugins/meson-templates/resources/src/window.cpp
index 44060f0d8..8ceedee1e 100644
--- a/src/plugins/meson-templates/resources/src/window.cpp
+++ b/src/plugins/meson-templates/resources/src/window.cpp
@@ -2,17 +2,34 @@
#include "{{prefix}}-window.h"
-{{Prefix}}Window::{{Prefix}}Window()
- : Glib::ObjectBase("{{Prefix}}Window")
- , Gtk::Window()
- , headerbar(nullptr)
- , label(nullptr)
+{{Prefix}}Window::{{Prefix}}Window(BaseObjectType* cobject,
+ const Glib::RefPtr<Gtk::Builder>& refBuilder)
+ : Gtk::ApplicationWindow(cobject)
+ , m_refBuilder(refBuilder)
+ , m_pLabel(nullptr)
{
- builder = Gtk::Builder::create_from_resource("{{appid_path}}/{{ui_file}}");
- builder->get_widget("headerbar", headerbar);
- builder->get_widget("label", label);
- add(*label);
- label->show();
- set_titlebar(*headerbar);
- headerbar->show();
+ // Just an example of getting widgets in derived ApplicationWindow constructor.
+ // You have to declare pointer in header file of needed type and initialize a
+ // member pointer to nullptr in constructor initializer list. But you have to
+ // do it only for widgets that you want to access or change from code.
+ //
+ // Here we just tilt the label by 30 degrees.
+ m_refBuilder->get_widget("label", m_pLabel);
+ m_pLabel->set_angle(30);
+}
+
+{{Prefix}}Window* {{Prefix}}Window::create()
+{
+ // Parse a resource file containing a GtkBuilder UI definition.
+ auto builder = Gtk::Builder::create_from_resource("{{appid_path}}/{{ui_file}}");
+
+ // Get ApplicationWindow that is specified in the UI file but
+ // implemented in our code. So our ApplicationWindow is derived.
+ {{Prefix}}Window* window = nullptr;
+ builder->get_widget_derived("appwindow", window);
+
+ if (!window)
+ throw std::runtime_error("No \"appwindow\" object {{ui_file}}");
+
+ return window;
}
diff --git a/src/plugins/meson-templates/resources/src/window.hpp
b/src/plugins/meson-templates/resources/src/window.hpp
index e1151cb66..28207f624 100644
--- a/src/plugins/meson-templates/resources/src/window.hpp
+++ b/src/plugins/meson-templates/resources/src/window.hpp
@@ -5,15 +5,19 @@
#include <gtkmm/builder.h>
#include <gtkmm/headerbar.h>
#include <gtkmm/label.h>
-#include <gtkmm/window.h>
+#include <gtkmm/applicationwindow.h>
-class {{Prefix}}Window : public Gtk::Window
+class {{Prefix}}Window : public Gtk::ApplicationWindow
{
public:
- {{Prefix}}Window();
+ // Due to convention of using Gtk::Builder::get_widget_derived()
+ // constructor of the class should look like this. You can read
+ // more about it in the reference.
+ {{Prefix}}Window(BaseObjectType* cobject,
+ const Glib::RefPtr<Gtk::Builder>& refBuilder);
+ static {{Prefix}}Window* create();
private:
- Gtk::HeaderBar *headerbar;
- Gtk::Label *label;
- Glib::RefPtr<Gtk::Builder> builder;
+ Glib::RefPtr<Gtk::Builder> m_refBuilder;
+ Gtk::Label* m_pLabel;
};
diff --git a/src/plugins/meson-templates/resources/src/window.ui
b/src/plugins/meson-templates/resources/src/window.ui
index 7522b7229..9443efccb 100644
--- a/src/plugins/meson-templates/resources/src/window.ui
+++ b/src/plugins/meson-templates/resources/src/window.ui
@@ -1,26 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.24"/>
-{{if language == "c++"}}
- <object class="GtkHeaderBar" id="headerbar">
- <property name="can_focus">False</property>
- <property name="title">Hello, World!</property>
- <property name="show_close_button">True</property>
- </object>
- <object class="GtkLabel" id="label">
- <property name="can_focus">False</property>
- <property name="label">Hello, World!</property>
- <attributes>
- <attribute name="weight" value="bold"/>
- <attribute name="scale" value="2"/>
- </attributes>
- </object>
-{{else}}
{{if language == "rust"}}
<object class="GtkApplicationWindow" id="window">
+ {{else if language == "c++"}}
+ <object class="GtkApplicationWindow" id="appwindow">
{{else}}
<template class="{{PreFix}}Window" parent="GtkApplicationWindow">
- {{end}}
<property name="default-width">600</property>
<property name="default-height">300</property>
<child type="titlebar">
@@ -46,6 +32,8 @@
</child>
{{if language == "rust"}}
</object>
+ {{else if language == "c++"}}
+ </object>
{{else}}
</template>
{{end}}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]