20.6.1 Problem
20.6.2 Solution
// 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.