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.
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.
Example 2.4. Using the same callback for more than one window
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.
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.
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.