From ad98e728845244e5275bcdf13eb8580140fc86a3 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 20 Jun 2020 13:11:48 -0400 Subject: [PATCH] dropdown: Add GtkDropDown:selected-item This is the same readonly property we have in GtkSingleSelection. Fixes: #2861 --- docs/reference/gtk/gtk4-sections.txt | 1 + gtk/gtkdropdown.c | 37 ++++++++++++++++++++++++++++ gtk/gtkdropdown.h | 3 +++ 3 files changed, 41 insertions(+) diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt index c61cd3452c..59df2f46e8 100644 --- a/docs/reference/gtk/gtk4-sections.txt +++ b/docs/reference/gtk/gtk4-sections.txt @@ -7540,6 +7540,7 @@ gtk_drop_down_set_model gtk_drop_down_get_model gtk_drop_down_set_selected gtk_drop_down_get_selected +gtk_drop_down_get_selected_item gtk_drop_down_set_factory gtk_drop_down_get_factory gtk_drop_down_set_list_factory diff --git a/gtk/gtkdropdown.c b/gtk/gtkdropdown.c index c0a37f4089..a95775eab6 100644 --- a/gtk/gtkdropdown.c +++ b/gtk/gtkdropdown.c @@ -131,6 +131,7 @@ enum PROP_LIST_FACTORY, PROP_MODEL, PROP_SELECTED, + PROP_SELECTED_ITEM, PROP_ENABLE_SEARCH, PROP_EXPRESSION, @@ -314,6 +315,10 @@ gtk_drop_down_get_property (GObject *object, g_value_set_uint (value, gtk_drop_down_get_selected (self)); break; + case PROP_SELECTED_ITEM: + g_value_set_object (value, gtk_drop_down_get_selected_item (self)); + break; + case PROP_ENABLE_SEARCH: g_value_set_boolean (value, self->enable_search); break; @@ -488,6 +493,18 @@ gtk_drop_down_class_init (GtkDropDownClass *klass) 0, G_MAXUINT, GTK_INVALID_LIST_POSITION, G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); + /** + * GtkDropDown:selected-item: + * + * The selected item. + */ + properties[PROP_SELECTED_ITEM] = + g_param_spec_object ("selected-item", + P_("Selected Item"), + P_("The selected item"), + G_TYPE_OBJECT, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + /** * GtkDropDown:enable-search: * @@ -794,6 +811,7 @@ gtk_drop_down_set_selected (GtkDropDown *self, gtk_single_selection_set_selected (GTK_SINGLE_SELECTION (self->selection), position); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED]); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED_ITEM]); } /** @@ -816,6 +834,25 @@ gtk_drop_down_get_selected (GtkDropDown *self) return gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (self->selection)); } +/** + * gtk_drop_down_get_selected_item: + * @self: a #GtkDropDown + * + * Gets the selected item. If no item is selected, %NULL is returned. + * + * Returns: (transfer none): The selected item + */ +gpointer +gtk_drop_down_get_selected_item (GtkDropDown *self) +{ + g_return_val_if_fail (GTK_IS_DROP_DOWN (self), NULL); + + if (self->selection == NULL) + return NULL; + + return gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (self->selection)); +} + /** * gtk_drop_down_set_enable_search: * @self: a #GtkDropDown diff --git a/gtk/gtkdropdown.h b/gtk/gtkdropdown.h index 84813b3877..0795d0f77b 100644 --- a/gtk/gtkdropdown.h +++ b/gtk/gtkdropdown.h @@ -49,6 +49,9 @@ void gtk_drop_down_set_selected (GtkDropDown GDK_AVAILABLE_IN_ALL guint gtk_drop_down_get_selected (GtkDropDown *self); +GDK_AVAILABLE_IN_ALL +gpointer gtk_drop_down_get_selected_item (GtkDropDown *self); + GDK_AVAILABLE_IN_ALL void gtk_drop_down_set_factory (GtkDropDown *self, GtkListItemFactory *factory);