Compare commits

...

3 Commits

Author SHA1 Message Date
Matthias Clasen
842df3cdff testsuite: Add selection model tests 2019-02-07 22:26:03 -05:00
Benjamin Otte
dd7b9d931a gtk: Add GtkSingleSelection
GtkSingleSelection is a GtkSelectionModel that allows selecting a single
item.
2019-02-07 22:26:03 -05:00
Benjamin Otte
e41236bc1f gtk: Add GtkSelectionModel
The selection model is a list model interface that takes care of
selections and is to be used by the list model widgets to manage their
selections.
2019-02-07 22:26:03 -05:00
9 changed files with 1550 additions and 0 deletions

View File

@@ -411,6 +411,30 @@ gtk_list_box_get_type
gtk_list_box_row_get_type
</SECTION>
<SECTION>
<FILE>gtkselectionmodel</FILE>
<TITLE>GtkSelectionModel</TITLE>
GtkSelectionModel
gtk_selection_model_is_selected
gtk_selection_model_select_item
gtk_selection_model_unselect_item
gtk_selection_model_select_range
gtk_selection_model_unselect_range
gtk_selection_model_select_all
gtk_selection_model_unselect_all
<SUBSECTION>
gtk_selection_model_selection_changed
<SUBSECTION Standard>
GTK_SELECTION_MODEL
GTK_SELECTION_MODEL_CLASS
GTK_SELECTION_MODEL_GET_CLASS
GTK_IS_SELECTION_MODEL
GTK_IS_SELECTION_MODEL_CLASS
GTK_TYPE_SELECTION_MODEL
<SUBSECTION Private>
gtk_selection_model_get_type
</SECTION>
<SECTION>
<FILE>gtkbuildable</FILE>
GtkBuildable

View File

@@ -184,6 +184,7 @@
#include <gtk/gtksearchbar.h>
#include <gtk/gtksearchentry.h>
#include <gtk/gtkselection.h>
#include <gtk/gtkselectionmodel.h>
#include <gtk/gtkseparator.h>
#include <gtk/gtkseparatormenuitem.h>
#include <gtk/gtkseparatortoolitem.h>
@@ -194,6 +195,7 @@
#include <gtk/gtkshortcutsshortcut.h>
#include <gtk/gtkshortcutswindow.h>
#include <gtk/gtkshow.h>
#include <gtk/gtksingleselection.h>
#include <gtk/gtkslicelistmodel.h>
#include <gtk/gtksnapshot.h>
#include <gtk/gtksortlistmodel.h>

288
gtk/gtkselectionmodel.c Normal file
View File

