[gimp/gimp-2-10] app: add gimp_container_search()



commit 01cebc43078f0319111253396029f1d863f8d9d0
Author: Ell <ell_se yahoo com>
Date:   Sat Feb 1 12:56:16 2020 +0200

    app: add gimp_container_search()
    
    Add a new GimpContainer::search() virtual function, and a
    corresponding gimp_container_search() function, which works
    similarly to gimp_container_foreach(), except that the callback
    returns a boolean.  When the callback returns TRUE, the search is
    stopped, and the current object is returned.
    
    Implement GimpContainer::search() in GimpList.
    
    (cherry picked from commit c4cc015c43f643d9beb7a4c9b44835d1d1930f30)

 app/core/gimpcontainer.c |  18 ++++++
 app/core/gimpcontainer.h | 156 +++++++++++++++++++++++++----------------------
 app/core/gimplist.c      | 103 +++++++++++++++++++------------
 3 files changed, 166 insertions(+), 111 deletions(-)
---
diff --git a/app/core/gimpcontainer.c b/app/core/gimpcontainer.c
index 99b0d8c093..1bb2a2c8f9 100644
--- a/app/core/gimpcontainer.c
+++ b/app/core/gimpcontainer.c
@@ -195,6 +195,7 @@ gimp_container_class_init (GimpContainerClass *klass)
   klass->clear                   = NULL;
   klass->have                    = NULL;
   klass->foreach                 = NULL;
+  klass->search                  = NULL;
   klass->get_unique_names        = NULL;
   klass->get_child_by_name       = NULL;
   klass->get_child_by_index      = NULL;
@@ -821,6 +822,23 @@ gimp_container_foreach (GimpContainer *container,
     GIMP_CONTAINER_GET_CLASS (container)->foreach (container, func, user_data);
 }
 
+GimpObject *
+gimp_container_search (GimpContainer           *container,
+                       GimpContainerSearchFunc  func,
+                       gpointer                 user_data)
+{
+  g_return_val_if_fail (GIMP_IS_CONTAINER (container), NULL);
+  g_return_val_if_fail (func != NULL, NULL);
+
+  if (container->priv->n_children > 0)
+    {
+      return GIMP_CONTAINER_GET_CLASS (container)->search (container,
+                                                           func, user_data);
+    }
+
+  return NULL;
+}
+
 gboolean
 gimp_container_get_unique_names (GimpContainer *container)
 {
diff --git a/app/core/gimpcontainer.h b/app/core/gimpcontainer.h
index 1966e3f795..dda4bbdbae 100644
--- a/app/core/gimpcontainer.h
+++ b/app/core/gimpcontainer.h
@@ -33,6 +33,10 @@
 #define GIMP_CONTAINER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_CONTAINER, 
GimpContainerClass))
 
 
+typedef gboolean (* GimpContainerSearchFunc) (GimpObject *object,
+                                              gpointer    user_data);
+
+
 typedef struct _GimpContainerClass   GimpContainerClass;
 typedef struct _GimpContainerPrivate GimpContainerPrivate;
 
@@ -48,86 +52,92 @@ struct _GimpContainerClass
   GimpObjectClass  parent_class;
 
   /*  signals  */
-  void         (* add)                (GimpContainer *container,
-                                       GimpObject    *object);
-  void         (* remove)             (GimpContainer *container,
-                                       GimpObject    *object);
-  void         (* reorder)            (GimpContainer *container,
-                                       GimpObject    *object,
-                                       gint           new_index);
-  void         (* freeze)             (GimpContainer *container);
-  void         (* thaw)               (GimpContainer *container);
+  void         (* add)                (GimpContainer           *container,
+                                       GimpObject              *object);
+  void         (* remove)             (GimpContainer           *container,
+                                       GimpObject              *object);
+  void         (* reorder)            (GimpContainer           *container,
+                                       GimpObject              *object,
+                                       gint                     new_index);
+  void         (* freeze)             (GimpContainer           *container);
+  void         (* thaw)               (GimpContainer           *container);
 
   /*  virtual functions  */
