[gimp] libgimp: add GimpImageProcedure with (run_mode, image, drawable) args



commit 98bfe065e8c8e3e6d9c455bee0c6d6d16614610a
Author: Michael Natterer <mitch gimp org>
Date:   Sun Aug 18 12:46:07 2019 +0200

    libgimp: add GimpImageProcedure with (run_mode, image, drawable) args
    
    to save the boulerplate of handlin these 3 arguments in a lot of
    plug-ins.

 libgimp/Makefile.gi          |   2 +
 libgimp/gimp.h               |   1 +
 libgimp/gimpimageprocedure.c | 207 +++++++++++++++++++++++++++++++++++++++++++
 libgimp/gimpimageprocedure.h |  93 +++++++++++++++++++
 4 files changed, 303 insertions(+)
---
diff --git a/libgimp/Makefile.gi b/libgimp/Makefile.gi
index 584f12d86e..d8f20b493c 100644
--- a/libgimp/Makefile.gi
+++ b/libgimp/Makefile.gi
@@ -118,6 +118,7 @@ libgimp_introspectable_headers = \
        ../libgimp/gimpgradientselect.h         \
        ../libgimp/gimpimage.h                  \
        ../libgimp/gimpimagecolorprofile.h      \
+       ../libgimp/gimpimageprocedure.h         \
        ../libgimp/gimplayer.h                  \
        ../libgimp/gimploadprocedure.h          \
        ../libgimp/gimppaletteselect.h          \
@@ -144,6 +145,7 @@ libgimp_introspectable = \
        ../libgimp/gimpgradientselect.c         \
        ../libgimp/gimpimage.c                  \
        ../libgimp/gimpimagecolorprofile.c      \
+       ../libgimp/gimpimageprocedure.c         \
        ../libgimp/gimplayer.c                  \
        ../libgimp/gimploadprocedure.c          \
        ../libgimp/gimppaletteselect.c          \
diff --git a/libgimp/gimp.h b/libgimp/gimp.h
index 846d8758cd..b536ca6cdc 100644
--- a/libgimp/gimp.h
+++ b/libgimp/gimp.h
@@ -43,6 +43,7 @@
 #include <libgimp/gimpgradientselect.h>
 #include <libgimp/gimpimage.h>
 #include <libgimp/gimpimagecolorprofile.h>
+#include <libgimp/gimpimageprocedure.h>
 #include <libgimp/gimplayer.h>
 #include <libgimp/gimploadprocedure.h>
 #include <libgimp/gimplegacy.h>
