From 5ed3ad6cc802b39b02262cdcf5c2be032ff2e5fe Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 10 Nov 2020 16:12:16 -0500 Subject: [PATCH] gtk-demo: Split off SvgPaintable Put the SvgPaintable implementation into its own source files, for ease of copying. --- demos/gtk-demo/demo.gresource.xml | 2 + demos/gtk-demo/meson.build | 1 + demos/gtk-demo/paintable_svg.c | 173 +----------------------------- demos/gtk-demo/svgpaintable.c | 166 ++++++++++++++++++++++++++++ demos/gtk-demo/svgpaintable.h | 13 +++ 5 files changed, 187 insertions(+), 168 deletions(-) create mode 100644 demos/gtk-demo/svgpaintable.c create mode 100644 demos/gtk-demo/svgpaintable.h diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml index 32b830a67b..a6e6e48a8f 100644 --- a/demos/gtk-demo/demo.gresource.xml +++ b/demos/gtk-demo/demo.gresource.xml @@ -208,6 +208,8 @@ demo3widget.ui + svgpaintable.h + svgpaintable.c org.gtk.gtk4.NodeEditor.Devel.svg diff --git a/demos/gtk-demo/meson.build b/demos/gtk-demo/meson.build index b14f1e04f2..fcddbde09e 100644 --- a/demos/gtk-demo/meson.build +++ b/demos/gtk-demo/meson.build @@ -139,6 +139,7 @@ librsvg_dep = dependency('librsvg-2.0', version: '>= 2.46.0', required: false) if librsvg_dep.found() demos += files('paintable_svg.c') + extra_demo_sources += files(['svgpaintable.c']) gtkdemo_deps += [ librsvg_dep ] endif diff --git a/demos/gtk-demo/paintable_svg.c b/demos/gtk-demo/paintable_svg.c index 975c650f93..eab506f4ed 100644 --- a/demos/gtk-demo/paintable_svg.c +++ b/demos/gtk-demo/paintable_svg.c @@ -9,183 +9,20 @@ #include #include -#define SVG_TYPE_PAINTABLE (svg_paintable_get_type ()) +#include "svgpaintable.h" -G_DECLARE_FINAL_TYPE (SvgPaintable, svg_paintable, SVG, PAINTABLE, GObject) - -struct _SvgPaintable -{ - GObject parent_instance; - GFile *file; - RsvgHandle *handle; -}; - -struct _SvgPaintableClass -{ - GObjectClass parent_class; -}; - -enum { - PROP_FILE = 1, - NUM_PROPERTIES -}; - -static void -svg_paintable_snapshot (GdkPaintable *paintable, - GdkSnapshot *snapshot, - double width, - double height) -{ - SvgPaintable *self = SVG_PAINTABLE (paintable); - cairo_t *cr; - GError *error = NULL; - - cr = gtk_snapshot_append_cairo (GTK_SNAPSHOT (snapshot), - &GRAPHENE_RECT_INIT (0, 0, width, height)); - - if (!rsvg_handle_render_document (self->handle, cr, - &(RsvgRectangle) {0, 0, width, height}, - &error)) - { - g_error ("%s", error->message); - } - - cairo_destroy (cr); -} - -static int -svg_paintable_get_intrinsic_width (GdkPaintable *paintable) -{ - SvgPaintable *self = SVG_PAINTABLE (paintable); - RsvgDimensionData data; - - rsvg_handle_get_dimensions (self->handle, &data); - - return data.width; -} - -static int -svg_paintable_get_intrinsic_height (GdkPaintable *paintable) -{ - SvgPaintable *self = SVG_PAINTABLE (paintable); - RsvgDimensionData data; - - rsvg_handle_get_dimensions (self->handle, &data); - - return data.height; -} - -static void -svg_paintable_init_interface (GdkPaintableInterface *iface) -{ - iface->snapshot = svg_paintable_snapshot; - iface->get_intrinsic_width = svg_paintable_get_intrinsic_width; - iface->get_intrinsic_height = svg_paintable_get_intrinsic_height; -} - -G_DEFINE_TYPE_WITH_CODE (SvgPaintable, svg_paintable, G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE, - svg_paintable_init_interface)) - -static void -svg_paintable_init (SvgPaintable *self) -{ -} - -static void -svg_paintable_dispose (GObject *object) -{ - SvgPaintable *self = SVG_PAINTABLE (object); - - g_clear_object (&self->file); - g_clear_object (&self->handle); - - G_OBJECT_CLASS (svg_paintable_parent_class)->dispose (object); -} - -static void -svg_paintable_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - SvgPaintable *self = SVG_PAINTABLE (object); - - switch (prop_id) - { - case PROP_FILE: - { - GFile *file = g_value_get_object (value); - RsvgHandle *handle = rsvg_handle_new_from_gfile_sync (file, - RSVG_HANDLE_FLAGS_NONE, - NULL, - NULL); - rsvg_handle_set_dpi (handle, 90); - - g_set_object (&self->file, file); - g_set_object (&self->handle, handle); - } - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - } -} - -static void -svg_paintable_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - SvgPaintable *self = SVG_PAINTABLE (object); - - switch (prop_id) - { - case PROP_FILE: - g_value_set_object (value, self->file); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - } -} - - -static void -svg_paintable_class_init (SvgPaintableClass *class) -{ - GObjectClass *object_class = G_OBJECT_CLASS (class); - - object_class->dispose = svg_paintable_dispose; - object_class->set_property = svg_paintable_set_property; - object_class->get_property = svg_paintable_get_property; - - g_object_class_install_property (object_class, PROP_FILE, - g_param_spec_object ("file", "File", "File", - G_TYPE_FILE, - G_PARAM_READWRITE)); -} - -static SvgPaintable * -svg_paintable_new (GFile *file) -{ - return g_object_new (SVG_TYPE_PAINTABLE, - "file", file, - NULL); -} static void file_set (GtkFileChooserButton *button, GtkWidget *picture) { GFile *file; - SvgPaintable *paintable; + GdkPaintable *paintable; file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (button)); paintable = svg_paintable_new (file); - gtk_picture_set_paintable (GTK_PICTURE (picture), GDK_PAINTABLE (paintable)); + gtk_picture_set_paintable (GTK_PICTURE (picture), paintable); g_object_unref (paintable); g_object_unref (file); @@ -201,7 +38,7 @@ do_paintable_svg (GtkWidget *do_widget) GtkFileFilter *filter; GtkWidget *button; GFile *file; - SvgPaintable *paintable; + GdkPaintable *paintable; if (!window) { @@ -228,7 +65,7 @@ do_paintable_svg (GtkWidget *do_widget) file = g_file_new_for_uri ("resource:///paintable_svg/org.gtk.gtk4.NodeEditor.Devel.svg"); paintable = svg_paintable_new (file); - gtk_picture_set_paintable (GTK_PICTURE (picture), GDK_PAINTABLE (paintable)); + gtk_picture_set_paintable (GTK_PICTURE (picture), paintable); g_object_unref (paintable); g_object_unref (file); } diff --git a/demos/gtk-demo/svgpaintable.c b/demos/gtk-demo/svgpaintable.c new file mode 100644 index 0000000000..21879ed0d9 --- /dev/null +++ b/demos/gtk-demo/svgpaintable.c @@ -0,0 +1,166 @@ +#include "svgpaintable.h" + +#include +#include + +struct _SvgPaintable +{ + GObject parent_instance; + GFile *file; + RsvgHandle *handle; +}; + +struct _SvgPaintableClass +{ + GObjectClass parent_class; +}; + +enum { + PROP_FILE = 1, + NUM_PROPERTIES +}; + +static void +svg_paintable_snapshot (GdkPaintable *paintable, + GdkSnapshot *snapshot, + double width, + double height) +{ + SvgPaintable *self = SVG_PAINTABLE (paintable); + cairo_t *cr; + GError *error = NULL; + + cr = gtk_snapshot_append_cairo (GTK_SNAPSHOT (snapshot), + &GRAPHENE_RECT_INIT (0, 0, width, height)); + + if (!rsvg_handle_render_document (self->handle, cr, + &(RsvgRectangle) {0, 0, width, height}, + &error)) + { + g_error ("%s", error->message); + } + + cairo_destroy (cr); +} + +static int +svg_paintable_get_intrinsic_width (GdkPaintable *paintable) +{ + SvgPaintable *self = SVG_PAINTABLE (paintable); + RsvgDimensionData data; + + rsvg_handle_get_dimensions (self->handle, &data); + + return data.width; +} + +static int +svg_paintable_get_intrinsic_height (GdkPaintable *paintable) +{ + SvgPaintable *self = SVG_PAINTABLE (paintable); + RsvgDimensionData data; + + rsvg_handle_get_dimensions (self->handle, &data); + + return data.height; +} + +static void +svg_paintable_init_interface (GdkPaintableInterface *iface) +{ + iface->snapshot = svg_paintable_snapshot; + iface->get_intrinsic_width = svg_paintable_get_intrinsic_width; + iface->get_intrinsic_height = svg_paintable_get_intrinsic_height; +} + +G_DEFINE_TYPE_WITH_CODE (SvgPaintable, svg_paintable, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE, + svg_paintable_init_interface)) + +static void +svg_paintable_init (SvgPaintable *self) +{ +} + +static void +svg_paintable_dispose (GObject *object) +{ + SvgPaintable *self = SVG_PAINTABLE (object); + + g_clear_object (&self->file); + g_clear_object (&self->handle); + + G_OBJECT_CLASS (svg_paintable_parent_class)->dispose (object); +} + +static void +svg_paintable_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + SvgPaintable *self = SVG_PAINTABLE (object); + + switch (prop_id) + { + case PROP_FILE: + { + GFile *file = g_value_get_object (value); + RsvgHandle *handle = rsvg_handle_new_from_gfile_sync (file, + RSVG_HANDLE_FLAGS_NONE, + NULL, + NULL); + rsvg_handle_set_dpi (handle, 90); + + g_set_object (&self->file, file); + g_set_object (&self->handle, handle); + } + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +svg_paintable_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + SvgPaintable *self = SVG_PAINTABLE (object); + + switch (prop_id) + { + case PROP_FILE: + g_value_set_object (value, self->file); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + + +static void +svg_paintable_class_init (SvgPaintableClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->dispose = svg_paintable_dispose; + object_class->set_property = svg_paintable_set_property; + object_class->get_property = svg_paintable_get_property; + + g_object_class_install_property (object_class, PROP_FILE, + g_param_spec_object ("file", "File", "File", + G_TYPE_FILE, + G_PARAM_READWRITE)); +} + +GdkPaintable * +svg_paintable_new (GFile *file) +{ + return g_object_new (SVG_TYPE_PAINTABLE, + "file", file, + NULL); +} diff --git a/demos/gtk-demo/svgpaintable.h b/demos/gtk-demo/svgpaintable.h new file mode 100644 index 0000000000..f6015e517e --- /dev/null +++ b/demos/gtk-demo/svgpaintable.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +G_BEGIN_DECLS + +#define SVG_TYPE_PAINTABLE (svg_paintable_get_type ()) + +G_DECLARE_FINAL_TYPE (SvgPaintable, svg_paintable, SVG, PAINTABLE, GObject) + +GdkPaintable * svg_paintable_new (GFile *file); + +G_END_DECLS