Radio buttons are used to give the user one choice among many options. Only one option can be selected at any given
time, and selecting another option will automatically deselect whatever current option is active. This is accomplished
with a group of GtkRadioButton widgets. Creating a group is achieved with this line of code:
/*-- Create the first radio button with a label --*/
radio1 = gtk_radio_button_new_with_label (NULL, "Button 1");
   
The NULL means that that the radio1 button does not belong to an existing group.
Therefore, it will exist in a new group of its own. Now, we add need to add the other radio buttons to the same group. In the code below, the (GTK_RADIO_BUTTON(radio1)) means that the new radio button will be added to
the same group as radio1.
/*-- Create the rest of the radio buttons with labels --*/
radio2 = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (radio1)), "Button 2");
radio3 = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (radio1)), "Button 3");
radio4 = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (radio1)), "Button 4");
radio5 = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (radio1)), "Button 5");
|
Source code for this example is also available in the file radiobutton.c
/*
*File name: radiobutton.c
*/
#include <gtk/gtk.h>
#include <glib.h>
/*-- This function allows the program to exit properly when the window is closed --*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{
g_print ("Quitting...\n");
gtk_main_quit();
return (FALSE);
}
int main (int argc, char *argv[])
{
/*-- Declare the GTK Widgets used in the program --*/
GtkWidget *window;
GtkWidget *radio1;
GtkWidget *radio2;
GtkWidget *radio3;
GtkWidget *radio4;
GtkWidget *radio5;
GtkWidget *hbox;
/*-- Initialize GTK --*/
gtk_init (&argc, &argv);
/*-- Create the new window --*/
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/*-- Create the hbox --*/
hbox = gtk_hbox_new(FALSE, 0);
/*-- Connect the window to the destroyapp function --*/
gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL);
/*-- Create the first radio button with a label --*/
radio1 = gtk_radio_button_new_with_label (NULL, "Button 1");
/*-- Create the rest of the radio buttons with labels --*/
radio2 = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (radio1)), "Button 2");
radio3 = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (radio1)), "Button 3");
radio4 = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (radio1)), "Button 4");
radio5 = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (radio1)), "Button 5");
/*-- Pack all the radio buttons into the hbox --*/
gtk_box_pack_start(GTK_BOX(hbox), radio1, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), radio2, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), radio3, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), radio4, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), radio5, TRUE, TRUE, 2);
/*-- Add the hbox to the window --*/
gtk_container_add(GTK_CONTAINER(window), hbox);
/*-- Add a border to the window to give the button a little room --*/
gtk_container_border_width (GTK_CONTAINER (window), 15);
/*-- Display the widgets --*/
gtk_widget_show(radio1);
gtk_widget_show(radio2);
gtk_widget_show(radio3);
gtk_widget_show(radio4);
gtk_widget_show(radio5);
gtk_widget_show(hbox);
gtk_widget_show(window);
/*-- Start the GTK event loop --*/
gtk_main();
/*-- Return 0 if exit is successful --*/
return 0;
}
|