[gtk/wip/otte/listview: 772/896] listview: Add GtkListItem



commit f4781f02f7e5fb2b743fd1acc878f77270e1f4e3
Author: Benjamin Otte <otte redhat com>
Date:   Fri Sep 21 05:05:34 2018 +0200

    listview: Add GtkListItem
    
    GtkListItem is a generic row widget that is supposed to replace
    GtkListBoxRow and GtkFlowBoxChild.

 docs/reference/gtk/gtk4-sections.txt |  19 +++
 docs/reference/gtk/gtk4.types.in     |   1 +
 docs/reference/gtk/meson.build       |   1 +
 gtk/gtk.h                            |   1 +
 gtk/gtklistitem.c                    | 279 +++++++++++++++++++++++++++++++++++
 gtk/gtklistitem.h                    |  57 +++++++
 gtk/gtklistitemfactory.c             |  31 +++-
 gtk/gtklistitemfactoryprivate.h      |   7 +-
 gtk/gtklistitemmanager.c             |   6 +-
 gtk/gtklistitemprivate.h             |  35 +++++
 gtk/meson.build                      |   2 +
 11 files changed, 429 insertions(+), 10 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index eb52073e6a..40d0c6f0f5 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -490,6 +490,24 @@ gtk_single_selection_set_can_unselect
 gtk_single_selection_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtklistitem</FILE>
+<TITLE>GtkListItem</TITLE>
+GtkListItem
+gtk_list_item_get_item
+gtk_list_item_get_child
+gtk_list_item_set_child
+<SUBSECTION Standard>
+GTK_LIST_ITEM
+GTK_LIST_ITEM_CLASS
+GTK_LIST_ITEM_GET_CLASS
+GTK_IS_LIST_ITEM
+GTK_IS_LIST_ITEM_CLASS
+GTK_TYPE_LIST_ITEM
+<SUBSECTION Private>
+gtk_list_item_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtklistitemfactory</FILE>
 <TITLE>GtkListItemFactory</TITLE>
@@ -505,6 +523,7 @@ GTK_TYPE_LIST_ITEM_FACTORY
 gtk_list_item_factory_get_type
 </SECTION>
 
+<SECTION>
 <FILE>gtklistview</FILE>
 <TITLE>GtkListView</TITLE>
 GtkListView
diff --git a/docs/reference/gtk/gtk4.types.in b/docs/reference/gtk/gtk4.types.in
index 8685556081..9b940cce28 100644
--- a/docs/reference/gtk/gtk4.types.in
+++ b/docs/reference/gtk/gtk4.types.in
@@ -121,6 +121,7 @@ gtk_label_get_type
 gtk_layout_child_get_type
 gtk_layout_manager_get_type
 gtk_link_button_get_type
+gtk_list_item_get_type
 gtk_list_item_factory_get_type
 gtk_list_store_get_type
 gtk_list_box_get_type
