PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



3.5 Passing additional data to callback function - Part 2
Written by kksou   
Saturday, 31 May 2008

Objective

The additional data to be passed to the callback function can be of any valid PHP data type, e.g. integer, float, string, array, object, etc.

This example achieves the same effect as the previous example except that we pass along an array instead of a string.

Overview

  • Use previous example as the base.
  • In the connect statement, specify the array $radio_button_def instead of $state_code as the third argument.
  • Change the second parameter of the callback function definition accordingly.

Sample Output

3.5.gif

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy',array('Gtk','main_quit'));
$window->set_size_request(200, 100);
$vbox = new GtkVBox();
$window->add($vbox);
$vbox->pack_start(new GtkLabel('Select a State:'), false);

$radio = null;
$radio_button_def = array('New York'=>'NY', 'California'=>'CA', 'Washington'=>'WA');
foreach ($radio_button_def as $state_name => $state_code) {
    $radio = new GtkRadioButton($radio, $state_name);
    $radio->connect('toggled', 'on_toggle', $radio_button_def); // note 1
    $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

  1. Pass the array $radio_button_def along with the signal.
  2. The first parameter is the widget that emitted the signal. This is passed along automatically by php-gtk. The second parameter $button_def is the additional data that you have specified in the connect statement.
  3. Get the 2-letter state code from the array, using $label as the index.


User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved