|
Problem If you have followed the previous two articles (How to setup and process a GtkButton using GtkAction - Part 1? and How to setup and process a GtkButton using GtkAction - Part 2 - add stock image?) you might be wondering what's the use of GtkAction?
This example will clearly illustrate one of its uses.
Suppose you need to activate or deactivate a group of related buttons as shown below. Try "click on an even number". The entire column of odd numbers is grayed out. Although you can also acheive this by using set_sensitive(false) on all the odd-number buttons, using GtkActionGroup, it's a one-liner!

Solution
Sample Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| <?php $window = new GtkWindow(); $window->set_size_request(400, 320); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Activate/deactivate a bunch of GtkButtons\n". " using GtkAction and GtkActionGroup"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $vbox->pack_start($title, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0);
setup_menu($vbox);
setup_numbers($vbox);
$window->show_all(); Gtk::main();
function setup_numbers($vbox) {
function setup_buttons($button_label, $vbox, $action_grp) { $button = new GtkButton($button_label); $button->set_size_request(60, -1); $vbox->pack_start($hbox = new GtkHBox(), 0); $hbox->pack_start($button, 0);
$action = new GtkAction($button_label, '_'.$button_label, '', ''); // note 3
$action->connect_proxy($button); // note 3
$action->connect('activate', "on_click", $button_label); $action_grp->add_action($action); // note 4
return $button; }
function on_click($button, $label) { echo "button clicked: $label\n"; }
$vbox->pack_start($hbox = new GtkHBox()); $hbox->pack_start($vbox1 = new GtkVBox()); $hbox->pack_start($vbox2 = new GtkVBox());
global $odd_nummbers_action_grp; $odd_nummbers_action_grp = new GtkActionGroup("OddActions"); // note 1
global $buttons; $vbox1->pack_start($hbox1 = new GtkHBox, 0); $hbox1->pack_start($label = new GtkLabel('Odd'), 0); $label->modify_font(new PangoFontDescription("Arial Bold")); for ($i=1; $i<=10; $i+=2) { $buttons[$i] = setup_buttons($i, $vbox1, $odd_nummbers_action_grp); }
global $even_nummbers_action_grp; $even_nummbers_action_grp = new GtkActionGroup("EvenActions"); // note 2
$vbox2->pack_start($hbox2 = new GtkHBox, 0); $hbox2->pack_start($label = new GtkLabel('Even'), 0); $label->modify_font(new PangoFontDescription("Arial Bold")); for ($i=2; $i<=10; $i+=2) { $buttons[$i] = setup_buttons($i, $vbox2, $even_nummbers_action_grp); }
}
function setup_menu($vbox) {
function setup_radio($radio_button_grp, $button_label, $button_value) { $radio = new GtkRadioButton($radio_button_grp, $button_label); $radio->connect('toggled', "on_toggle", $button_value); return $radio; }
function on_toggle($radio, $value) { global $status_area; $label = $radio->child->get_label(); $active = $radio->get_active();
|
- 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 We make use of the code from How to setup and process a GtkButton using GtkAction - Part 1? to set up GtkButtons with GtkActions.
We also make use of the code from How to display and process grouped radio buttons? to set up and process radio buttons.
What's new here:
- Create the odd-number action group.
- Create the even-number action group.
- Create a new action and binds the button to the action.
- Add the action to the action group.
- Turn the respective group on and off based on the user selection.
- Gray out all the odd numbers.
Related Links
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |