Article

Build Cross-Platform Windowed Apps with PHP

Page: 1 2 3 4 5 6 7 8 Next

Building Our First PHP-GTK App

Now that PHP-GTK's installed, we're ready to create our first GUI application. Create a new PHP script called window.php and enter the following code into it:

<?php  
 
// Load the PHP-GTK extension  
$extension = "php_gtk" . (strpos(PHP_OS, "WIN") >= 0 ? ".dll" : ".so");  
dl($extension);  
 
// Create a new window  
$window = &new GtkWindow();  
$window->set_title("Test App");  
$window->set_usize(300, 100);  
$window->set_position(GTK_WIN_POS_CENTER);  
$window->set_policy(false, false, false);  
 
// Set a callback function for the destroy signal  
$window->connect("destroy", "killwin");  
 
function killwin()  
{  
echo("Window Destroyed!\n");  
gtk::main_quit();  
}  
 
// Show the window  
$window->show_all();  
 
// Start the main PHP-GTK listener loop  
gtk::main();  
 
?>

Change into the directory where you created window.php and use the following command to execute it:

php --q window.php

Here's the result of window.php on my Windows 2000 machine:

697_fig1

Our First PHP-GTK Window

If you play around with the window, then you'll notice that you can't resize or maximize it, because of the calls that window.php makes to various GTK functions. When you close the window, you'll notice that some text has been output to the console window:

C:\>php -q window.php  
Window Destroyed!

Let's run through the code we used to create window.php.

// Load the PHP-GTK extension  
$extension = "php_gtk" . (strpos(PHP_OS, "WIN") >= 0 ? ".dll" : ".so");  
dl($extension);

As the comment indicates, we load the PHP-GTK extension. The PHP_OS variable contains a word that represents your operating system. On my machine, PHP_OS is "WINNT". We set the value of the $library variable to either php_gtk.dll (for Windows) or php_gtk.so (for Linux), depending on whether or not PHP_OS contains the string "WIN". We then call the dl() function to load the extension into memory.

// Create a new window  
$window = &new GtkWindow();  
$window->set_title("Test App");  
$window->set_usize(300, 100);  
$window->set_position(GTK_WIN_POS_CENTER);  
$window->set_policy(false, false, false);

The most important aspect of any application is the user interface. PHP-GTK defines a class called GtkWindow, which we can instantiate by reference to create a new window. The GtkWindow class includes several functions that we can use to specify how the window will look, where it will be located, etc. In our example I've set the windows title using set_title, set its width and height using set_usize, set its position to the center of the screen, and also used set_policy to tell PHP-GTK that the window can't be resized.

// Set a callback function for the destroy signal  
$window->connect("destroy", "killwin");  
 
function killwin()  
{  
echo("Window Destroyed!\n");  
gtk::main_quit();  
}

Most PHP-GTK widgets include a method called connect(), which allows us to specify callback functions that should be executed when a specific event is captured. The PHP-GTK library monitors our application in much the same way as any other object-orientated, event-driven programming language. PHP-GTK is based on the concept of signals and callbacks, which means that if we move the mouse over a button, for example, a signal will be emitted. We can register a callback function to tell PHP-GTK which function(s) to run when this specific signal is emitted.

In our example, I've used the connect function to tell PHP-GTK that when our window emits the "destroy" signal (which is emitted when we close the window) that it should execute our killwin function. Our killwin function contains a call to the static function main_quit of the gtk class, which unloads our window and terminates our application.

// Show the window  
$window->show_all();  
 
// Start the main PHP-GTK listener loop  
gtk::main();

The show_all() function displays our window, and if it contained any widgets then these would be displayed as well. The main function of the gtk class is called statically, and tells the PHP-GTK library to begin its listener loop.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links