listbase: Move selection handling here

This commit is contained in:
Benjamin Otte
2019-10-23 15:02:02 +02:00
parent 238b719909
commit 236c2a7d51
4 changed files with 226 additions and 318 deletions

View File

@@ -75,8 +75,6 @@ struct _GtkGridView
double anchor_yalign;
guint anchor_xstart : 1;
guint anchor_ystart : 1;
/* the last item that was selected - basically the location to extend selections from */
GtkListItemTracker *selected;
/* the item that has input focus */
GtkListItemTracker *focus;
};
@@ -407,25 +405,6 @@ gtk_grid_view_set_anchor (GtkGridView *self,
}
}
static void
gtk_grid_view_select_item (GtkGridView *self,
guint pos,
gboolean modify,
gboolean extend)
{
if (gtk_selection_model_user_select_item (gtk_list_item_manager_get_model (self->item_manager),
pos,
modify,
extend ? gtk_list_item_tracker_get_position (self->item_manager, self->selected)
: GTK_INVALID_LIST_POSITION))
{
gtk_list_item_tracker_set_position (self->item_manager,
self->selected,
pos,
0, 0);
}
}
static gboolean
gtk_grid_view_focus (GtkWidget *widget,
GtkDirectionType direction)
@@ -473,7 +452,7 @@ moved_focus:
extend = TRUE;
}
gtk_grid_view_select_item (self,
gtk_list_base_select_item (GTK_LIST_BASE (self),
gtk_list_item_get_position (GTK_LIST_ITEM (new_focus_child)),
modify,
extend);
@@ -1055,11 +1034,6 @@ gtk_grid_view_dispose (GObject *object)
gtk_list_item_tracker_free (self->item_manager, self->anchor);
self->anchor = NULL;
}
if (self->selected)
{
gtk_list_item_tracker_free (self->item_manager, self->selected);
self->selected = NULL;
}
if (self->focus)
{
gtk_list_item_tracker_free (self->item_manager, self->focus);
@@ -1151,46 +1125,6 @@ gtk_grid_view_set_property (GObject *object,
}
}
static void
gtk_grid_view_select_item_action (GtkWidget *widget,
const char *action_name,
GVariant *parameter)
{
GtkGridView *self = GTK_GRID_VIEW (widget);
guint pos;
gboolean modify, extend;
g_variant_get (parameter, "(ubb)", &pos, &modify, &extend);
gtk_grid_view_select_item (self, pos, modify, extend);
}
static void
gtk_grid_view_select_all (GtkWidget *widget,
const char *action_name,
GVariant *parameter)
{
GtkGridView *self = GTK_GRID_VIEW (widget);
GtkSelectionModel *selection_model;
selection_model = gtk_list_item_manager_get_model (self->item_manager);
gtk_selection_model_select_all (selection_model);
}
static void
gtk_grid_view_unselect_all (GtkWidget *widget,
const char *action_name,
GVariant *parameter)
{
GtkGridView *self = GTK_GRID_VIEW (widget);
GtkSelectionModel *selection_model;
selection_model = gtk_list_item_manager_get_model (self->item_manager);
gtk_selection_model_unselect_all (selection_model);
}
static void
gtk_grid_view_compute_scroll_align (GtkGridView *self,
GtkOrientation orientation,
@@ -1337,47 +1271,6 @@ gtk_grid_view_activate_item (GtkWidget *widget,
g_signal_emit (widget, signals[ACTIVATE], 0, pos);
}
static void
gtk_grid_view_move_to (GtkGridView *self,
guint pos,
gboolean select,
gboolean modify,
gboolean extend)
{
Cell *cell;
cell = gtk_list_item_manager_get_nth (self->item_manager, pos, NULL);
if (cell == NULL)
return;
if (!cell->parent.widget)
{
GtkListItemTracker *tracker = gtk_list_item_tracker_new (self->item_manager);
/* We need a tracker here to create the widget.
* That needs to have happened or we can't grab it.
* And we can't use a different tracker, because they manage important rows,
* so we create a temporary one. */
gtk_list_item_tracker_set_position (self->item_manager, tracker, pos, 0, 0);
cell = gtk_list_item_manager_get_nth (self->item_manager, pos, NULL);
g_assert (cell->parent.widget);
if (!gtk_widget_grab_focus (cell->parent.widget))
return; /* FIXME: What now? Can this even happen? */
gtk_list_item_tracker_free (self->item_manager, tracker);
}
else
{
if (!gtk_widget_grab_focus (cell->parent.widget))
return; /* FIXME: What now? Can this even happen? */
}
if (select)
gtk_grid_view_select_item (self, pos, modify, extend);
}
static void
gtk_grid_view_move_cursor (GtkWidget *widget,
GVariant *args,
@@ -1413,7 +1306,7 @@ gtk_grid_view_move_cursor (GtkWidget *widget,
return;
}
gtk_grid_view_move_to (self, pos + amount, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), pos + amount, select, modify, extend);
}
static void
@@ -1429,7 +1322,7 @@ gtk_grid_view_move_cursor_to_start (GtkWidget *widget,
g_variant_get (args, "(bbb)", &select, &modify, &extend);
gtk_grid_view_move_to (self, 0, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), 0, select, modify, extend);
}
static void
@@ -1450,7 +1343,7 @@ gtk_grid_view_move_cursor_to_end (GtkWidget *widget,
g_variant_get (args, "(bbb)", &select, &modify, &extend);
gtk_grid_view_move_to (self, n_items - 1, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), n_items - 1, select, modify, extend);
}
static void
@@ -1486,7 +1379,7 @@ gtk_grid_view_move_cursor_page_up (GtkWidget *widget,
g_variant_get (args, "(bbb)", &select, &modify, &extend);
gtk_grid_view_move_to (self, new_pos, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), new_pos, select, modify, extend);
}
static void
@@ -1537,7 +1430,7 @@ gtk_grid_view_move_cursor_page_down (GtkWidget *widget,
g_variant_get (args, "(bbb)", &select, &modify, &extend);
gtk_grid_view_move_to (self, new_pos, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), new_pos, select, modify, extend);
}
static void
@@ -1723,51 +1616,6 @@ gtk_grid_view_class_init (GtkGridViewClass *klass)
"u",
gtk_grid_view_activate_item);
/**
* GtkGridView|list.select-item:
* @position: position of item to select
* @modify: %TRUE to toggle the existing selection, %FALSE to select
* @extend: %TRUE to extend the selection
*
* Changes selection.
*
* If @extend is %TRUE and the model supports selecting ranges, the
* affected items are all items from the last selected item to the item
* in @position.
* If @extend is %FALSE or selecting ranges is not supported, only the
* item in @position is affected.
*
* If @modify is %TRUE, the affected items will be set to the same state.
* If @modify is %FALSE, the affected items will be selected and
* all other items will be deselected.
*/
gtk_widget_class_install_action (widget_class,
"list.select-item",
"(ubb)",
gtk_grid_view_select_item_action);
/**
* GtkGridView|list.select-all:
*
* If the selection model supports it, select all items in the model.
* If not, do nothing.
*/
gtk_widget_class_install_action (widget_class,
"list.select-all",
NULL,
gtk_grid_view_select_all);
/**
* GtkGridView|list.unselect-all:
*
* If the selection model supports it, unselect all items in the model.
* If not, do nothing.
*/
gtk_widget_class_install_action (widget_class,
"list.unselect-all",
NULL,
gtk_grid_view_unselect_all);
/**
* GtkGridView|list.scroll-to-item:
* @position: position of item to scroll to
@@ -1817,7 +1665,6 @@ gtk_grid_view_init (GtkGridView *self)
self->anchor = gtk_list_item_tracker_new (self->item_manager);
self->anchor_xstart = TRUE;
self->anchor_ystart = TRUE;
self->selected = gtk_list_item_tracker_new (self->item_manager);
self->focus = gtk_list_item_tracker_new (self->item_manager);
self->min_columns = 1;

View File

@@ -31,6 +31,9 @@ struct _GtkListBasePrivate
GtkListItemManager *item_manager;
GtkAdjustment *adjustment[2];
GtkScrollablePolicy scroll_policy[2];
/* the last item that was selected - basically the location to extend selections from */
GtkListItemTracker *selected;
};
enum
@@ -84,6 +87,47 @@ gtk_list_base_clear_adjustment (GtkListBase *self,
g_clear_object (&priv->adjustment[orientation]);
}
/*
* gtk_list_base_select_item:
* @self: a %GtkListBase
* @pos: item to select
* @modify: %TRUE if the selection should be modified, %FALSE
* if a new selection should be done. This is usually set
* to %TRUE if the user keeps the <Shift> key pressed.
* @extend_pos: %TRUE if the selection should be extended.
* Selections are usually extended from the last selected
* position if the user presses the <Ctrl> key.
*
* Selects the item at @pos according to how GTK list widgets modify
* selections, both when clicking rows with the mouse or when using
* the keyboard.
**/
void
gtk_list_base_select_item (GtkListBase *self,
guint pos,
gboolean modify,
gboolean extend)
{
GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
GtkSelectionModel *model;
model = gtk_list_item_manager_get_model (priv->item_manager);
if (model == NULL)
return;
if (gtk_selection_model_user_select_item (model,
pos,
modify,
extend ? gtk_list_item_tracker_get_position (priv->item_manager, priv->selected)
: GTK_INVALID_LIST_POSITION))
{
gtk_list_item_tracker_set_position (priv->item_manager,
priv->selected,
pos,
0, 0);
}
}
static void
gtk_list_base_dispose (GObject *object)
{
@@ -93,6 +137,11 @@ gtk_list_base_dispose (GObject *object)
gtk_list_base_clear_adjustment (self, GTK_ORIENTATION_HORIZONTAL);
gtk_list_base_clear_adjustment (self, GTK_ORIENTATION_VERTICAL);
if (priv->selected)
{
gtk_list_item_tracker_free (priv->item_manager, priv->selected);
priv->selected = NULL;
}
g_clear_object (&priv->item_manager);
G_OBJECT_CLASS (gtk_list_base_parent_class)->dispose (object);
@@ -208,9 +257,56 @@ gtk_list_base_set_property (GObject *object,
}
}
static void
gtk_list_base_select_item_action (GtkWidget *widget,
const char *action_name,
GVariant *parameter)
{
GtkListBase *self = GTK_LIST_BASE (widget);
guint pos;
gboolean modify, extend;
g_variant_get (parameter, "(ubb)", &pos, &modify, &extend);
gtk_list_base_select_item (self, pos, modify, extend);
}
static void
gtk_list_base_select_all (GtkWidget *widget,
const char *action_name,
GVariant *parameter)
{
GtkListBase *self = GTK_LIST_BASE (widget);
GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
GtkSelectionModel *selection_model;
selection_model = gtk_list_item_manager_get_model (priv->item_manager);
if (selection_model == NULL)
return;
gtk_selection_model_select_all (selection_model);
}
static void
gtk_list_base_unselect_all (GtkWidget *widget,
const char *action_name,
GVariant *parameter)
{
GtkListBase *self = GTK_LIST_BASE (widget);
GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
GtkSelectionModel *selection_model;
selection_model = gtk_list_item_manager_get_model (priv->item_manager);
if (selection_model == NULL)
return;
gtk_selection_model_unselect_all (selection_model);
}
static void
gtk_list_base_class_init (GtkListBaseClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gpointer iface;
@@ -234,6 +330,52 @@ gtk_list_base_class_init (GtkListBaseClass *klass)
g_object_interface_find_property (iface, "vscroll-policy"));
g_object_class_install_properties (gobject_class, N_PROPS, properties);
/**
* GtkListBase|list.select-item:
* @position: position of item to select
* @modify: %TRUE to toggle the existing selection, %FALSE to select
* @extend: %TRUE to extend the selection
*
* Changes selection.
*
* If @extend is %TRUE and the model supports selecting ranges, the
* affected items are all items from the last selected item to the item
* in @position.
* If @extend is %FALSE or selecting ranges is not supported, only the
* item in @position is affected.
*
* If @modify is %TRUE, the affected items will be set to the same state.
* If @modify is %FALSE, the affected items will be selected and
* all other items will be deselected.
*/
gtk_widget_class_install_action (widget_class,
"list.select-item",
"(ubb)",
gtk_list_base_select_item_action);
/**
* GtkListBase|list.select-all:
*
* If the selection model supports it, select all items in the model.
* If not, do nothing.
*/
gtk_widget_class_install_action (widget_class,
"list.select-all",
NULL,
gtk_list_base_select_all);
/**
* GtkListBase|list.unselect-all:
*
* If the selection model supports it, unselect all items in the model.
* If not, do nothing.
*/
gtk_widget_class_install_action (widget_class,
"list.unselect-all",
NULL,
gtk_list_base_unselect_all);
}
static void
@@ -248,6 +390,8 @@ gtk_list_base_init_real (GtkListBase *self,
g_class->list_item_augment_size,
g_class->list_item_augment_func);
priv->selected = gtk_list_item_tracker_new (priv->item_manager);
priv->adjustment[GTK_ORIENTATION_HORIZONTAL] = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
priv->adjustment[GTK_ORIENTATION_VERTICAL] = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
@@ -337,3 +481,63 @@ gtk_list_base_get_manager (GtkListBase *self)
return priv->item_manager;
}
/*
* gtk_list_base_grab_focus_on_item:
* @self: a #GtkListBase
* @pos: position of the item to focus
* @select: %TRUE to select the item
* @modify: if selecting, %TRUE to modify the selected
* state, %FALSE to always select
* @extend: if selecting, %TRUE to extend the selection,
* %FALSE to only operate on this item
*
* Tries to grab focus on the given item. If there is no item
* at this position or grabbing focus failed, %FALSE will be
* returned.
*
* Returns: %TRUE if focusing the item succeeded
**/
gboolean
gtk_list_base_grab_focus_on_item (GtkListBase *self,
guint pos,
gboolean select,
gboolean modify,
gboolean extend)
{
GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
GtkListItemManagerItem *item;
item = gtk_list_item_manager_get_nth (priv->item_manager, pos, NULL);
if (item == NULL)
return FALSE;
if (!item->widget)
{
GtkListItemTracker *tracker = gtk_list_item_tracker_new (priv->item_manager);
/* We need a tracker here to create the widget.
* That needs to have happened or we can't grab it.
* And we can't use a different tracker, because they manage important rows,
* so we create a temporary one. */
gtk_list_item_tracker_set_position (priv->item_manager, tracker, pos, 0, 0);
item = gtk_list_item_manager_get_nth (priv->item_manager, pos, NULL);
g_assert (item->widget);
if (!gtk_widget_grab_focus (item->widget))
return FALSE;
gtk_list_item_tracker_free (priv->item_manager, tracker);
}
else
{
if (!gtk_widget_grab_focus (item->widget))
return FALSE;
}
if (select)
gtk_list_base_select_item (self, pos, modify, extend);
return TRUE;
}

