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.
This commit is contained in:
Robert Ancell 2018-10-08 10:22:04 +13:00
parent 05b9a99661
commit b83caec6b9
2 changed files with 85 additions and 3 deletions

View File

@ -50,6 +50,7 @@
* rules:
* |[
* <object class="GtkFileFilter">
* <property name="name" translatable="yes">Text and Images</property>
* <mime-types>
* <mime-type>text/plain</mime-type>
* <mime-type>image/ *</mime-type>
@ -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]);
}
/**

View File

@ -2482,6 +2482,7 @@ test_file_filter (void)
const gchar buffer[] =
"<interface>"
" <object class='GtkFileFilter' id='filter1'>"
" <property name='name'>Text and Images</property>"
" <mime-types>"
" <mime-type>text/plain</mime-type>"
" <mime-type>image/*</mime-type>"
@ -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);