Compare commits

...

3 Commits

Author SHA1 Message Date
Matthias Clasen
9dfa663990 filechooser: Use dropdown convenience api
No need to do things the hard way.
2022-10-22 08:43:05 -04:00
Matthias Clasen
831b1e1cde examples: Use the new convenience API 2022-10-22 08:43:05 -04:00
Matthias Clasen
801a261250 dropdown: Add some convenience api
Add a selected-string property to GtkDropDown, which
holds the value of the string property of the selected
item, if it is a GtkStringObject.

This brings gtk_drop_down_new_from_strings()
on par with GtkComboBoxText, in terms of convenience.
2022-10-22 08:43:05 -04:00
4 changed files with 115 additions and 33 deletions

View File

@@ -360,7 +360,7 @@ combo_changed (GtkDropDown *combo,
char **accels;
char *str;
action = gtk_string_object_get_string (GTK_STRING_OBJECT (gtk_drop_down_get_selected_item (combo)));
action = gtk_drop_down_get_selected_string (combo);
if (!action)
return;
@@ -400,7 +400,7 @@ response (GtkDialog *dialog,
return;
}
action = gtk_string_object_get_string (GTK_STRING_OBJECT (gtk_drop_down_get_selected_item (combo)));
action = gtk_drop_down_get_selected_string (combo);
if (!action)
return;

View File

@@ -133,6 +133,7 @@ enum
PROP_MODEL,
PROP_SELECTED,
PROP_SELECTED_ITEM,
PROP_SELECTED_STRING,
PROP_ENABLE_SEARCH,
PROP_EXPRESSION,
PROP_SHOW_ARROW,
@@ -247,6 +248,7 @@ selection_item_changed (GtkSingleSelection *selection,
}
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED_ITEM]);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED_STRING]);
}
static void
@@ -358,6 +360,10 @@ gtk_drop_down_get_property (GObject *object,
g_value_set_object (value, gtk_drop_down_get_selected_item (self));
break;
case PROP_SELECTED_STRING:
g_value_set_string (value, gtk_drop_down_get_selected_string (self));
break;
case PROP_ENABLE_SEARCH:
g_value_set_boolean (value, self->enable_search);
break;
@@ -402,6 +408,10 @@ gtk_drop_down_set_property (GObject *object,
gtk_drop_down_set_selected (self, g_value_get_uint (value));
break;
case PROP_SELECTED_STRING:
gtk_drop_down_set_selected_string (self, g_value_get_string (value));
break;
case PROP_ENABLE_SEARCH:
gtk_drop_down_set_enable_search (self, g_value_get_boolean (value));
break;
@@ -545,6 +555,22 @@ gtk_drop_down_class_init (GtkDropDownClass *klass)
G_TYPE_OBJECT,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GtkDropDown:selected-string: (attributes org.gtk.Property.get=gtk_drop_down_get_selected_string org.gtk.Property.set=gtk_drop_down_set_selected_string)
*
* The value of the string property of the selected item,
* if it is a [class@Gtk.StringObject].
*
* This is only useful for dropdowns with a [class@Gtk.StringList] as model,
* such as those created by [ctor@Gtk.DropDown.new_from_strings].
*
* Since: 4.10
*/
properties[PROP_SELECTED_STRING] =
g_param_spec_string ("selected-string", NULL, NULL,
NULL,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* GtkDropDown:enable-search: (attributes org.gtk.Property.get=gtk_drop_down_get_enable_search org.gtk.Property.set=gtk_drop_down_set_enable_search)
*
@@ -784,8 +810,7 @@ gtk_drop_down_new (GListModel *model,
* gtk_drop_down_new_from_strings:
* @strings: (array zero-terminated=1): The strings to put in the dropdown
*
* Creates a new `GtkDropDown` that is populated with
* the strings.
* Creates a new `GtkDropDown` that is populated with the strings.
*
* Returns: a new `GtkDropDown`
*/
@@ -1016,6 +1041,81 @@ gtk_drop_down_get_selected_item (GtkDropDown *self)
return gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (self->selection));
}
/**
* gtk_drop_down_get_selected_string: (attributes org.gtk.Method.get_property=selected-string)
* @self: a `GtkDropDown`
*
* Gets the string value for the selected [class@Gtk.StringObject].
*
* If no item is selected, or items are of another type, %NULL is returned.
*
* This function is meant for dropdowns with a [class@Gtk.StringList] as model,
* such as those created by [ctor@Gtk.DropDown.new_from_strings].
*
* Returns: (transfer none) (nullable): The string value for selected item
*
* Since: 4.10
*/
const char *
gtk_drop_down_get_selected_string (GtkDropDown *self)
{
gpointer item;
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), NULL);
if (self->selection == NULL)
return NULL;
item = gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (self->selection));
if (GTK_IS_STRING_OBJECT (item))
return gtk_string_object_get_string (GTK_STRING_OBJECT (item));
return NULL;
}
/**
* gtk_drop_down_set_selected_string:
* @self: a `GtkDropDown`
* @string: the string to select
*
* Selects the first [class@Gtk.StringObject] whose string property
* matches @string.
*
* If items are not `GtkStringObjects`, the selection is not changed.
*
* This function is meant for dropdowns with a [class@Gtk.StringList] as model,
* such as those created by [ctor@Gtk.DropDown.new_from_strings].
*
* Since: 4.10
*/
void
gtk_drop_down_set_selected_string (GtkDropDown *self,
const char *string)
{
g_return_if_fail (GTK_IS_DROP_DOWN (self));
g_return_if_fail (string != NULL);
if (self->selection == NULL)
return;
for (guint i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (self->selection)); i++)
{
gpointer item = g_list_model_get_item (G_LIST_MODEL (self->selection), i);
g_object_unref (item);
if (!GTK_IS_STRING_OBJECT (item))
break;
if (g_str_equal (gtk_string_object_get_string (GTK_STRING_OBJECT (item)), string))
{
gtk_single_selection_set_selected (GTK_SINGLE_SELECTION (self->selection), i);
break;
}
}
}
/**
* gtk_drop_down_set_enable_search: (attributes org.gtk.Method.set_property=enable-search)
* @self: a `GtkDropDown`

View File

@@ -52,6 +52,13 @@ guint gtk_drop_down_get_selected (GtkDropDown
GDK_AVAILABLE_IN_ALL
gpointer gtk_drop_down_get_selected_item (GtkDropDown *self);
GDK_AVAILABLE_IN_4_10
const char * gtk_drop_down_get_selected_string (GtkDropDown *self);
GDK_AVAILABLE_IN_4_10
void gtk_drop_down_set_selected_string (GtkDropDown *self,
const char *string);
GDK_AVAILABLE_IN_ALL
void gtk_drop_down_set_factory (GtkDropDown *self,
GtkListItemFactory *factory);

View File

@@ -7338,8 +7338,6 @@ gtk_file_chooser_widget_add_choice (GtkFileChooser *chooser,
gtk_box_append (GTK_BOX (box), gtk_label_new (label));
combo = gtk_drop_down_new_from_strings ((const char * const *)option_labels);
g_object_set_data_full (G_OBJECT (combo), "options",
g_strdupv ((char **)options), (GDestroyNotify)g_strfreev);
g_hash_table_insert (impl->choices, g_strdup (id), combo);
gtk_box_append (GTK_BOX (box), combo);
@@ -7396,21 +7394,8 @@ gtk_file_chooser_widget_set_choice (GtkFileChooser *chooser,
if (GTK_IS_BOX (widget))
{
guint i;
const char **options;
GtkWidget *dropdown;
dropdown = gtk_widget_get_last_child (widget);
options = (const char **) g_object_get_data (G_OBJECT (dropdown), "options");
for (i = 0; options[i]; i++)
{
if (strcmp (option, options[i]) == 0)
{
gtk_drop_down_set_selected (GTK_DROP_DOWN (dropdown), i);
break;
}
}
GtkWidget *dropdown = gtk_widget_get_last_child (widget);
gtk_drop_down_set_selected_string (GTK_DROP_DOWN (dropdown), option);
}
else if (GTK_IS_CHECK_BUTTON (widget))
gtk_check_button_set_active (GTK_CHECK_BUTTON (widget), g_str_equal (option, "true"));
@@ -7428,19 +7413,9 @@ gtk_file_chooser_widget_get_choice (GtkFileChooser *chooser,
widget = (GtkWidget *)g_hash_table_lookup (impl->choices, id);
if (GTK_IS_DROP_DOWN (widget))
{
const char **options;
guint selected;
options = (const char **) g_object_get_data (G_OBJECT (widget), "options");
selected = gtk_drop_down_get_selected (GTK_DROP_DOWN (widget));
return options[selected];
}
return gtk_drop_down_get_selected_string (GTK_DROP_DOWN (widget));
else if (GTK_IS_CHECK_BUTTON (widget))
{
return gtk_check_button_get_active (GTK_CHECK_BUTTON (widget)) ? "true" : "false";
}
return gtk_check_button_get_active (GTK_CHECK_BUTTON (widget)) ? "true" : "false";
return NULL;
}