-  void         (* clear)              (GimpContainer *container);
-  gboolean     (* have)               (GimpContainer *container,
-                                       GimpObject    *object);
-  void         (* foreach)            (GimpContainer *container,
-                                       GFunc          func,
-                                       gpointer       user_data);
-  gboolean     (* get_unique_names)   (GimpContainer *container);
-  GimpObject * (* get_child_by_name)  (GimpContainer *container,
-                                       const gchar   *name);
-  GimpObject * (* get_child_by_index) (GimpContainer *container,
-                                       gint           index);
-  gint         (* get_child_index)    (GimpContainer *container,
-                                       GimpObject    *object);
+  void         (* clear)              (GimpContainer           *container);
+  gboolean     (* have)               (GimpContainer           *container,
+                                       GimpObject              *object);
+  void         (* foreach)            (GimpContainer           *container,
+                                       GFunc                    func,
+                                       gpointer                 user_data);
+  GimpObject * (* search)             (GimpContainer           *container,
+                                       GimpContainerSearchFunc  func,
+                                       gpointer                 user_data);
+  gboolean     (* get_unique_names)   (GimpContainer           *container);
+  GimpObject * (* get_child_by_name)  (GimpContainer           *container,
+                                       const gchar             *name);
+  GimpObject * (* get_child_by_index) (GimpContainer           *container,
+                                       gint                     index);
+  gint         (* get_child_index)    (GimpContainer           *container,
+                                       GimpObject              *object);
 };
 
 
 GType        gimp_container_get_type           (void) G_GNUC_CONST;
 
