note 94 added to gtk.gtkradiobutton.php
| From: | Andy Chase | Date: | Wed, 30 Jan 2002 20:11:29 +0000 |
| Subject: | note 94 added to gtk.gtkradiobutton.php | ||
| Groups: | php.gtk.webmaster | ||
| Request: | Send a blank email to php-gtk-webmaster+get-261@lists.php.net to get a copy of this message | ||
A varation on the previous example - you can use variable variables to create new GtkRadioButton
objects, too. This makes it pretty easy to expand the button list; just add new key/value pairs to
the '$buttons' array:
<?
dl( 'php_gtk.' . (strstr( PHP_OS, 'WIN') ? 'dll' : 'so'));
function which_pressed($widget,$label){
/*Dumps $label to STDOUT */
echo "$label\n";
}
$window = &new GtkWindow();
$vbox = &new GtkVbox();
/* Create associative array of buttons - array keys will be button object
names, array values will be button labels: */
$buttons = array("button1" => "Foo","button2" =>
"Bar","button3" => "Baz","button4" =>
"Bat");
$buttonnames = array_keys($buttons);
/* Get the name of the first button to be created; other buttons will be
linked to this one: */
$anchor = $buttonnames[0];
/* Loop through the array keys and create new radio buttons using
variable variables: */
foreach($buttonnames as $temp){
/*Get the correct label from the $buttons array: */
$label = $buttons[$temp];
if($temp == $anchor){
/* If this is the first button in the group, pass null
to the constructor: */
$$temp = &new GtkRadioButton(null, $label);
}else{
/* Otherwise, pass the name of the first button in the
group to the constructor: */
$$temp = &new GtkRadioButton($$anchor, $label);
}
$$temp->show();
/* Connect the new button the the which_pressed() function,
passing the button label as an argument: */
$$temp->connect('pressed','which_pressed',$label);
$vbox->pack_start($$temp);
}
$vbox->show();
$window->add($vbox);
$window->show();
/* Now that the buttons have been created with variable variables, you can
access them by name; let's preselect button3: */
$button3->set_active(true);
Gtk::main();
?>
http://gtk.php.net/manual/en/gtk.gtkradiobutton.php