Using Custom Parameters

When connecting signals, it is possible to add an extra custom parameter to the callback. This is useful for passing the object you want to perform an action on, to your callback function.

For example, when a button is pressed we might want to destroy the parent instance of GtkWindow that that instance of GtkButton has been added to.

You can do this by including an optional third parameter to the connect() call. This will then be passed to your signal handler function as the final callback parameter.

Example 2.3. Using Custom Parameters with the connect() method.

<?php

function button_clicked($button, $window) 
{
    $window->destroy();
    gtk::main_quit();
}

$window = &new GtkWindow();

$button = &new GtkButton("exit");
$button->connect("clicked","button_clicked", $window);

$window->add($button);
$window->show_all();

gtk::main();

?>
In the above code you can see that we pass not only the $button variable, an instance of GtkButton, to the "clicked" callback but also the $window variable, an instance of the GtkWindow class. This allows us to call the destroy() method on the window.

You can have as many custom parameters as you want.

By passing the $button variable as our calling object parameter and the $window variable as our custom parameter, we could use this same callback for more than one GtkButton on more than one GtkWindow. Note that the names given to the parameters within the callback are irrelevant outside the callback function; PHP-GTK picks up on the positions of the parameters in the connect* method calls and passes these to the variables listed in the callback declaration as an array, so that any instance of a connection using the same parameter structure can use that same callback. This is demonstrated in the code below using a single custom parameter, but is equally true for more than one.

Both connect_object() and connect_object_after() allow you to pass an object other than the calling object as the first parameter to your callback function. This is mainly used for calling static PHP-GTK functions, as in (for example) the gtk::main_quit() function:

Example 2.5. Using the connect_object() method to specify a built-in function as callback.

<?php

$window = &new GtkWindow();
$window->connect_object("destroy", array("gtk", 
"main_quit"));
$window->show();

gtk::main();

?>
This could be called on any static function or method by using the gtkobject::method syntax expressed as an array.

It also means you can have a single callback for multiple signals. For example; you might create a window containing (within the necessary container widgets) a GtkMenuBar, a GtkToolbar and a GtkButton. When Exit is chosen by the user through any of these widgets, a shutdown function could be invoked by passing the instance of GtkWindow as its first parameter, allowing the window to be destroyed from any of those connections. The callback function and the connections in this instance would look like this:

Example 2.6. Using the connect_object() method to pass another object as first parameter.

<?php

function destroy_window($window)
{
    $window->destroy();
    gtk::main_quit();
}

$exit_button->connect_object("clicked", 
"destroy_window", $window);

?>

The connect_after* methods allow callbacks to be "run after" the default signal handler for that signal. This can be useful in some situations; for example, where you want to destroy only one of several windows in a given circumstance. However, connect_after* methods will only work when a signal has been created in the GTK source with a GTK_RUN_LAST flag. The "destroy" signal and all the 'event' signals have this flag; beyond that, the only way to check is to either test the signal within PHP-GTK or read the GTK source.

Example 2.7. Using the connect_after() method.

<?php

function quit_routine($window) 
{
    print("Shutting down...\n");
    gtk::main_quit();
}

$window1 = &new GtkWindow();
$window1->set_title("Quit the main loop");
$window1->connect("destroy", "quit_routine");

$window2 = &new GtkWindow();
$window2->set_title("Destroy this window");
$window2->connect_after("destroy", "quit_routine");

$window1->show();
$window2->show();

gtk::main();

?>

See also: GtkObject, connect_after() connect_object() and connect_object_after() .

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.