[gtk/wip/otte/listview: 181/183] Add GtkMultiSorter
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/otte/listview: 181/183] Add GtkMultiSorter
- Date: Tue, 10 Dec 2019 06:26:07 +0000 (UTC)
commit f4869e192ecf0ebd16f0681f8f22e4fa06c298a2
Author: Matthias Clasen <mclasen redhat com>
Date: Sun Dec 8 13:54:57 2019 -0500
Add GtkMultiSorter
This is a sorter that tries multiple sorters in turn.
docs/reference/gtk/gtk4-docs.xml | 1 +
docs/reference/gtk/gtk4-sections.txt | 17 +++
docs/reference/gtk/gtk4.types.in | 1 +
gtk/gtk.h | 1 +
gtk/gtkmultisorter.c | 236 +++++++++++++++++++++++++++++++++++
gtk/gtkmultisorter.h | 49 ++++++++
gtk/meson.build | 2 +
7 files changed, 307 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-docs.xml b/docs/reference/gtk/gtk4-docs.xml
index 292c5bc048..86c7aac6c0 100644
--- a/docs/reference/gtk/gtk4-docs.xml
+++ b/docs/reference/gtk/gtk4-docs.xml
@@ -54,6 +54,7 @@
<xi:include href="xml/gtksorter.xml" />
<xi:include href="xml/gtkcustomsorter.xml" />
<xi:include href="xml/gtkstringsorter.xml" />
+ <xi:include href="xml/gtkmultisorter.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 23c586cf3c..35292d9e87 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -2909,6 +2909,23 @@ GTK_CUSTOM_SORTER_GET_CLASS
gtk_custom_sorter_get_type
</SECTION>
+<SECTION>
+<FILE>gtkmultisorter</FILE>
+<TITLE>GtkMultiSorter</TITLE>
+GtkMultiSorter
+gtk_multi_sorter_new
+gtk_multi_sorter_append
+gtk_multi_sorter_remove
+<SUBSECTION Standard>
+GTK_MULTI_SORTER
+GTK_IS_MULTI_SORTER
+GTK_TYPE_MULTI_SORTER
+GTK_IS_MULTI_SORTER_CLASS
+GTK_MULTI_SORTER_GET_CLASS
+<SUBSECTION Private>
+gtk_multi_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 7db1dbb345..a483d093d7 100644
--- a/docs/reference/gtk/gtk4.types.in
+++ b/docs/reference/gtk/gtk4.types.in
@@ -125,6 +125,7 @@ gtk_menu_shell_get_type
gtk_menu_tool_button_get_type
gtk_message_dialog_get_type
gtk_mount_operation_get_type
+gtk_multi_sorter_get_type
gtk_native_get_type
gtk_no_selection_get_type
gtk_notebook_get_type
diff --git a/gtk/gtk.h b/gtk/gtk.h
index ee4b096874..7a8d076a53 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -179,6 +179,7 @@
#include <gtk/gtkmenutoolbutton.h>
#include <gtk/gtkmessagedialog.h>
#include <gtk/gtkmountoperation.h>
+#include <gtk/gtkmultisorter.h>
#include <gtk/gtknative.h>
#include <gtk/gtknativedialog.h>
#include <gtk/gtknoselection.h>
diff --git a/gtk/gtkmultisorter.c b/gtk/gtkmultisorter.c
new file mode 100644
index 0000000000..7eae9579d3
--- /dev/null
+++ b/gtk/gtkmultisorter.c
@@ -0,0 +1,236 @@
+/*
+ * 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 "gtkmultisorter.h"
+
+#include "gtkintl.h"
+#include "gtktypebuiltins.h"
+
+/**
+ * SECTION:multi-sorter
+ * @Title: GtkMultiSorter
+ * @Short_description: Combining multiple sorters
+ *
+ * GtkMultiSorter combines multiple sorters by trying them
+ * in turn. If the first sorter compares to items as equal,
+ * the second is tried next, and so on.
+ */
+struct _GtkMultiSorter
+{
+ GtkSorter parent_instance;
+
+ GSequence *sorters;
+};
+
+static GType
+gtk_multi_sorter_get_item_type (GListModel *list)
+{
+ return GTK_TYPE_SORTER;
+}
+
+static guint
+gtk_multi_sorter_get_n_items (GListModel *list)
+{
+ GtkMultiSorter *self = GTK_MULTI_SORTER (list);
+
+ return g_sequence_get_length (self->sorters);
+}
+
+static gpointer
+gtk_multi_sorter_get_item (GListModel *list,
+ guint position)
+{
+ GtkMultiSorter *self = GTK_MULTI_SORTER (list);
+ GSequenceIter *iter;
+
+ iter = g_sequence_get_iter_at_pos (self->sorters, position);
+
+ if (g_sequence_iter_is_end (iter))
+ return NULL;
+ else
+ return g_object_ref (g_sequence_get (iter));
+}
+
+static void
+gtk_multi_sorter_list_model_init (GListModelInterface *iface)
+{
+ iface->get_item_type = gtk_multi_sorter_get_item_type;
+ iface->get_n_items = gtk_multi_sorter_get_n_items;
+ iface->get_item = gtk_multi_sorter_get_item;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GtkMultiSorter, gtk_multi_sorter, GTK_TYPE_SORTER,
+ G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_multi_sorter_list_model_init))
+
+static GtkOrdering
+gtk_multi_sorter_compare (GtkSorter *sorter,
+ gpointer item1,
+ gpointer item2)
+{
+ GtkMultiSorter *self = GTK_MULTI_SORTER (sorter);
+ GtkOrdering result = GTK_ORDERING_EQUAL;
+ GSequenceIter *iter;
+
+ for (iter = g_sequence_get_begin_iter (self->sorters);
+ !g_sequence_iter_is_end (iter);
+ iter = g_sequence_iter_next (iter))
+ {
+ GtkSorter *child = g_sequence_get (iter);
+
+ result = gtk_sorter_compare (child, item1, item2);
+ if (result != GTK_ORDERING_EQUAL)
+ break;
+ }
+
+ return result;
+}
+
+static GtkSorterOrder
+gtk_multi_sorter_get_order (GtkSorter *sorter)
+{
+ GtkMultiSorter *self = GTK_MULTI_SORTER (sorter);
+ GtkSorterOrder result = GTK_SORTER_ORDER_NONE;
+ GSequenceIter *iter;
+
+ for (iter = g_sequence_get_begin_iter (self->sorters);
+ !g_sequence_iter_is_end (iter);
+ iter = g_sequence_iter_next (iter))
+ {
+ GtkSorter *child = g_sequence_get (iter);
+ GtkSorterOrder child_order;
+
+ child_order = gtk_sorter_get_order (child);
+ switch (child_order)
+ {
+ case GTK_SORTER_ORDER_PARTIAL:
+ result = GTK_SORTER_ORDER_PARTIAL;
+ break;
+ case GTK_SORTER_ORDER_INVALID:
+ return result;
+ case GTK_SORTER_ORDER_NONE:
+ break;
+ case GTK_SORTER_ORDER_TOTAL:
+ return GTK_SORTER_ORDER_TOTAL;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+ }
+
+ return result;
+}
+
+static void
+gtk_multi_sorter_dispose (GObject *object)
+{
+ GtkMultiSorter *self = GTK_MULTI_SORTER (object);
+
+ g_clear_pointer (&self->sorters, g_sequence_free);
+
+ G_OBJECT_CLASS (gtk_multi_sorter_parent_class)->dispose (object);
+}
+
+static void
+gtk_multi_sorter_class_init (GtkMultiSorterClass *class)
+{
+ GtkSorterClass *sorter_class = GTK_SORTER_CLASS (class);
+ GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+ sorter_class->compare = gtk_multi_sorter_compare;
+ sorter_class->get_order = gtk_multi_sorter_get_order;
+
+ object_class->dispose = gtk_multi_sorter_dispose;
+}
+
+static void
+gtk_multi_sorter_init (GtkMultiSorter *self)
+{
+ self->sorters = g_sequence_new (g_object_unref);
+}
+
+/**
+ * gtk_multi_sorter_new:
+ *
+ * Creates a new multi sorter.
+ *
+ * This sorter compares items by trying each of the sorters
+ * in turn, until one returns non-zero. In particular, if
+ * no sorter has been added to it, it will always compare
+ * items as equal.
+ *
+ * Returns: a new #GtkSorter
+ */
+GtkSorter *
+gtk_multi_sorter_new (void)
+{
+ return g_object_new (GTK_TYPE_MULTI_SORTER, NULL);
+}
+
+/**
+ * gtk_multi_sorter_append:
+ * @self: a #GtkMultiSorter
+ * @sorter: (transfer none): a sorter to add
+ *
+ * Add @sorter to @self to use for sorting at the end. @self
+ * will consult all existing sorters before it will sort with
+ * the given @sorter.
+ */
+void
+gtk_multi_sorter_append (GtkMultiSorter *self,
+ GtkSorter *sorter)
+{
+ g_return_if_fail (GTK_IS_MULTI_SORTER (self));
+ g_return_if_fail (GTK_IS_SORTER (sorter));
+
+ g_sequence_append (self->sorters, g_object_ref (sorter));
+
+ gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_MORE_STRICT);
+}
+
+/**
+ * gtk_multi_sorter_remove:
+ * @self: a #GtkMultiSorter
+ * @position: position of sorter to remove
+ *
+ * Removes the sorter at the given @position from the list of sorter
+ * used by @self.
+ *
+ * If @position is larger than the number of sorters, nothing happens.
+ */
+void
+gtk_multi_sorter_remove (GtkMultiSorter *self,
+ guint position)
+{
+ GSequenceIter *iter;
+ guint length;
+
+ g_return_if_fail (GTK_IS_MULTI_SORTER (self));
+
+ length = g_sequence_get_length (self->sorters);
+ if (position >= length)
+ return;
+
+ iter = g_sequence_get_iter_at_pos (self->sorters, position);
+ g_sequence_remove (iter);
+
+ gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_LESS_STRICT);
+}
+
diff --git a/gtk/gtkmultisorter.h b/gtk/gtkmultisorter.h
new file mode 100644
index 0000000000..4f265bf6a2
--- /dev/null
+++ b/gtk/gtkmultisorter.h
@@ -0,0 +1,49 @@
+/*
+ * 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_MULTI_SORTER_H__
+#define __GTK_MULTI_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_MULTI_SORTER (gtk_multi_sorter_get_type ())
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkMultiSorter, gtk_multi_sorter, GTK, MULTI_SORTER, GtkSorter)
+
+GDK_AVAILABLE_IN_ALL
+GtkSorter * gtk_multi_sorter_new (void);
+
+GDK_AVAILABLE_IN_ALL
+void gtk_multi_sorter_append (GtkMultiSorter *self,
+ GtkSorter *sorter);
+
+GDK_AVAILABLE_IN_ALL
+void gtk_multi_sorter_remove (GtkMultiSorter *self,
+ guint position);
+
+G_END_DECLS
+
+#endif /* __GTK_MULTI_SORTER_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index 65c8c7b3b5..263753481b 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -316,6 +316,7 @@ gtk_public_sources = files([
'gtkmodelmenuitem.c',
'gtkmodules.c',
'gtkmountoperation.c',
+ 'gtkmultisorter.c',
'gtknativedialog.c',
'gtknomediafile.c',
'gtknoselection.c',
@@ -596,6 +597,7 @@ gtk_public_headers = files([
'gtkmenutoolbutton.h',
'gtkmessagedialog.h',
'gtkmountoperation.h',
+ 'gtkmultisorter.h',
'gtknative.h',
'gtknativedialog.h',
'gtknoselection.h',
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]