diff --git a/libgimp/gimpimageprocedure.c b/libgimp/gimpimageprocedure.c
new file mode 100644
index 0000000000..c300bf1e07
--- /dev/null
+++ b/libgimp/gimpimageprocedure.c
@@ -0,0 +1,207 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpimageprocedure.c
+ * Copyright (C) 2019 Michael Natterer <mitch gimp org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#define GIMP_DISABLE_COMPAT_CRUFT
+
+#include "gimp.h"
+
+#include "gimpimageprocedure.h"
+
+
+struct _GimpImageProcedurePrivate
+{
+  GimpRunImageFunc run_func;
+  gpointer         run_data;
+  GDestroyNotify   run_data_destroy;
+};
+
+
+static void   gimp_image_procedure_constructed (GObject              *object);
+static void   gimp_image_procedure_finalize    (GObject              *object);
+
+static GimpValueArray *
+              gimp_image_procedure_run         (GimpProcedure        *procedure,
+                                                const GimpValueArray *args);
+
+
+G_DEFINE_TYPE_WITH_PRIVATE (GimpImageProcedure, gimp_image_procedure,
+                            GIMP_TYPE_PROCEDURE)
+
+#define parent_class gimp_image_procedure_parent_class
+
+
+static void
+gimp_image_procedure_class_init (GimpImageProcedureClass *klass)
+{
+  GObjectClass       *object_class    = G_OBJECT_CLASS (klass);
+  GimpProcedureClass *procedure_class = GIMP_PROCEDURE_CLASS (klass);
+
+  object_class->constructed  = gimp_image_procedure_constructed;
+  object_class->finalize     = gimp_image_procedure_finalize;
+
+  procedure_class->run       = gimp_image_procedure_run;
+}
+
+static void
+gimp_image_procedure_init (GimpImageProcedure *procedure)
+{
+  procedure->priv = gimp_image_procedure_get_instance_private (procedure);
+}
+
+static void
+gimp_image_procedure_constructed (GObject *object)
+{
+  GimpProcedure *procedure = GIMP_PROCEDURE (object);
+
+  G_OBJECT_CLASS (parent_class)->constructed (object);
+
+  gimp_procedure_add_argument (procedure,
+                               g_param_spec_enum ("run-mode",
+                                                  "Run mode",
+                                                  "The run mode",
+                                                  GIMP_TYPE_RUN_MODE,
+                                                  GIMP_RUN_NONINTERACTIVE,
+                                                  G_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               gimp_param_spec_image_id ("image",
+                                                         "Image",
+                                                         "The input image",
+                                                         FALSE,
+                                                         G_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               gimp_param_spec_drawable_id ("drawable",
+                                                            "Drawable",
+                                                            "The input drawable",
+                                                            FALSE,
+                                                            G_PARAM_READWRITE));
+}
+
+static void
+gimp_image_procedure_finalize (GObject *object)
+{
+  GimpImageProcedure *procedure = GIMP_IMAGE_PROCEDURE (object);
+
+  if (procedure->priv->run_data_destroy)
+    procedure->priv->run_data_destroy (procedure->priv->run_data);
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static GimpValueArray *
+gimp_image_procedure_run (GimpProcedure        *procedure,
+                          const GimpValueArray *args)
+{
+  GimpImageProcedure *image_proc = GIMP_IMAGE_PROCEDURE (procedure);
+  GimpValueArray     *remaining;
+  GimpValueArray     *return_values;
+  GimpRunMode         run_mode;
+  gint32              image_id;
+  gint32              drawable_id;
+  gint                i;
+
+  run_mode    = g_value_get_enum           (gimp_value_array_index (args, 0));
+  image_id    = gimp_value_get_image_id    (gimp_value_array_index (args, 1));
+  drawable_id = gimp_value_get_drawable_id (gimp_value_array_index (args, 2));
+
+  remaining = gimp_value_array_new (gimp_value_array_length (args) - 3);
+
+  for (i = 3; i < gimp_value_array_length (args); i++)
+    {
+      GValue *value = gimp_value_array_index (args, i);
+
+      gimp_value_array_append (remaining, value);
+    }
+
+  return_values = image_proc->priv->run_func (procedure,
+                                              run_mode,
+                                              image_id, drawable_id,
+                                              remaining,
+                                              image_proc->priv->run_data);
+
+  gimp_value_array_unref (remaining);
+
+  return return_values;
+}
+
+
+/*  public functions  */
+
+/**
+ * gimp_image_procedure_new:
+ * @plug_in:          a #GimpPlugIn.
+ * @name:             the new procedure's name.
+ * @proc_type:        the new procedure's #GimpPDBProcType.
+ * @run_func:         the run function for the new procedure.
+ * @run_data:         user data passed to @run_func.
+ * @run_data_destroy: (nullable): free function for @run_data, or %NULL.
+ *
+ * Creates a new image procedure named @name which will call @run_func
+ * when invoked.
+ *
+ * See gimp_procedure_new() for information about @proc_type.
+ *
+ * #GimpImageProcedure is a #GimpProcedure subclass that makes it easier
+ * to write standard plug-in procedures that operate on drawables.
+ *
+ * It automatically adds the standard
+ *
+ * (run-mode, image-id, drawable-id)
+ *
+ * arguments of an image procedure. It is possible to add additional
+ * arguments.
+ *
+ * When invoked via gimp_procedure_run(), it unpacks these standard
+ * arguemnts and calls @run_func which is a #GimpRunImageFunc. The
+ * "args" #GimpValueArray of #GimpRunImageFunc only contains
+ * additionally added arguments.
+ *
+ * Returns: a new #GimpProcedure.
+ *
+ * Since: 3.0
+ **/
+GimpProcedure  *
+gimp_image_procedure_new (GimpPlugIn       *plug_in,
+                          const gchar      *name,
+                          GimpPDBProcType   proc_type,
+                          GimpRunImageFunc  run_func,
+                          gpointer          run_data,
+                          GDestroyNotify    run_data_destroy)
+{
+  GimpImageProcedure *procedure;
+
+  g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), NULL);
+  g_return_val_if_fail (gimp_is_canonical_identifier (name), NULL);
+  g_return_val_if_fail (proc_type != GIMP_INTERNAL, NULL);
+  g_return_val_if_fail (run_func != NULL, NULL);
+
+  procedure = g_object_new (GIMP_TYPE_IMAGE_PROCEDURE,
+                            "plug-in",        plug_in,
+                            "name",           name,
+                            "procedure-type", proc_type,
+                            NULL);
+
+  procedure->priv->run_func         = run_func;
+  procedure->priv->run_data         = run_data;
+  procedure->priv->run_data_destroy = run_data_destroy;
+
+  return GIMP_PROCEDURE (procedure);
+}
diff --git a/libgimp/gimpimageprocedure.h b/libgimp/gimpimageprocedure.h
new file mode 100644
index 0000000000..7473dff863
--- /dev/null
+++ b/libgimp/gimpimageprocedure.h
@@ -0,0 +1,93 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpimageprocedure.h
+ * Copyright (C) 2019 Michael Natterer <mitch gimp org>
+ *
+ * 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 3 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/>.
+ */
+
+#ifndef __GIMP_IMAGE_PROCEDURE_H__
+#define __GIMP_IMAGE_PROCEDURE_H__
+
+#include <libgimp/gimpprocedure.h>
+
+G_BEGIN_DECLS
+
+/* For information look into the C source or the html documentation */
+
+
+/**
+ * GimpRunImageFunc:
+ * @procedure:   the #GimpProcedure that runs.
+ * @run_mode:    the #GimpRunMode.
+ * @image_id:    the image id.
+ * @drawable_id: the drawable id.
+ * @args:        the @procedure's remaining arguments.
+ * @run_data:    the run_data given in gimp_image_procedure_new().
+ *
+ * The image function is run during the lifetime of the GIMP session,
+ * each time a plug-in image procedure is called.
+ *
+ * Returns: (transfer full): the @procedure's return values.
+ *
+ * Since: 3.0
+ **/
+typedef GimpValueArray * (* GimpRunImageFunc) (GimpProcedure        *procedure,
+                                              GimpRunMode           run_mode,
+                                              gint32                image_id,
+                                              gint32                drawable_id,
+                                              const GimpValueArray *args,
+                                              gpointer              run_data);
+
+
+#define GIMP_TYPE_IMAGE_PROCEDURE            (gimp_image_procedure_get_type ())
+#define GIMP_IMAGE_PROCEDURE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_IMAGE_PROCEDURE, 
GimpImageProcedure))
+#define GIMP_IMAGE_PROCEDURE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_IMAGE_PROCEDURE, 
GimpImageProcedureClass))
+#define GIMP_IS_IMAGE_PROCEDURE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_IMAGE_PROCEDURE))
+#define GIMP_IS_IMAGE_PROCEDURE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_IMAGE_PROCEDURE))
+#define GIMP_IMAGE_PROCEDURE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_IMAGE_PROCEDURE, 
GimpImageProcedureClass))
+
+
+typedef struct _GimpImageProcedure        GimpImageProcedure;
+typedef struct _GimpImageProcedureClass   GimpImageProcedureClass;
+typedef struct _GimpImageProcedurePrivate GimpImageProcedurePrivate;
+
+struct _GimpImageProcedure
+{
+  GimpProcedure              parent_instance;
+
+  GimpImageProcedurePrivate *priv;
+};
+
+struct _GimpImageProcedureClass
+{
+  GimpProcedureClass parent_class;
+};
+
+
+GType           gimp_image_procedure_get_type (void) G_GNUC_CONST;
+
+GimpProcedure * gimp_image_procedure_new      (GimpPlugIn       *plug_in,
+                                               const gchar      *name,
+                                               GimpPDBProcType   proc_type,
+                                               GimpRunImageFunc  run_func,
+                                               gpointer          run_data,
+                                               GDestroyNotify    run_data_destroy);
+
+
+G_END_DECLS
+
+#endif  /*  __GIMP_IMAGE_PROCEDURE_H__  */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]