[gtk+/radio-group: 9/9] Convert the "group" property on radio widgets to a GtkRadioGroup



commit 5f62ba26930c64e358e9e917836d56d7269eeb8a
Author: Alexander Larsson <alexl redhat com>
Date:   Fri Nov 5 14:21:24 2010 +0100

    Convert the "group" property on radio widgets to a GtkRadioGroup
    
    The various radio widgets all have a write-only "group" property
    of the same type as the widget. This let you set the group in a
    bindable way, but is quite weird when we have a real GtkRadioGroup
    object.
    
    We convert this to a real read-write GtkRadioGroup type. Additionally
    we make it CONSTRUCT and use it in the various constructors instead
    of hand-rolling the set_group call.

 gtk/gtkradioaction.c     |   25 ++++++++-----------
 gtk/gtkradiobutton.c     |   42 +++++++++++++-------------------
 gtk/gtkradiomenuitem.c   |   22 +++++++++--------
 gtk/gtkradiotoolbutton.c |   58 ++++++++++++++++++++++++++++++----------------
 tests/testgtk.c          |   11 ++++----
 5 files changed, 83 insertions(+), 75 deletions(-)
---
diff --git a/gtk/gtkradioaction.c b/gtk/gtkradioaction.c
index a0c5c31..35acfce 100644
--- a/gtk/gtkradioaction.c
+++ b/gtk/gtkradioaction.c
@@ -134,9 +134,9 @@ gtk_radio_action_class_init (GtkRadioActionClass *klass)
 				   PROP_GROUP,
 				   g_param_spec_object ("group",
 							P_("Group"),
-							P_("The radio action whose group this action belongs to."),
-							GTK_TYPE_RADIO_ACTION,
-							GTK_PARAM_WRITABLE));
+							P_("The radio group this action belongs to."),
+							GTK_TYPE_RADIO_GROUP,
+							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
   /**
    * GtkRadioAction:current-value:
@@ -263,18 +263,12 @@ gtk_radio_action_set_property (GObject         *object,
     case PROP_VALUE:
       radio_action->private_data->value = g_value_get_int (value);
       break;
-    case PROP_GROUP: 
+    case PROP_GROUP:
       {
-	GtkRadioAction *arg;
-	GtkRadioGroup *group = NULL;
-
-	if (G_VALUE_HOLDS_OBJECT (value))
-	  {
-	    arg = GTK_RADIO_ACTION (g_value_get_object (value));
-	    if (arg)
-	      group = gtk_radio_action_get_group (arg);
-	    gtk_radio_action_set_group (radio_action, group);
-	  }
+	GtkRadioGroup *group;
+
+	group = g_value_get_object (value);
+	gtk_radio_action_set_group (radio_action, group);
       }
       break;
     case PROP_CURRENT_VALUE:
@@ -299,6 +293,9 @@ gtk_radio_action_get_property (GObject    *object,
 
   switch (prop_id)
     {
+    case PROP_GROUP:
+      g_value_set_object (value, gtk_radio_action_get_group (radio_action));
+      break;
     case PROP_VALUE:
       g_value_set_int (value, radio_action->private_data->value);
       break;
diff --git a/gtk/gtkradiobutton.c b/gtk/gtkradiobutton.c
index dc74946..837e3e5 100644
--- a/gtk/gtkradiobutton.c
+++ b/gtk/gtkradiobutton.c
@@ -163,9 +163,9 @@ gtk_radio_button_class_init (GtkRadioButtonClass *class)
 				   PROP_GROUP,
 				   g_param_spec_object ("group",
 							P_("Group"),
-							P_("The radio button whose group this widget belongs to."),
-							GTK_TYPE_RADIO_BUTTON,
-							GTK_PARAM_WRITABLE));
+							P_("The radio group this button belongs to."),
+							GTK_TYPE_RADIO_GROUP,
+							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
   widget_class->destroy = gtk_radio_button_destroy;
   widget_class->focus = gtk_radio_button_focus;
 
@@ -235,15 +235,9 @@ gtk_radio_button_set_property (GObject      *object,
   switch (prop_id)
     {
       GtkRadioGroup *group;
-      GtkRadioButton *button;
 
     case PROP_GROUP:
-        button = g_value_get_object (value);
-
-      if (button)
-	group = gtk_radio_button_get_group (button);
-      else
-	group = NULL;
+      group = g_value_get_object (value);
       gtk_radio_button_set_group (radio_button, group);
       break;
     default:
@@ -258,8 +252,15 @@ gtk_radio_button_get_property (GObject    *object,
 			       GValue     *value,
 			       GParamSpec *pspec)
 {
+  GtkRadioButton *radio_button;
+
+  radio_button = GTK_RADIO_BUTTON (object);
+
   switch (prop_id)
     {
+    case PROP_GROUP:
+      g_value_set_object (value, gtk_radio_button_get_group (radio_button));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -365,10 +366,7 @@ gtk_radio_button_new (GtkRadioGroup *group)
 {
   GtkRadioButton *radio_button;
 
-  radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, NULL);
-
-  if (group)
-    gtk_radio_button_set_group (radio_button, group);
+  radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "group", group, NULL);
 
   return GTK_WIDGET (radio_button);
 }
@@ -389,11 +387,7 @@ gtk_radio_button_new_with_label (GtkRadioGroup *group,
 {
   GtkWidget *radio_button;
 
-  radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, NULL) ;
-
-  if (group)
-    gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
-
+  radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "group", group, "label", label, NULL) ;
   return radio_button;
 }
 
@@ -417,14 +411,12 @@ gtk_radio_button_new_with_mnemonic (GtkRadioGroup *group,
 {
   GtkWidget *radio_button;
 
-  radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, 
-			       "label", label, 
-			       "use-underline", TRUE, 
+  radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON,
+			       "group", group,
+			       "label", label,
+			       "use-underline", TRUE,
 			       NULL);
 
-  if (group)
-    gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
-
   return radio_button;
 }
 
diff --git a/gtk/gtkradiomenuitem.c b/gtk/gtkradiomenuitem.c
index 73aabd0..1be1702 100644
--- a/gtk/gtkradiomenuitem.c
+++ b/gtk/gtkradiomenuitem.c
@@ -65,9 +65,7 @@ gtk_radio_menu_item_new (GtkRadioGroup *group)
 {
   GtkRadioMenuItem *radio_menu_item;
 
-  radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
-
-  gtk_radio_menu_item_set_group (radio_menu_item, group);
+  radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, "group", group, NULL);
 
   return GTK_WIDGET (radio_menu_item);
 }
@@ -87,10 +85,7 @@ gtk_radio_menu_item_set_property (GObject      *object,
       GtkRadioGroup *group;
 
     case PROP_GROUP:
-      if (G_VALUE_HOLDS_OBJECT (value))
-	group = gtk_radio_menu_item_get_group ((GtkRadioMenuItem*) g_value_get_object (value));
-      else
-	group = NULL;
+      group = g_value_get_object (value);
       gtk_radio_menu_item_set_group (radio_menu_item, group);
       break;
     default:
@@ -105,8 +100,15 @@ gtk_radio_menu_item_get_property (GObject    *object,
 				  GValue     *value,
 				  GParamSpec *pspec)
 {
+  GtkRadioMenuItem *radio_menu_item;
+
+  radio_menu_item = GTK_RADIO_MENU_ITEM (object);
+
   switch (prop_id)
     {
+    case PROP_GROUP:
+      g_value_set_object (value, gtk_radio_menu_item_get_group (radio_menu_item));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -374,9 +376,9 @@ gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
 				   PROP_GROUP,
 				   g_param_spec_object ("group",
 							P_("Group"),
-							P_("The radio menu item whose group this widget belongs to."),
-							GTK_TYPE_RADIO_MENU_ITEM,
-							GTK_PARAM_WRITABLE));
+							P_("The radio group this menu item belongs to."),
+							GTK_TYPE_RADIO_GROUP,
+							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
   /**
    * GtkStyle::group-changed:
diff --git a/gtk/gtkradiotoolbutton.c b/gtk/gtkradiotoolbutton.c
index fdd5863..26e9d40 100644
--- a/gtk/gtkradiotoolbutton.c
+++ b/gtk/gtkradiotoolbutton.c
@@ -36,6 +36,10 @@ static void gtk_radio_tool_button_set_property (GObject         *object,
 						guint            prop_id,
 						const GValue    *value,
 						GParamSpec      *pspec);
+static void gtk_radio_tool_button_get_property (GObject    *object,
+						guint       prop_id,
+						GValue     *value,
+						GParamSpec *pspec);
 
 G_DEFINE_TYPE (GtkRadioToolButton, gtk_radio_tool_button, GTK_TYPE_TOGGLE_TOOL_BUTTON)
 
@@ -49,7 +53,8 @@ gtk_radio_tool_button_class_init (GtkRadioToolButtonClass *klass)
   toolbutton_class = (GtkToolButtonClass *)klass;
 
   object_class->set_property = gtk_radio_tool_button_set_property;
-  
+  object_class->get_property = gtk_radio_tool_button_get_property;
+
   toolbutton_class->button_type = GTK_TYPE_RADIO_BUTTON;  
 
   /**
@@ -63,9 +68,9 @@ gtk_radio_tool_button_class_init (GtkRadioToolButtonClass *klass)
 				   PROP_GROUP,
 				   g_param_spec_object ("group",
 							P_("Group"),
-							P_("The radio tool button whose group this button belongs to."),
-							GTK_TYPE_RADIO_TOOL_BUTTON,
-							GTK_PARAM_WRITABLE));
+							P_("The radio group this tool button belongs to."),
+							GTK_TYPE_RADIO_GROUP,
+							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
 }
 
@@ -90,15 +95,10 @@ gtk_radio_tool_button_set_property (GObject         *object,
     {
     case PROP_GROUP:
       {
-	GtkRadioToolButton *arg;
-	GtkRadioGroup *group = NULL;
-	if (G_VALUE_HOLDS_OBJECT (value))
-	  {
-	    arg = GTK_RADIO_TOOL_BUTTON (g_value_get_object (value));
-	    if (arg)
-	      group = gtk_radio_tool_button_get_group (arg);
-	    gtk_radio_tool_button_set_group (button, group);
-	  }
+	GtkRadioGroup *group;
+
+	group = g_value_get_object (value);
+	gtk_radio_tool_button_set_group (button, group);
       }
       break;
     default:
@@ -107,6 +107,27 @@ gtk_radio_tool_button_set_property (GObject         *object,
     }
 }
 
+static void
+gtk_radio_tool_button_get_property (GObject    *object,
+				    guint       prop_id,
+				    GValue     *value,
+				    GParamSpec *pspec)
+{
+  GtkRadioToolButton *button;
+
+  button = GTK_RADIO_TOOL_BUTTON (object);
+
+  switch (prop_id)
+    {
+    case PROP_GROUP:
+      g_value_set_object (value, gtk_radio_tool_button_get_group (button));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
 /**
  * gtk_radio_tool_button_new:
  * @group: (allow-none): An existing radio button group, or %NULL if you are creating a new group
@@ -121,12 +142,11 @@ GtkToolItem *
 gtk_radio_tool_button_new (GtkRadioGroup *group)
 {
   GtkRadioToolButton *button;
-  
+
   button = g_object_new (GTK_TYPE_RADIO_TOOL_BUTTON,
+			 "group", group,
 			 NULL);
 
-  gtk_radio_tool_button_set_group (button, group);
-  
   return GTK_TOOL_ITEM (button);
 }
 
@@ -150,14 +170,12 @@ gtk_radio_tool_button_new_from_stock (GtkRadioGroup *group,
   GtkRadioToolButton *button;
 
   g_return_val_if_fail (stock_id != NULL, NULL);
-  
+
   button = g_object_new (GTK_TYPE_RADIO_TOOL_BUTTON,
+			 "group", group,
 			 "stock-id", stock_id,
 			 NULL);
 
-
-  gtk_radio_tool_button_set_group (button, group);
-  
   return GTK_TOOL_ITEM (button);
 }
 
diff --git a/tests/testgtk.c b/tests/testgtk.c
index 9f4e1d5..1d9131b 100644
--- a/tests/testgtk.c
+++ b/tests/testgtk.c
@@ -858,9 +858,9 @@ static GtkWidget *
 create_widget_grid (GType widget_type)
 {
   GtkWidget *table;
-  GtkWidget *group_widget = NULL;
+  GtkRadioGroup *group = NULL;
   gint i, j;
-  
+
   table = gtk_table_new (FALSE, 3, 3);
   
   for (i = 0; i < 5; i++)
@@ -892,10 +892,9 @@ create_widget_grid (GType widget_type)
 	      
 	      if (g_type_is_a (widget_type, GTK_TYPE_RADIO_BUTTON))
 		{
-		  if (!group_widget)
-		    group_widget = widget;
-		  else
-		    g_object_set (widget, "group", group_widget, NULL);
+		  if (!group)
+		    group = gtk_radio_group_new ();
+		  g_object_set (widget, "group", group, NULL);
 		}
 	    }
 	  



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]