2019-10-07 04:28:28 +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 "gtknoselection.h"
|
|
|
|
|
2020-06-14 03:30:06 +00:00
|
|
|
#include "gtkbitset.h"
|
2019-10-07 04:28:28 +00:00
|
|
|
#include "gtkselectionmodel.h"
|
|
|
|
|
|
|
|
/**
|
2021-03-01 06:49:48 +00:00
|
|
|
* GtkNoSelection:
|
2019-10-07 04:28:28 +00:00
|
|
|
*
|
2021-03-01 06:49:48 +00:00
|
|
|
* `GtkNoSelection` is a `GtkSelectionModel` that does not allow selecting
|
|
|
|
* anything.
|
2019-10-07 04:28:28 +00:00
|
|
|
*
|
2021-03-01 06:49:48 +00:00
|
|
|
* This model is meant to be used as a simple wrapper around a `GListModel`
|
|
|
|
* when a `GtkSelectionModel` is required.
|
2019-10-07 04:28:28 +00:00
|
|
|
*/
|
|
|
|
struct _GtkNoSelection
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
|
|
|
|
GListModel *model;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkNoSelectionClass
|
|
|
|
{
|
|
|
|
GObjectClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
2022-06-11 02:56:58 +00:00
|
|
|
PROP_ITEM_TYPE,
|
2019-10-07 04:28:28 +00:00
|
|
|
PROP_MODEL,
|
2022-06-11 02:56:58 +00:00
|
|
|
PROP_N_ITEMS,
|
2019-10-07 04:28:28 +00:00
|
|
|
N_PROPS
|
|
|
|
};
|
|
|
|
|
|
|
|
static GParamSpec *properties[N_PROPS] = { NULL, };
|
|
|
|
|
|
|
|
static GType
|
|
|
|
gtk_no_selection_get_item_type (GListModel *list)
|
|
|
|
{
|
2020-07-04 19:47:48 +00:00
|
|
|
return G_TYPE_OBJECT;
|
2019-10-07 04:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gtk_no_selection_get_n_items (GListModel *list)
|
|
|
|
{
|
|
|
|
GtkNoSelection *self = GTK_NO_SELECTION (list);
|
|
|
|
|
2020-07-04 23:08:12 +00:00
|
|
|
if (self->model == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2019-10-07 04:28:28 +00:00
|
|
|
return g_list_model_get_n_items (self->model);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gtk_no_selection_get_item (GListModel *list,
|
2020-07-04 23:08:12 +00:00
|
|
|
guint position)
|
2019-10-07 04:28:28 +00:00
|
|
|
{
|
|
|
|
GtkNoSelection *self = GTK_NO_SELECTION (list);
|
|
|
|
|
2020-07-04 23:08:12 +00:00
|
|
|
if (self->model == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-10-07 04:28:28 +00:00
|
|
|
return g_list_model_get_item (self->model, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_no_selection_list_model_init (GListModelInterface *iface)
|
|
|
|
{
|
|
|
|
iface->get_item_type = gtk_no_selection_get_item_type;
|
|
|
|
iface->get_n_items = gtk_no_selection_get_n_items;
|
|
|
|
iface->get_item = gtk_no_selection_get_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_no_selection_is_selected (GtkSelectionModel *model,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-06-14 03:30:06 +00:00
|
|
|
static GtkBitset *
|
|
|
|
gtk_no_selection_get_selection_in_range (GtkSelectionModel *model,
|
|
|
|
guint pos,
|
|
|
|
guint n_items)
|
2019-10-07 04:28:28 +00:00
|
|
|
{
|
2020-06-14 03:30:06 +00:00
|
|
|
return gtk_bitset_new_empty ();
|
2019-10-07 04:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_no_selection_selection_model_init (GtkSelectionModelInterface *iface)
|
|
|
|
{
|
2020-06-14 03:30:06 +00:00
|
|
|
iface->is_selected = gtk_no_selection_is_selected;
|
|
|
|
iface->get_selection_in_range = gtk_no_selection_get_selection_in_range;
|
2019-10-07 04:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_EXTENDED (GtkNoSelection, gtk_no_selection, G_TYPE_OBJECT, 0,
|
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
|
|
|
|
gtk_no_selection_list_model_init)
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_SELECTION_MODEL,
|
|
|
|
gtk_no_selection_selection_model_init))
|
|
|
|
|
2022-06-11 02:56:58 +00:00
|
|
|
static void
|
|
|
|
gtk_no_selection_items_changed_cb (GListModel *model,
|
|
|
|
guint position,
|
|
|
|
guint removed,
|
|
|
|
guint added,
|
|
|
|
GtkNoSelection *self)
|
|
|
|
{
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), position, removed, added);
|
|
|
|
if (removed != added)
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
|
|
|
|
}
|
|
|
|
|
2019-10-07 04:28:28 +00:00
|
|
|
static void
|
|
|
|
gtk_no_selection_clear_model (GtkNoSelection *self)
|
|
|
|
{
|
|
|
|
if (self->model == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (self->model,
|
2022-06-11 02:56:58 +00:00
|
|
|
gtk_no_selection_items_changed_cb,
|
2019-10-07 04:28:28 +00:00
|
|
|
self);
|
|
|
|
g_clear_object (&self->model);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_no_selection_set_property (GObject *object,
|
2020-07-04 23:08:12 +00:00
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2019-10-07 04:28:28 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
GtkNoSelection *self = GTK_NO_SELECTION (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_MODEL:
|
2020-07-04 23:08:12 +00:00
|
|
|
gtk_no_selection_set_model (self, g_value_get_object (value));
|
2019-10-07 04:28:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_no_selection_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkNoSelection *self = GTK_NO_SELECTION (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2022-06-11 02:56:58 +00:00
|
|
|
case PROP_ITEM_TYPE:
|
|
|
|
g_value_set_gtype (value, gtk_no_selection_get_item_type (G_LIST_MODEL (self)));
|
|
|
|
break;
|
|
|
|
|
2019-10-07 04:28:28 +00:00
|
|
|
case PROP_MODEL:
|
|
|
|
g_value_set_object (value, self->model);
|
|
|
|
break;
|
|
|
|
|
2022-06-11 02:56:58 +00:00
|
|
|
case PROP_N_ITEMS:
|
|
|
|
g_value_set_uint (value, gtk_no_selection_get_n_items (G_LIST_MODEL (self)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2019-10-07 04:28:28 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_no_selection_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkNoSelection *self = GTK_NO_SELECTION (object);
|
|
|
|
|
|
|
|
gtk_no_selection_clear_model (self);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_no_selection_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_no_selection_class_init (GtkNoSelectionClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->get_property = gtk_no_selection_get_property;
|
|
|
|
gobject_class->set_property = gtk_no_selection_set_property;
|
|
|
|
gobject_class->dispose = gtk_no_selection_dispose;
|
|
|
|
|
2022-06-11 02:56:58 +00:00
|
|
|
/**
|
|
|
|
* GtkNoSelection:item-type:
|
|
|
|
*
|
|
|
|
* The type of items. See [method@Gio.ListModel.get_item_type].
|
|
|
|
*
|
|
|
|
* Since: 4.8
|
|
|
|
**/
|
|
|
|
properties[PROP_ITEM_TYPE] =
|
|
|
|
g_param_spec_gtype ("item-type", NULL, NULL,
|
|
|
|
G_TYPE_OBJECT,
|
|
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
2019-10-07 04:28:28 +00:00
|
|
|
/**
|
2021-03-01 06:49:48 +00:00
|
|
|
* GtkNoSelection:model: (attributes org.gtk.property.get=gtk_no_selection_get_model org.gtk.Property.set=gtk_no_selection_set_model)
|
2019-10-07 04:28:28 +00:00
|
|
|
*
|
2021-03-01 06:49:48 +00:00
|
|
|
* The model being managed.
|
2019-10-07 04:28:28 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_MODEL] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_object ("model", NULL, NULL,
|
2019-10-07 04:28:28 +00:00
|
|
|
G_TYPE_LIST_MODEL,
|
2020-07-04 23:08:12 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
|
2019-10-07 04:28:28 +00:00
|
|
|
|
2022-06-11 02:56:58 +00:00
|
|
|
/**
|
|
|
|
* GtkNoSelection:n-items:
|
|
|
|
*
|
|
|
|
* The number of items. See [method@Gio.ListModel.get_n_items].
|
|
|
|
*
|
|
|
|
* Since: 4.8
|
|
|
|
**/
|
|
|
|
properties[PROP_N_ITEMS] =
|
|
|
|
g_param_spec_uint ("n-items", NULL, NULL,
|
|
|
|
0, G_MAXUINT, 0,
|
|
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
2019-10-07 04:28:28 +00:00
|
|
|
g_object_class_install_properties (gobject_class, N_PROPS, properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_no_selection_init (GtkNoSelection *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_no_selection_new:
|
2021-05-21 00:45:06 +00:00
|
|
|
* @model: (nullable) (transfer full): the `GListModel` to manage
|
2019-10-07 04:28:28 +00:00
|
|
|
*
|
|
|
|
* Creates a new selection to handle @model.
|
|
|
|
*
|
2021-03-01 06:49:48 +00:00
|
|
|
* Returns: (transfer full) (type GtkNoSelection): a new `GtkNoSelection`
|
|
|
|
*/
|
2019-10-07 04:28:28 +00:00
|
|
|
GtkNoSelection *
|
|
|
|
gtk_no_selection_new (GListModel *model)
|
|
|
|
{
|
2020-07-26 20:55:43 +00:00
|
|
|
GtkNoSelection *self;
|
|
|
|
|
2021-03-15 13:30:33 +00:00
|
|
|
g_return_val_if_fail (model == NULL || G_IS_LIST_MODEL (model), NULL);
|
2019-10-07 04:28:28 +00:00
|
|
|
|
2020-07-26 20:55:43 +00:00
|
|
|
self = g_object_new (GTK_TYPE_NO_SELECTION,
|
2019-10-07 04:28:28 +00:00
|
|
|
"model", model,
|
|
|
|
NULL);
|
2020-07-26 20:55:43 +00:00
|
|
|
|
|
|
|
/* consume the reference */
|
|
|
|
g_clear_object (&model);
|
|
|
|
|
|
|
|
return self;
|
2019-10-07 04:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-01 06:49:48 +00:00
|
|
|
* gtk_no_selection_get_model: (attributes org.gtk.Method.get_property=model)
|
|
|
|
* @self: a `GtkNoSelection`
|
2019-10-07 04:28:28 +00:00
|
|
|
*
|
|
|
|
* Gets the model that @self is wrapping.
|
|
|
|
*
|
2022-01-01 16:56:07 +00:00
|
|
|
* Returns: (transfer none) (nullable): The model being wrapped
|
2021-03-01 06:49:48 +00:00
|
|
|
*/
|
2019-10-07 04:28:28 +00:00
|
|
|
GListModel *
|
|
|
|
gtk_no_selection_get_model (GtkNoSelection *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_NO_SELECTION (self), NULL);
|
|
|
|
|
|
|
|
return self->model;
|
|
|
|
}
|
|
|
|
|
2020-07-04 23:08:12 +00:00
|
|
|
/**
|
2021-03-01 06:49:48 +00:00
|
|
|
* gtk_no_selection_set_model: (attributes org.gtk.Method.set_property=model)
|
|
|
|
* @self: a `GtkNoSelection`
|
2021-05-19 11:24:34 +00:00
|
|
|
* @model: (nullable): A `GListModel` to wrap
|
2020-07-04 23:08:12 +00:00
|
|
|
*
|
2020-07-26 20:55:43 +00:00
|
|
|
* Sets the model that @self should wrap.
|
2021-03-01 06:49:48 +00:00
|
|
|
*
|
2020-07-26 20:55:43 +00:00
|
|
|
* If @model is %NULL, this model will be empty.
|
2021-03-01 06:49:48 +00:00
|
|
|
*/
|
2020-07-04 23:08:12 +00:00
|
|
|
void
|
|
|
|
gtk_no_selection_set_model (GtkNoSelection *self,
|
|
|
|
GListModel *model)
|
|
|
|
{
|
2022-06-11 02:56:58 +00:00
|
|
|
guint n_items_before, n_items_after;
|
2020-07-04 23:08:12 +00:00
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_NO_SELECTION (self));
|
|
|
|
g_return_if_fail (model == NULL || G_IS_LIST_MODEL (model));
|
|
|
|
|
|
|
|
if (self->model == model)
|
|
|
|
return;
|
|
|
|
|
|
|
|
n_items_before = self->model ? g_list_model_get_n_items (self->model) : 0;
|
|
|
|
gtk_no_selection_clear_model (self);
|
|
|
|
|
|
|
|
if (model)
|
|
|
|
{
|
|
|
|
self->model = g_object_ref (model);
|
2022-06-11 02:56:58 +00:00
|
|
|
g_signal_connect (self->model, "items-changed",
|
|
|
|
G_CALLBACK (gtk_no_selection_items_changed_cb), self);
|
|
|
|
n_items_after = g_list_model_get_n_items (self->model);
|
2020-07-04 23:08:12 +00:00
|
|
|
}
|
2022-06-11 02:56:58 +00:00
|
|
|
else
|
|
|
|
n_items_after = 0;
|
2020-07-04 23:08:12 +00:00
|
|
|
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self),
|
|
|
|
0,
|
|
|
|
n_items_before,
|
2022-06-11 02:56:58 +00:00
|
|
|
n_items_after);
|
|
|
|
if (n_items_before != n_items_after)
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
|
2020-07-04 23:08:12 +00:00
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL]);
|
|
|
|
}
|