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.
This commit is contained in:
Matthias Clasen
2010-11-30 09:34:05 -05:00
parent d19c6228d5
commit d285e9dbf5
2 changed files with 61 additions and 5 deletions

View File

@@ -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;
}

View File

@@ -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