View File

@@ -56,4 +56,14 @@ int gtk_list_base_set_adjustment_values (GtkListBase
int size,
int page_size);
void gtk_list_base_select_item (GtkListBase *self,
guint pos,
gboolean modify,
gboolean extend);
gboolean gtk_list_base_grab_focus_on_item (GtkListBase *self,
guint pos,
gboolean select,
gboolean modify,
gboolean extend);
#endif /* __GTK_LIST_BASE_PRIVATE_H__ */

View File

@@ -70,8 +70,6 @@ struct _GtkListView
GtkListItemTracker *anchor;
double anchor_align;
gboolean anchor_start;
/* the last item that was selected - basically the location to extend selections from */
GtkListItemTracker *selected;
/* the item that has input focus */
GtkListItemTracker *focus;
};
@@ -695,25 +693,6 @@ gtk_list_view_size_allocate (GtkWidget *widget,
}
}
static void
gtk_list_view_select_item (GtkListView *self,
guint pos,
gboolean modify,
gboolean extend)
{
if (gtk_selection_model_user_select_item (gtk_list_item_manager_get_model (self->item_manager),
pos,
modify,
extend ? gtk_list_item_tracker_get_position (self->item_manager, self->selected)
: GTK_INVALID_LIST_POSITION))
{
gtk_list_item_tracker_set_position (self->item_manager,
self->selected,
pos,
0, 0);
}
}
static gboolean
gtk_list_view_focus (GtkWidget *widget,
GtkDirectionType direction)
@@ -761,7 +740,7 @@ moved_focus:
extend = TRUE;
}
gtk_list_view_select_item (self,
gtk_list_base_select_item (GTK_LIST_BASE (self),
gtk_list_item_get_position (GTK_LIST_ITEM (new_focus_child)),
modify,
extend);
@@ -782,11 +761,6 @@ gtk_list_view_dispose (GObject *object)
gtk_list_item_tracker_free (self->item_manager, self->anchor);
self->anchor = NULL;
}
if (self->selected)
{
gtk_list_item_tracker_free (self->item_manager, self->selected);
self->selected = NULL;
}
if (self->focus)
{
gtk_list_item_tracker_free (self->item_manager, self->focus);
@@ -870,46 +844,6 @@ gtk_list_view_set_property (GObject *object,
}
}
static void
gtk_list_view_select_item_action (GtkWidget *widget,
const char *action_name,
GVariant *parameter)
{
GtkListView *self = GTK_LIST_VIEW (widget);
guint pos;
gboolean modify, extend;
g_variant_get (parameter, "(ubb)", &pos, &modify, &extend);
gtk_list_view_select_item (self, pos, modify, extend);
}
static void
gtk_list_view_select_all (GtkWidget *widget,
const char *action_name,
GVariant *parameter)
{
GtkListView *self = GTK_LIST_VIEW (widget);
GtkSelectionModel *selection_model;
selection_model = gtk_list_item_manager_get_model (self->item_manager);
gtk_selection_model_select_all (selection_model);
}
static void
gtk_list_view_unselect_all (GtkWidget *widget,
const char *action_name,
GVariant *parameter)
{
GtkListView *self = GTK_LIST_VIEW (widget);
GtkSelectionModel *selection_model;
selection_model = gtk_list_item_manager_get_model (self->item_manager);
gtk_selection_model_unselect_all (selection_model);
}
static void
gtk_list_view_update_focus_tracker (GtkListView *self)
{
@@ -1049,47 +983,6 @@ gtk_list_view_activate_item (GtkWidget *widget,
g_signal_emit (widget, signals[ACTIVATE], 0, pos);
}
static void
gtk_list_view_move_to (GtkListView *self,
guint pos,
gboolean select,
gboolean modify,
gboolean extend)
{
ListRow *row;
row = gtk_list_item_manager_get_nth (self->item_manager, pos, NULL);
if (row == NULL)
return;
if (!row->parent.widget)
{
GtkListItemTracker *tracker = gtk_list_item_tracker_new (self->item_manager);
/* We need a tracker here to create the widget.
* That needs to have happened or we can't grab it.
* And we can't use a different tracker, because they manage important rows,
* so we create a temporary one. */
gtk_list_item_tracker_set_position (self->item_manager, tracker, pos, 0, 0);
row = gtk_list_item_manager_get_nth (self->item_manager, pos, NULL);
g_assert (row->parent.widget);
if (!gtk_widget_grab_focus (row->parent.widget))
return; /* FIXME: What now? Can this even happen? */
gtk_list_item_tracker_free (self->item_manager, tracker);
}
else
{
if (!gtk_widget_grab_focus (row->parent.widget))
return; /* FIXME: What now? Can this even happen? */
}
if (select)
gtk_list_view_select_item (self, pos, modify, extend);
}
static void
gtk_list_view_move_cursor (GtkWidget *widget,
GVariant *args,
@@ -1120,7 +1013,7 @@ gtk_list_view_move_cursor (GtkWidget *widget,
if (new_pos >= n_items)
return;
gtk_list_view_move_to (self, new_pos, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), new_pos, select, modify, extend);
}
static void
@@ -1136,7 +1029,7 @@ gtk_list_view_move_cursor_to_start (GtkWidget *widget,
g_variant_get (args, "(bbb)", &select, &modify, &extend);
gtk_list_view_move_to (self, 0, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), 0, select, modify, extend);
}
static void
@@ -1157,7 +1050,7 @@ gtk_list_view_move_cursor_to_end (GtkWidget *widget,
g_variant_get (args, "(bbb)", &select, &modify, &extend);
gtk_list_view_move_to (self, n_items - 1, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), n_items - 1, select, modify, extend);
}
static void
@@ -1201,7 +1094,7 @@ gtk_list_view_move_cursor_page_up (GtkWidget *widget,
g_variant_get (args, "(bbb)", &select, &modify, &extend);
gtk_list_view_move_to (self, pos, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), pos, select, modify, extend);
}
static void
@@ -1244,7 +1137,7 @@ gtk_list_view_move_cursor_page_down (GtkWidget *widget,
g_variant_get (args, "(bbb)", &select, &modify, &extend);
gtk_list_view_move_to (self, pos, select, modify, extend);
gtk_list_base_grab_focus_on_item (GTK_LIST_BASE (self), pos, select, modify, extend);
}
static void
@@ -1414,51 +1307,6 @@ gtk_list_view_class_init (GtkListViewClass *klass)
"u",
gtk_list_view_activate_item);
/**
* GtkListView|list.select-item:
* @position: position of item to select
* @modify: %TRUE to toggle the existing selection, %FALSE to select
* @extend: %TRUE to extend the selection
*
* Changes selection.
*
* If @extend is %TRUE and the model supports selecting ranges, the
* affected items are all items from the last selected item to the item
* in @position.
* If @extend is %FALSE or selecting ranges is not supported, only the
* item in @position is affected.
*
* If @modify is %TRUE, the affected items will be set to the same state.
* If @modify is %FALSE, the affected items will be selected and
* all other items will be deselected.
*/
gtk_widget_class_install_action (widget_class,
"list.select-item",
"(ubb)",
gtk_list_view_select_item_action);
/**
* GtkListView|list.select-all:
*
* If the selection model supports it, select all items in the model.
* If not, do nothing.
*/
gtk_widget_class_install_action (widget_class,
"list.select-all",
NULL,
gtk_list_view_select_all);
/**
* GtkListView|list.unselect-all:
*
* If the selection model supports it, unselect all items in the model.
* If not, do nothing.
*/
gtk_widget_class_install_action (widget_class,
"list.unselect-all",
NULL,
gtk_list_view_unselect_all);
/**
* GtkListView|list.scroll-to-item:
* @position: position of item to scroll to
@@ -1506,7 +1354,6 @@ gtk_list_view_init (GtkListView *self)
self->item_manager = gtk_list_base_get_manager (GTK_LIST_BASE (self));
self->focus = gtk_list_item_tracker_new (self->item_manager);
self->anchor = gtk_list_item_tracker_new (self->item_manager);
self->selected = gtk_list_item_tracker_new (self->item_manager);
self->orientation = GTK_ORIENTATION_VERTICAL;
}