[gtk/wip/otte/listview: 26/161] Add GtkCustomSorter



commit de26550c87e1732f4d45390254a493b718e65665
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Dec 2 23:43:14 2019 -0500

    Add GtkCustomSorter
    
    This is a GtkSorter implementation which uses a GCompareDataFunc.

 docs/reference/gtk/gtk4-docs.xml     |   1 +
 docs/reference/gtk/gtk4-sections.txt |  15 +++++
 docs/reference/gtk/gtk4.types.in     |   1 +
 gtk/gtk.h                            |   1 +
 gtk/gtkcustomsorter.c                | 108 +++++++++++++++++++++++++++++++++++
 gtk/gtkcustomsorter.h                |  43 ++++++++++++++
 gtk/meson.build                      |   2 +
 7 files changed, 171 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-docs.xml b/docs/reference/gtk/gtk4-docs.xml
index 704f6a107d..90ef981655 100644
--- a/docs/reference/gtk/gtk4-docs.xml
+++ b/docs/reference/gtk/gtk4-docs.xml
@@ -56,6 +56,7 @@
       <xi:include href="xml/gtksortlistmodel.xml" />
       <section>
         <xi:include href="xml/gtksorter.xml" />
+        <xi:include href="xml/gtkcustomsorter.xml" />
       </section>
       <xi:include href="xml/gtktreelistmodel.xml" />
       <xi:include href="xml/gtkselectionmodel.xml" />
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index e9dac2c9e9..bfa4d9e9bc 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -2789,6 +2789,21 @@ GTK_SORTER_GET_CLASS
 gtk_sorter_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtkcustomsorter</FILE>
+<TITLE>GtkCustomSorter</TITLE>
+GtkCustomSorter
+gtk_custom_sorter_new
+<SUBSECTION Standard>
+GTK_CUSTOM_SORTER
+GTK_IS_CUSTOM_SORTER
+GTK_TYPE_CUSTOM_SORTER
+GTK_IS_CUSTOM_SORTER_CLASS
+GTK_CUSTOM_SORTER_GET_CLASS
+<SUBSECTION Private>
+gtk_custom_sorter_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtksortlistmodel</FILE>
 <TITLE>GtkSortListModel</TITLE>
diff --git a/docs/reference/gtk/gtk4.types.in b/docs/reference/gtk/gtk4.types.in
index 3e8ddf531c..1af9856714 100644
--- a/docs/reference/gtk/gtk4.types.in
+++ b/docs/reference/gtk/gtk4.types.in
@@ -54,6 +54,7 @@ gtk_constraint_guide_get_type
 gtk_constraint_layout_get_type
 gtk_constraint_target_get_type
 gtk_container_get_type
+gtk_custom_sorter_get_type
 gtk_css_provider_get_type
 gtk_custom_filter_get_type
 gtk_dialog_get_type
diff --git a/gtk/gtk.h b/gtk/gtk.h
index df2c7569b5..10420b5768 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -89,6 +89,7 @@
 #include <gtk/gtkcontainer.h>
 #include <gtk/gtkcssprovider.h>
 #include <gtk/gtkcustomlayout.h>
+#include <gtk/gtkcustomsorter.h>
 #include <gtk/gtkdebug.h>
 #include <gtk/gtkdialog.h>
 #include <gtk/gtkdirectorylist.h>
