[tepl] utils: add list_box_get_row_at_index_with_filter()



commit 6a11805dd00497d756646dea34f4417a6324b74f
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Nov 4 12:24:59 2020 +0100

    utils: add list_box_get_row_at_index_with_filter()

 docs/reference/tepl-sections.txt |  1 +
 tepl/tepl-utils.c                | 54 ++++++++++++++++++++++++++++++++++++++++
 tepl/tepl-utils.h                |  6 +++++
 3 files changed, 61 insertions(+)
---
diff --git a/docs/reference/tepl-sections.txt b/docs/reference/tepl-sections.txt
index 637831d..5eb93fb 100644
--- a/docs/reference/tepl-sections.txt
+++ b/docs/reference/tepl-sections.txt
@@ -517,6 +517,7 @@ tepl_utils_list_box_clear
 tepl_utils_list_box_setup_scrolling
 tepl_utils_list_box_scroll_to_row
 tepl_utils_list_box_scroll_to_selected_row
+tepl_utils_list_box_get_row_at_index_with_filter
 tepl_utils_binding_transform_func_smart_bool
 </SECTION>
 
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index 3aa6518..ec88c7a 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -898,6 +898,60 @@ tepl_utils_list_box_scroll_to_selected_row (GtkListBox *list_box)
        }
 }
 
+/**
+ * tepl_utils_list_box_get_row_at_index_with_filter:
+ * @list_box: a #GtkListBox.
+ * @index: the index of the row.
+ * @filter_func: (scope call): non-%NULL callback function.
+ * @user_data: user data passed to @filter_func.
+ *
+ * This function has the same semantics as gtk_list_box_get_row_at_index(), but
+ * it takes into account only the rows for which @filter_func returns %TRUE.
+ *
+ * Returns: (transfer none) (nullable): the child #GtkListBoxRow or %NULL.
+ * Since: 5.2
+ */
+GtkListBoxRow *
+tepl_utils_list_box_get_row_at_index_with_filter (GtkListBox           *list_box,
+                                                 gint                  index,
+                                                 GtkListBoxFilterFunc  filter_func,
+                                                 gpointer              user_data)
+{
+       GList *all_rows;
+       GList *l;
+       gint remaining_rows_to_find = index + 1;
+       GtkListBoxRow *ret = NULL;
+
+       g_return_val_if_fail (GTK_IS_LIST_BOX (list_box), NULL);
+       g_return_val_if_fail (filter_func != NULL, NULL);
+
+       if (index < 0)
+       {
+               return NULL;
+       }
+
+       all_rows = gtk_container_get_children (GTK_CONTAINER (list_box));
+
+       for (l = all_rows; l != NULL; l = l->next)
+       {
+               GtkListBoxRow *cur_row = GTK_LIST_BOX_ROW (l->data);
+
+               if (filter_func (cur_row, user_data))
+               {
+                       remaining_rows_to_find--;
+
+                       if (remaining_rows_to_find == 0)
+                       {
+                               ret = cur_row;
+                               break;
+                       }
+               }
+       }
+
+       g_list_free (all_rows);
+       return ret;
+}
+
 /**
  * tepl_utils_binding_transform_func_smart_bool:
  * @binding: a #GBinding.
diff --git a/tepl/tepl-utils.h b/tepl/tepl-utils.h
index 79f50d3..68f285b 100644
--- a/tepl/tepl-utils.h
+++ b/tepl/tepl-utils.h
@@ -97,6 +97,12 @@ void         tepl_utils_list_box_scroll_to_row               (GtkListBox    *list_box,
 _TEPL_EXTERN
 void           tepl_utils_list_box_scroll_to_selected_row      (GtkListBox *list_box);
 
+_TEPL_EXTERN
+GtkListBoxRow *        tepl_utils_list_box_get_row_at_index_with_filter (GtkListBox           *list_box,
+                                                                 gint                  index,
+                                                                 GtkListBoxFilterFunc  filter_func,
+                                                                 gpointer              user_data);
+
 /* Other */
 
 _TEPL_EXTERN


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]