@@ -0,0 +1,288 @@
/*
* Copyright © 2018 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 "gtkselectionmodel.h"
#include "gtkintl.h"
#include "gtkmarshalers.h"
/**
* SECTION:gtkselectionmodel
* @Title: GtkSelectionModel
* @Short_description: An extension of the list model interface that handles selections
* @See_also: #GListModel, #GtkSingleSelection
*
* #GtkSelectionModel is an interface that extends the #GListModel interface by adding
* support for selections. This support is then used by widgets using list models to add
* the ability to select and unselect various items.
*
* GTK provides default implementations of the mode common selection modes such as
* #GtkSingleSelection, so you will only need to implement this interface if you want
* detailed control about how selections should be handled.
*
* A #GtkSelectionModel supports a single boolean per row indicating if a row is selected
* or not. This can be queried via gtk_selection_model_is_selected(). When the selected
* state of one or more rows changes, the model will emit the
* GtkSelectionModel::selection-changed signal by calling the
* gtk_selection_model_selection_changed() function. The positions given in that signal
* may have their selection state changed, though that is not a requirement.
* If new items added to the model via the #GListModel::items-changed signal are selected
* or not is up to the implementation.
*
* Additionally, the interface can expose functionality to select and unselect items.
* If these functions are implemented, GTK's list widgets will allow users to select and
* unselect items. However, #GtkSelectionModels are free to only implement them
* partially or not at all. In that case the widgets will not support the unimplemented
* operations.
*
* When selecting or unselecting is supported by a model, the return values of the
* selection functions do NOT indicate if selection or unselection happened. They are
* only meant to indicate complete failure, like when this mode of selecting is not
* supported by the model.
* Selections may happen asynchronously, so the only reliable way to find out when an
* item was selected is to listen to the signals that indicate selection.
*/
G_DEFINE_INTERFACE (GtkSelectionModel, gtk_selection_model, G_TYPE_LIST_MODEL)
enum {
SELECTION_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static gboolean
gtk_selection_model_default_is_selected (GtkSelectionModel *model,
guint position)
{
return FALSE;
}
static gboolean
gtk_selection_model_default_select_item (GtkSelectionModel *model,
guint position,
gboolean exclusive)
{
return FALSE;
}
static gboolean
gtk_selection_model_default_unselect_item (GtkSelectionModel *model,
guint position)
{
return FALSE;
}
static gboolean
gtk_selection_model_default_select_range (GtkSelectionModel *model,
guint position,
guint n_items,
gboolean exclusive)
{
return FALSE;
}
static gboolean
gtk_selection_model_default_unselect_range (GtkSelectionModel *model,
guint position,
guint n_items)
{
return FALSE;
}
static gboolean
gtk_selection_model_default_select_all (GtkSelectionModel *model)
{
return gtk_selection_model_select_range (model, 0, g_list_model_get_n_items (G_LIST_MODEL (model)), FALSE);
}
static gboolean
gtk_selection_model_default_unselect_all (GtkSelectionModel *model)
{
return gtk_selection_model_unselect_range (model, 0, g_list_model_get_n_items (G_LIST_MODEL (model)));;
}
static void
gtk_selection_model_default_init (GtkSelectionModelInterface *iface)
{
iface->is_selected = gtk_selection_model_default_is_selected;
iface->select_item = gtk_selection_model_default_select_item;
iface->unselect_item = gtk_selection_model_default_unselect_item;
iface->select_range = gtk_selection_model_default_select_range;
iface->unselect_range = gtk_selection_model_default_unselect_range;
iface->select_all = gtk_selection_model_default_select_all;
iface->unselect_all = gtk_selection_model_default_unselect_all;
/**
* GtkSelectionModel::selection-changed
* @model: a #GtkSelectionModel
* @position: The first item that may have changed
* @n_items: number of items with changes
*
* Emitted when the selection state of some of the items in @model changes.
*
* Note that this signal does not specify the new selection state of the items,
* they need to be queried manually.
* It is also not necessary for a model to change the selection state of any of
* the items in the selection model, though it would be rather useless to emit
* such a signal.
*/
signals[SELECTION_CHANGED] =
g_signal_new ("selection-changed",
GTK_TYPE_SELECTION_MODEL,
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
_gtk_marshal_VOID__UINT_UINT,
G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
g_signal_set_va_marshaller (signals[SELECTION_CHANGED],
GTK_TYPE_SELECTION_MODEL,
_gtk_marshal_VOID__UINT_UINTv);
g_object_interface_install_property (iface,
g_param_spec_object ("model",
P_("Model"),
P_("List managed by this selection"),
G_TYPE_LIST_MODEL,
G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
| G_PARAM_EXPLICIT_NOTIFY
| G_PARAM_STATIC_STRINGS));
}
/**
* gtk_selection_model_is_selected:
* @model: a #GtkSelectionModel
* @position: the position of the item to query
*
* Checks if the given item is selected.
*
* Returns: %TRUE if the item is selected
**/
gboolean
gtk_selection_model_is_selected (GtkSelectionModel *model,
guint position)
{
GtkSelectionModelInterface *iface;
g_return_val_if_fail (GTK_IS_SELECTION_MODEL (model), 0);
iface = GTK_SELECTION_MODEL_GET_IFACE (model);
return iface->is_selected (model, position);
}
gboolean
gtk_selection_model_select_item (GtkSelectionModel *model,
guint position,
gboolean exclusive)
{
GtkSelectionModelInterface *iface;
g_return_val_if_fail (GTK_IS_SELECTION_MODEL (model), 0);
iface = GTK_SELECTION_MODEL_GET_IFACE (model);
return iface->select_item (model, position, exclusive);
}
gboolean
gtk_selection_model_unselect_item (GtkSelectionModel *model,
guint position)
{
GtkSelectionModelInterface *iface;
g_return_val_if_fail (GTK_IS_SELECTION_MODEL (model), 0);
iface = GTK_SELECTION_MODEL_GET_IFACE (model);
return iface->unselect_item (model, position);
}
gboolean
gtk_selection_model_select_range (GtkSelectionModel *model,
guint position,
guint n_items,
gboolean exclusive)
{
GtkSelectionModelInterface *iface;
g_return_val_if_fail (GTK_IS_SELECTION_MODEL (model), 0);
iface = GTK_SELECTION_MODEL_GET_IFACE (model);
return iface->select_range (model, position, n_items, exclusive);
}
gboolean
gtk_selection_model_unselect_range (GtkSelectionModel *model,
guint position,
guint n_items)
{
GtkSelectionModelInterface *iface;
g_return_val_if_fail (GTK_IS_SELECTION_MODEL (model), 0);
iface = GTK_SELECTION_MODEL_GET_IFACE (model);
return iface->unselect_range (model, position, n_items);
}
gboolean
gtk_selection_model_select_all (GtkSelectionModel *model)
{
GtkSelectionModelInterface *iface;
g_return_val_if_fail (GTK_IS_SELECTION_MODEL (model), 0);
iface = GTK_SELECTION_MODEL_GET_IFACE (model);
return iface->select_all (model);
}
gboolean
gtk_selection_model_unselect_all (GtkSelectionModel *model)
{
GtkSelectionModelInterface *iface;
g_return_val_if_fail (GTK_IS_SELECTION_MODEL (model), 0);
iface = GTK_SELECTION_MODEL_GET_IFACE (model);
return iface->unselect_all (model);
}
GListModel *
gtk_selection_model_get_model (GtkSelectionModel *model)
{
GListModel *child;
g_object_get (model, "model", &child, NULL);
if (child)
g_object_unref (child);
return child;
}
void
gtk_selection_model_selection_changed (GtkSelectionModel *model,
guint position,
guint n_items)
{
g_return_if_fail (GTK_IS_SELECTION_MODEL (model));
g_signal_emit (model, signals[SELECTION_CHANGED], 0, position, n_items);
}

120
gtk/gtkselectionmodel.h Normal file
View File

@@ -0,0 +1,120 @@
/*
* Copyright © 2018 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_SELECTION_MODEL_H__
#define __GTK_SELECTION_MODEL_H__
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gdk/gdk.h>
G_BEGIN_DECLS
#define GTK_TYPE_SELECTION_MODEL (gtk_selection_model_get_type ())
GDK_AVAILABLE_IN_ALL
G_DECLARE_INTERFACE (GtkSelectionModel, gtk_selection_model, GTK, SELECTION_MODEL, GListModel)
/**
* GtkSelectionModelInterface:
* @is_selected: Return if the item at the given position is selected.
* @select_item: Select the item in the given position. If the operation
* is known to fail, return %FALSE.
* @unselect_item: Unselect the item in the given position. If the
* operation is known to fail, return %FALSE.
* @select_range: Select all items in the given range. If the operation
* is unsupported or known to fail for all items, return %FALSE.
* @unselect_range: Unselect all items in the given range. If the
* operation is unsupported or known to fail for all items, return
* %FALSE.
* @select_all: Select all items in the model. If the operation is
* unsupported or known to fail for all items, return %FALSE.
* @unselect_all: Unselect all items in the model. If the operation is
* unsupported or known to fail for all items, return %FALSE.
*
* The list of virtual functions for the #GtkSelectionModel interface.
* All getter functions are mandatory to implement, but the model does
* not need to implement any functions to support selecting or unselecting
* items. Of course, if the model does not do that, it means that users
* cannot select or unselect items in a list widgets using the model.
*/
struct _GtkSelectionModelInterface
{
/*< private >*/
GTypeInterface g_iface;
/*< public >*/
gboolean (* is_selected) (GtkSelectionModel *model,
guint position);
gboolean (* select_item) (GtkSelectionModel *model,
guint position,
gboolean exclusive);
gboolean (* unselect_item) (GtkSelectionModel *model,
guint position);
gboolean (* select_range) (GtkSelectionModel *model,
guint position,
guint n_items,
gboolean exclusive);
gboolean (* unselect_range) (GtkSelectionModel *model,
guint position,
guint n_items);
gboolean (* select_all) (GtkSelectionModel *model);
gboolean (* unselect_all) (GtkSelectionModel *model);
};
GDK_AVAILABLE_IN_ALL
gboolean gtk_selection_model_is_selected (GtkSelectionModel *model,
guint position);
GDK_AVAILABLE_IN_ALL
gboolean gtk_selection_model_select_item (GtkSelectionModel *model,
guint position,
gboolean exclusive);
GDK_AVAILABLE_IN_ALL
gboolean gtk_selection_model_unselect_item (GtkSelectionModel *model,
guint position);
GDK_AVAILABLE_IN_ALL
gboolean gtk_selection_model_select_range (GtkSelectionModel *model,
guint position,
guint n_items,
gboolean exclusive);
GDK_AVAILABLE_IN_ALL
gboolean gtk_selection_model_unselect_range (GtkSelectionModel *model,
guint position,
guint n_items);
GDK_AVAILABLE_IN_ALL
gboolean gtk_selection_model_select_all (GtkSelectionModel *model);
GDK_AVAILABLE_IN_ALL
gboolean gtk_selection_model_unselect_all (GtkSelectionModel *model);
GDK_AVAILABLE_IN_ALL
GListModel * gtk_selection_model_get_model (GtkSelectionModel *model);
/* for implementations only */
GDK_AVAILABLE_IN_ALL
void gtk_selection_model_selection_changed (GtkSelectionModel *model,
guint position,
guint n_items);
G_END_DECLS
#endif /* __GTK_SELECTION_MODEL_H__ */

557
gtk/gtksingleselection.c Normal file
View File

@@ -0,0 +1,557 @@
/*
* Copyright © 2018 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 "gtksingleselection.h"
#include "gtkintl.h"
#include "gtkselectionmodel.h"
/**
* SECTION:gtksingleselection
* @Short_description: A selection model that allows selecting a single item
* @Title: GtkSingleSelection
* @see_also: #GtkSelectionModel
*
* GtkSingleSelection is an implementation of the #GtkSelectionModel interface
* that allows selecting a single element. It is the default selection method
* used by list widgets in GTK.
*/
struct _GtkSingleSelection
{
GObject parent_instance;
GListModel *model;
guint selected;
gpointer selected_item;
guint autoselect : 1;
guint can_unselect : 1;
};
struct _GtkSingleSelectionClass
{
GObjectClass parent_class;
};
enum {
PROP_0,
PROP_AUTOSELECT,
PROP_CAN_UNSELECT,
PROP_SELECTED,
/* selectionmodel */
PROP_MODEL,
N_PROPS = PROP_MODEL
};
static GParamSpec *properties[N_PROPS] = { NULL, };
static GType
gtk_single_selection_get_item_type (GListModel *list)
{
GtkSingleSelection *self = GTK_SINGLE_SELECTION (list);
return g_list_model_get_item_type (self->model);
}
static guint
gtk_single_selection_get_n_items (GListModel *list)
{
GtkSingleSelection *self = GTK_SINGLE_SELECTION (list);
return g_list_model_get_n_items (self->model);
}
static gpointer
gtk_single_selection_get_item (GListModel *list,
guint position)
{
GtkSingleSelection *self = GTK_SINGLE_SELECTION (list);
return g_list_model_get_item (self->model, position);
}
static void
gtk_single_selection_list_model_init (GListModelInterface *iface)
{
iface->get_item_type = gtk_single_selection_get_item_type;
iface->get_n_items = gtk_single_selection_get_n_items;
iface->get_item = gtk_single_selection_get_item;
}
static gboolean
gtk_single_selection_is_selected (GtkSelectionModel *model,
guint position)
{
GtkSingleSelection *self = GTK_SINGLE_SELECTION (model);
return self->selected == position;
}
static gboolean
gtk_single_selection_select_item (GtkSelectionModel *model,
guint position,
gboolean exclusive)
{
GtkSingleSelection *self = GTK_SINGLE_SELECTION (model);
/* XXX: Should we check that position < n_items here? */
gtk_single_selection_set_selected (self, position);
return TRUE;
}
static gboolean
gtk_single_selection_unselect_item (GtkSelectionModel *model,
guint position)
{
GtkSingleSelection *self = GTK_SINGLE_SELECTION (model);
if (!self->can_unselect)
return FALSE;
if (self->selected == position)
gtk_single_selection_set_selected (self, GTK_INVALID_LIST_POSITION);
return TRUE;
}
static void
gtk_single_selection_selection_model_init (GtkSelectionModelInterface *iface)
{
iface->is_selected = gtk_single_selection_is_selected;
iface->select_item = gtk_single_selection_select_item;
iface->unselect_item = gtk_single_selection_unselect_item;
}
G_DEFINE_TYPE_EXTENDED (GtkSingleSelection, gtk_single_selection, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
gtk_single_selection_list_model_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_SELECTION_MODEL,
gtk_single_selection_selection_model_init))
static void
gtk_single_selection_items_changed_cb (GListModel *model,
guint position,
guint removed,
guint added,
GtkSingleSelection *self)
{
gboolean emit_selection_changed = FALSE;
g_object_freeze_notify (G_OBJECT (self));
if (self->selected_item == NULL)
{
if (self->autoselect)
{
self->selected_item = g_list_model_get_item (self->model, 0);
if (self->selected_item)
{
self->selected = 0;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED]);
}
}
}
else if (self->selected < position)
{
/* unchanged */
}
else if (self->selected >= position + removed)
{
self->selected += added - removed;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED]);
}
else
{
guint i;
for (i = 0; i < added; i++)
{
gpointer item = g_list_model_get_item (model, position + i);
if (item == self->selected_item)
{
/* the item moved */
if (self->selected != position + i)
{
self->selected = position + i;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED]);
}
break;
}
}
if (i == added)
{
/* the item really was deleted */
g_clear_object (&self->selected_item);
if (self->autoselect)
{
self->selected = position + (self->selected - position) * added / removed;
self->selected_item = g_list_model_get_item (self->model, self->selected);
if (self->selected_item == NULL && position > 0)
{
self->selected = position - 1;
self->selected_item = g_list_model_get_item (self->model, self->selected);
g_assert (self->selected_item);
}
emit_selection_changed = TRUE;
}
else
{
g_clear_object (&self->selected_item);
self->selected = GTK_INVALID_LIST_POSITION;
}
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED]);
}
}
g_list_model_items_changed (G_LIST_MODEL (self), position, removed, added);
if (emit_selection_changed && self->selected != GTK_INVALID_LIST_POSITION)
gtk_selection_model_selection_changed (GTK_SELECTION_MODEL (self), self->selected, 1);
g_object_thaw_notify (G_OBJECT (self));
}
static void
gtk_single_selection_clear_model (GtkSingleSelection *self)
{
if (self->model == NULL)
return;
g_signal_handlers_disconnect_by_func (self->model,
gtk_single_selection_items_changed_cb,
self);
g_clear_object (&self->model);
}
static void
gtk_single_selection_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkSingleSelection *self = GTK_SINGLE_SELECTION (object);
switch (prop_id)
{
case PROP_AUTOSELECT:
gtk_single_selection_set_autoselect (self, g_value_get_boolean (value));
break;
case PROP_CAN_UNSELECT:
gtk_single_selection_set_can_unselect (self, g_value_get_boolean (value));
break;
case PROP_MODEL:
gtk_single_selection_clear_model (self);
self->model = g_value_dup_object (value);
if (self->model)
g_signal_connect (self->model, "items-changed",
G_CALLBACK (gtk_single_selection_items_changed_cb), self);
if (self->autoselect)
gtk_single_selection_set_selected (self, 0);
break;
case PROP_SELECTED:
gtk_single_selection_set_selected (self, g_value_get_uint (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_single_selection_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkSingleSelection *self = GTK_SINGLE_SELECTION (object);
switch (prop_id)
{
case PROP_AUTOSELECT:
g_value_set_boolean (value, self->autoselect);
break;
case PROP_CAN_UNSELECT:
g_value_set_boolean (value, self->can_unselect);
break;
case PROP_MODEL:
g_value_set_object (value, self->model);
break;
case PROP_SELECTED:
g_value_set_uint (value, self->selected);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_single_selection_dispose (GObject *object)
{
GtkSingleSelection *self = GTK_SINGLE_SELECTION (object);
gtk_single_selection_clear_model (self);
self->selected = GTK_INVALID_LIST_POSITION;
g_clear_object (&self->selected_item);
G_OBJECT_CLASS (gtk_single_selection_parent_class)->dispose (object);
}
static void
gtk_single_selection_class_init (GtkSingleSelectionClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->get_property = gtk_single_selection_get_property;
gobject_class->set_property = gtk_single_selection_set_property;
gobject_class->dispose = gtk_single_selection_dispose;
g_object_class_override_property (gobject_class, PROP_MODEL, "model");
/**
* GtkSingleSelection:autoselect
*
* If the selection will always select an item
*/
properties[PROP_AUTOSELECT] =
g_param_spec_boolean ("autoselect",
P_("Autoselect"),
P_("If the selection will always select an item"),
TRUE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* GtkSingleSelection:can-unselect
*
* If unselecting the selected item is allowed
*/
properties[PROP_CAN_UNSELECT] =
g_param_spec_boolean ("can-unselect",
P_("Can unselect"),
P_("If unselecting the selected item is allowed"),
FALSE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* GtkSingleSelection:selected
*
* Position of the selected item
*/
properties[PROP_SELECTED] =
g_param_spec_uint ("selected",
P_("Selected"),
P_("Position of the selected item"),
0, G_MAXUINT, GTK_INVALID_LIST_POSITION,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, N_PROPS, properties);
}
static void
gtk_single_selection_init (GtkSingleSelection *self)
{
self->selected = GTK_INVALID_LIST_POSITION;
self->autoselect = TRUE;
}
/**
* gtk_single_selection_new:
* @model: (transfer none): the #GListModel to manage
*
* Creates a new selection to handle @model.
*
* Returns: (transfer full) (type GtkSingleSelection): a new #GtkSingleSelection
**/
GtkSingleSelection *
gtk_single_selection_new (GListModel *model)
{
g_return_val_if_fail (G_IS_LIST_MODEL (model), NULL);
return g_object_new (GTK_TYPE_SINGLE_SELECTION,
"model", model,
NULL);
}
/**
* gtk_single_selection_get_selected:
* @self: a #GtkSingleSelection
*
* Gets the position of the selected item. If no item is selected,
* #GTK_INVALID_LIST_POSITION is returned.
*
* Returns: The position of the selected item
**/
guint
gtk_single_selection_get_selected (GtkSingleSelection *self)
{
g_return_val_if_fail (GTK_IS_SINGLE_SELECTION (self), GTK_INVALID_LIST_POSITION);
return self->selected;
}
/**
* gtk_single_selection_set_selected:
* @self: a #GtkSingleSelection
* @position: the item to select or #GTK_INVALID_LIST_POSITION
*
* Selects the item at the given position. If the list does not have an item at
* @position or #GTK_INVALID_LIST_POSITION is given, the behavior depends on the
* value of the GtkSingleSelection:autoselect property: If it is set, no change
* will occur and the old item will stay selected. If it is unset, the selection
* will be unset and no item will be selected.
**/
void
gtk_single_selection_set_selected (GtkSingleSelection *self,
guint position)
{
gpointer new_selected = NULL;
guint old_position;
g_return_if_fail (GTK_IS_SINGLE_SELECTION (self));
if (self->selected == position)
return;
if (self->model)
new_selected = g_list_model_get_item (self->model, position);
if (new_selected == NULL)
position = GTK_INVALID_LIST_POSITION;
if (self->selected == position)
return;
old_position = self->selected;
self->selected = position;
g_clear_object (&self->selected_item);
self->selected_item = new_selected;
if (old_position == GTK_INVALID_LIST_POSITION)
gtk_selection_model_selection_changed (GTK_SELECTION_MODEL (self), position, 1);
else if (position == GTK_INVALID_LIST_POSITION)
gtk_selection_model_selection_changed (GTK_SELECTION_MODEL (self), old_position, 1);
else if (position < old_position)
gtk_selection_model_selection_changed (GTK_SELECTION_MODEL (self), position, old_position - position + 1);
else
gtk_selection_model_selection_changed (GTK_SELECTION_MODEL (self), old_position, position - old_position + 1);
}
/**
* gtk_single_selection_get_autoselect:
* @self: a #GtkSingleSelection
*
* Checks if autoselect has been enabled or disabled via
* gtk_single_selection_set_autoselect().
*
* Returns: %TRUE if autoselect is enabled
**/
gboolean
gtk_single_selection_get_autoselect (GtkSingleSelection *self)
{
g_return_val_if_fail (GTK_IS_SINGLE_SELECTION (self), TRUE);
return self->autoselect;
}
/**
* gtk_single_selection_set_autoselect:
* @self: a #GtkSingleSelection
* @autoselect: %TRUE to always select an item
*
* If @autoselect is %TRUE, @self will enforce that an item is always
* selected. It will select a new item when the currently selected
* item is deleted and it will disallow unselecting the current item.
**/
void
gtk_single_selection_set_autoselect (GtkSingleSelection *self,
gboolean autoselect)
{
g_return_if_fail (GTK_IS_SINGLE_SELECTION (self));
if (self->autoselect == autoselect)
return;
self->autoselect = autoselect;
g_object_freeze_notify (G_OBJECT (self));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_AUTOSELECT]);
if (self->autoselect && !self->selected_item)
gtk_single_selection_set_selected (self, 0);
g_object_thaw_notify (G_OBJECT (self));
}
/**
* gtk_single_selection_get_can_unselect:
* @self: a #GtkSingleSelection
*
* If %TRUE, gtk_selection_model_unselect_item() is supported and allows
* unselecting the selected item.
*
* Returns: %TRUE to support unselecting
**/
gboolean
gtk_single_selection_get_can_unselect (GtkSingleSelection *self)
{
g_return_val_if_fail (GTK_IS_SINGLE_SELECTION (self), FALSE);
return self->can_unselect;
}
/**
* gtk_single_selection_set_can_unselect:
* @self: a #GtkSingleSelection
* @can_unselect: %TRUE to allow unselecting
*
* If %TRUE, unselecting the current item via
* gtk_selection_model_unselect_item() is supported.
*
* Note that setting GtkSingleSelection:autoselect will cause the
* unselecting to not work, so it practically makes no sense to set
* both at the same time the same time..
**/
void
gtk_single_selection_set_can_unselect (GtkSingleSelection *self,
gboolean can_unselect)
{
g_return_if_fail (GTK_IS_SINGLE_SELECTION (self));
if (self->can_unselect == can_unselect)
return;
self->can_unselect = can_unselect;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CAN_UNSELECT]);
}

65
gtk/gtksingleselection.h Normal file
View File

@@ -0,0 +1,65 @@
/*
* Copyright © 2018 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_SINGLE_SELECTION_H__
#define __GTK_SINGLE_SELECTION_H__
#include <gtk/gtktypes.h>
G_BEGIN_DECLS
#define GTK_TYPE_SINGLE_SELECTION (gtk_single_selection_get_type ())
/**
* GTK_INVALID_LIST_POSITION:
*
* The value used to refer to a guaranteed invalid position in a #GListModel. This
* value may be returned from some functions, others may accept it as input.
* Its interpretion may differ for different functions.
*
* Refer to each function's documentation for if this value is allowed and what it
* does.
*/
#define GTK_INVALID_LIST_POSITION (G_MAXUINT)
GDK_AVAILABLE_IN_ALL
G_DECLARE_FINAL_TYPE (GtkSingleSelection, gtk_single_selection, GTK, SINGLE_SELECTION, GObject)
GDK_AVAILABLE_IN_ALL
GtkSingleSelection * gtk_single_selection_new (GListModel *model);
GDK_AVAILABLE_IN_ALL
guint gtk_single_selection_get_selected (GtkSingleSelection *self);
GDK_AVAILABLE_IN_ALL
void gtk_single_selection_set_selected (GtkSingleSelection *self,
guint position);
GDK_AVAILABLE_IN_ALL
gboolean gtk_single_selection_get_autoselect (GtkSingleSelection *self);
GDK_AVAILABLE_IN_ALL
void gtk_single_selection_set_autoselect (GtkSingleSelection *self,
gboolean autoselect);
GDK_AVAILABLE_IN_ALL
gboolean gtk_single_selection_get_can_unselect (GtkSingleSelection *self);
GDK_AVAILABLE_IN_ALL
void gtk_single_selection_set_can_unselect (GtkSingleSelection *self,
gboolean can_unselect);
G_END_DECLS
#endif /* __GTK_SINGLE_SELECTION_H__ */

View File

@@ -323,6 +323,7 @@ gtk_public_sources = files([
'gtksearchbar.c',
'gtksearchentry.c',
'gtkselection.c',
'gtkselectionmodel.c',
'gtkseparator.c',
'gtkseparatormenuitem.c',
'gtkseparatortoolitem.c',
@@ -334,6 +335,7 @@ gtk_public_sources = files([
'gtkshortcutswindow.c',
'gtkshow.c',
'gtksidebarrow.c',
'gtksingleselection.c',
'gtksizegroup.c',
'gtksizerequest.c',
'gtkslicelistmodel.c',
@@ -557,6 +559,7 @@ gtk_public_headers = files([
'gtksearchbar.h',
'gtksearchentry.h',
'gtkselection.h',
'gtkselectionmodel.h',
'gtkseparator.h',
'gtkseparatormenuitem.h',
'gtkseparatortoolitem.h',
@@ -567,6 +570,7 @@ gtk_public_headers = files([
'gtkshortcutsshortcut.h',
'gtkshortcutswindow.h',
'gtkshow.h',
'gtksingleselection.h',
'gtksizegroup.h',
'gtksizerequest.h',
'gtkslicelistmodel.h',

View File

@@ -44,6 +44,7 @@ tests = [
['regression-tests'],
['scrolledwindow'],
['searchbar'],
['singleselection'],
['slicelistmodel'],
['sortlistmodel'],
['spinbutton'],

View File

@@ -0,0 +1,489 @@
/*
* Copyright (C) 2019, Red Hat, Inc.
* Authors: Matthias Clasen <mclasen@redhat.com>
*
* 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 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/>.
*/
#include <locale.h>
#include <gtk/gtk.h>
static GQuark number_quark;
static GQuark changes_quark;
static GQuark selection_quark;
static guint
get (GListModel *model,
guint position)
{
GObject *object = g_list_model_get_item (model, position);
g_assert (object != NULL);
return GPOINTER_TO_UINT (g_object_get_qdata (object, number_quark));
}
static char *
model_to_string (GListModel *model)
{
GString *string = g_string_new (NULL);
guint i;
for (i = 0; i < g_list_model_get_n_items (model); i++)
{
if (i > 0)
g_string_append (string, " ");
g_string_append_printf (string, "%u", get (model, i));
}
return g_string_free (string, FALSE);
}
static char *
selection_to_string (GListModel *model)
{
GString *string = g_string_new (NULL);
guint i;
for (i = 0; i < g_list_model_get_n_items (model); i++)
{
if (!gtk_selection_model_is_selected (GTK_SELECTION_MODEL (model), i))
continue;
if (string->len > 0)
g_string_append (string, " ");
g_string_append_printf (string, "%u", get (model, i));
}
return g_string_free (string, FALSE);
}
static GListStore *
new_store (guint start,
guint end,
guint step);
static GObject *
make_object (guint number)
{
GObject *object;
/* 0 cannot be differentiated from NULL, so don't use it */
g_assert (number != 0);
object = g_object_new (G_TYPE_OBJECT, NULL);
g_object_set_qdata (object, number_quark, GUINT_TO_POINTER (number));
return object;
}
static void
splice (GListStore *store,
guint pos,
guint removed,
guint *numbers,
guint added)
{
GObject *objects[added];
guint i;
for (i = 0; i < added; i++)
objects[i] = make_object (numbers[i]);
g_list_store_splice (store, pos, removed, (gpointer *) objects, added);
for (i = 0; i < added; i++)
g_object_unref (objects[i]);
}
static void
add (GListStore *store,
guint number)
{
GObject *object = make_object (number);
g_list_store_append (store, object);
g_object_unref (object);
}
static void
insert (GListStore *store,
guint position,
guint number)
{
GObject *object = make_object (number);
g_list_store_insert (store, position, object);
g_object_unref (object);
}
#define assert_model(model, expected) G_STMT_START{ \
char *s = model_to_string (G_LIST_MODEL (model)); \
if (!g_str_equal (s, expected)) \
g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#model " == " #expected, s, "==", expected); \
g_free (s); \
}G_STMT_END
#define ignore_changes(model) G_STMT_START{ \
GString *changes = g_object_get_qdata (G_OBJECT (model), changes_quark); \
g_string_set_size (changes, 0); \
}G_STMT_END
#define assert_changes(model, expected) G_STMT_START{ \
GString *changes = g_object_get_qdata (G_OBJECT (model), changes_quark); \
if (!g_str_equal (changes->str, expected)) \
g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#model " == " #expected, changes->str, "==", expected); \
g_string_set_size (changes, 0); \
}G_STMT_END
#define assert_selection(model, expected) G_STMT_START{ \
char *s = selection_to_string (G_LIST_MODEL (model)); \
if (!g_str_equal (s, expected)) \
g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#model " == " #expected, s, "==", expected); \
g_free (s); \
}G_STMT_END
#define assert_selection_changes(model, expected) G_STMT_START{ \
GString *changes = g_object_get_qdata (G_OBJECT (model), selection_quark); \
if (!g_str_equal (changes->str, expected)) \
g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#model " == " #expected, changes->str, "==", expected); \
g_string_set_size (changes, 0); \
}G_STMT_END
static GListStore *
new_empty_store (void)
{
return g_list_store_new (G_TYPE_OBJECT);
}
static GListStore *
new_store (guint start,
guint end,
guint step)
{
GListStore *store = new_empty_store ();
guint i;
for (i = start; i <= end; i += step)
add (store, i);
return store;
}
static void
items_changed (GListModel *model,
guint position,
guint removed,
guint added,
GString *changes)
{
g_assert (removed != 0 || added != 0);
if (changes->len)
g_string_append (changes, ", ");
if (removed == 1 && added == 0)
{
g_string_append_printf (changes, "-%u", position);
}
else if (removed == 0 && added == 1)
{
g_string_append_printf (changes, "+%u", position);
}
else
{
g_string_append_printf (changes, "%u", position);
if (removed > 0)
g_string_append_printf (changes, "-%u", removed);
if (added > 0)
g_string_append_printf (changes, "+%u", added);
}
}
static void
selection_changed (GListModel *model,
guint position,
guint n_items,
GString *changes)
{
if (changes->len)
g_string_append (changes, ", ");
g_string_append_printf (changes, "%u:%u", position, n_items);
}
static void
free_changes (gpointer data)
{
GString *changes = data;
/* all changes must have been checked via assert_changes() before */
g_assert_cmpstr (changes->str, ==, "");
g_string_free (changes, TRUE);
}
static GtkSelectionModel *
new_model (GListStore *store, gboolean autoselect, gboolean can_unselect)
{
GtkSelectionModel *result;
GString *changes;
result = GTK_SELECTION_MODEL (gtk_single_selection_new (G_LIST_MODEL (store)));
/* We want to return an empty selection unless autoselect is true,
* so undo the initial selection due to autoselect defaulting to TRUE.
*/
gtk_single_selection_set_autoselect (GTK_SINGLE_SELECTION (result), FALSE);
gtk_single_selection_set_can_unselect (GTK_SINGLE_SELECTION (result), TRUE);
gtk_selection_model_unselect_item (result, 0);
gtk_single_selection_set_autoselect (GTK_SINGLE_SELECTION (result), autoselect);
gtk_single_selection_set_can_unselect (GTK_SINGLE_SELECTION (result), can_unselect);
changes = g_string_new ("");
g_object_set_qdata_full (G_OBJECT(result), changes_quark, changes, free_changes);
g_signal_connect (result, "items-changed", G_CALLBACK (items_changed), changes);
changes = g_string_new ("");
g_object_set_qdata_full (G_OBJECT(result), selection_quark, changes, free_changes);
g_signal_connect (result, "selection-changed", G_CALLBACK (selection_changed), changes);
return result;
}
static void
test_create (void)
{
GtkSelectionModel *selection;
GListStore *store;
store = new_store (1, 5, 2);
selection = new_model (store, FALSE, FALSE);
g_assert_false (gtk_single_selection_get_autoselect (GTK_SINGLE_SELECTION (selection)));
assert_model (selection, "1 3 5");
assert_changes (selection, "");
assert_selection (selection, "");
assert_selection_changes (selection, "");
g_object_unref (store);
assert_model (selection, "1 3 5");
assert_changes (selection, "");
assert_selection (selection, "");
assert_selection_changes (selection, "");
g_object_unref (selection);
}
static void
test_changes (void)
{
GtkSelectionModel *selection;
GListStore *store;
store = new_store (1, 5, 1);
selection = new_model (store, FALSE, FALSE);
assert_model (selection, "1 2 3 4 5");
assert_changes (selection, "");
assert_selection (selection, "");
assert_selection_changes (selection, "");
g_list_store_remove (store, 3);
assert_model (selection, "1 2 3 5");
assert_changes (selection, "-3");
assert_selection (selection, "");
assert_selection_changes (selection, "");
insert (store, 3, 99);
assert_model (selection, "1 2 3 99 5");
assert_changes (selection, "+3");
assert_selection (selection, "");
assert_selection_changes (selection, "");
splice (store, 3, 2, (guint[]) { 97 }, 1);
assert_model (selection, "1 2 3 97");
assert_changes (selection, "3-2+1");
assert_selection (selection, "");
assert_selection_changes (selection, "");
g_object_unref (store);
g_object_unref (selection);
}
static void
test_selection (void)
{
GtkSelectionModel *selection;
GListStore *store;
gboolean ret;
store = new_store (1, 5, 1);
selection = new_model (store, TRUE, FALSE);
assert_selection (selection, "1");
assert_selection_changes (selection, "");
ret = gtk_selection_model_select_item (selection, 3, FALSE, FALSE);
g_assert_true (ret);
assert_selection (selection, "4");
assert_selection_changes (selection, "0:4");
ret = gtk_selection_model_unselect_item (selection, 3);
g_assert_false (ret);
assert_selection (selection, "4");
assert_selection_changes (selection, "");
ret = gtk_selection_model_select_item (selection, 1, FALSE, FALSE);
g_assert_true (ret);
assert_selection (selection, "2");
assert_selection_changes (selection, "1:3");
ret = gtk_selection_model_select_range (selection, 3, 2, FALSE);
g_assert_false (ret);
assert_selection (selection, "2");
assert_selection_changes (selection, "");
ret = gtk_selection_model_unselect_range (selection, 4, 2);
g_assert_false (ret);
assert_selection (selection, "2");
assert_selection_changes (selection, "");
ret = gtk_selection_model_select_all (selection);
g_assert_false (ret);
assert_selection (selection, "2");
assert_selection_changes (selection, "");
ret = gtk_selection_model_unselect_all (selection);
g_assert_false (ret);
assert_selection (selection, "2");
assert_selection_changes (selection, "");
g_object_unref (store);
g_object_unref (selection);
}
static void
test_autoselect (void)
{
GtkSelectionModel *selection;
GListStore *store;
store = new_store (2, 1, 1);
selection = new_model (store, TRUE, FALSE);
assert_selection (selection, "");
assert_selection_changes (selection, "");
add (store, 1);
assert_selection (selection, "1");
assert_selection_changes (selection, "0:1");
splice (store, 0, 1, (guint[]) { 97 }, 1);
assert_selection (selection, "97");
assert_selection_changes (selection, "0:1");
ignore_changes (selection);
g_object_unref (store);
g_object_unref (selection);
}
static void
test_can_unselect (void)
{
GtkSelectionModel *selection;
GListStore *store;
gboolean ret;
store = new_store (1, 5, 1);
selection = new_model (store, TRUE, FALSE);
assert_selection (selection, "1");
assert_selection_changes (selection, "");
ret = gtk_selection_model_unselect_item (selection, 0);
g_assert_false (ret);
assert_selection (selection, "1");
assert_selection_changes (selection, "");
gtk_single_selection_set_can_unselect (GTK_SINGLE_SELECTION (selection), TRUE);
assert_selection (selection, "1");
ret = gtk_selection_model_unselect_item (selection, 0);
g_assert_true (ret);
assert_selection (selection, "");
assert_selection_changes (selection, "0:1");
ignore_changes (selection);
g_object_unref (store);
g_object_unref (selection);
}
static int
sort_inverse (gconstpointer a, gconstpointer b, gpointer data)
{
int ia = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (a), number_quark));
int ib = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (b), number_quark));
return ib - ia;
}
static void
test_persistence (void)
{
GtkSelectionModel *selection;
GListStore *store;
store = new_store (1, 5, 1);
selection = new_model (store, TRUE, FALSE);
assert_selection (selection, "1");
assert_selection_changes (selection, "");
g_assert_true (gtk_selection_model_is_selected (selection, 0));
g_assert_false (gtk_selection_model_is_selected (selection, 4));
g_list_store_sort (store, sort_inverse, NULL);
assert_selection (selection, "1");
assert_selection_changes (selection, "");
g_assert_false (gtk_selection_model_is_selected (selection, 0));
g_assert_true (gtk_selection_model_is_selected (selection, 4));
ignore_changes (selection);
g_object_unref (store);
g_object_unref (selection);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
setlocale (LC_ALL, "C");
g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=%s");
number_quark = g_quark_from_static_string ("Hell and fire was spawned to be released.");
changes_quark = g_quark_from_static_string ("What did I see? Can I believe what I saw?");
selection_quark = g_quark_from_static_string ("Mana mana, badibidibi");
g_test_add_func ("/singleselection/create", test_create);
#if GLIB_CHECK_VERSION (2, 58, 0) /* g_list_store_splice() is broken before 2.58 */
g_test_add_func ("/singleselection/changes", test_changes);
#endif
g_test_add_func ("/singleselection/selection", test_selection);
g_test_add_func ("/singleselection/autoselect", test_autoselect);
g_test_add_func ("/singleselection/can-unselect", test_can_unselect);
g_test_add_func ("/singleselection/persistence", test_persistence);
return g_test_run ();
}