Commit 4f5f1757 authored by James Westman's avatar James Westman Committed by Marcus Lundblad
Browse files

demos: Use UI files for the demo

- Separate the application window from the main file
- Build the window using a GtkBuilder UI file
parent 5f1b0666
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,
......
<?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>
/*
* 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));
}
/*
* 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
<?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>
/*
* 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[])
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment