Scriptindex.de

[ Menü ]

Home
News
Scripts
Neuzugänge
Suchen
Bücher
Manuals

[ Inhalt ]

Script eintragen
Tutorial eintragen
Newsletter
Umfragen
Link zu uns
Werbung bei uns
Kontakt
Impressum

[ Statistik ]

Hits gesamt: 5203871
Hits Heute: 528
max. Hits (10.07.07): 6964
User Online: 28
Scripts: 2828

[ Partner ]

CodeBase
I.S.U.M.
LUG Bayreuth
PEAR NEWS
PHP Classes

[ Facebook ]

[ Eigene Domain? ]

[ Buchtipp ]

ASP.NET für Dummies
ASP.NET für Dummies

Manuals > PHP-GTK > Verwendung von eigenen Parametern

Beim verbinden von Signalen ist es möglich, einen zusätzlichen eigenen Parameter an den Callback zu übergeben. Dies ist oft nützlich um das Objekt auf das man eine Aktion anwenden will an den Callback zu übergeben.

Wenn zum Beispiel ein Button gedrückt wird, möchte man vielleicht die Vaterinstanz von GtkWindow, zu der diese Instanz von GtkButton hinzugefügt (geaddet) wurde, zerstören.

Dies kann man dann z.B. tun indem man einen optionalen dritten Parameter zum connect() Aufruf hinzufügt. Dieser Parameter wird dann zu Ihrer Callbackfunktion als letzter an den Callback übergebenen Parameter weitergegeben.

Beispiel 2.3. Verwendung von eigenen Parametern mit connect()

<?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(); ?>
Im obigen Code sieht man dass nicht nur die $button Variable, eine Instanz von GtkButton, zum "clicked" Callback hinzugefügt wurde, sondern auch die $window Variable, eine Instanz der GtkWindow Klasse. Dies erlaubt den Aufruf von destroy() und das Schliessen des Fensters.

Sie können so viele eigene Parameter verwenden wie Sie möchten.

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:

Beispiel 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:

Beispiel 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.

Beispiel 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 1998 - 2009 by I.S.U.M.