[ubuntu-in] GTK+ RadioButton Signal Handler issue

Anil Vishnoi vishnoianil at gmail.com
Sun Mar 21 11:37:12 GMT 2010


Hello People,
    I wrote a small C program with GTK+ interfaces. This program is 
related to GtkRadioButton.Following is the whole program:

#include <stdlib.h>
#include <gtk/gtk.h>
#include <gtk/gtkdialog.h>
#include <string.h>

void radio1signalhandler(GtkWidget *widget,gpointer data)
{
    if(GTK_TOGGLE_BUTTON(widget)->active)
            g_print("Radio 1 pressed\n");
    else
            g_print("Radio 1 released\n");
}

void radio2signalhandler(GtkWidget *widget,gpointer data)
{
    if(GTK_TOGGLE_BUTTON(widget)->active)
            g_print("Radio 2 pressed\n");
    else
            g_print("Radio 2 released\n");
}
int main(int argc,char *argv[])
{
    GtkWidget * button;
    GtkWidget *button2;
    GSList *group;

    gtk_init(&argc,&argv);


    GtkWidget * userinputwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    
g_signal_connect(G_OBJECT(userinputwindow),"delete_event",G_CALLBACK(exit),NULL);
    gtk_window_set_title(GTK_WINDOW(userinputwindow),"Radio Button Demo");
    gtk_widget_show(userinputwindow);

    GtkWidget *hbox = gtk_hbox_new(TRUE,0);

    button = gtk_radio_button_new_with_label(NULL,"Radio1");
    gtk_box_pack_start(GTK_BOX(hbox),button,FALSE,FALSE,0);
    
g_signal_connect(G_OBJECT(button),"toggled",G_CALLBACK(radio1signalhandler),NULL);
    gtk_widget_show(button);

    group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(button));
    button2 = gtk_radio_button_new_with_label(group,"Radio2");
    gtk_box_pack_start(GTK_BOX(hbox),button2,FALSE,FALSE,0);
    
g_signal_connect(G_OBJECT(button),"toggled",G_CALLBACK(radio2signalhandler),NULL);
    gtk_widget_show(button2);
    gtk_widget_show(hbox);

    gtk_container_add(GTK_CONTAINER(userinputwindow),hbox);

    gtk_widget_show(hbox);

    gtk_main();

    return 0;
}


So from this program as per GtkRadioButton interface API documentation,i 
should get following output from this code if i select the radio button 2:

Radio 1 released
Radio 2 pressed

but i am getting the following output

Radio 1 released
Radio 2 released

So folks can anyone tell me if is there anything wrong in my logic,or i 
didnt handled the signal handler part properly ? or is this bug within 
GTK+ only?

You can compile the program using the following command line

gcc -o RadioButton RadioButton.c `pkg-config --cflags --libs gtk+-2.0`

Thanks
Anil






More information about the ubuntu-in mailing list