GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Sep 01, 2014 1:37 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Dumb radio button question
PostPosted: Wed Oct 10, 2007 9:07 pm 
Offline

Joined: Wed Oct 10, 2007 8:53 pm
Posts: 2
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,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 9:25 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
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.)

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 11, 2007 7:04 pm 
Offline

Joined: Wed Oct 10, 2007 8:53 pm
Posts: 2
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 :?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 11, 2007 9:44 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
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.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group