From b83caec6b987930fe4bfd51128595970a54aa107 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Mon, 8 Oct 2018 10:22:04 +1300 Subject: [PATCH] GtkFileFilter: Add a GObject property for the name The name field was previously not a property, which meant you couldn't set in in GtkBuilder as translatable. --- gtk/gtkfilefilter.c | 85 ++++++++++++++++++++++++++++++++++++++++- testsuite/gtk/builder.c | 3 +- 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/gtk/gtkfilefilter.c b/gtk/gtkfilefilter.c index 7938b4cdef..2551fdc714 100644 --- a/gtk/gtkfilefilter.c +++ b/gtk/gtkfilefilter.c @@ -50,6 +50,7 @@ * rules: * |[ * + * Text and Images * * text/plain * image/ * @@ -119,6 +120,25 @@ struct _FilterRule } u; }; +enum { + PROP_0, + PROP_NAME, + NUM_PROPERTIES +}; + +static GParamSpec *props[NUM_PROPERTIES] = { NULL, }; + + +static void gtk_file_filter_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void gtk_file_filter_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); + + static void gtk_file_filter_finalize (GObject *object); @@ -154,7 +174,26 @@ gtk_file_filter_class_init (GtkFileFilterClass *class) { GObjectClass *gobject_class = G_OBJECT_CLASS (class); + gobject_class->set_property = gtk_file_filter_set_property; + gobject_class->get_property = gtk_file_filter_get_property; gobject_class->finalize = gtk_file_filter_finalize; + + /** + * GtkFileFilter:name: + * + * The human-readable name of the filter. + * + * This is the string that will be displayed in the file selector user + * interface if there is a selectable list of filters. + */ + props[PROP_NAME] = + g_param_spec_string ("name", + P_("Name"), + P_("The human-readable name for this filter"), + NULL, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY); + + g_object_class_install_properties (gobject_class, NUM_PROPERTIES, props); } static void @@ -182,6 +221,44 @@ filter_rule_free (FilterRule *rule) g_slice_free (FilterRule, rule); } +static void +gtk_file_filter_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + GtkFileFilter *filter = GTK_FILE_FILTER (object); + + switch (prop_id) + { + case PROP_NAME: + gtk_file_filter_set_name (filter, g_value_get_string (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gtk_file_filter_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + GtkFileFilter *filter = GTK_FILE_FILTER (object); + + switch (prop_id) + { + case PROP_NAME: + g_value_set_string (value, filter->name); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + static void gtk_file_filter_finalize (GObject *object) { @@ -416,10 +493,14 @@ gtk_file_filter_set_name (GtkFileFilter *filter, const gchar *name) { g_return_if_fail (GTK_IS_FILE_FILTER (filter)); - - g_free (filter->name); + if (g_strcmp0 (filter->name, name) == 0) + return; + + g_free (filter->name); filter->name = g_strdup (name); + + g_object_notify_by_pspec (G_OBJECT (filter), props[PROP_NAME]); } /** diff --git a/testsuite/gtk/builder.c b/testsuite/gtk/builder.c index 645f86bb03..cda5b7b67f 100644 --- a/testsuite/gtk/builder.c +++ b/testsuite/gtk/builder.c @@ -2482,6 +2482,7 @@ test_file_filter (void) const gchar buffer[] = "" " " + " Text and Images" " " " text/plain" " image/*" @@ -2497,7 +2498,7 @@ test_file_filter (void) obj = gtk_builder_get_object (builder, "filter1"); g_assert (GTK_IS_FILE_FILTER (obj)); filter = GTK_FILE_FILTER (obj); - g_assert_cmpstr (gtk_file_filter_get_name (filter), ==, "filter1"); + g_assert_cmpstr (gtk_file_filter_get_name (filter), ==, "Text and Images"); g_assert (gtk_file_filter_get_needed (filter) & GTK_FILE_FILTER_MIME_TYPE); g_assert (gtk_file_filter_get_needed (filter) & GTK_FILE_FILTER_DISPLAY_NAME);