[gtk/prop-list: 60/179] listitemfactory: Sanitize APIs



commit 4d58b4af3daacd388adc3f136fb16518cc6c5cb2
Author: Benjamin Otte <otte redhat com>
Date:   Sun Jun 9 22:50:46 2019 +0200

    listitemfactory: Sanitize APIs
    
    Make sure the APIs follow a predictable path:
    
    setup
      bind
        rebind/update (0-N times)
      unbind
    teardown
    
    This is the first step towards providing multiple different factories.

 gtk/gtklistitemfactory.c        | 57 ++++++++++++++++++++++++++++-------------
 gtk/gtklistitemfactoryprivate.h | 22 ++++++++++++++++
 gtk/gtklistitemmanager.c        |  3 ++-
 3 files changed, 63 insertions(+), 19 deletions(-)
---
diff --git a/gtk/gtklistitemfactory.c b/gtk/gtklistitemfactory.c
index 4a7fef9e92..c9bd2b079e 100644
--- a/gtk/gtklistitemfactory.c
+++ b/gtk/gtklistitemfactory.c
@@ -71,23 +71,6 @@
  * Reusing factories across different views is allowed, but very uncommon.
  */
 
-struct _GtkListItemFactory
-{
-  GObject parent_instance;
-
-  GtkListItemSetupFunc setup_func;
-  GtkListItemBindFunc bind_func;
-  gpointer user_data;
-  GDestroyNotify user_destroy;
-};
-
-G_DEFINE_TYPE (GtkListItemFactory, gtk_list_item_factory, G_TYPE_OBJECT)
-
-struct _GtkListItemFactoryClass
-{
-  GObjectClass parent_class;
-};
-
 G_DEFINE_TYPE (GtkListItemFactory, gtk_list_item_factory, G_TYPE_OBJECT)
 
 static void
@@ -137,7 +120,7 @@ gtk_list_item_factory_new (GtkListItemSetupFunc setup_func,
 
 void
 gtk_list_item_factory_setup (GtkListItemFactory *self,
-                             GtkListItem         *list_item)
+                             GtkListItem        *list_item)
 {
   g_return_if_fail (GTK_IS_LIST_ITEM_FACTORY (self));
 
@@ -167,6 +150,28 @@ gtk_list_item_factory_bind (GtkListItemFactory *self,
   g_object_thaw_notify (G_OBJECT (list_item));
 }
 
+void
+gtk_list_item_factory_rebind (GtkListItemFactory *self,
+                              GtkListItem        *list_item,
+                              guint               position,
+                              gpointer            item,
+                              gboolean            selected)
+{
+  g_return_if_fail (GTK_IS_LIST_ITEM_FACTORY (self));
+  g_return_if_fail (GTK_IS_LIST_ITEM (list_item));
+
+  g_object_freeze_notify (G_OBJECT (list_item));
+
+  gtk_list_item_set_item (list_item, item);
+  gtk_list_item_set_position (list_item, position);
+  gtk_list_item_set_selected (list_item, selected);
+
+  if (self->bind_func)  
+    self->bind_func (list_item, self->user_data);
+
+  g_object_thaw_notify (G_OBJECT (list_item));
+}
+
 void
 gtk_list_item_factory_update (GtkListItemFactory *self,
                               GtkListItem        *list_item,
@@ -199,3 +204,19 @@ gtk_list_item_factory_unbind (GtkListItemFactory *self,
 
   g_object_thaw_notify (G_OBJECT (list_item));
 }
+
+void
+gtk_list_item_factory_teardown (GtkListItemFactory *self,
+                                GtkListItem        *list_item)
+{
+  GtkWidget *child;
+
+  g_return_if_fail (GTK_IS_LIST_ITEM_FACTORY (self));
+
+  child = gtk_bin_get_child (GTK_BIN (list_item));
+  if (child)
+    gtk_container_remove (GTK_CONTAINER (list_item), child);
+
+  gtk_list_item_set_selectable (list_item, TRUE);
+}
+
diff --git a/gtk/gtklistitemfactoryprivate.h b/gtk/gtklistitemfactoryprivate.h
index dc538f09fa..34582257bc 100644
--- a/gtk/gtklistitemfactoryprivate.h
+++ b/gtk/gtklistitemfactoryprivate.h
@@ -36,6 +36,21 @@ G_BEGIN_DECLS
 typedef struct _GtkListItemFactory GtkListItemFactory;
 typedef struct _GtkListItemFactoryClass GtkListItemFactoryClass;
 
+struct _GtkListItemFactory
+{
+  GObject parent_instance;
+
+  GtkListItemSetupFunc setup_func;
+  GtkListItemBindFunc bind_func;
+  gpointer user_data;
+  GDestroyNotify user_destroy;
+};
+
+struct _GtkListItemFactoryClass
+{
+  GObjectClass parent_class;
+};
+
 GType                   gtk_list_item_factory_get_type          (void) G_GNUC_CONST;
 
 GtkListItemFactory *    gtk_list_item_factory_new               (GtkListItemSetupFunc    setup_func,
@@ -45,12 +60,19 @@ GtkListItemFactory *    gtk_list_item_factory_new               (GtkListItemSetu
 
 void                    gtk_list_item_factory_setup             (GtkListItemFactory     *self,
                                                                  GtkListItem            *list_item);
+void                    gtk_list_item_factory_teardown          (GtkListItemFactory     *self,
+                                                                 GtkListItem            *list_item);
 
 void                    gtk_list_item_factory_bind              (GtkListItemFactory     *self,
                                                                  GtkListItem            *list_item,
                                                                  guint                   position,
                                                                  gpointer                item,
                                                                  gboolean                selected);
+void                    gtk_list_item_factory_rebind            (GtkListItemFactory     *self,
+                                                                 GtkListItem            *list_item,
+                                                                 guint                   position,
+                                                                 gpointer                item,
+                                                                 gboolean                selected);
 void                    gtk_list_item_factory_update            (GtkListItemFactory     *self,
                                                                  GtkListItem            *list_item,
                                                                  guint                   position,
diff --git a/gtk/gtklistitemmanager.c b/gtk/gtklistitemmanager.c
index 35a894ed51..fde93c9708 100644
--- a/gtk/gtklistitemmanager.c
+++ b/gtk/gtklistitemmanager.c
@@ -1007,7 +1007,7 @@ gtk_list_item_manager_move_list_item (GtkListItemManager     *self,
 
   item = g_list_model_get_item (G_LIST_MODEL (self->model), position);
   selected = gtk_selection_model_is_selected (self->model, position);
-  gtk_list_item_factory_bind (self->factory, GTK_LIST_ITEM (list_item), position, item, selected);
+  gtk_list_item_factory_rebind (self->factory, GTK_LIST_ITEM (list_item), position, item, selected);
   gtk_widget_insert_after (list_item, _gtk_widget_get_parent (list_item), prev_sibling);
   g_object_unref (item);
 }
@@ -1063,6 +1063,7 @@ gtk_list_item_manager_release_list_item (GtkListItemManager *self,
     }
 
   gtk_list_item_factory_unbind (self->factory, GTK_LIST_ITEM (item));
+  gtk_list_item_factory_teardown (self->factory, GTK_LIST_ITEM (item));
   gtk_widget_unparent (item);
 }
 


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