From bfa2e6725e68881d92a486ab1ab1657046f4fffd Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 20 May 2023 08:24:11 -0400 Subject: [PATCH] gridview: Add some helpers Add helper functions to get the section of an item and to computes the column for an item, based on sections and the current number of columns. --- gtk/gtkgridview.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/gtk/gtkgridview.c b/gtk/gtkgridview.c index 6d3dec22eb..b9ad272d01 100644 --- a/gtk/gtkgridview.c +++ b/gtk/gtkgridview.c @@ -30,6 +30,7 @@ #include "gtkmultiselection.h" #include "gtktypebuiltins.h" #include "gtkwidgetprivate.h" +#include "gtksectionmodel.h" /* Maximum number of list items created by the gridview. * For debugging, you can set this to G_MAXUINT to ensure @@ -434,6 +435,51 @@ gtk_grid_view_get_allocation (GtkListBase *base, return TRUE; } +/* Returns the section that position falls into + */ +static void +get_section_for_position (GtkGridView *self, + unsigned int position, + unsigned int *section_start, + unsigned int *section_end) +{ + GListModel *model; + unsigned int start, end; + + model = G_LIST_MODEL (gtk_list_item_manager_get_model (self->item_manager)); + + if (!gtk_list_item_manager_get_has_sections (self->item_manager)) + { + start = 0; + end = g_list_model_get_n_items (model); + } + else + { + gtk_section_model_get_section (GTK_SECTION_MODEL (model), position, &start, &end); + } + + if (section_start) + *section_start = start; + if (section_end) + *section_end = end; +} + +/* Returns the column that the given item will fall in, taking + * sections into account. Note that this depends on whether + * we are currently showing sections, and on the number of + * columns that the grid is allocating. + */ +static unsigned int +get_column_for_position (GtkGridView *self, + unsigned int position) +{ + unsigned int start; + + get_section_for_position (self, position, &start, NULL); + + return (position - start) % self->n_columns; +} + static gboolean gtk_grid_view_get_position_from_allocation (GtkListBase *base, int x,