diff --git a/gtk/gtkcustomsorter.c b/gtk/gtkcustomsorter.c
new file mode 100644
index 0000000000..9510795710
--- /dev/null
+++ b/gtk/gtkcustomsorter.c
@@ -0,0 +1,108 @@
+/*
+ * Copyright © 2019 Matthias Clasen
+ *
+ * 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: Matthias Clasen <mclasen redhat com>
+ */
+
+#include "config.h"
+
+#include "gtkcustomsorter.h"
+
+#include "gtkintl.h"
+#include "gtktypebuiltins.h"
+
+/**
+ * SECTION:gtkcustomsorter
+ * @Title: GtkCustomSorter
+ * @Short_description: Sorting with a callback
+ *
+ * GtkCustomSorter is a #GtkSorter implementation that sorts
+ * via a traditional #GCompareDataFunc callback.
+ */
+struct _GtkCustomSorter
+{
+  GtkSorter parent_instance;
+
+  GCompareDataFunc sort_func;
+  gpointer user_data;
+  GDestroyNotify user_destroy;
+};
+
+G_DEFINE_TYPE (GtkCustomSorter, gtk_custom_sorter, GTK_TYPE_SORTER)
+
+static GtkOrdering
+gtk_custom_sorter_compare (GtkSorter *sorter,
+                           gpointer   item1,
+                           gpointer   item2)
+{
+  GtkCustomSorter *self = GTK_CUSTOM_SORTER (sorter);
+
+  return gtk_ordering_from_cmpfunc (self->sort_func (item1, item2, self->user_data));
+}
+
+static void
+gtk_custom_sorter_dispose (GObject *object)
+{
+  GtkCustomSorter *self = GTK_CUSTOM_SORTER (object);
+
+  if (self->user_destroy)
+    self->user_destroy (self->user_data);
+
+  G_OBJECT_CLASS (gtk_custom_sorter_parent_class)->dispose (object);
+}
+
+static void
+gtk_custom_sorter_class_init (GtkCustomSorterClass *class)
+{
+  GtkSorterClass *sorter_class = GTK_SORTER_CLASS (class);
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  sorter_class->compare = gtk_custom_sorter_compare;
+
+  object_class->dispose = gtk_custom_sorter_dispose;
+}
+
+static void
+gtk_custom_sorter_init (GtkCustomSorter *self)
+{
+}
+
+/**
+ * gtk_custom_sorter_new:
+ * @sort_func: the #GCompareDataFunc to use for sorting
+ * @user_data: (nullable): user data to pass to @sort_func
+ * @user_destroy: (nullable): destroy notify for @user_data
+ *
+ * Creates a new #GtkSorter that works by calling
+ * @sort_func to compare items.
+ *
+ * Returns: a new #GTkSorter
+ */ 
+GtkSorter *
+gtk_custom_sorter_new (GCompareDataFunc sort_func,
+                       gpointer         user_data,
+                       GDestroyNotify   user_destroy)
+{
+  GtkCustomSorter *sorter;
+
+  sorter = g_object_new (GTK_TYPE_CUSTOM_SORTER, NULL);
+
+  sorter->sort_func = sort_func;
+  sorter->user_data = user_data;
+  sorter->user_destroy = user_destroy;
+
+  return GTK_SORTER (sorter);
+}
diff --git a/gtk/gtkcustomsorter.h b/gtk/gtkcustomsorter.h
new file mode 100644
index 0000000000..e003cddce9
--- /dev/null
+++ b/gtk/gtkcustomsorter.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2019 Matthias Clasen
+ *
+ * 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: Matthias Clasen <mclasen redhat com>
+ */
+
+#ifndef __GTK_CUSTOM_SORTER_H__
+#define __GTK_CUSTOM_SORTER_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtkexpression.h>
+#include <gtk/gtksorter.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_CUSTOM_SORTER             (gtk_custom_sorter_get_type ())
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkCustomSorter, gtk_custom_sorter, GTK, CUSTOM_SORTER, GtkSorter)
+
+GDK_AVAILABLE_IN_ALL
+GtkSorter *             gtk_custom_sorter_new                   (GCompareDataFunc        sort_func,
+                                                                 gpointer                user_data,
+                                                                 GDestroyNotify          user_destroy);
+
+G_END_DECLS
+
+#endif /* __GTK_CUSTOM_SORTER_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index 61873947d6..ed49bf37a1 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -214,6 +214,7 @@ gtk_public_sources = files([
   'gtkcontainer.c',
   'gtkcssprovider.c',
   'gtkcustomfilter.c',
+  'gtkcustomsorter.c',
   'gtkdialog.c',
   'gtkdirectorylist.c',
   'gtkdnd.c',
@@ -487,6 +488,7 @@ gtk_public_headers = files([
   'gtkcssprovider.h',
   'gtkcustomfilter.h',
   'gtkcustomlayout.h',
+  'gtkcustomsorter.h',
   'gtkdebug.h',
   'gtkdialog.h',
   'gtkdirectorylist.h',


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