Introduction
Toggle buttons allow the user to set a button to on or off. Unlike radio buttons where only one button out of a group of buttons can be selected, toggle buttons can be set to on or off individually. GtkToggleButtons are easy to work with. They have all the characteristics of an ordinary GtkButton except that when clicked on, they stay in the down position until they are clicked again.
Source Code
Source code for this example is also available in the file togglebutton.c
/*
*File name: togglebutton.c
*/#include
#include/*– 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 *toggle1;
GtkWidget *toggle2;
GtkWidget *toggle3;
GtkWidget *toggle4;
GtkWidget *toggle5;
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 –*/
toggle1 = gtk_toggle_button_new_with_label (“Toggle 1″);
toggle2 = gtk_toggle_button_new_with_label (“Toggle 2″);
toggle3 = gtk_toggle_button_new_with_label (“Toggle 3″);
toggle4 = gtk_toggle_button_new_with_label (“Toggle 4″);
toggle5 = gtk_toggle_button_new_with_label (“Toggle 5″);/*– Pack all the radio buttons into the hbox –*/
gtk_box_pack_start(GTK_BOX(hbox), toggle1, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), toggle2, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), toggle3, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), toggle4, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), toggle5, 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(toggle1);
gtk_widget_show(toggle2);
gtk_widget_show(toggle3);
gtk_widget_show(toggle4);
gtk_widget_show(toggle5);
gtk_widget_show(hbox);
gtk_widget_show(window);/*– Start the GTK event loop –*/
gtk_main();/*– Return 0 if exit is successful –*/
return 0;
}
Compile the Source Code
gcc -Wall -g togglebutton.c -o togglebutton `gtk-config –cflags` `gtk-config –libs`
Execute the Program
./togglebutton






