[gtk/wip/otte/listview: 10/46] listview: Add GtkListItem



commit db438f5fc69c25361e1d236a1a70507a9175dcc1
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 |  16 +++
 gtk/gtk.h                            |   1 +
 gtk/gtklistitem.c                    | 211 +++++++++++++++++++++++++++++++++++
 gtk/gtklistitem.h                    |  41 +++++++
 gtk/gtklistitemfactory.c             |  31 ++++-
 gtk/gtklistitemfactoryprivate.h      |   7 +-
 gtk/gtklistitemmanager.c             |   6 +-
 gtk/gtklistitemprivate.h             |  38 +++++++
 gtk/meson.build                      |   2 +
 9 files changed, 343 insertions(+), 10 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index be3ebc3bc4..c3b0642a58 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -493,6 +493,22 @@ 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
+<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>gtklistview</FILE>
 <TITLE>GtkListView</TITLE>
diff --git a/gtk/gtk.h b/gtk/gtk.h
index 9c374597dc..7e4c1a52fc 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -148,6 +148,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..2cddbea044
--- /dev/null
+++ b/gtk/gtklistitem.c
@@ -0,0 +1,211 @@
+/*
+ * 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 "gtkintl.h"
+
+/**
+ * SECTION:gtklistitem
+ * @title: GtkListItem
+ * @short_description: Widget used to represent items of a ListModel
+ * @see_also: #GtkListView, #GListModel
+ *
+ * #GtkListItem is the widget that GTK list-handling containers such
+ * as #GtkListView create to represent items in a #GListModel.  
+ * They are managed by the container and cannot be created by application
+ * code.
+ *
+ * #GtkListIems are container widgets that need to be populated by
+ * application code. The container provides functions to do that.
+ *
+ * #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
+{
+  GtkBin parent_instance;
+
+  GObject *item;
+};
+
+enum
+{
+  PROP_0,
+  PROP_ITEM,
+
+  N_PROPS
+};
+
+G_DEFINE_TYPE (GtkListItem, gtk_list_item, GTK_TYPE_BIN)
+
+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_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_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)
+    {
+    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: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"));
+}
+
+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;
+}
+
+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));
+
+  _gtk_bin_set_child (GTK_BIN (self), child);
+  gtk_widget_insert_after (child, GTK_WIDGET (self), NULL);
+}
+
+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..bd09ba028b
--- /dev/null
+++ b/gtk/gtklistitem.h
@@ -0,0 +1,41 @@
+/*
+ * 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/gtkbin.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_LIST_ITEM         (gtk_list_item_get_type ())
+
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkListItem, gtk_list_item, GTK, LIST_ITEM, GtkBin)
+
+GDK_AVAILABLE_IN_ALL
+gpointer        gtk_list_item_get_item                          (GtkListItem            *self);
+
+G_END_DECLS
+
+#endif  /* __GTK_LIST_ITEM_H__ */
diff --git a/gtk/gtklistitemfactory.c b/gtk/gtklistitemfactory.c
index cc69210aeb..9df1165eeb 100644
--- a/gtk/gtklistitemfactory.c
+++ b/gtk/gtklistitemfactory.c
@@ -21,6 +21,8 @@
 
 #include "gtklistitemfactoryprivate.h"
 
+#include "gtklistitemprivate.h"
+
 struct _GtkListItemFactory
 {
   GObject parent_instance;
@@ -84,22 +86,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_bin_get_child (GTK_BIN (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..254f8ae72f
--- /dev/null
+++ b/gtk/gtklistitemprivate.h
@@ -0,0 +1,38 @@
+/*
+ * 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_set_child                         (GtkListItem            *item,
+                                                                 GtkWidget              *child);
+
+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 bd2fee1df1..c6c3af73cc 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -274,6 +274,7 @@ gtk_public_sources = files([
   'gtklevelbar.c',
   'gtklinkbutton.c',
   'gtklistbox.c',
+  'gtklistitem.c',
   'gtklistitemfactory.c',
   'gtklistitemmanager.c',
   'gtklistlistmodel.c',
@@ -540,6 +541,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]