Recipe 20.6 Displaying a GUI Widget in a Window
20.6.1 Problem
You want to display a window with a GUI
widget, such as a button, in it.
20.6.2 Solution
Create the window, create the
widget, and then add the widget to the window:
// create the window
$window = &new GtkWindow();
// create the button and add it to the window
$button = &new GTKButton('Click Me, Alice');
$window->add($button);
// display the window
$window->show_all();
// necessary so that the program exits properly
function shutdown() { gtk::main_quit(); }
$window->connect('destroy','shutdown');
// start GTK's signal handling loop
gtk::main();
20.6.3 Discussion
First,
you create a window by instantiating a new
GtkWindow
object. GTK objects must be created
as references: &new GtkWindow( ), not
new GtkWindow( ). You then create a new
GtkButton
object with a label
"Click Me, Alice". Passing
$button to the window's
add( ) method
adds the button to the window. The show_all(
) method displays the window and any
widgets inside of it. The only widget inside the window in this
example is the button. The next two lines ensure that the program
quits when the window is closed. The shutdown(
) function is a callback, as is
explained later in Recipe 20.8.
The last line is necessary in all PHP-GTK programs. Calling
gtk::main( ) starts the
signal-handling loop. This means that
the program waits for signals emitted by its GUI widgets and then
responds to the signals as they occur. These signals are activities
like clicking on buttons, resizing windows, and typing in text boxes.
The only signal this program pays attention to is the
destroy signal. When the user closes the
program's main window, the
destroy signal is emitted, and
gtk::main_quit( ) is called. This function exits
the program.
20.6.4 See Also
Documentation on the GtkWindow class at
http://gtk.php.net/manual/en/gtk.gtkwindow.php,
on GTKContainer::add( ) at
http://gtk.php.net/manual/en/gtk.gtkcontainer.method.add.php,
on GtkWidget::show_all( ) at
http://gtk.php.net/manual/en/gtk.gtkwidget.method.show_all.php,
on the GtkButton class at
http://gtk.php.net/manual/en/gtk.gtkbutton.php,
on gtk::main_quit( ) at
http://gtk.php.net/manual/en/gtk.method.main_quit.php,
on and gtk::main( ) at
http://gtk.php.net/manual/en/gtk.method.main.php;
the tutorial at
http://gtk.php.net/manual/en/tutorials.hellow.php
is a helpful introduction to basic GTK programming.
|