2019-12-11 20:28:38 +00:00
|
|
|
/*
|
|
|
|
* 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 "gtkmultifilter.h"
|
|
|
|
|
|
|
|
#include "gtkbuildable.h"
|
|
|
|
#include "gtkintl.h"
|
|
|
|
#include "gtktypebuiltins.h"
|
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
#define GDK_ARRAY_TYPE_NAME GtkFilters
|
|
|
|
#define GDK_ARRAY_NAME gtk_filters
|
|
|
|
#define GDK_ARRAY_ELEMENT_TYPE GtkFilter *
|
|
|
|
#define GDK_ARRAY_FREE_FUNC g_object_unref
|
|
|
|
|
|
|
|
#include "gdk/gdkarrayimpl.c"
|
|
|
|
|
2019-12-11 20:28:38 +00:00
|
|
|
/*** MULTI FILTER ***/
|
|
|
|
|
|
|
|
/**
|
2021-02-27 15:52:22 +00:00
|
|
|
* GtkMultiFilter:
|
2019-12-11 20:28:38 +00:00
|
|
|
*
|
2021-02-27 23:22:13 +00:00
|
|
|
* `GtkMultiFilter` is the base class for filters that combine multiple filters.
|
2021-02-27 15:52:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkAnyFilter:
|
2019-12-11 20:28:38 +00:00
|
|
|
*
|
2021-02-27 15:52:22 +00:00
|
|
|
* `GtkAnyFilter` matches an item when at least one of its filters matches.
|
2021-02-27 23:22:13 +00:00
|
|
|
*
|
|
|
|
* To add filters to a `GtkAnyFilter`, use [method@Gtk.MultiFilter.append].
|
2021-02-27 15:52:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkEveryFilter:
|
2019-12-11 20:28:38 +00:00
|
|
|
*
|
2021-02-27 15:52:22 +00:00
|
|
|
* `GtkEveryFilter` matches an item when each of its filters matches.
|
2021-02-27 23:22:13 +00:00
|
|
|
*
|
|
|
|
* To add filters to a `GtkEveryFilter`, use [method@Gtk.MultiFilter.append].
|
2019-12-11 20:28:38 +00:00
|
|
|
*/
|
2021-02-27 15:52:22 +00:00
|
|
|
|
2019-12-11 20:28:38 +00:00
|
|
|
struct _GtkMultiFilter
|
|
|
|
{
|
|
|
|
GtkFilter parent_instance;
|
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
GtkFilters filters;
|
2019-12-11 20:28:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkMultiFilterClass
|
|
|
|
{
|
|
|
|
GtkFilterClass parent_class;
|
|
|
|
|
|
|
|
GtkFilterChange addition_change;
|
|
|
|
GtkFilterChange removal_change;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GType
|
|
|
|
gtk_multi_filter_get_item_type (GListModel *list)
|
|
|
|
{
|
|
|
|
return GTK_TYPE_FILTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gtk_multi_filter_get_n_items (GListModel *list)
|
|
|
|
{
|
|
|
|
GtkMultiFilter *self = GTK_MULTI_FILTER (list);
|
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
return gtk_filters_get_size (&self->filters);
|
2019-12-11 20:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gtk_multi_filter_get_item (GListModel *list,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
GtkMultiFilter *self = GTK_MULTI_FILTER (list);
|
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
if (position < gtk_filters_get_size (&self->filters))
|
|
|
|
return g_object_ref (gtk_filters_get (&self->filters, position));
|
2019-12-11 20:28:38 +00:00
|
|
|
else
|
2020-07-16 17:12:25 +00:00
|
|
|
return NULL;
|
2019-12-11 20:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_multi_filter_list_model_init (GListModelInterface *iface)
|
|
|
|
{
|
|
|
|
iface->get_item_type = gtk_multi_filter_get_item_type;
|
|
|
|
iface->get_n_items = gtk_multi_filter_get_n_items;
|
|
|
|
iface->get_item = gtk_multi_filter_get_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkBuildableIface *parent_buildable_iface;
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_multi_filter_buildable_add_child (GtkBuildable *buildable,
|
|
|
|
GtkBuilder *builder,
|
|
|
|
GObject *child,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *type)
|
2019-12-11 20:28:38 +00:00
|
|
|
{
|
|
|
|
if (GTK_IS_FILTER (child))
|
|
|
|
gtk_multi_filter_append (GTK_MULTI_FILTER (buildable), g_object_ref (GTK_FILTER (child)));
|
|
|
|
else
|
|
|
|
parent_buildable_iface->add_child (buildable, builder, child, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_multi_filter_buildable_init (GtkBuildableIface *iface)
|
|
|
|
{
|
|
|
|
parent_buildable_iface = g_type_interface_peek_parent (iface);
|
|
|
|
|
|
|
|
iface->add_child = gtk_multi_filter_buildable_add_child;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkMultiFilter, gtk_multi_filter, GTK_TYPE_FILTER,
|
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_multi_filter_list_model_init)
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_multi_filter_buildable_init))
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_multi_filter_changed_cb (GtkFilter *filter,
|
|
|
|
GtkFilterChange change,
|
|
|
|
GtkMultiFilter *self)
|
|
|
|
{
|
|
|
|
gtk_filter_changed (GTK_FILTER (self), change);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_multi_filter_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkMultiFilter *self = GTK_MULTI_FILTER (object);
|
2020-07-16 17:12:25 +00:00
|
|
|
guint i;
|
2019-12-11 20:28:38 +00:00
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
for (i = 0; i < gtk_filters_get_size (&self->filters); i++)
|
|
|
|
{
|
|
|
|
GtkFilter *filter = gtk_filters_get (&self->filters, i);
|
|
|
|
g_signal_handlers_disconnect_by_func (filter, gtk_multi_filter_changed_cb, self);
|
|
|
|
}
|
2019-12-11 20:28:38 +00:00
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
gtk_filters_clear (&self->filters);
|
2019-12-11 20:28:38 +00:00
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
G_OBJECT_CLASS (gtk_multi_filter_parent_class)->dispose (object);
|
2019-12-11 20:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_multi_filter_class_init (GtkMultiFilterClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
object_class->dispose = gtk_multi_filter_dispose;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_multi_filter_init (GtkMultiFilter *self)
|
|
|
|
{
|
2020-07-16 17:12:25 +00:00
|
|
|
gtk_filters_init (&self->filters);
|
2019-12-11 20:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_multi_filter_append:
|
2021-02-27 15:52:22 +00:00
|
|
|
* @self: a `GtkMultiFilter`
|
2019-12-11 20:28:38 +00:00
|
|
|
* @filter: (transfer full): A new filter to use
|
|
|
|
*
|
|
|
|
* Adds a @filter to @self to use for matching.
|
2021-02-27 15:52:22 +00:00
|
|
|
*/
|
2019-12-11 20:28:38 +00:00
|
|
|
void
|
|
|
|
gtk_multi_filter_append (GtkMultiFilter *self,
|
|
|
|
GtkFilter *filter)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_MULTI_FILTER (self));
|
|
|
|
g_return_if_fail (GTK_IS_FILTER (filter));
|
|
|
|
|
|
|
|
g_signal_connect (filter, "changed", G_CALLBACK (gtk_multi_filter_changed_cb), self);
|
2020-07-16 17:12:25 +00:00
|
|
|
gtk_filters_append (&self->filters, filter);
|
2019-12-11 20:28:38 +00:00
|
|
|
|
|
|
|
gtk_filter_changed (GTK_FILTER (self),
|
|
|
|
GTK_MULTI_FILTER_GET_CLASS (self)->addition_change);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_multi_filter_remove:
|
2021-02-27 15:52:22 +00:00
|
|
|
* @self: a `GtkMultiFilter`
|
2019-12-11 20:28:38 +00:00
|
|
|
* @position: position of filter to remove
|
|
|
|
*
|
|
|
|
* Removes the filter at the given @position from the list of filters used
|
|
|
|
* by @self.
|
2021-02-27 15:52:22 +00:00
|
|
|
*
|
2019-12-11 20:28:38 +00:00
|
|
|
* If @position is larger than the number of filters, nothing happens and
|
|
|
|
* the function returns.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gtk_multi_filter_remove (GtkMultiFilter *self,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
guint length;
|
2020-07-16 17:12:25 +00:00
|
|
|
GtkFilter *filter;
|
2019-12-11 20:28:38 +00:00
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
length = gtk_filters_get_size (&self->filters);
|
2019-12-11 20:28:38 +00:00
|
|
|
if (position >= length)
|
|
|
|
return;
|
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
filter = gtk_filters_get (&self->filters, position);
|
|
|
|
g_signal_handlers_disconnect_by_func (filter, gtk_multi_filter_changed_cb, self);
|
2020-12-23 19:04:29 +00:00
|
|
|
gtk_filters_splice (&self->filters, position, 1, FALSE, NULL, 0);
|
2019-12-11 20:28:38 +00:00
|
|
|
|
|
|
|
gtk_filter_changed (GTK_FILTER (self),
|
|
|
|
GTK_MULTI_FILTER_GET_CLASS (self)->removal_change);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** ANY FILTER ***/
|
|
|
|
|
|
|
|
struct _GtkAnyFilter
|
|
|
|
{
|
|
|
|
GtkMultiFilter parent_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkAnyFilterClass
|
|
|
|
{
|
|
|
|
GtkMultiFilterClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkAnyFilter, gtk_any_filter, GTK_TYPE_MULTI_FILTER)
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_any_filter_match (GtkFilter *filter,
|
|
|
|
gpointer item)
|
|
|
|
{
|
|
|
|
GtkMultiFilter *self = GTK_MULTI_FILTER (filter);
|
2020-07-16 17:12:25 +00:00
|
|
|
guint i;
|
2019-12-11 20:28:38 +00:00
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
for (i = 0; i < gtk_filters_get_size (&self->filters); i++)
|
2019-12-11 20:28:38 +00:00
|
|
|
{
|
2020-07-16 17:12:25 +00:00
|
|
|
GtkFilter *child = gtk_filters_get (&self->filters, i);
|
2019-12-11 20:28:38 +00:00
|
|
|
|
|
|
|
if (gtk_filter_match (child, item))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkFilterMatch
|
|
|
|
gtk_any_filter_get_strictness (GtkFilter *filter)
|
|
|
|
{
|
2020-07-16 17:12:25 +00:00
|
|
|
GtkMultiFilter *self = GTK_MULTI_FILTER (filter);
|
|
|
|
guint i;
|
2019-12-11 20:28:38 +00:00
|
|
|
GtkFilterMatch result = GTK_FILTER_MATCH_NONE;
|
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
for (i = 0; i < gtk_filters_get_size (&self->filters); i++)
|
2019-12-11 20:28:38 +00:00
|
|
|
{
|
2020-07-16 17:12:25 +00:00
|
|
|
GtkFilter *child = gtk_filters_get (&self->filters, i);
|
2019-12-11 20:28:38 +00:00
|
|
|
|
|
|
|
switch (gtk_filter_get_strictness (child))
|
|
|
|
{
|
|
|
|
case GTK_FILTER_MATCH_SOME:
|
|
|
|
result = GTK_FILTER_MATCH_SOME;
|
|
|
|
break;
|
|
|
|
case GTK_FILTER_MATCH_NONE:
|
|
|
|
break;
|
|
|
|
case GTK_FILTER_MATCH_ALL:
|
|
|
|
return GTK_FILTER_MATCH_ALL;
|
|
|
|
default:
|
|
|
|
g_return_val_if_reached (GTK_FILTER_MATCH_NONE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_any_filter_class_init (GtkAnyFilterClass *class)
|
|
|
|
{
|
|
|
|
GtkMultiFilterClass *multi_filter_class = GTK_MULTI_FILTER_CLASS (class);
|
|
|
|
GtkFilterClass *filter_class = GTK_FILTER_CLASS (class);
|
|
|
|
|
|
|
|
multi_filter_class->addition_change = GTK_FILTER_CHANGE_LESS_STRICT;
|
|
|
|
multi_filter_class->removal_change = GTK_FILTER_CHANGE_MORE_STRICT;
|
|
|
|
|
|
|
|
filter_class->match = gtk_any_filter_match;
|
|
|
|
filter_class->get_strictness = gtk_any_filter_get_strictness;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_any_filter_init (GtkAnyFilter *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_any_filter_new:
|
|
|
|
*
|
2020-06-21 17:07:08 +00:00
|
|
|
* Creates a new empty "any" filter.
|
2021-02-27 15:52:22 +00:00
|
|
|
*
|
|
|
|
* Use [method@Gtk.MultiFilter.append] to add filters to it.
|
2019-12-11 20:28:38 +00:00
|
|
|
*
|
|
|
|
* This filter matches an item if any of the filters added to it
|
2021-02-27 15:52:22 +00:00
|
|
|
* matches the item. In particular, this means that if no filter
|
|
|
|
* has been added to it, the filter matches no item.
|
2019-12-11 20:28:38 +00:00
|
|
|
*
|
2021-02-27 15:52:22 +00:00
|
|
|
* Returns: a new `GtkAnyFilter`
|
|
|
|
*/
|
2020-08-03 19:04:33 +00:00
|
|
|
GtkAnyFilter *
|
2019-12-11 20:28:38 +00:00
|
|
|
gtk_any_filter_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_ANY_FILTER, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** EVERY FILTER ***/
|
|
|
|
|
|
|
|
struct _GtkEveryFilter
|
|
|
|
{
|
|
|
|
GtkMultiFilter parent_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkEveryFilterClass
|
|
|
|
{
|
|
|
|
GtkMultiFilterClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkEveryFilter, gtk_every_filter, GTK_TYPE_MULTI_FILTER)
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_every_filter_match (GtkFilter *filter,
|
|
|
|
gpointer item)
|
|
|
|
{
|
|
|
|
GtkMultiFilter *self = GTK_MULTI_FILTER (filter);
|
2020-07-16 17:12:25 +00:00
|
|
|
guint i;
|
2019-12-11 20:28:38 +00:00
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
for (i = 0; i < gtk_filters_get_size (&self->filters); i++)
|
2019-12-11 20:28:38 +00:00
|
|
|
{
|
2020-07-16 17:12:25 +00:00
|
|
|
GtkFilter *child = gtk_filters_get (&self->filters, i);
|
2019-12-11 20:28:38 +00:00
|
|
|
|
|
|
|
if (!gtk_filter_match (child, item))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkFilterMatch
|
|
|
|
gtk_every_filter_get_strictness (GtkFilter *filter)
|
|
|
|
{
|
2020-07-16 17:12:25 +00:00
|
|
|
GtkMultiFilter *self = GTK_MULTI_FILTER (filter);
|
|
|
|
guint i;
|
2019-12-11 20:28:38 +00:00
|
|
|
GtkFilterMatch result = GTK_FILTER_MATCH_ALL;
|
|
|
|
|
2020-07-16 17:12:25 +00:00
|
|
|
for (i = 0; i < gtk_filters_get_size (&self->filters); i++)
|
2019-12-11 20:28:38 +00:00
|
|
|
{
|
2020-07-16 17:12:25 +00:00
|
|
|
GtkFilter *child = gtk_filters_get (&self->filters, i);
|
2019-12-11 20:28:38 +00:00
|
|
|
|
|
|
|
switch (gtk_filter_get_strictness (child))
|
|
|
|
{
|
|
|
|
case GTK_FILTER_MATCH_SOME:
|
|
|
|
result = GTK_FILTER_MATCH_SOME;
|
|
|
|
break;
|
|
|
|
case GTK_FILTER_MATCH_NONE:
|
|
|
|
return GTK_FILTER_MATCH_NONE;
|
|
|
|
case GTK_FILTER_MATCH_ALL:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_return_val_if_reached (GTK_FILTER_MATCH_NONE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_every_filter_class_init (GtkEveryFilterClass *class)
|
|
|
|
{
|
|
|
|
GtkMultiFilterClass *multi_filter_class = GTK_MULTI_FILTER_CLASS (class);
|
|
|
|
GtkFilterClass *filter_class = GTK_FILTER_CLASS (class);
|
|
|
|
|
|
|
|
multi_filter_class->addition_change = GTK_FILTER_CHANGE_MORE_STRICT;
|
|
|
|
multi_filter_class->removal_change = GTK_FILTER_CHANGE_LESS_STRICT;
|
|
|
|
|
|
|
|
filter_class->match = gtk_every_filter_match;
|
|
|
|
filter_class->get_strictness = gtk_every_filter_get_strictness;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_every_filter_init (GtkEveryFilter *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_every_filter_new:
|
|
|
|
*
|
2020-07-16 17:12:25 +00:00
|
|
|
* Creates a new empty "every" filter.
|
2021-02-27 15:52:22 +00:00
|
|
|
*
|
|
|
|
* Use [method@Gtk.MultiFilter.append] to add filters to it.
|
2019-12-11 20:28:38 +00:00
|
|
|
*
|
|
|
|
* This filter matches an item if each of the filters added to it
|
2021-02-27 15:52:22 +00:00
|
|
|
* matches the item. In particular, this means that if no filter
|
|
|
|
* has been added to it, the filter matches every item.
|
2019-12-11 20:28:38 +00:00
|
|
|
*
|
2021-02-27 15:52:22 +00:00
|
|
|
* Returns: a new `GtkEveryFilter`
|
|
|
|
*/
|
2020-08-03 19:04:33 +00:00
|
|
|
GtkEveryFilter *
|
2019-12-11 20:28:38 +00:00
|
|
|
gtk_every_filter_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_EVERY_FILTER, NULL);
|
|
|
|
}
|
|
|
|
|