[tepl] utils: add list_box_scroll_to_row() and scroll_to_selected_row()



commit bf43d2606418349c5ba207f2f21f62e5fa755ada
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Nov 3 00:26:00 2020 +0100

    utils: add list_box_scroll_to_row() and scroll_to_selected_row()

 docs/reference/tepl-sections.txt        |  2 ++
 tepl/tepl-style-scheme-chooser-widget.c | 25 +++-----------------
 tepl/tepl-utils.c                       | 42 +++++++++++++++++++++++++++++++++
 tepl/tepl-utils.h                       |  7 ++++++
 4 files changed, 54 insertions(+), 22 deletions(-)
---
diff --git a/docs/reference/tepl-sections.txt b/docs/reference/tepl-sections.txt
index 508affc..ef0627c 100644
--- a/docs/reference/tepl-sections.txt
+++ b/docs/reference/tepl-sections.txt
@@ -514,6 +514,8 @@ tepl_utils_file_query_exists_finish
 tepl_utils_create_close_button
 tepl_utils_show_warning_dialog
 tepl_utils_list_box_clear
+tepl_utils_list_box_scroll_to_row
+tepl_utils_list_box_scroll_to_selected_row
 tepl_utils_binding_transform_func_smart_bool
 </SECTION>
 
diff --git a/tepl/tepl-style-scheme-chooser-widget.c b/tepl/tepl-style-scheme-chooser-widget.c
index 2f58608..5379334 100644
--- a/tepl/tepl-style-scheme-chooser-widget.c
+++ b/tepl/tepl-style-scheme-chooser-widget.c
@@ -145,25 +145,6 @@ tepl_style_scheme_chooser_widget_dispose (GObject *object)
        G_OBJECT_CLASS (tepl_style_scheme_chooser_widget_parent_class)->dispose (object);
 }
 
-static void
-scroll_to_row (GtkListBox    *list_box,
-              GtkListBoxRow *row)
-{
-       /* See also the call to gtk_container_set_focus_vadjustment() below. */
-       gtk_container_set_focus_child (GTK_CONTAINER (list_box), GTK_WIDGET (row));
-}
-
-static void
-scroll_to_selected_row (GtkListBox *list_box)
-{
-       GtkListBoxRow *selected_row = gtk_list_box_get_selected_row (list_box);
-
-       if (selected_row != NULL)
-       {
-               scroll_to_row (list_box, selected_row);
-       }
-}
-
 static void
 tepl_style_scheme_chooser_widget_map (GtkWidget *widget)
 {
@@ -174,7 +155,7 @@ tepl_style_scheme_chooser_widget_map (GtkWidget *widget)
                GTK_WIDGET_CLASS (tepl_style_scheme_chooser_widget_parent_class)->map (widget);
        }
 
-       scroll_to_selected_row (chooser->priv->list_box);
+       tepl_utils_list_box_scroll_to_selected_row (chooser->priv->list_box);
 }
 
 static void
@@ -251,7 +232,7 @@ tepl_style_scheme_chooser_widget_set_style_scheme (GtkSourceStyleSchemeChooser *
                if (style_scheme_equal (cur_style_scheme, style_scheme))
                {
                        gtk_list_box_select_row (chooser->priv->list_box, cur_list_box_row);
-                       scroll_to_row (chooser->priv->list_box, cur_list_box_row);
+                       tepl_utils_list_box_scroll_to_row (chooser->priv->list_box, cur_list_box_row);
                        break;
                }
        }
@@ -366,7 +347,7 @@ style_scheme_manager_notify_scheme_ids_cb (GtkSourceStyleSchemeManager  *manager
         */
        tepl_style_scheme_chooser_widget_set_style_scheme_id (chooser, style_scheme_id);
 
-       scroll_to_selected_row (chooser->priv->list_box);
+       tepl_utils_list_box_scroll_to_selected_row (chooser->priv->list_box);
 
        g_signal_handlers_unblock_by_func (chooser->priv->list_box,
                                           list_box_selected_rows_changed_cb,
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index 4f15a93..760d60b 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -823,6 +823,48 @@ tepl_utils_list_box_clear (GtkListBox *list_box)
                               NULL);
 }
 
+/**
+ * tepl_utils_list_box_scroll_to_row:
+ * @list_box: a #GtkListBox.
+ * @row: a #GtkListBoxRow.
+ *
+ * Since: 5.2
+ */
+void
+tepl_utils_list_box_scroll_to_row (GtkListBox    *list_box,
+                                  GtkListBoxRow *row)
+{
+       g_return_if_fail (GTK_IS_LIST_BOX (list_box));
+       g_return_if_fail (GTK_IS_LIST_BOX_ROW (row));
+
+       /* gtk_container_set_focus_vadjustment() must also be called beforehand. */
+       gtk_container_set_focus_child (GTK_CONTAINER (list_box), GTK_WIDGET (row));
+}
+
+/**
+ * tepl_utils_list_box_scroll_to_selected_row:
+ * @list_box: a #GtkListBox.
+ *
+ * Calls tepl_utils_list_box_scroll_to_row() on the row returned by
+ * gtk_list_box_get_selected_row(). This function assumes that there is either
+ * zero or one selected row.
+ *
+ * Since: 5.2
+ */
+void
+tepl_utils_list_box_scroll_to_selected_row (GtkListBox *list_box)
+{
+       GtkListBoxRow *selected_row;
+
+       g_return_if_fail (GTK_IS_LIST_BOX (list_box));
+
+       selected_row = gtk_list_box_get_selected_row (list_box);
+       if (selected_row != NULL)
+       {
+               tepl_utils_list_box_scroll_to_row (list_box, selected_row);
+       }
+}
+
 /**
  * tepl_utils_binding_transform_func_smart_bool:
  * @binding: a #GBinding.
diff --git a/tepl/tepl-utils.h b/tepl/tepl-utils.h
index 1699046..485f743 100644
--- a/tepl/tepl-utils.h
+++ b/tepl/tepl-utils.h
@@ -86,6 +86,13 @@ void         tepl_utils_show_warning_dialog                  (GtkWindow   *parent,
 _TEPL_EXTERN
 void           tepl_utils_list_box_clear                       (GtkListBox *list_box);
 
+_TEPL_EXTERN
+void           tepl_utils_list_box_scroll_to_row               (GtkListBox    *list_box,
+                                                                GtkListBoxRow *row);
+
+_TEPL_EXTERN
+void           tepl_utils_list_box_scroll_to_selected_row      (GtkListBox *list_box);
+
 /* Other */
 
 _TEPL_EXTERN


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