[gtk/wip/matthiasc/listview-2: 6/179] Add GtkCustomFilter



commit f4ec02ef4067a162a6ac181929f140838747ebf3
Author: Benjamin Otte <otte redhat com>
Date:   Sun Nov 10 01:30:02 2019 +0100

    Add GtkCustomFilter

 docs/reference/gtk/gtk4-docs.xml     |   1 +
 docs/reference/gtk/gtk4-sections.txt |  19 +++++
 docs/reference/gtk/gtk4.types.in     |   1 +
 gtk/gtk.h                            |   1 +
 gtk/gtkcustomfilter.c                | 157 +++++++++++++++++++++++++++++++++++
 gtk/gtkcustomfilter.h                |  60 +++++++++++++
 gtk/meson.build                      |   2 +
 7 files changed, 241 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-docs.xml b/docs/reference/gtk/gtk4-docs.xml
index 48d28d412ca..3495ec16079 100644
--- a/docs/reference/gtk/gtk4-docs.xml
+++ b/docs/reference/gtk/gtk4-docs.xml
@@ -51,6 +51,7 @@
       <xi:include href="xml/gtkfilterlistmodel.xml" />
       <section>
         <xi:include href="xml/gtkfilter.xml" />
+        <xi:include href="xml/gtkcustomfilter.xml" />
       </section>
       <xi:include href="xml/gtkflattenlistmodel.xml" />
       <xi:include href="xml/gtkmaplistmodel.xml" />
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index a3f4be6ad65..dc0df344c2d 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -1193,6 +1193,8 @@ gtk_filter_get_strictness
 <SUBSECTION>
 GtkFilterChange
 gtk_filter_changed
+<SUBSECTION>
+gtk_custom_filter_new
 <SUBSECTION Standard>
 GTK_FILTER
 GTK_IS_FILTER
@@ -1204,6 +1206,23 @@ GTK_FILTER_GET_CLASS
 gtk_filter_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtkcustomfilter</FILE>
+<TITLE>GtkCustomFilter</TITLE>
+GtkCustomFilter
+GtkCustomFilterFunc
+gtk_custom_filter_new
+<SUBSECTION Standard>
+GTK_CUSTOM_FILTER
+GTK_IS_CUSTOM_FILTER
+GTK_TYPE_CUSTOM_FILTER
+GTK_CUSTOM_FILTER_CLASS
+GTK_IS_CUSTOM_FILTER_CLASS
+GTK_CUSTOM_FILTER_GET_CLASS
+<SUBSECTION Private>
+gtk_custom_filter_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtkfilterlistmodel</FILE>
 <TITLE>GtkFilterListModel</TITLE>
diff --git a/docs/reference/gtk/gtk4.types.in b/docs/reference/gtk/gtk4.types.in
index df153a01e46..a8975f30bb8 100644
--- a/docs/reference/gtk/gtk4.types.in
+++ b/docs/reference/gtk/gtk4.types.in
@@ -53,6 +53,7 @@ gtk_constraint_guide_get_type
 gtk_constraint_layout_get_type
 gtk_constraint_target_get_type
 gtk_css_provider_get_type
+gtk_custom_filter_get_type
 gtk_dialog_get_type
 gtk_directory_list_get_type
 gtk_drag_icon_get_type
diff --git a/gtk/gtk.h b/gtk/gtk.h
index 6953d0d3e50..60aaf1754ca 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -116,6 +116,7 @@
 #include <gtk/gtkfilefilter.h>
 #include <gtk/gtkfilter.h>
 #include <gtk/gtkfilterlistmodel.h>
+#include <gtk/gtkcustomfilter.h>
 #include <gtk/gtkflattenlistmodel.h>
 #include <gtk/gtkflowbox.h>
 #include <gtk/gtkfontbutton.h>
