From abe2d1cb16ec4391942781c47116295fa6f2a122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tyrychtr?= Date: Wed, 18 Sep 2024 14:53:55 +0200 Subject: [PATCH] listbox: Introduce tab-behavior property This is similar to the other tab-behavior properties, and allows to control how the tabbing behaves in context of a GtkListBox. It allows to make gnome-initial-setup#216 history, for example. --- gtk/gtklistbox.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++-- gtk/gtklistbox.h | 6 +++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/gtk/gtklistbox.c b/gtk/gtklistbox.c index 47829ba0fb..b7dcc3e739 100644 --- a/gtk/gtklistbox.c +++ b/gtk/gtklistbox.c @@ -155,6 +155,8 @@ struct _GtkListBox GtkListBoxCreateWidgetFunc create_widget_func; gpointer create_widget_func_data; GDestroyNotify create_widget_func_data_destroy; + + GtkListTabBehavior tab_behavior; }; struct _GtkListBoxClass @@ -214,6 +216,7 @@ enum { PROP_ACTIVATE_ON_SINGLE_CLICK, PROP_ACCEPT_UNPAIRED_RELEASE, PROP_SHOW_SEPARATORS, + PROP_TAB_BEHAVIOR, LAST_PROPERTY }; @@ -405,6 +408,9 @@ gtk_list_box_get_property (GObject *obj, case PROP_SHOW_SEPARATORS: g_value_set_boolean (value, box->show_separators); break; + case PROP_TAB_BEHAVIOR: + g_value_set_enum (value, box->tab_behavior); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec); break; @@ -433,6 +439,9 @@ gtk_list_box_set_property (GObject *obj, case PROP_SHOW_SEPARATORS: gtk_list_box_set_show_separators (box, g_value_get_boolean (value)); break; + case PROP_TAB_BEHAVIOR: + gtk_list_box_set_tab_behavior (box, g_value_get_enum (value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec); break; @@ -547,6 +556,17 @@ gtk_list_box_class_init (GtkListBoxClass *klass) FALSE, G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY); + /** + * GtkListBox:tab-behavior: + * + * Behavior of the Tab key + */ + properties[PROP_TAB_BEHAVIOR] = + g_param_spec_enum ("tab-behavior", NULL, NULL, + GTK_TYPE_LIST_TAB_BEHAVIOR, + GTK_LIST_TAB_ALL, + G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY); + g_object_class_install_properties (object_class, LAST_PROPERTY, properties); /** @@ -2022,7 +2042,7 @@ gtk_list_box_focus (GtkWidget *widget, if (gtk_widget_child_focus (focus_child, direction)) return TRUE; - if (direction == GTK_DIR_UP || direction == GTK_DIR_TAB_BACKWARD) + if (direction == GTK_DIR_UP || (direction == GTK_DIR_TAB_BACKWARD && box->tab_behavior == GTK_LIST_TAB_ALL)) { if (GTK_IS_LIST_BOX_ROW (focus_child)) { @@ -2052,7 +2072,7 @@ gtk_list_box_focus (GtkWidget *widget, i = gtk_list_box_get_previous_visible (box, i); } } - else if (direction == GTK_DIR_DOWN || direction == GTK_DIR_TAB_FORWARD) + else if (direction == GTK_DIR_DOWN || (direction == GTK_DIR_TAB_FORWARD && box->tab_behavior == GTK_LIST_TAB_ALL)) { if (GTK_IS_LIST_BOX_ROW (focus_child)) i = gtk_list_box_get_next_visible (box, ROW_PRIV (GTK_LIST_BOX_ROW (focus_child))->iter); @@ -3870,3 +3890,43 @@ gtk_list_box_get_show_separators (GtkListBox *box) return box->show_separators; } + +/** + * gtk_list_box_set_tab_behavior: + * @box: a `GtkListBox` + * @tab_behavior: the tab behavior + * + * Sets the behavior of the Tab and Shift+Tab keys. + * + * Since: 4.18 + */ +void +gtk_list_box_set_tab_behavior (GtkListBox *box, + GtkListTabBehavior tab_behavior) +{ + g_return_if_fail (GTK_IS_LIST_BOX (box)); + + if (box->tab_behavior == tab_behavior) + return; + + box->tab_behavior = tab_behavior; + + g_object_notify_by_pspec (G_OBJECT (box), properties[PROP_TAB_BEHAVIOR]); +} + +/** + * gtk_list_box_get_tab_behavior: + * @box: a `GtkListBox` + * + * Returns the behavior of the Tab and Shift+Tab keys. + * + * Returns: the tab behavior + * Since: 4.18 + */ +GtkListTabBehavior +gtk_list_box_get_tab_behavior (GtkListBox *box) +{ + g_return_val_if_fail (GTK_IS_LIST_BOX (box), GTK_LIST_TAB_ALL); + + return box->tab_behavior; +} \ No newline at end of file diff --git a/gtk/gtklistbox.h b/gtk/gtklistbox.h index 71d42e1ee2..493ff74087 100644 --- a/gtk/gtklistbox.h +++ b/gtk/gtklistbox.h @@ -274,6 +274,12 @@ void gtk_list_box_set_show_separators (GtkListBox GDK_AVAILABLE_IN_ALL gboolean gtk_list_box_get_show_separators (GtkListBox *box); +GDK_AVAILABLE_IN_4_18 +void gtk_list_box_set_tab_behavior (GtkListBox *box, + GtkListTabBehavior behavior); +GDK_AVAILABLE_IN_4_18 +GtkListTabBehavior gtk_list_box_get_tab_behavior (GtkListBox *box); + G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkListBox, g_object_unref) G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkListBoxRow, g_object_unref)