|
|
| Author |
Message |
|
|
Almach
Joined: 10 Oct 2007 Posts: 2
|
Posted: Wed Oct 10, 2007 9:07 pm Post subject: Dumb radio button question |
|
|
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: 386 Location: Fairfax, Virginia
|
Posted: Wed Oct 10, 2007 9:25 pm Post subject: |
|
|
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
|
Posted: Thu Oct 11, 2007 7:04 pm Post subject: |
|
|
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: 386 Location: Fairfax, Virginia
|
Posted: Thu Oct 11, 2007 9:44 pm Post subject: |
|
|
| 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 |
|
 |
|