[gtk/wip/otte/listview: 201/215] filter: Add a custom filter
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/otte/listview: 201/215] filter: Add a custom filter
- Date: Tue, 26 Nov 2019 03:15:27 +0000 (UTC)
commit d085c1c23dec643b185d1cbe8f0b9873c95aa8d4
Author: Benjamin Otte <otte redhat com>
Date: Sun Nov 10 01:30:02 2019 +0100
filter: Add a custom filter
docs/reference/gtk/gtk4-sections.txt | 2 +
gtk/gtk.h | 1 +
gtk/gtkfilters.c | 103 +++++++++++++++++++++++++++++++++++
gtk/gtkfilters.h | 55 +++++++++++++++++++
gtk/meson.build | 2 +
5 files changed, 163 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index e368288901..9e48cb3a44 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -1502,6 +1502,8 @@ GtkFilter
gtk_filter_filter
<SUBSECTION>
gtk_filter_changed
+<SUBSECTION>
+gtk_custom_filter_new
<SUBSECTION Standard>
GTK_FILTER
GTK_IS_FILTER
diff --git a/gtk/gtk.h b/gtk/gtk.h
index 003f401c6d..c0061f8ae9 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -120,6 +120,7 @@
#include <gtk/gtkfilefilter.h>
#include <gtk/gtkfilter.h>
#include <gtk/gtkfilterlistmodel.h>
+#include <gtk/gtkfilters.h>
#include <gtk/gtkflattenlistmodel.h>
#include <gtk/gtkflowbox.h>
#include <gtk/gtkfontbutton.h>
diff --git a/gtk/gtkfilters.c b/gtk/gtkfilters.c
new file mode 100644
index 0000000000..5750717d08
--- /dev/null
+++ b/gtk/gtkfilters.c
@@ -0,0 +1,103 @@
+/*
+ * 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 "gtkfilters.h"
+
+#include "gtkintl.h"
+#include "gtktypebuiltins.h"
+
+struct _GtkCustomFilter
+{
+ GtkFilter parent_instance;
+
+ GtkCustomFilterFunc filter_func;
+ gpointer user_data;
+ GDestroyNotify user_destroy;
+};
+
+G_DEFINE_TYPE (GtkCustomFilter, gtk_custom_filter, GTK_TYPE_FILTER)
+
+static gboolean
+gtk_custom_filter_filter (GtkFilter *filter,
+ gpointer item)
+{
+ GtkCustomFilter *self = GTK_CUSTOM_FILTER (filter);
+
+ return self->filter_func (item, self->user_data);
+}
+
+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->filter = gtk_custom_filter_filter;
+
+ object_class->dispose = gtk_custom_filter_dispose;
+}
+
+static void
+gtk_custom_filter_init (GtkCustomFilter *self)
+{
+}
+
+/**
+ * gtk_custom_filter_new:
+ * @filter_func:
+ * @user_data: (allow none): user data to pass to @filter_func
+ * @user_destroy: destory notify
+ *
+ * Creates a new filter using the given @filter_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 filter_func,
+ gpointer user_data,
+ GDestroyNotify user_destroy)
+{
+ GtkCustomFilter *result;
+
+ result = g_object_new (GTK_TYPE_CUSTOM_FILTER, NULL);
+
+ result->filter_func = filter_func;
+ result->user_data = user_data;
+ result->user_destroy = user_destroy;
+
+ return GTK_FILTER (result);
+}
+
diff --git a/gtk/gtkfilters.h b/gtk/gtkfilters.h
new file mode 100644
index 0000000000..b86f53b7ba
--- /dev/null
+++ b/gtk/gtkfilters.h
@@ -0,0 +1,55 @@
+/*
+ * 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_FILTERS_H__
+#define __GTK_FILTERS_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 that may be filtered
+ * @user_data: user data
+ *
+ * User function that is called to determine if the @item should be visible.
+ * If it should be visible, 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 filter_func,
+ gpointer user_data,
+ GDestroyNotify user_destroy);
+
+
+G_END_DECLS
+
+#endif /* __GTK_FILTERS_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index 91c111c603..fc3764f9e0 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -246,6 +246,7 @@ gtk_public_sources = files([
'gtkfilefilter.c',
'gtkfilter.c',
'gtkfilterlistmodel.c',
+ 'gtkfilters.c',
'gtkfixed.c',
'gtkfixedlayout.c',
'gtkflattenlistmodel.c',
@@ -528,6 +529,7 @@ gtk_public_headers = files([
'gtkfilefilter.h',
'gtkfilter.h',
'gtkfilterlistmodel.h',
+ 'gtkfilters.h',
'gtkfixed.h',
'gtkfixedlayout.h',
'gtkflattenlistmodel.h',
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]