From d285e9dbf57717c4ad4d64ebcbbe11dfe8ea1ede Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 30 Nov 2010 09:34:05 -0500 Subject: [PATCH] Add a GtkRadioGroup::active-value property This is only supported for radio groups which contain radio actions, and in that case is just the ::string-value property of the active item. The property is readable and writable. Setting it has the effect of changing the active item, if any of the items has a matching string-value. This property is intended for easy binding to settings. --- gtk/gtkradiogroup.c | 55 ++++++++++++++++++++++++++++++++++++++++++++- gtk/gtkradiogroup.h | 11 +++++---- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/gtk/gtkradiogroup.c b/gtk/gtkradiogroup.c index 2c19633cbc..9831f328a6 100644 --- a/gtk/gtkradiogroup.c +++ b/gtk/gtkradiogroup.c @@ -26,6 +26,7 @@ #include "config.h" #include "gtkradiogroupprivate.h" +#include "gtkradioaction.h" #include "gtkprivate.h" #include "gtkmarshalers.h" #include "gtkintl.h" @@ -56,7 +57,8 @@ struct _GtkRadioGroupPrivate enum { PROP_0, - PROP_ACTIVE_ITEM + PROP_ACTIVE_ITEM, + PROP_ACTIVE_VALUE }; @@ -102,6 +104,14 @@ gtk_radio_group_class_init (GtkRadioGroupClass *class) GTK_PARAM_READABLE)); class->active_changed = NULL; + g_object_class_install_property (gobject_class, + PROP_ACTIVE_VALUE, + g_param_spec_string ("active-value", + P_("Active value"), + P_("The value of the active item in the radio group"), + NULL, + GTK_PARAM_READWRITE)); + /** * GtkRadioGroup::active-changed: * @radio_group: the radio group on which the signal is emitted @@ -147,6 +157,9 @@ gtk_radio_group_set_property (GObject *object, switch (prop_id) { + case PROP_ACTIVE_VALUE: + gtk_radio_group_set_active_value (radio_group, g_value_get_string (value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -168,6 +181,9 @@ gtk_radio_group_get_property (GObject *object, case PROP_ACTIVE_ITEM: g_value_set_object (value, radio_group->priv->active); break; + case PROP_ACTIVE_VALUE: + g_value_set_string (value, gtk_radio_group_get_active_value (radio_group)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -302,5 +318,42 @@ void _gtk_radio_group_emit_active_changed (GtkRadioGroup *radio_group) { g_object_notify (G_OBJECT (radio_group), "active-item"); + g_object_notify (G_OBJECT (radio_group), "active-value"); g_signal_emit (radio_group, signals[ACTIVE_CHANGED], 0, radio_group->priv->active); } + +void +gtk_radio_group_set_active_value (GtkRadioGroup *radio_group, + const gchar *value) +{ + GSList *l; + + for (l = radio_group->priv->items; l; l = l->next) + { + GObject *item = l->data; + + if (GTK_IS_RADIO_ACTION (item)) + { + const gchar *s; + + s = gtk_radio_action_get_string_value (GTK_RADIO_ACTION (item)); + + if (strcmp (value, s) == 0) + { + _gtk_radio_group_set_active_item (radio_group, item); + _gtk_radio_group_emit_active_changed (radio_group); + + break; + } + } + } +} + +const gchar * +gtk_radio_group_get_active_value (GtkRadioGroup *radio_group) +{ + if (GTK_IS_RADIO_ACTION (radio_group->priv->active)) + return gtk_radio_action_get_string_value (GTK_RADIO_ACTION (radio_group->priv->active)); + else + return NULL; +} diff --git a/gtk/gtkradiogroup.h b/gtk/gtkradiogroup.h index dc3058f677..115a5ee04c 100644 --- a/gtk/gtkradiogroup.h +++ b/gtk/gtkradiogroup.h @@ -70,11 +70,14 @@ struct _GtkRadioGroupClass }; -GType gtk_radio_group_get_type (void) G_GNUC_CONST; +GType gtk_radio_group_get_type (void) G_GNUC_CONST; -GtkRadioGroup* gtk_radio_group_new (void); -GSList * gtk_radio_group_get_items (GtkRadioGroup *radio_group); -GObject * gtk_radio_group_get_active_item (GtkRadioGroup *radio_group); +GtkRadioGroup* gtk_radio_group_new (void); +GSList * gtk_radio_group_get_items (GtkRadioGroup *radio_group); +GObject * gtk_radio_group_get_active_item (GtkRadioGroup *radio_group); +void gtk_radio_group_set_active_value (GtkRadioGroup *radio_group, + const gchar *value); +const gchar * gtk_radio_group_get_active_value (GtkRadioGroup *radio_group); G_END_DECLS