Re: setting initial state of radio buttons not working



Hello, Brandon
As I see you have only one problem: if you're creating a new group of
radio buttons you have to pass NULL pointer as group. Then you have
to obtain the groups pointer via gtk_radio_button_group(). You may also
use gtk_radio_button_new_with_label_from_widget() function to create
the next button in group without specifying the group.

Brandon McCombs wrote:

globally declared I have the following:

GtkWidget *radio_straightplay;
GtkWidget *radio_boxplay;

then in main() I have:

radio_straightplay =
gtk_radio_button_new_with_label(radiogroup,"Straight");

Nope, an error here. You have to pass NULL GSlist pointer - a new group will be created.

radio_straightplay = gtk_radio_button_new_with_label(NULL,"Straight");

Then you have to obtain the pointer to group via gtk_radio_button_group() :

radiogroup = gtk_radio_button_group((GtkRadioButton*)radio_straightplay);


radio_boxplay = gtk_radio_button_new_with_label(radiogroup,"Box");

This call is correct now.



radiogroup = gtk_radio_button_group(GTK_RADIO_BUTTON(radio_boxplay));
radiogroup =
gtk_radio_button_group(GTK_RADIO_BUTTON(radio_straightplay));

You have to remove this lines, cause you're already have a pointer to group.



Now, i don't get any errors or warnings with these and they are
displayed but both radio buttons are checked. Is that normal default
behavior in the first place?
Yes, cause you've created two buttons in two different groups.



To turn that off I tried the following:
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_straightplay),TRUE);
gtk_toggle_button_set_active((radio_boxplay),FALSE);

This will work ok in all cases. Typecasting needed only for avoiding compiler warnings and checking the type of the object in run-time. You may use usual C typecasting (GtkToggleButton*)radio_button in case you are sure about the type
of radio_button pointer (this can save a  couple of microseconds :).

Hope this help. Look at little demo (gtk+-1.2) I've attached.

Regards,
   Olexiy
/*
        To compile use smth like this:
        
        gcc -O -Wall `gtk-config --cflags --libs` radio-test.c -o radio-test

        Olexiy Avramchenko
*/

#include <gtk/gtk.h>


static void set_active(GtkWidget *button, GtkToggleButton *radio)
{
        gtk_toggle_button_set_active(radio, TRUE);
}

int main(int argc, char **argv)
{
GSList    *group;
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *radio0, *radio1, *radio2;
GtkWidget *sep;
GtkWidget *button;

        gtk_init(&argc, &argv);

        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width(GTK_CONTAINER(window), 8);
        gtk_signal_connect(
                                GTK_OBJECT(window),
                                "delete_event",
                                gtk_main_quit,
                                NULL
        );

        vbox = gtk_vbox_new(FALSE, 2);
        gtk_container_add(GTK_CONTAINER(window), vbox);

        /*      creating the first radio button in group, passing
                NULL as group.
        */
        radio0 = gtk_radio_button_new_with_label(NULL, "radio button 0");
        gtk_box_pack_start(GTK_BOX(vbox), radio0, FALSE,FALSE, 0);
        /*      getting the pointer to new group
        */
        group = gtk_radio_button_group(GTK_RADIO_BUTTON(radio0));

        /*      creating new radio button in group, passing group pointer
        */
        radio1 = gtk_radio_button_new_with_label(group, "radio button 1");
        gtk_box_pack_start(GTK_BOX(vbox), radio1, FALSE,FALSE, 0);

        /*      creating new radio button in group in another way
        */
        radio2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio1), "radio button 2");
        gtk_box_pack_start(GTK_BOX(vbox), radio2, FALSE,FALSE, 0);

        sep = gtk_hseparator_new();
        gtk_box_pack_start(GTK_BOX(vbox), sep, FALSE,FALSE, 4);

        /*      three buttons for manually radio buttons activation
        */
        button = gtk_button_new_with_label("set *radio button 0* active");
        gtk_signal_connect(
                                GTK_OBJECT(button),
                                "clicked",
                                set_active,
                                radio0
        );
        gtk_box_pack_start(GTK_BOX(vbox), button, FALSE,FALSE, 4);

        button = gtk_button_new_with_label("set *radio button 1* active");
        gtk_signal_connect(
                                GTK_OBJECT(button),
                                "clicked",
                                set_active,
                                radio1
        );
        gtk_box_pack_start(GTK_BOX(vbox), button, FALSE,FALSE, 4);
        button = gtk_button_new_with_label("set *radio button 2* active");
        gtk_signal_connect(
                                GTK_OBJECT(button),
                                "clicked",
                                set_active,
                                radio2
        );
        gtk_box_pack_start(GTK_BOX(vbox), button, FALSE,FALSE, 4);

        gtk_widget_show_all(window);

        gtk_main();

        return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]