3.3 Handling multiple signals with one signal handler - one more example |
|
Written by kksou
|
|
Wednesday, 30 April 2008 |
Objective
Let's have one more example of handling multiple signals using only one signal handler. This time instead of GtkButton, we will use GtkRadioButton.
Overview
- To display radio buttons, use GtkRadiobutton.
- When the user selects one of the radio buttons, the signal emitted is called toggled.
- As in the previous example, we connect this signal for all the three radio buttons to the same callback function
on_toggle().
- To retrieve the label of the button, we use GtkButton::get_label(), where $radio is the first argument being passed along by php-gtk when the callback function is activated.
Sample Output

Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 100); $vbox = new GtkVBox(); $window->add($vbox);
$radio = null; for ($i=1; $i<=3; ++$i) { $radio = new GtkRadioButton($radio, 'radio button '.$i); // note 1
$radio->connect('toggled', 'on_toggle'); // note 2
$vbox->pack_start($radio, false); }
$window->show_all(); Gtk::main();
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
- Create the radio buttons using
new GtkRadioButton(buttongrp, button_label). Note that for the first radio button, we pass in NULL as the first argument. For the second radio buttons, we pass in the pointer of the first radio button as the first argument. Similarly for the third. PHP-GTK will then group all these into the same group.
- Connect the signal 'toggled' to the function
on_toggle().
- Note the first argument
$radio. This is passed along by php-gtk when calling your signal handler.
- Get the label of the button that is being clicked on.
- Check if the radio button is selected using the method GtkTogglebutton::method().
|