[gtk/wip/otte/listview: 885/1040] filter: Add GtkStringFilter
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/otte/listview: 885/1040] filter: Add GtkStringFilter
- Date: Sun, 2 Feb 2020 23:37:00 +0000 (UTC)
commit 2a3030269bc3182c39a6ca56c1d9b63e6dc67ba0
Author: Benjamin Otte <otte redhat com>
Date: Sat Nov 16 22:15:51 2019 +0100
filter: Add GtkStringFilter
Users provide a search filter and an expression that evaluates the items
to a string and then the filter goes and matches those strings to the
search term.
docs/reference/gtk/gtk4-docs.xml | 1 +
docs/reference/gtk/gtk4-sections.txt | 18 ++
docs/reference/gtk/gtk4.types.in | 1 +
gtk/gtk.h | 1 +
gtk/gtkstringfilter.c | 485 +++++++++++++++++++++++++++++++++++
gtk/gtkstringfilter.h | 81 ++++++
gtk/meson.build | 2 +
7 files changed, 589 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-docs.xml b/docs/reference/gtk/gtk4-docs.xml
index cb93447ed0..d148e28efd 100644
--- a/docs/reference/gtk/gtk4-docs.xml
+++ b/docs/reference/gtk/gtk4-docs.xml
@@ -49,6 +49,7 @@
<section>
<xi:include href="xml/gtkfilter.xml" />
<xi:include href="xml/gtkcustomfilter.xml" />
+ <xi:include href="xml/gtkstringfilter.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 4497825b34..289ac80a15 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -7240,3 +7240,21 @@ GTK_TYPE_EXPRESSION
<SUBSECTION Private>
gtk_expression_get_type
</SECTION>
+
+<SECTION>
+<FILE>gtkstringfilter</FILE>
+GtkStringFilter
+GtkStringFilterMatchMode
+gtk_string_filter_new
+gtk_string_filter_get_search
+gtk_string_filter_set_search
+gtk_string_filter_get_expression
+gtk_string_filter_set_expression
+gtk_string_filter_get_ignore_case
+gtk_string_filter_set_ignore_case
+gtk_string_filter_get_match_mode
+gtk_string_filter_set_match_mode
+
+<SUBSECTION Private>
+gtk_string_filter_get_type
+</SECTION>
diff --git a/docs/reference/gtk/gtk4.types.in b/docs/reference/gtk/gtk4.types.in
index 4013766bed..cbf2bcc42e 100644
--- a/docs/reference/gtk/gtk4.types.in
+++ b/docs/reference/gtk/gtk4.types.in
@@ -186,6 +186,7 @@ gtk_stack_page_get_type
gtk_stack_sidebar_get_type
gtk_stack_switcher_get_type
gtk_statusbar_get_type
+gtk_string_filter_get_type
gtk_switch_get_type
gtk_level_bar_get_type
gtk_style_context_get_type
diff --git a/gtk/gtk.h b/gtk/gtk.h
index eb145dc0f4..e69f48883e 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -220,6 +220,7 @@
#include <gtk/gtkstack.h>
#include <gtk/gtkstackswitcher.h>
#include <gtk/gtkstatusbar.h>
+#include <gtk/gtkstringfilter.h>
#include <gtk/gtkstylecontext.h>
#include <gtk/gtkstyleprovider.h>
#include <gtk/gtkswitch.h>
diff --git a/gtk/gtkstringfilter.c b/gtk/gtkstringfilter.c
new file mode 100644
index 0000000000..59c2726354
--- /dev/null
+++ b/gtk/gtkstringfilter.c
@@ -0,0 +1,485 @@
+/*
+ * 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 "gtkstringfilter.h"
+
+#include "gtkintl.h"
+#include "gtktypebuiltins.h"
+
+/**
+ * SECTION:gtkstringfilter
+ * @Title: GtkStringFilter
+ * @Short_description: Filtering by string
+ *
+ * GtkStringFilter determines whether to include items by looking
+ * at strings and comparing them to a fixed search term. The strings
+ * are obtained from the items by evaluating a #GtkExpression.
+ *
+ * GtkStringFilter has several different modes of comparison - it
+ * can match the whole string, just a prefix, or any substring.
+ */
+
+struct _GtkStringFilter
+{
+ GtkFilter parent_instance;
+
+ char *search;
+ char *search_prepared;
+
+ gboolean ignore_case;
+ GtkStringFilterMatchMode match_mode;
+
+ GtkExpression *expression;
+};
+
+enum {
+ PROP_0,
+ PROP_EXPRESSION,
+ PROP_IGNORE_CASE,
+ PROP_MATCH_MODE,
+ PROP_SEARCH,
+ NUM_PROPERTIES
+};
+
+G_DEFINE_TYPE (GtkStringFilter, gtk_string_filter, GTK_TYPE_FILTER)
+
+static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
+
+static char *
+gtk_string_filter_prepare (GtkStringFilter *self,
+ const char *s)
+{
+ char *tmp;
+ char *result;
+
+ if (s == NULL || s[0] == '\0')
+ return NULL;
+
+ tmp = g_utf8_normalize (s, -1, G_NORMALIZE_ALL);
+
+ if (!self->ignore_case)
+ return tmp;
+
+ result = g_utf8_casefold (tmp, -1);
+ g_free (tmp);
+
+ return result;
+}
+
+/* This is necessary because code just looks at self->search otherwise
+ * and that can be the empty string...
+ */
+static gboolean
+gtk_string_filter_has_search (GtkStringFilter *self)
+{
+ return self->search_prepared != NULL;
+}
+
+static gboolean
+gtk_string_filter_match (GtkFilter *filter,
+ gpointer item)
+{
+ GtkStringFilter *self = GTK_STRING_FILTER (filter);
+ GValue value = G_VALUE_INIT;
+ char *prepared;
+ const char *s;
+ gboolean result;
+
+ if (!gtk_string_filter_has_search (self))
+ return TRUE;
+
+ if (self->expression == NULL ||
+ !gtk_expression_evaluate (self->expression, item, &value))
+ return FALSE;
+ s = g_value_get_string (&value);
+ if (s == NULL)
+ return FALSE;
+ prepared = gtk_string_filter_prepare (self, s);
+
+ switch (self->match_mode)
+ {
+ case GTK_STRING_FILTER_MATCH_MODE_EXACT:
+ result = strcmp (prepared, self->search_prepared) == 0;
+ break;
+ case GTK_STRING_FILTER_MATCH_MODE_SUBSTRING:
+ result = strstr (prepared, self->search_prepared) != NULL;
+ break;
+ case GTK_STRING_FILTER_MATCH_MODE_PREFIX:
+ result = g_str_has_prefix (prepared, self->search_prepared);
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
+#if 0
+ g_print ("%s (%s) %s %s (%s)\n", s, prepared, result ? "==" : "!=", self->search, self->search_prepared);
+#endif
+
+ g_free (prepared);
+ g_value_unset (&value);
+
+ return result;
+}
+
+static GtkFilterMatch
+gtk_string_filter_get_strictness (GtkFilter *filter)
+{
+ GtkStringFilter *self = GTK_STRING_FILTER (filter);
+
+ if (!gtk_string_filter_has_search (self))
+ return GTK_FILTER_MATCH_ALL;
+
+ if (self->expression == NULL)
+ return GTK_FILTER_MATCH_NONE;
+
+ return GTK_FILTER_MATCH_SOME;
+}
+
+static void
+gtk_string_filter_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GtkStringFilter *self = GTK_STRING_FILTER (object);
+
+ switch (prop_id)
+ {
+ case PROP_EXPRESSION:
+ gtk_string_filter_set_expression (self, g_value_get_boxed (value));
+ break;
+
+ case PROP_IGNORE_CASE:
+ gtk_string_filter_set_ignore_case (self, g_value_get_boolean (value));
+ break;
+
+ case PROP_MATCH_MODE:
+ gtk_string_filter_set_match_mode (self, g_value_get_enum (value));
+ break;
+
+ case PROP_SEARCH:
+ gtk_string_filter_set_search (self, g_value_get_string (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gtk_string_filter_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GtkStringFilter *self = GTK_STRING_FILTER (object);
+
+ switch (prop_id)
+ {
+ case PROP_EXPRESSION:
+ g_value_set_boxed (value, self->expression);
+ break;
+
+ case PROP_IGNORE_CASE:
+ g_value_set_boolean (value, self->ignore_case);
+ break;
+
+ case PROP_MATCH_MODE:
+ g_value_set_enum (value, self->match_mode);
+ break;
+
+ case PROP_SEARCH:
+ g_value_set_string (value, self->search);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gtk_string_filter_dispose (GObject *object)
+{
+ GtkStringFilter *self = GTK_STRING_FILTER (object);
+
+ g_clear_pointer (&self->search, g_free);
+ g_clear_pointer (&self->search_prepared, g_free);
+ g_clear_pointer (&self->expression, gtk_expression_unref);
+
+ G_OBJECT_CLASS (gtk_string_filter_parent_class)->dispose (object);
+}
+
+static void
+gtk_string_filter_class_init (GtkStringFilterClass *class)
+{
+ GtkFilterClass *filter_class = GTK_FILTER_CLASS (class);
+ GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+ filter_class->match = gtk_string_filter_match;
+ filter_class->get_strictness = gtk_string_filter_get_strictness;
+
+ object_class->get_property = gtk_string_filter_get_property;
+ object_class->set_property = gtk_string_filter_set_property;
+ object_class->dispose = gtk_string_filter_dispose;
+
+ /**
+ * GtkStringFilter:expression:
+ *
+ * The expression to evalute on item to get a string to compare with
+ */
+ properties[PROP_EXPRESSION] =
+ g_param_spec_boxed ("expression",
+ P_("Expression"),
+ P_("Expression to compare with"),
+ GTK_TYPE_EXPRESSION,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
+ /**
+ * GtkStringFilter:ignore-case:
+ *
+ * If matching is case sensitive
+ */
+ properties[PROP_IGNORE_CASE] =
+ g_param_spec_boolean ("ignore-case",
+ P_("Ignore case"),
+ P_("If matching is case sensitive"),
+ TRUE,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
+ /**
+ * GtkStringFilter:match-mode:
+ *
+ * If exact matches are necessary or if substrings are allowed
+ */
+ properties[PROP_MATCH_MODE] =
+ g_param_spec_enum ("match-mode",
+ P_("Match mode"),
+ P_("If exact matches are necessary or if substrings are allowed"),
+ GTK_TYPE_STRING_FILTER_MATCH_MODE,
+ GTK_STRING_FILTER_MATCH_MODE_SUBSTRING,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
+ /**
+ * GtkStringFilter:search:
+ *
+ * The search term
+ */
+ properties[PROP_SEARCH] =
+ g_param_spec_string ("search",
+ P_("Search"),
+ P_("The search term"),
+ NULL,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
+ g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
+
+}
+
+static void
+gtk_string_filter_init (GtkStringFilter *self)
+{
+ self->ignore_case = TRUE;
+ self->match_mode = GTK_STRING_FILTER_MATCH_MODE_SUBSTRING;
+}
+
+/**
+ * gtk_string_filter_new:
+ *
+ * Creates a new string filter.
+ *
+ * You will want to set up the filter by providing a string to search for
+ * and by providing a property to look up on the item.
+ *
+ * Returns: a new #GtkStringFilter
+ **/
+GtkFilter *
+gtk_string_filter_new (void)
+{
+ return g_object_new (GTK_TYPE_STRING_FILTER, NULL);
+}
+
+/**
+ * gtk_string_filter_get_search:
+ * @self: a #GtkStringFilter
+ *
+ * Gets the search string set via gtk_string_filter_set_search().
+ *
+ * Returns: (allow-none) (transfer none): The search string
+ **/
+const char *
+gtk_string_filter_get_search (GtkStringFilter *self)
+{
+ g_return_val_if_fail (GTK_IS_STRING_FILTER (self), NULL);
+
+ return self->search;
+}
+
+/**
+ * gtk_string_filter_set_search:
+ * @self: a #GtkStringFilter
+ * @search: (transfer none) (nullable): The string to search for
+ * or %NULL to clear the search
+ *
+ * Sets the string to search for.
+ **/
+void
+gtk_string_filter_set_search (GtkStringFilter *self,
+ const char *search)
+{
+ GtkFilterChange change;
+
+ g_return_if_fail (GTK_IS_STRING_FILTER (self));
+
+ if (g_strcmp0 (self->search, search) == 0)
+ return;
+
+ if (search == NULL || search[0] == 0)
+ change = GTK_FILTER_CHANGE_LESS_STRICT;
+ else if (!gtk_string_filter_has_search (self))
+ change = GTK_FILTER_CHANGE_MORE_STRICT;
+ else if (g_str_has_prefix (search, self->search))
+ change = GTK_FILTER_CHANGE_MORE_STRICT;
+ else if (g_str_has_prefix (self->search, search))
+ change = GTK_FILTER_CHANGE_LESS_STRICT;
+ else
+ change = GTK_FILTER_CHANGE_DIFFERENT;
+
+ g_free (self->search);
+ g_free (self->search_prepared);
+
+ self->search = g_strdup (search);
+ self->search_prepared = gtk_string_filter_prepare (self, search);
+
+ gtk_filter_changed (GTK_FILTER (self), change);
+
+ g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SEARCH]);
+}
+
+GtkExpression *
+gtk_string_filter_get_expression (GtkStringFilter *self)
+{
+ g_return_val_if_fail (GTK_IS_STRING_FILTER (self), NULL);
+
+ return self->expression;
+}
+
+void
+gtk_string_filter_set_expression (GtkStringFilter *self,
+ GtkExpression *expression)
+{
+ g_return_if_fail (GTK_IS_STRING_FILTER (self));
+ g_return_if_fail (expression == NULL || gtk_expression_get_value_type (expression) == G_TYPE_STRING);
+
+ if (self->expression == expression)
+ return;
+
+ g_clear_pointer (&self->expression, gtk_expression_unref);
+ self->expression = gtk_expression_ref (expression);
+
+ if (gtk_string_filter_has_search (self))
+ gtk_filter_changed (GTK_FILTER (self), GTK_FILTER_CHANGE_DIFFERENT);
+
+ g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EXPRESSION]);
+}
+
+gboolean
+gtk_string_filter_get_ignore_case (GtkStringFilter *self)
+{
+ g_return_val_if_fail (GTK_IS_STRING_FILTER (self), TRUE);
+
+ return self->ignore_case;
+}
+
+void
+gtk_string_filter_set_ignore_case (GtkStringFilter *self,
+ gboolean ignore_case)
+{
+ g_return_if_fail (GTK_IS_STRING_FILTER (self));
+
+ if (self->ignore_case == ignore_case)
+ return;
+
+ self->ignore_case = ignore_case;
+
+ if (self->search)
+ {
+ g_free (self->search_prepared);
+ self->search_prepared = gtk_string_filter_prepare (self, self->search);
+ gtk_filter_changed (GTK_FILTER (self), ignore_case ? GTK_FILTER_CHANGE_LESS_STRICT :
GTK_FILTER_CHANGE_MORE_STRICT);
+ }
+
+ g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_IGNORE_CASE]);
+}
+
+GtkStringFilterMatchMode
+gtk_string_filter_get_match_mode (GtkStringFilter *self)
+{
+ g_return_val_if_fail (GTK_IS_STRING_FILTER (self), GTK_STRING_FILTER_MATCH_MODE_EXACT);
+
+ return self->match_mode;
+}
+
+
+void
+gtk_string_filter_set_match_mode (GtkStringFilter *self,
+ GtkStringFilterMatchMode mode)
+{
+ GtkStringFilterMatchMode old_mode;
+
+ g_return_if_fail (GTK_IS_STRING_FILTER (self));
+
+ if (self->match_mode == mode)
+ return;
+
+ old_mode = self->match_mode;
+ self->match_mode = mode;
+
+ if (self->search_prepared && self->expression)
+ {
+ switch (old_mode)
+ {
+ case GTK_STRING_FILTER_MATCH_MODE_EXACT:
+ gtk_filter_changed (GTK_FILTER (self), GTK_FILTER_CHANGE_LESS_STRICT);
+ break;
+
+ case GTK_STRING_FILTER_MATCH_MODE_SUBSTRING:
+ gtk_filter_changed (GTK_FILTER (self), GTK_FILTER_CHANGE_MORE_STRICT);
+ break;
+
+ case GTK_STRING_FILTER_MATCH_MODE_PREFIX:
+ if (mode == GTK_STRING_FILTER_MATCH_MODE_SUBSTRING)
+ gtk_filter_changed (GTK_FILTER (self), GTK_FILTER_CHANGE_LESS_STRICT);
+ else
+ gtk_filter_changed (GTK_FILTER (self), GTK_FILTER_CHANGE_MORE_STRICT);
+ break;
+
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+ }
+
+ g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MATCH_MODE]);
+}
+
+
diff --git a/gtk/gtkstringfilter.h b/gtk/gtkstringfilter.h
new file mode 100644
index 0000000000..2c5e866bac
--- /dev/null
+++ b/gtk/gtkstringfilter.h
@@ -0,0 +1,81 @@
+/*
+ * 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_STRING_FILTER_H__
+#define __GTK_STRING_FILTER_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/gtkfilter.h>
+
+G_BEGIN_DECLS
+
+/**
+ * GtkStringFilterMatchMode:
+ * @GTK_STRING_FILTER_MATCH_MODE_EXACT: The search string and
+ * text must match exactly.
+ * @GTK_STRING_FILTER_MATCH_MODE_SUBSTRING: The search string
+ * must be contained as a substring inside the text.
+ * @GTK_STRING_FILTER_MATCH_MODE_PREFIX: The text must begin
+ * with the search string.
+ *
+ * Specifies how search strings are matched inside text.
+ */
+typedef enum {
+ GTK_STRING_FILTER_MATCH_MODE_EXACT,
+ GTK_STRING_FILTER_MATCH_MODE_SUBSTRING,
+ GTK_STRING_FILTER_MATCH_MODE_PREFIX
+} GtkStringFilterMatchMode;
+
+#define GTK_TYPE_STRING_FILTER (gtk_string_filter_get_type ())
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkStringFilter, gtk_string_filter, GTK, STRING_FILTER, GtkFilter)
+
+GDK_AVAILABLE_IN_ALL
+GtkFilter * gtk_string_filter_new (void);
+
+GDK_AVAILABLE_IN_ALL
+const char * gtk_string_filter_get_search (GtkStringFilter *self);
+GDK_AVAILABLE_IN_ALL
+void gtk_string_filter_set_search (GtkStringFilter *self,
+ const char *search);
+GDK_AVAILABLE_IN_ALL
+GtkExpression * gtk_string_filter_get_expression (GtkStringFilter *self);
+GDK_AVAILABLE_IN_ALL
+void gtk_string_filter_set_expression (GtkStringFilter *self,
+ GtkExpression *expression);
+GDK_AVAILABLE_IN_ALL
+gboolean gtk_string_filter_get_ignore_case (GtkStringFilter *self);
+GDK_AVAILABLE_IN_ALL
+void gtk_string_filter_set_ignore_case (GtkStringFilter *self,
+ gboolean ignore_case);
+GDK_AVAILABLE_IN_ALL
+GtkStringFilterMatchMode gtk_string_filter_get_match_mode (GtkStringFilter *self);
+GDK_AVAILABLE_IN_ALL
+void gtk_string_filter_set_match_mode (GtkStringFilter *self,
+ GtkStringFilterMatchMode mode);
+
+
+
+G_END_DECLS
+
+#endif /* __GTK_STRING_FILTER_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index 35361cdaec..cbb149aac4 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -355,6 +355,7 @@ gtk_public_sources = files([
'gtkstacksidebar.c',
'gtkstackswitcher.c',
'gtkstatusbar.c',
+ 'gtkstringfilter.c',
'gtkstylecontext.c',
'gtkstyleprovider.c',
'gtkswitch.c',
@@ -600,6 +601,7 @@ gtk_public_headers = files([
'gtkstacksidebar.h',
'gtkstackswitcher.h',
'gtkstatusbar.h',
+ 'gtkstringfilter.h',
'gtkstylecontext.h',
'gtkstyleprovider.h',
'gtkswitch.h',
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]