Radio Buttons

GTK+ 2.0 Tutorial
<<< Previous | The Button Widget | Next >>>

# Radio Buttons

Radio buttons are similar to check buttons except they are grouped so that only one may be selected/depressed at a time. This is good for places in your application where you need to select from a short list of options.

Creating a new radio button is done with one of these calls:

    GtkWidget *gtk_radio_button_new( GSList *group );

    GtkWidget *gtk_radio_button_new_from_widget( GtkRadioButton *group );

    GtkWidget *gtk_radio_button_new_with_label( GSList *group,
                                                const gchar  *label );

    GtkWidget* gtk_radio_button_new_with_label_from_widget( GtkRadioButton *group,
                                                            const gchar    *label );

    GtkWidget *gtk_radio_button_new_with_mnemonic( GSList *group,
                                                   const gchar  *label );

    GtkWidget *gtk_radio_button_new_with_mnemonic_from_widget( GtkRadioButton *group,
                                                               const gchar  *label );

You'll notice the extra argument to these calls. They require a group to perform their duty properly. The first call to gtk_radio_button_new() or gtk_radio_button_new_with_label() should pass NULL as the first argument. Then create a group using:

    GSList *gtk_radio_button_get_group( GtkRadioButton *radio_button );

The important thing to remember is that gtk_radio_button_get_group() must be called for each new button added to the group, with the previous button passed in as an argument. The result is then passed into the next call to gtk_radio_button_new() or gtk_radio_button_new_with_label(). This allows a chain of buttons to be established. The example below should make this clear.

You can shorten this slightly by using the following syntax, which removes the need for a variable to hold the list of buttons:

         button2 = gtk_radio_button_new_with_label(
                     gtk_radio_button_get_group (GTK_RADIO_BUTTON (button1)),
                     "button2");

The _from_widget() variants of the creation functions allow you to shorten this further, by omitting the gtk_radio_button_get_group() call. This form is used in the example to create the third button:

         button2 = gtk_radio_button_new_with_label_from_widget(
    	         GTK_RADIO_BUTTON (button1),
                     "button2");

It is also a good idea to explicitly set which button should be the default depressed button with:

    void gtk_toggle_button_set_active( GtkToggleButton *toggle_button,
                                       gboolean        state );

This is described in the section on toggle buttons, and works in exactly the same way. Once the radio buttons are grouped together, only one of the group may be active at a time. If the user clicks on one radio button, and then on another, the first radio button will first emit a "toggled" signal (to report becoming inactive), and then the second will emit its "toggled" signal (to report becoming active).

The following example creates a radio button group with three buttons.

    #include <glib.h>
    #include <gtk/gtk.h>

    static gboolean close_application( GtkWidget *widget,
                                       GdkEvent  *event,
                                       gpointer   data )
    {
      gtk_main_quit ();
      return FALSE;
    }

    int main( int   argc,
              char *argv[] )
    {
        GtkWidget *window = NULL;
        GtkWidget *box1;
        GtkWidget *box2;
        GtkWidget *button;
        GtkWidget *separator;
        GSList *group;

        gtk_init (&argc, &argv);

        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

        g_signal_connect (window, "delete-event",
    		      G_CALLBACK (close_application),
                          NULL);

        gtk_window_set_title (GTK_WINDOW (window), "radio buttons");
        gtk_container_set_border_width (GTK_CONTAINER (window), 0);

        box1 = gtk_vbox_new (FALSE, 0);
        gtk_container_add (GTK_CONTAINER (window), box1);
        gtk_widget_show (box1);

        box2 = gtk_vbox_new (FALSE, 10);
        gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
        gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
        gtk_widget_show (box2);

        button = gtk_radio_button_new_with_label (NULL, "button1");
        gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
        gtk_widget_show (button);

        group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
        button = gtk_radio_button_new_with_label (group, "button2");
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
        gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
        gtk_widget_show (button);

        button = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (button),
    	                                                  "button3");
        gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
        gtk_widget_show (button);

        separator = gtk_hseparator_new ();
        gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
        gtk_widget_show (separator);

        box2 = gtk_vbox_new (FALSE, 10);
        gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
        gtk_widget_show (box2);

        button = gtk_button_new_with_label ("close");
        g_signal_connect_swapped (button, "clicked",
                                  G_CALLBACK (close_application),
                                  window);
        gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
        gtk_widget_set_can_default (button, TRUE);
        gtk_widget_grab_default (button);
        gtk_widget_show (button);
        gtk_widget_show (window);

        gtk_main ();

        return 0;
    }

<<< Previous | Home | Next >>>
Check Buttons | Up | Adjustments