GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dumb radio button question

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
Almach



Joined: 10 Oct 2007
Posts: 2

PostPosted: Wed Oct 10, 2007 9:07 pm    Post subject: Dumb radio button question Reply with quote

I'm new to this event driven stuff.
How do I tell which button in a radiobutton group is selected ? Using gtk_toggle_button_get_active tells me a button is active but not which one. The API docs say mysteriously "To be of any practical value, a widget should then be packed into the radio button". I'm currently packing a GtkEntry with text into each radiobutton. In the callback I check the text in each radiobutton to see which button is selected.
Surely the group the button is in knows something ( as it has to fill in the selected buttons graphic ). Is there any way to access this value directly ?

thanks,
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Wed Oct 10, 2007 9:25 pm    Post subject: Reply with quote

GtkRadioButton is derived from GtkToggleButton. You should connect each radio button to the "toggled" signal. You can either connect each to the same signal because the callback function receives the radio button that caused the signal. You could also connect each radio button to its own callback function, which would allow you to provide code to handle each change. It really depends on whether you want to just figure out which radio button has been selected or react to every change.

(If you just want to figure out which radio button is chosen to process the selection, just retrieve the group and then move through the list.)
Back to top
Almach



Joined: 10 Oct 2007
Posts: 2

PostPosted: Thu Oct 11, 2007 7:04 pm    Post subject: Reply with quote

I've got eight radiobuttons to set the value of a gloabal variable. So assuming all the buttons are connected to the same callback it could look something like this :

void radio_cb ( GtkToggleButton *rb , gpointer d )
{
GSList *grp;
int index = 0;

grp = gtk_radio_button_get_group ( rb );
do
{
if ( rb = grp->data )
break;
index++;
} while (( grp = grp->next ) != NULL );
switch ( index )
{
more code here to set global variable
}
}

It should work but it just looks a bit creaky :?
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Thu Oct 11, 2007 9:44 pm    Post subject: Reply with quote

Another option would be to use g_object_set_data() on each radio button. Encode its index as a pointer with G_INT_TO_POINTER(). Then, in the callback function, just retrieve the data with g_object_get_data(), and use G_POINTER_TO_INT() to convert it back to an integer. This would avoid the list altogether.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP