[libshumate] demos: Use UI files for the demo
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libshumate] demos: Use UI files for the demo
- Date: Sat, 13 Mar 2021 21:54:27 +0000 (UTC)
commit 4f5f1757863ff4a1767f75bf28c34407b847979d
Author: James Westman <james jwestman net>
Date: Sat Feb 27 15:39:12 2021 -0600
demos: Use UI files for the demo
- Separate the application window from the main file
- Build the window using a GtkBuilder UI file
demos/meson.build | 11 +++-
demos/org.gnome.Shumate.Demo.gresources.xml | 6 +++
demos/shumate-demo-window.c | 83 +++++++++++++++++++++++++++++
demos/shumate-demo-window.h | 31 +++++++++++
demos/shumate-demo-window.ui | 24 +++++++++
demos/shumate-demo.c | 59 ++++----------------
6 files changed, 164 insertions(+), 50 deletions(-)
---
diff --git a/demos/meson.build b/demos/meson.build
index 02d85fa..7085e40 100644
--- a/demos/meson.build
+++ b/demos/meson.build
@@ -1,8 +1,17 @@
+shumate_demo_resources = gnome.compile_resources(
+ 'shumate-demo-resources',
+ 'org.gnome.Shumate.Demo.gresources.xml',
+ c_name: 'shumate_demo',
+)
+
shumate_demo_sources = [
+ shumate_demo_resources,
+
'shumate-demo.c',
+ 'shumate-demo-window.c',
]
-executable(
+shumate_demo = executable(
'shumate-demo',
shumate_demo_sources,
install: true,
diff --git a/demos/org.gnome.Shumate.Demo.gresources.xml b/demos/org.gnome.Shumate.Demo.gresources.xml
new file mode 100644
index 0000000..e27b991
--- /dev/null
+++ b/demos/org.gnome.Shumate.Demo.gresources.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+ <gresource prefix="/org/gnome/Shumate/Demo/ui">
+ <file preprocess="xml-stripblanks">shumate-demo-window.ui</file>
+ </gresource>
+</gresources>
diff --git a/demos/shumate-demo-window.c b/demos/shumate-demo-window.c
new file mode 100644
index 0000000..c30351d
--- /dev/null
+++ b/demos/shumate-demo-window.c
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2021 James Westman <james jwestman net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+
+#include "shumate-demo-window.h"
+
+struct _ShumateDemoWindow
+{
+ GtkApplicationWindow parent_instance;
+
+ ShumateView *view;
+ GtkOverlay *overlay;
+ ShumateLicense *license;
+};
+
+G_DEFINE_TYPE (ShumateDemoWindow, shumate_demo_window, GTK_TYPE_APPLICATION_WINDOW)
+
+
+ShumateDemoWindow *
+shumate_demo_window_new (GtkApplication *app)
+{
+ return g_object_new (SHUMATE_TYPE_DEMO_WINDOW,
+ "application", app,
+ NULL);
+}
+
+
+static void
+shumate_demo_window_class_init (ShumateDemoWindowClass *klass)
+{
+ GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+ gtk_widget_class_set_template_from_resource (widget_class,
"/org/gnome/Shumate/Demo/ui/shumate-demo-window.ui");
+ gtk_widget_class_bind_template_child (widget_class, ShumateDemoWindow, view);
+ gtk_widget_class_bind_template_child (widget_class, ShumateDemoWindow, overlay);
+ gtk_widget_class_bind_template_child (widget_class, ShumateDemoWindow, license);
+}
+
+
+static void
+shumate_demo_window_init (ShumateDemoWindow *self)
+{
+ g_autoptr(ShumateMapSourceFactory) factory = NULL;
+ g_autoptr(ShumateMapSource) map_source = NULL;
+ ShumateMapLayer *layer;
+ ShumateScale *scale;
+ ShumateViewport *viewport;
+
+ gtk_widget_init_template (GTK_WIDGET (self));
+
+ factory = shumate_map_source_factory_dup_default ();
+ map_source = shumate_map_source_factory_create_cached_source (factory, SHUMATE_MAP_SOURCE_OSM_MAPNIK);
+ shumate_view_set_map_source (self->view, map_source);
+
+ viewport = shumate_view_get_viewport (self->view);
+ shumate_viewport_set_reference_map_source (viewport, map_source);
+
+ layer = shumate_map_layer_new (map_source, viewport);
+ shumate_view_add_layer (self->view, SHUMATE_LAYER (layer));
+
+ shumate_license_append_map_source (self->license, map_source);
+
+ scale = shumate_scale_new (viewport);
+ g_object_set (scale,
+ "halign", GTK_ALIGN_START,
+ "valign", GTK_ALIGN_END,
+ NULL);
+ gtk_overlay_add_overlay (self->overlay, GTK_WIDGET (scale));
+}
diff --git a/demos/shumate-demo-window.h b/demos/shumate-demo-window.h
new file mode 100644
index 0000000..6e8c573
--- /dev/null
+++ b/demos/shumate-demo-window.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2021 James Westman <james jwestman net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+
+#pragma once
+
+#include <shumate/shumate.h>
+
+G_BEGIN_DECLS
+
+#define SHUMATE_TYPE_DEMO_WINDOW (shumate_demo_window_get_type())
+
+G_DECLARE_FINAL_TYPE (ShumateDemoWindow, shumate_demo_window, SHUMATE, DEMO_WINDOW, GtkApplicationWindow)
+
+ShumateDemoWindow *shumate_demo_window_new (GtkApplication *app);
+
+G_END_DECLS
diff --git a/demos/shumate-demo-window.ui b/demos/shumate-demo-window.ui
new file mode 100644
index 0000000..eb2edc9
--- /dev/null
+++ b/demos/shumate-demo-window.ui
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+ <requires lib="gtk" version="4.0"/>
+ <template class="ShumateDemoWindow" parent="GtkApplicationWindow">
+ <property name="can_focus">False</property>
+ <property name="title" translatable="yes">Shumate Demo</property>
+ <property name="default_width">800</property>
+ <property name="default_height">600</property>
+ <child>
+ <object class="GtkOverlay" id="overlay">
+ <child>
+ <object class="ShumateView" id="view">
+ </object>
+ </child>
+ <child type="overlay">
+ <object class="ShumateLicense" id="license">
+ <property name="halign">end</property>
+ <property name="valign">end</property>
+ </object>
+ </child>
+ </object>
+ </child>
+ </template>
+</interface>
diff --git a/demos/shumate-demo.c b/demos/shumate-demo.c
index 2b5e17f..8113cd7 100644
--- a/demos/shumate-demo.c
+++ b/demos/shumate-demo.c
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2010-2013 Jiri Techet <techet gmail com>
* Copyright (C) 2019 Marcus Lundblad <ml update uu se>
+ * Copyright (C) 2021 James Westman <james jwestman net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -13,66 +14,26 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
*/
-#include <gtk/gtk.h>
+
#include <shumate/shumate.h>
+#include "shumate-demo-window.h"
+
+
static void
activate (GtkApplication* app,
gpointer user_data)
{
- GtkWindow *window;
- GtkWidget *overlay;
- ShumateView *view;
- ShumateViewport *viewport;
- ShumateScale *scale;
- ShumateLicense *license;
- ShumatePathLayer *path_layer;
- ShumateMapSourceFactory *factory;
- ShumateMapSource *source;
- ShumateMapLayer *layer;
-
- /* Create the map view */
- overlay = gtk_overlay_new ();
- view = shumate_view_new ();
-
- viewport = shumate_view_get_viewport (view);
- factory = shumate_map_source_factory_dup_default ();
- source = shumate_map_source_factory_create_cached_source (factory, SHUMATE_MAP_SOURCE_OSM_MAPNIK);
- shumate_viewport_set_reference_map_source (viewport, source);
+ ShumateDemoWindow *window;
- layer = shumate_map_layer_new (source, viewport);
- shumate_view_add_layer (view, SHUMATE_LAYER (layer));
-
- gtk_overlay_set_child (GTK_OVERLAY (overlay), GTK_WIDGET (view));
- scale = shumate_scale_new (shumate_view_get_viewport (view));
- g_object_set (G_OBJECT (scale),
- "valign", GTK_ALIGN_END,
- "halign", GTK_ALIGN_START,
- NULL);
- gtk_overlay_add_overlay (GTK_OVERLAY (overlay), GTK_WIDGET (scale));
-
- license = shumate_license_new ();
- g_object_set (G_OBJECT (license),
- "valign", GTK_ALIGN_END,
- "halign", GTK_ALIGN_END,
- NULL);
- shumate_license_append_map_source (license, source);
- gtk_overlay_add_overlay (GTK_OVERLAY (overlay), GTK_WIDGET (license));
-
- path_layer = shumate_path_layer_new (viewport);
- shumate_view_add_layer (view, SHUMATE_LAYER (path_layer));
-
- window = GTK_WINDOW (gtk_application_window_new (app));
- gtk_window_set_title (window, "Window");
- gtk_window_set_default_size (window, 800, 600);
- gtk_window_set_child (window, GTK_WIDGET (overlay));
- gtk_window_present (window);
+ window = shumate_demo_window_new (app);
+ gtk_widget_show (GTK_WIDGET (window));
}
+
int
main (int argc, char *argv[])
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]