HomePHP Page 4 - Programming with PHP and GTK, Part 1
A PHP-GTK Window - PHP
Have you ever thought of writing a PHP application for client side execution without having a web server present? Learn the basics of creating a front end for PHP using PHP-GTK.
Let's start off with PHP-GTK programming by making a simple window. Before you start any PHP-GTK code, you'll need to load the GTK extensions for PHP. This is achieved by using the following code:
if (!extension_loaded('gtk')) { dl( 'php_gtk.' . PHP_SHLIB_SUFFIX); }
This snippet of code should be present at the start of all your programs using the PHP-GTK extension. What this code does is to load the GTK extension if it's not already loaded.
Now let's get to the code to create a window. Let's create a window with the Title "First Window" and save the code as window.php
<? if (!extension_loaded('gtk')) { dl( 'php_gtk.' . PHP_SHLIB_SUFFIX); }
//function to handle destroy signal function killwindow() { Gtk::main_quit(); }
// Create a new Window $window = &new GtkWindow(); $window->set_title("First Window"); $window->set_usize(200, 60);
//Callback for destroy signal $window->connect("destroy", "killwindow");
// Show the window $window->show_all();
// Start the main PHP-GTK listener loop gtk::main(); ?>
Does this look cryptic? It's quite easy to understand. Let's look at the following snippet of code:
The first line $window = &new GtkWindow(); creates a new instance of a GTK Window. The next two lines sets the window properties.
window->set_title allows you to set the Window's Title, which is the text in the Title Bar.
window->set_usize allows you to set the size of the window. In this example, we are creating a 200x600 window.
Now we come to the the callback function for signal handling. The following line allows you to capture the destroy signal for the window.
//Callback for destroy signal $window->connect("destroy", "killwindow");
Now what's a signal? In GTK, any action you take on a components, generates a signal. We need to trap these signals to respond properly to the action. In our window we need to trap the destroy signal, which will be generated when the window is closed. In the above code, we are telling the program to call the function killwindow, when the destroy signal is generated.
function killwindow() { Gtk::main_quit(); }
The function killwindow() is used to respond to the destroy signal. Gtk::main_quit() tells GTK to close the program. This is an important step to shut down the program. If you do not call Gtk::main_quit() to shutdown the program, the window might disappear when you close the it, but the application will still be running in the background.
$window->show_all(); gtk::main();
These two lines are important to your code. The first line $window->show_all(); tells the program to display your window. If you do not call this code, the window will not appear on screen. The next line, gtk::main(); is the GTK event loop. This loop keeps running till Gtk::main_quit() is called.
Now when you run this program window.php, you should get an output like this: