This example implement radioButton_toggled() to handle the "toggled" signal from RadioButtons. Inside radioButton_toggled(), check if the button is active by calling gtk_toggle_button_get_active(), to determine the active selected RadioButton in group, then display on Label.
|
handle "toggled" signal of RadioButton |
#include <gtk/gtk.h>
static void
radioButton_toggled (GtkWidget *button, GtkLabel *label)
{
char *button_state;
const char *button_label =
gtk_button_get_label (GTK_BUTTON (button));
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button))){
button_state = "on";
gtk_label_set_text (label, button_label);
}
else {
button_state = "off";
}
g_print ("%s : %s\n", button_label, button_state);
}
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *box;
GtkWidget *label;
GtkWidget *radioButton1;
GtkWidget *radioButton2;
GtkWidget *radioButton3;
GtkWidget *radioButton4;
GtkWidget *radioButton5;
GSList *groupB;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
label = gtk_label_new ("");
gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);
//new RadioButton1, 2, 3 in same group
radioButton1 = gtk_radio_button_new_with_label (
NULL, "Radio Button 1");
radioButton2 = gtk_radio_button_new_with_label_from_widget (
GTK_RADIO_BUTTON (radioButton1), "Radio Button 2");
radioButton3 = gtk_radio_button_new_with_label_from_widget (
GTK_RADIO_BUTTON (radioButton1), "Radio Button 3");
g_signal_connect (GTK_TOGGLE_BUTTON (radioButton1), "toggled",
G_CALLBACK (radioButton_toggled), label);
g_signal_connect (GTK_TOGGLE_BUTTON (radioButton2), "toggled",
G_CALLBACK (radioButton_toggled), label);
g_signal_connect (GTK_TOGGLE_BUTTON (radioButton3), "toggled",
G_CALLBACK (radioButton_toggled), label);
gtk_box_pack_start(GTK_BOX(box), radioButton1, FALSE, FALSE, 3);
gtk_box_pack_start(GTK_BOX(box), radioButton2, FALSE, FALSE, 3);
gtk_box_pack_start(GTK_BOX(box), radioButton3, FALSE, FALSE, 3);
//new RadioButton4, 5 in another group
radioButton4 = gtk_radio_button_new_with_label (
NULL, "Group B Button 4");
groupB = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radioButton4));
radioButton5 = gtk_radio_button_new_with_label (
groupB, "Group B Button 5");
gtk_box_pack_start(GTK_BOX(box), radioButton4, FALSE, FALSE, 3);
gtk_box_pack_start(GTK_BOX(box), radioButton5, FALSE, FALSE, 3);
g_signal_connect (radioButton4, "toggled",
G_CALLBACK (radioButton_toggled), label);
g_signal_connect (radioButton5, "toggled",
G_CALLBACK (radioButton_toggled), label);
//Connects GCallback function gtk_main_quit to "destroy" signal for "window"
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_container_add(GTK_CONTAINER(window), box);
gtk_widget_show_all(window);
gtk_main();
return 0;
}