diff --git a/gtk/gtkcustomfilter.c b/gtk/gtkcustomfilter.c
new file mode 100644
index 00000000000..d2ec8dc23a7
--- /dev/null
+++ b/gtk/gtkcustomfilter.c
@@ -0,0 +1,157 @@
+/*
+ * Copyright © 2019 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 "gtkcustomfilter.h"
+
+#include "gtkintl.h"
+#include "gtktypebuiltins.h"
+
+/**
+ * SECTION:gtkcustomfilter
+ * @Title: GtkCustomFilter
+ * @Short_description: Filtering with callbacks
+ *
+ * #GtkCustomFilter is a #GtkFilter that uses a callback to determine whether
+ * to include an item or not.
+ */
+struct _GtkCustomFilter
+{
+  GtkFilter parent_instance;
+
+  GtkCustomFilterFunc match_func;
+  gpointer user_data;
+  GDestroyNotify user_destroy;
+};
+
+G_DEFINE_TYPE (GtkCustomFilter, gtk_custom_filter, GTK_TYPE_FILTER)
+
+static gboolean
+gtk_custom_filter_match (GtkFilter *filter,
+                         gpointer   item)
+{
+  GtkCustomFilter *self = GTK_CUSTOM_FILTER (filter);
+
+  if (!self->match_func)
+    return TRUE;
+
+  return self->match_func (item, self->user_data);
+}
+
+static GtkFilterMatch
+gtk_custom_filter_get_strictness (GtkFilter *filter)
+{
+  GtkCustomFilter *self = GTK_CUSTOM_FILTER (filter);
+
+  if (!self->match_func)
+    return GTK_FILTER_MATCH_ALL;
+
+  return GTK_FILTER_MATCH_SOME;
+}
+
+static void
+gtk_custom_filter_dispose (GObject *object)
+{
+  GtkCustomFilter *self = GTK_CUSTOM_FILTER (object);
+
+  if (self->user_destroy)
+    self->user_destroy (self->user_data);
+
+  G_OBJECT_CLASS (gtk_custom_filter_parent_class)->dispose (object);
+}
+
+static void
+gtk_custom_filter_class_init (GtkCustomFilterClass *class)
+{
+  GtkFilterClass *filter_class = GTK_FILTER_CLASS (class);
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  filter_class->match = gtk_custom_filter_match;
+  filter_class->get_strictness = gtk_custom_filter_get_strictness;
+
+  object_class->dispose = gtk_custom_filter_dispose;
+}
+
+static void
+gtk_custom_filter_init (GtkCustomFilter *self)
+{
+}
+
+/**
+ * gtk_custom_filter_new:
+ * @match_func: (nullable): function to filter items
+ * @user_data: (nullable): user data to pass to @match_func
+ * @user_destroy: destory notify
+ *
+ * Creates a new filter using the given @match_func to filter
+ * items.
+ *
+ * If the filter func changes its filtering behavior,
+ * gtk_filter_changed() needs to be called.
+ *
+ * Returns: a new #GtkFilter
+ **/
+GtkFilter *
+gtk_custom_filter_new (GtkCustomFilterFunc match_func,
+                       gpointer            user_data,
+                       GDestroyNotify      user_destroy)
+{
+  GtkCustomFilter *result;
+
+  result = g_object_new (GTK_TYPE_CUSTOM_FILTER, NULL);
+
+  gtk_custom_filter_set_filter_func (result, match_func, user_data, user_destroy);
+
+  return GTK_FILTER (result);
+}
+
+/**
+ * gtk_custom_filter_set_filter_func:
+ * @self: a #GtkCustomFilter
+ * @match_func: (nullable): function to filter items
+ * @user_data: (nullable): user data to pass to @match_func
+ * @user_destroy: destory notify
+ *
+ * Sets (or unsets) the function used for filtering items.
+ * 
+ * If the filter func changes its filtering behavior,
+ * gtk_filter_changed() needs to be called.
+ *
+ * If a previous function was set, its @user_destroy will be
+ * called now.
+ **/
+void
+gtk_custom_filter_set_filter_func (GtkCustomFilter     *self,
+                                   GtkCustomFilterFunc  match_func,
+                                   gpointer             user_data,
+                                   GDestroyNotify       user_destroy)
+{
+  g_return_if_fail (GTK_IS_CUSTOM_FILTER (self));
+  g_return_if_fail (match_func || (user_data == NULL && !user_destroy));
+
+  if (self->user_destroy)
+    self->user_destroy (self->user_data);
+
+  self->match_func = match_func;
+  self->user_data = user_data;
+  self->user_destroy = user_destroy;
+
+  gtk_filter_changed (GTK_FILTER (self), GTK_FILTER_CHANGE_DIFFERENT);
+}
diff --git a/gtk/gtkcustomfilter.h b/gtk/gtkcustomfilter.h
new file mode 100644
index 00000000000..c6ee0638867
--- /dev/null
+++ b/gtk/gtkcustomfilter.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright © 2019 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_CUSTOM_FILTER_H__
+#define __GTK_CUSTOM_FILTER_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtkfilter.h>
+
+G_BEGIN_DECLS
+
+/**
+ * GtkCustomFilterFunc:
+ * @item: (type GObject): The item to be matched
+ * @user_data: user data
+ *
+ * User function that is called to determine if the @item should be matched.
+ * If the filter matches the item, this function must return %TRUE. If the
+ * item should be filtered out, %FALSE must be returned.
+ *
+ * Returns: %TRUE to keep the item around
+ */
+typedef gboolean (* GtkCustomFilterFunc) (gpointer item, gpointer user_data);
+
+#define GTK_TYPE_CUSTOM_FILTER             (gtk_custom_filter_get_type ())
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkCustomFilter, gtk_custom_filter, GTK, CUSTOM_FILTER, GtkFilter)
+GDK_AVAILABLE_IN_ALL
+GtkFilter *             gtk_custom_filter_new                   (GtkCustomFilterFunc     match_func,
+                                                                 gpointer                user_data,
+                                                                 GDestroyNotify          user_destroy);
+
+GDK_AVAILABLE_IN_ALL
+void                    gtk_custom_filter_set_filter_func       (GtkCustomFilter        *self,
+                                                                 GtkCustomFilterFunc     match_func,
+                                                                 gpointer                user_data,
+                                                                 GDestroyNotify          user_destroy);
+
+G_END_DECLS
+
+#endif /* __GTK_CUSTOM_FILTER_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index 91fb469abc2..6103a77e3f1 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -199,6 +199,7 @@ gtk_public_sources = files([
   'gtkconstraintlayout.c',
   'gtkconstraint.c',
   'gtkcssprovider.c',
+  'gtkcustomfilter.c',
   'gtkcustomlayout.c',
   'gtkdialog.c',
   'gtkdirectorylist.c',
@@ -454,6 +455,7 @@ gtk_public_headers = files([
   'gtkconstraintlayout.h',
   'gtkconstraint.h',
   'gtkcssprovider.h',
+  'gtkcustomfilter.h',
   'gtkcustomlayout.h',
   'gtkdebug.h',
   'gtkdialog.h',


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