-GType        gimp_container_get_children_type  (GimpContainer *container);
-GimpContainerPolicy gimp_container_get_policy  (GimpContainer *container);
-gint         gimp_container_get_n_children     (GimpContainer *container);
-
-gboolean     gimp_container_add                (GimpContainer *container,
-                                                GimpObject    *object);
-gboolean     gimp_container_remove             (GimpContainer *container,
-                                                GimpObject    *object);
-gboolean     gimp_container_insert             (GimpContainer *container,
-                                                GimpObject    *object,
-                                                gint           new_index);
-gboolean     gimp_container_reorder            (GimpContainer *container,
-                                                GimpObject    *object,
-                                                gint           new_index);
-
-void         gimp_container_freeze             (GimpContainer *container);
-void         gimp_container_thaw               (GimpContainer *container);
-gboolean     gimp_container_frozen             (GimpContainer *container);
-gint         gimp_container_freeze_count       (GimpContainer *container);
-
-void         gimp_container_clear              (GimpContainer *container);
-gboolean     gimp_container_is_empty           (GimpContainer *container);
-gboolean     gimp_container_have               (GimpContainer *container,
-                                                GimpObject    *object);
-void         gimp_container_foreach            (GimpContainer *container,
-                                                GFunc          func,
-                                                gpointer       user_data);
-
-gboolean     gimp_container_get_unique_names   (GimpContainer *container);
-
-GimpObject * gimp_container_get_child_by_name  (GimpContainer *container,
-                                                const gchar   *name);
-GimpObject * gimp_container_get_child_by_index (GimpContainer *container,
-                                                gint           index);
-GimpObject * gimp_container_get_first_child    (GimpContainer *container);
-GimpObject * gimp_container_get_last_child     (GimpContainer *container);
-gint         gimp_container_get_child_index    (GimpContainer *container,
-                                                GimpObject    *object);
-
-GimpObject * gimp_container_get_neighbor_of    (GimpContainer *container,
-                                                GimpObject    *object);
-
-gchar     ** gimp_container_get_name_array     (GimpContainer *container,
-                                                gint          *length);
-
-GQuark       gimp_container_add_handler        (GimpContainer *container,
-                                                const gchar   *signame,
-                                                GCallback      callback,
-                                                gpointer       callback_data);
-void         gimp_container_remove_handler     (GimpContainer *container,
-                                                GQuark         id);
+GType        gimp_container_get_children_type  (GimpContainer           *container);
+GimpContainerPolicy gimp_container_get_policy  (GimpContainer           *container);
+gint         gimp_container_get_n_children     (GimpContainer           *container);
+
+gboolean     gimp_container_add                (GimpContainer           *container,
+                                                GimpObject              *object);
+gboolean     gimp_container_remove             (GimpContainer           *container,
+                                                GimpObject              *object);
+gboolean     gimp_container_insert             (GimpContainer           *container,
+                                                GimpObject              *object,
+                                                gint                     new_index);
+gboolean     gimp_container_reorder            (GimpContainer           *container,
+                                                GimpObject              *object,
+                                                gint                     new_index);
+
+void         gimp_container_freeze             (GimpContainer           *container);
+void         gimp_container_thaw               (GimpContainer           *container);
+gboolean     gimp_container_frozen             (GimpContainer           *container);
+gint         gimp_container_freeze_count       (GimpContainer           *container);
+
+void         gimp_container_clear              (GimpContainer           *container);
+gboolean     gimp_container_is_empty           (GimpContainer           *container);
+gboolean     gimp_container_have               (GimpContainer           *container,
+                                                GimpObject              *object);
+void         gimp_container_foreach            (GimpContainer           *container,
+                                                GFunc                    func,
+                                                gpointer                 user_data);
+GimpObject * gimp_container_search             (GimpContainer           *container,
+                                                GimpContainerSearchFunc  func,
+                                                gpointer                 user_data);
+
+gboolean     gimp_container_get_unique_names   (GimpContainer           *container);
+
+GimpObject * gimp_container_get_child_by_name  (GimpContainer           *container,
+                                                const gchar             *name);
+GimpObject * gimp_container_get_child_by_index (GimpContainer           *container,
+                                                gint                     index);
+GimpObject * gimp_container_get_first_child    (GimpContainer           *container);
+GimpObject * gimp_container_get_last_child     (GimpContainer           *container);
+gint         gimp_container_get_child_index    (GimpContainer           *container,
+                                                GimpObject              *object);
+
+GimpObject * gimp_container_get_neighbor_of    (GimpContainer           *container,
+                                                GimpObject              *object);
+
+gchar     ** gimp_container_get_name_array     (GimpContainer           *container,
+                                                gint                    *length);
+
+GQuark       gimp_container_add_handler        (GimpContainer           *container,
+                                                const gchar             *signame,
+                                                GCallback                callback,
+                                                gpointer                 callback_data);
+void         gimp_container_remove_handler     (GimpContainer           *container,
+                                                GQuark                   id);
 
 
 #endif  /* __GIMP_CONTAINER_H__ */
diff --git a/app/core/gimplist.c b/app/core/gimplist.c
index 8e530b97d6..0fbca1f156 100644
--- a/app/core/gimplist.c
+++ b/app/core/gimplist.c
@@ -40,44 +40,47 @@ enum
 };
 
 
