[gimp/wip/Jehan/classy-GIMP: 21/22] app, libgimp: add a "destroyed" signal on libgimp's GimpItem.



commit bbc1fee32619a9619be0b144999339c2f2cf7869
Author: Jehan <jehan girinstud io>
Date:   Mon Aug 19 17:20:07 2019 +0200

    app, libgimp: add a "destroyed" signal on libgimp's GimpItem.
    
    Same as on GimpImage, it allows plug-ins to be notified when an item is
    removed on core side, and also to manage the item list.

 app/core/gimpitem.c |  6 ++++++
 libgimp/gimpitem.c  | 38 +++++++++++++++++++++++++++++++++++++-
 libgimp/gimpitem.h  |  3 +++
 3 files changed, 46 insertions(+), 1 deletion(-)
---
diff --git a/app/core/gimpitem.c b/app/core/gimpitem.c
index 36d02641b6..03d6d742fe 100644
--- a/app/core/gimpitem.c
+++ b/app/core/gimpitem.c
@@ -47,6 +47,8 @@
 
 #include "paint/gimppaintoptions.h"
 
+#include "plug-in/gimppluginmanager.h"
+
 #include "gimp-intl.h"
 
 
@@ -373,6 +375,10 @@ gimp_item_finalize (GObject *object)
 {
   GimpItemPrivate *private = GET_PRIVATE (object);
 
+  gimp_plug_in_manager_emit_signal (private->image->gimp->plug_in_manager,
+                                    object, gimp_item_get_ID (GIMP_ITEM (object)),
+                                    "destroyed");
+
   if (private->offset_nodes)
     {
       g_list_free_full (private->offset_nodes,
diff --git a/libgimp/gimpitem.c b/libgimp/gimpitem.c
index 66c645a058..c2095ee749 100644
--- a/libgimp/gimpitem.c
+++ b/libgimp/gimpitem.c
@@ -25,6 +25,12 @@
 
 #include "gimppixbuf.h"
 
+enum
+{
+  DESTROYED,
+  LAST_SIGNAL
+};
+
 enum
 {
   PROP_0,
@@ -53,7 +59,8 @@ G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GimpItem, gimp_item, G_TYPE_OBJECT)
 
 #define parent_class gimp_item_parent_class
 
-static GParamSpec *props[N_PROPS] = { NULL, };
+static GParamSpec *props[N_PROPS]       = { NULL, };
+static guint       signals[LAST_SIGNAL] = { 0 };
 
 static void
 gimp_item_class_init (GimpItemClass *klass)
@@ -63,6 +70,26 @@ gimp_item_class_init (GimpItemClass *klass)
   object_class->set_property = gimp_item_set_property;
   object_class->get_property = gimp_item_get_property;
 
+  /**
+   * GimpItemClass::destroy:
+   * @item: a #GimpItem
+   *
+   * This signal will be emitted when an item has been destroyed, just
+   * before we g_object_unref() it. This item is now invalid, none of
+   * its data can be accessed anymore, therefore all processing has to
+   * be stopped immediately.
+   * The only thing still feasible is to compare the item if you kept a
+   * reference to identify item, or to run gimp_item_get_id().
+   */
+  signals[DESTROYED] =
+    g_signal_new ("destroyed",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_FIRST,
+                  G_STRUCT_OFFSET (GimpItemClass, destroyed),
+                  NULL, NULL,
+                  g_cclosure_marshal_VOID__VOID,
+                  G_TYPE_NONE, 0);
+
   props[PROP_ID] =
     g_param_spec_int ("id",
                       "The item id",
@@ -259,6 +286,15 @@ _gimp_item_process_signal (gint32       item_id,
     return;
 
   /* Below process item signals. */
+
+  if (g_strcmp0 (name, "destroyed") == 0)
+    {
+      g_hash_table_steal (gimp_items,
+                          GINT_TO_POINTER (item->priv->id));
+
+      g_signal_emit (item, signals[DESTROYED], 0);
+      g_object_unref (item);
+    }
 }
 
 
diff --git a/libgimp/gimpitem.h b/libgimp/gimpitem.h
index 3d948e955f..65c2035425 100644
--- a/libgimp/gimpitem.h
+++ b/libgimp/gimpitem.h
@@ -52,6 +52,9 @@ struct _GimpItemClass
 {
   GObjectClass parent_class;
 
+  /* Signals. */
+  void (* destroyed) (GimpItem *image);
+
   /* Padding for future expansion */
   void (*_gimp_reserved1) (void);
   void (*_gimp_reserved2) (void);


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