diff --git a/docs/reference/gtk/meson.build b/docs/reference/gtk/meson.build
index ef97fb3cf0..112b7d42d6 100644
--- a/docs/reference/gtk/meson.build
+++ b/docs/reference/gtk/meson.build
@@ -131,6 +131,7 @@ private_headers = [
   'gtkimmoduleprivate.h',
   'gtkkineticscrollingprivate.h',
   'gtklabelprivate.h',
+  'gtklistitemprivate.h',
   'gtklistitemfactoryprivate.h',
   'gtklistitemmanagerprivate.h',
   'gtklockbuttonprivate.h',
diff --git a/gtk/gtk.h b/gtk/gtk.h
index 02e34301eb..517c1d3ced 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -157,6 +157,7 @@
 #include <gtk/gtklevelbar.h>
 #include <gtk/gtklinkbutton.h>
 #include <gtk/gtklistbox.h>
+#include <gtk/gtklistitem.h>
 #include <gtk/gtkliststore.h>
 #include <gtk/gtklistview.h>
 #include <gtk/gtklockbutton.h>
diff --git a/gtk/gtklistitem.c b/gtk/gtklistitem.c
new file mode 100644
index 0000000000..65f52acf22
--- /dev/null
+++ b/gtk/gtklistitem.c
@@ -0,0 +1,279 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * 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 2.1 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 <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "gtklistitemprivate.h"
+
+#include "gtkbinlayout.h"
+#include "gtkintl.h"
+#include "gtkwidget.h"
+
+/**
+ * SECTION:gtklistitem
+ * @title: GtkListItem
+ * @short_description: Object used to represent items of a ListModel
+ * @see_also: #GtkListView, #GListModel
+ *
+ * #GtkListItem is the object that list-handling containers such
+ * as #GtkListView use to represent items in a #GListModel. They are
+ * managed by the container and cannot be created by application code.
+ *
+ * #GtkListItems need to be populated by application code. This is done by
+ * calling gtk_list_item_set_child().
+ *
+ * #GtkListItems exist in 2 stages:
+ *
+ * 1. The unbound stage where the listitem is not currently connected to
+ *    an item in the list. In that case, the #GtkListItem:item property is
+ *    set to %NULL.
+ *
+ * 2. The bound stage where the listitem references an item from the list.
+ *    The #GtkListItem:item property is not %NULL.
+ */
+
+struct _GtkListItem
+{
+  GtkWidget parent_instance;
+
+  GObject *item;
+  GtkWidget *child;
+};
+
+struct _GtkListItemClass
+{
+  GtkWidgetClass parent_class;
+};
+
+enum
+{
+  PROP_0,
+  PROP_CHILD,
+  PROP_ITEM,
+
+  N_PROPS
+};
+
+G_DEFINE_TYPE (GtkListItem, gtk_list_item, GTK_TYPE_WIDGET)
+
+static GParamSpec *properties[N_PROPS] = { NULL, };
+
+static void
+gtk_list_item_dispose (GObject *object)
+{
+  GtkListItem *self = GTK_LIST_ITEM (object);
+
+  g_assert (self->item == NULL);
+  g_clear_pointer (&self->child, gtk_widget_unparent);
+
+  G_OBJECT_CLASS (gtk_list_item_parent_class)->dispose (object);
+}
+
+static void
+gtk_list_item_get_property (GObject    *object,
+                            guint       property_id,
+                            GValue     *value,
+                            GParamSpec *pspec)
+{
+  GtkListItem *self = GTK_LIST_ITEM (object);
+
+  switch (property_id)
+    {
+    case PROP_CHILD:
+      g_value_set_object (value, self->child);
+      break;
+
+    case PROP_ITEM:
+      g_value_set_object (value, self->item);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_list_item_set_property (GObject      *object,
+                            guint         property_id,
+                            const GValue *value,
+                            GParamSpec   *pspec)
+{
+  GtkListItem *self = GTK_LIST_ITEM (object);
+
+  switch (property_id)
+    {
+    case PROP_CHILD:
+      gtk_list_item_set_child (self, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_list_item_class_init (GtkListItemClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->dispose = gtk_list_item_dispose;
+  gobject_class->get_property = gtk_list_item_get_property;
+  gobject_class->set_property = gtk_list_item_set_property;
+
+  /**
+   * GtkListItem:child:
+   *
+   * Widget used for display
+   */
+  properties[PROP_CHILD] =
+    g_param_spec_object ("child",
+                         P_("Child"),
+                         P_("Widget used for display"),
+                         GTK_TYPE_WIDGET,
+                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
+  /**
+   * GtkListItem:item:
+   *
+   * Displayed item
+   */
+  properties[PROP_ITEM] =
+    g_param_spec_object ("item",
+                         P_("Item"),
+                         P_("Displayed item"),
+                         G_TYPE_OBJECT,
+                         G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (gobject_class, N_PROPS, properties);
+
+  /* This gets overwritten by gtk_list_item_new() but better safe than sorry */
+  gtk_widget_class_set_css_name (widget_class, I_("row"));
+  gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
+}
+
+static void
+gtk_list_item_init (GtkListItem *self)
+{
+}
+
+GtkWidget *
+gtk_list_item_new (const char *css_name)
+{
+  g_return_val_if_fail (css_name != NULL, NULL);
+
+  return g_object_new (GTK_TYPE_LIST_ITEM,
+                       "css-name", css_name,
+                       NULL);
+}
+
+/**
+ * gtk_list_item_get_item:
+ * @self: a #GtkListItem
+ *
+ * Gets the item that is currently displayed or model that @self is
+ * currently bound to or %NULL if @self is unbound.
+ *
+ * Returns: (nullable) (transfer none) (type GObject): The model in use
+ **/
+gpointer
+gtk_list_item_get_item (GtkListItem *self)
+{
+  g_return_val_if_fail (GTK_IS_LIST_ITEM (self), NULL);
+
+  return self->item;
+}
+
+/**
+ * gtk_list_item_get_child:
+ * @self: a #GtkListItem
+ *
+ * Gets the child previously set via gtk_list_item_set_child() or
+ * %NULL if none was set.
+ *
+ * Returns: (transfer none) (nullable): The child
+ **/
+GtkWidget *
+gtk_list_item_get_child (GtkListItem *self)
+{
+  g_return_val_if_fail (GTK_IS_LIST_ITEM (self), NULL);
+
+  return self->child;
+}
+
+/**
+ * gtk_list_item_set_child:
+ * @self: a #GtkListItem
+ * @child: (nullable): The list item's child or %NULL to unset
+ *
+ * Sets the child to be used for this listitem.
+ *
+ * This function is typically called by applications when
+ * setting up a listitem so that the widget can be reused when
+ * binding it multiple times.
+ **/
+void
+gtk_list_item_set_child (GtkListItem *self,
+                         GtkWidget   *child)
+{
+  g_return_if_fail (GTK_IS_LIST_ITEM (self));
+  g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
+
+  if (self->child == child)
+    return;
+
+  g_clear_pointer (&self->child, gtk_widget_unparent);
+
+  if (child)
+    {
+      gtk_widget_insert_after (child, GTK_WIDGET (self), NULL);
+      self->child = child;
+    }
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ITEM]);
+}
+
+void
+gtk_list_item_bind (GtkListItem *self,
+                    gpointer     item)
+{
+  g_return_if_fail (GTK_IS_LIST_ITEM (self));
+  g_return_if_fail (G_IS_OBJECT (item));
+  /* Must unbind before rebinding */
+  g_return_if_fail (self->item == NULL);
+
+  self->item = g_object_ref (item);
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ITEM]);
+}
+
+void
+gtk_list_item_unbind (GtkListItem *self)
+{
+  g_return_if_fail (GTK_IS_LIST_ITEM (self));
+  /* Must be bound */
+  g_return_if_fail (self->item != NULL);
+
+  g_clear_object (&self->item);
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ITEM]);
+}
+
diff --git a/gtk/gtklistitem.h b/gtk/gtklistitem.h
new file mode 100644
index 0000000000..3af77cb14e
--- /dev/null
+++ b/gtk/gtklistitem.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * 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 2.1 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 <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __GTK_LIST_ITEM_H__
+#define __GTK_LIST_ITEM_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtktypes.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_LIST_ITEM         (gtk_list_item_get_type ())
+#define GTK_TYPE_LIST_ITEM         (gtk_list_item_get_type ())
+#define GTK_LIST_ITEM(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_LIST_ITEM, GtkListItem))
+#define GTK_LIST_ITEM_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_LIST_ITEM, GtkListItemClass))
+#define GTK_IS_LIST_ITEM(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_LIST_ITEM))
+#define GTK_IS_LIST_ITEM_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_LIST_ITEM))
+#define GTK_LIST_ITEM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_LIST_ITEM, GtkListItemClass))
+
+typedef struct _GtkListItem GtkListItem;
+typedef struct _GtkListItemClass GtkListItemClass;
+
+GDK_AVAILABLE_IN_ALL
+GType           gtk_list_item_get_type                          (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_ALL
+gpointer        gtk_list_item_get_item                          (GtkListItem            *self);
+
+GDK_AVAILABLE_IN_ALL
+void            gtk_list_item_set_child                         (GtkListItem            *self,
+                                                                 GtkWidget              *child);
+GDK_AVAILABLE_IN_ALL
+GtkWidget *     gtk_list_item_get_child                         (GtkListItem            *self);
+
+
+G_END_DECLS
+
+#endif  /* __GTK_LIST_ITEM_H__ */
diff --git a/gtk/gtklistitemfactory.c b/gtk/gtklistitemfactory.c
index 7519bc01bb..a258dee1f4 100644
--- a/gtk/gtklistitemfactory.c
+++ b/gtk/gtklistitemfactory.c
@@ -21,6 +21,8 @@
 
 #include "gtklistitemfactoryprivate.h"
 
+#include "gtklistitemprivate.h"
+
 /**
  * SECTION:gtklistitemfactory
  * @Title: GtkListItemFactory
@@ -132,22 +134,41 @@ gtk_list_item_factory_new (GtkListCreateWidgetFunc create_func,
   return self;
 }
 
-GtkWidget *
+GtkListItem *
 gtk_list_item_factory_create (GtkListItemFactory *self)
 {
+  GtkWidget *widget, *result;
+
   g_return_val_if_fail (GTK_IS_LIST_ITEM_FACTORY (self), NULL);
 
-  return self->create_func (self->user_data);
+  widget = self->create_func (self->user_data);
+
+  result = gtk_list_item_new ("row");
+
+  gtk_list_item_set_child (GTK_LIST_ITEM (result), widget);
+
+  return GTK_LIST_ITEM (result);
 }
 
 void
 gtk_list_item_factory_bind (GtkListItemFactory *self,
-                            GtkWidget          *widget,
+                            GtkListItem        *list_item,
                             gpointer            item)
 {
   g_return_if_fail (GTK_IS_LIST_ITEM_FACTORY (self));
-  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (GTK_IS_LIST_ITEM (list_item));
+
+  gtk_list_item_bind (list_item, item);
 
-  self->bind_func (widget, item, self->user_data);
+  self->bind_func (gtk_list_item_get_child (list_item), item, self->user_data);
 }
 
+void
+gtk_list_item_factory_unbind (GtkListItemFactory *self,
+                              GtkListItem        *list_item)
+{
+  g_return_if_fail (GTK_IS_LIST_ITEM_FACTORY (self));
+  g_return_if_fail (GTK_IS_LIST_ITEM (list_item));
+
+  gtk_list_item_unbind (list_item);
+}
diff --git a/gtk/gtklistitemfactoryprivate.h b/gtk/gtklistitemfactoryprivate.h
index 28ae55b4aa..a6bec12faf 100644
--- a/gtk/gtklistitemfactoryprivate.h
+++ b/gtk/gtklistitemfactoryprivate.h
@@ -21,6 +21,7 @@
 #ifndef __GTK_LIST_ITEM_FACTORY_H__
 #define __GTK_LIST_ITEM_FACTORY_H__
 
+#include <gtk/gtklistitem.h>
 #include <gtk/gtklistview.h>
 
 G_BEGIN_DECLS
@@ -42,11 +43,13 @@ GtkListItemFactory *    gtk_list_item_factory_new               (GtkListCreateWi
                                                                  gpointer                user_data,
                                                                  GDestroyNotify          user_destroy);
 
-GtkWidget *             gtk_list_item_factory_create            (GtkListItemFactory     *self);
+GtkListItem *           gtk_list_item_factory_create            (GtkListItemFactory     *self);
 
 void                    gtk_list_item_factory_bind              (GtkListItemFactory     *self,
-                                                                 GtkWidget              *widget,
+                                                                 GtkListItem            *list_item,
                                                                  gpointer                item);
+void                    gtk_list_item_factory_unbind            (GtkListItemFactory     *self,
+                                                                 GtkListItem            *list_item);
 
 
 G_END_DECLS
diff --git a/gtk/gtklistitemmanager.c b/gtk/gtklistitemmanager.c
index 28630bda99..500cc38e5e 100644
--- a/gtk/gtklistitemmanager.c
+++ b/gtk/gtklistitemmanager.c
@@ -165,7 +165,7 @@ gtk_list_item_manager_create_list_item (GtkListItemManager *self,
                                         guint               position,
                                         GtkWidget          *next_sibling)
 {
-  GtkWidget *result;
+  GtkListItem *result;
   gpointer item;
 
   g_return_val_if_fail (GTK_IS_LIST_ITEM_MANAGER (self), NULL);
@@ -175,7 +175,7 @@ gtk_list_item_manager_create_list_item (GtkListItemManager *self,
   item = g_list_model_get_item (self->model, position);
   gtk_list_item_factory_bind (self->factory, result, item);
   g_object_unref (item);
-  gtk_widget_insert_before (result, self->widget, next_sibling);
+  gtk_widget_insert_before (GTK_WIDGET (result), self->widget, next_sibling);
 
-  return result;
+  return GTK_WIDGET (result);
 }
diff --git a/gtk/gtklistitemprivate.h b/gtk/gtklistitemprivate.h
new file mode 100644
index 0000000000..a0a90f7d79
--- /dev/null
+++ b/gtk/gtklistitemprivate.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * 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 2.1 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 <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __GTK_LIST_ITEM_PRIVATE_H__
+#define __GTK_LIST_ITEM_PRIVATE_H__
+
+#include "gtklistitem.h"
+
+G_BEGIN_DECLS
+
+GtkWidget *     gtk_list_item_new                               (const char             *css_name);
+
+void            gtk_list_item_bind                              (GtkListItem            *self,
+                                                                 gpointer                item);
+void            gtk_list_item_unbind                            (GtkListItem            *self);
+
+G_END_DECLS
+
+#endif  /* __GTK_LIST_ITEM_PRIVATE_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index e6b1bb271e..a7deda2bb7 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -278,6 +278,7 @@ gtk_public_sources = files([
   'gtklevelbar.c',
   'gtklinkbutton.c',
   'gtklistbox.c',
+  'gtklistitem.c',
   'gtklistitemfactory.c',
   'gtklistitemmanager.c',
   'gtklistlistmodel.c',
@@ -541,6 +542,7 @@ gtk_public_headers = files([
   'gtklevelbar.h',
   'gtklinkbutton.h',
   'gtklistbox.h',
+  'gtklistitem.h',
   'gtkliststore.h',
   'gtklistview.h',
   'gtklockbutton.h',


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