-static void         gimp_list_finalize           (GObject       *object);
-static void         gimp_list_set_property       (GObject       *object,
-                                                  guint          property_id,
-                                                  const GValue  *value,
-                                                  GParamSpec    *pspec);
-static void         gimp_list_get_property       (GObject       *object,
-                                                  guint          property_id,
-                                                  GValue        *value,
-                                                  GParamSpec    *pspec);
-
-static gint64       gimp_list_get_memsize        (GimpObject    *object,
-                                                  gint64        *gui_size);
-
-static void         gimp_list_add                (GimpContainer *container,
-                                                  GimpObject    *object);
-static void         gimp_list_remove             (GimpContainer *container,
-                                                  GimpObject    *object);
-static void         gimp_list_reorder            (GimpContainer *container,
-                                                  GimpObject    *object,
-                                                  gint           new_index);
-static void         gimp_list_clear              (GimpContainer *container);
-static gboolean     gimp_list_have               (GimpContainer *container,
-                                                  GimpObject    *object);
-static void         gimp_list_foreach            (GimpContainer *container,
-                                                  GFunc          func,
-                                                  gpointer       user_data);
-static gboolean     gimp_list_get_unique_names   (GimpContainer *container);
-static GimpObject * gimp_list_get_child_by_name  (GimpContainer *container,
-                                                  const gchar   *name);
-static GimpObject * gimp_list_get_child_by_index (GimpContainer *container,
-                                                  gint           index);
-static gint         gimp_list_get_child_index    (GimpContainer *container,
-                                                  GimpObject    *object);
-
-static void         gimp_list_uniquefy_name      (GimpList      *gimp_list,
-                                                  GimpObject    *object);
-static void         gimp_list_object_renamed     (GimpObject    *object,
-                                                  GimpList      *list);
+static void         gimp_list_finalize           (GObject                 *object);
+static void         gimp_list_set_property       (GObject                 *object,
+                                                  guint                    property_id,
+                                                  const GValue            *value,
+                                                  GParamSpec              *pspec);
+static void         gimp_list_get_property       (GObject                 *object,
+                                                  guint                    property_id,
+                                                  GValue                  *value,
+                                                  GParamSpec              *pspec);
+
+static gint64       gimp_list_get_memsize        (GimpObject              *object,
+                                                  gint64                  *gui_size);
+
+static void         gimp_list_add                (GimpContainer           *container,
+                                                  GimpObject              *object);
+static void         gimp_list_remove             (GimpContainer           *container,
+                                                  GimpObject              *object);
+static void         gimp_list_reorder            (GimpContainer           *container,
+                                                  GimpObject              *object,
+                                                  gint                     new_index);
+static void         gimp_list_clear              (GimpContainer           *container);
+static gboolean     gimp_list_have               (GimpContainer           *container,
+                                                  GimpObject              *object);
+static void         gimp_list_foreach            (GimpContainer           *container,
+                                                  GFunc                    func,
+                                                  gpointer                 user_data);
+static GimpObject * gimp_list_search             (GimpContainer           *container,
+                                                  GimpContainerSearchFunc  func,
+                                                  gpointer                 user_data);
+static gboolean     gimp_list_get_unique_names   (GimpContainer           *container);
+static GimpObject * gimp_list_get_child_by_name  (GimpContainer           *container,
+                                                  const gchar             *name);
+static GimpObject * gimp_list_get_child_by_index (GimpContainer           *container,
+                                                  gint                     index);
+static gint         gimp_list_get_child_index    (GimpContainer           *container,
+                                                  GimpObject              *object);
+
+static void         gimp_list_uniquefy_name      (GimpList                *gimp_list,
+                                                  GimpObject              *object);
+static void         gimp_list_object_renamed     (GimpObject              *object,
+                                                  GimpList                *list);
 
 
 G_DEFINE_TYPE (GimpList, gimp_list, GIMP_TYPE_CONTAINER)
@@ -104,6 +107,7 @@ gimp_list_class_init (GimpListClass *klass)
   container_class->clear              = gimp_list_clear;
   container_class->have               = gimp_list_have;
   container_class->foreach            = gimp_list_foreach;
+  container_class->search             = gimp_list_search;
   container_class->get_unique_names   = gimp_list_get_unique_names;
   container_class->get_child_by_name  = gimp_list_get_child_by_name;
   container_class->get_child_by_index = gimp_list_get_child_by_index;
@@ -328,6 +332,29 @@ gimp_list_foreach (GimpContainer *container,
   g_queue_foreach (list->queue, func, user_data);
 }
 
+static GimpObject *
+gimp_list_search (GimpContainer           *container,
+                  GimpContainerSearchFunc  func,
+                  gpointer                 user_data)
+{
+  GimpList *list = GIMP_LIST (container);
+  GList    *iter;
+
+  iter = list->queue->head;
+
+  while (iter)
+    {
+      GimpObject *object = iter->data;
+
+      iter = g_list_next (iter);
+
+      if (func (object, user_data))
+        return object;
+    }
+
+  return NULL;
+}
+
 static gboolean
 gimp_list_get_unique_names (GimpContainer *container)
 {


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