With the release of PHP5, there has been renewed interest in the PHP-GTK extension.
What does the PHP-GTK extension provide? "PHP-GTK implements language bindings for GTK+."
PHP-GTK provides the means for a developer, familiar with PHP, to create rich clients using the
PHP programming language.
Installation has already been cover in John Starkey's article
"Installing PHP-GTK on Linux". If you have
not already read this, I would highly recommend it. There are a few comments I'd like to add to the installation process.
Let me point out that the website gtk.php.net has the same
documentation layout as does PHP and the quality of the documentation is also top notch.
I also highly recommend reading the installation
section of the documentation for both Windows and Linux users. For those users of Gentoo, my favorite
distro, there is an ebuild that works great too.
To verify the installation we need to create a simple script that will test whether the
extension is loaded and, if not, will load the extension. The script is as follows:
When you run this code you should get a window similar to this one (however, your window decoration
will most likely be different than mine).
Let's run through this code and pick it apart. Skip down to the window comment and let's
begin there. Here we are going to create the main GTK window. We will create the window object first
with the following statement:
$window = &new GtkWindow();
This provides the main window where we can add widgets, too. Once we have the main window we can start modifing
the window's attributes by calling specific methods of the GtkWindow class. Here is a link to the PHP-GTK
reference documentation for the GtkWindow class: http://gtk.php.net/manual/en/gtk.gtkwindow.php. It's a good idea to bookmark the reference documentation
if you are going to be using PHP-GTK.
You will notice that we are using only three of the methods on the GtkWindow object.
One thing that I want to point out before we move forward is that these Objects follow a
heirarchial order. As such, they also inherit the parent functions. You will notice that
in the reference documentation there is a small section entitled "Object Heirarchy" that displays
the heirarchy of the class.
The set_default_size method allows you to set a default width and height for the window
when it is first created. The function takes two parameters that are width and height in terms of pixels.
The set_border_width method is a method from the GtkContainer class.
method is used to create and set a border that goes around the container which will surround the button.
set_title does what its name implies, which is to set the title of the window. Here, the title of
the window is set to "Simple Example".
The connect method, from GtkObject, allows the developer to connect a user defined function to a signal.
Additional information that can be sent as additional arguments are passed to the method. Check out
GtkObject::connect for a complete review of the method. With the connect method you can associate
the "destroy" signal with a gtk function called "main_quit". You can check out additional
GTK functions on PHP-GTK site under "GTK Functions".
With these method calls we have set up the size, title, container width, and associated a user
function with a specific signal. Next, let's add a button with a tooltip on it along with a
function associated with the action of clicking it.
Moving on to the GtkButton, let's discuss how we can add this button to the window that we
created. To create the button object we call "new" on the GtkButton class. This gives us a button
object that we can then add to the window. GtkButton is also a subclass of GtkObject, so we can
call the connect method on it to connect the "clicked" signal to our PHP function "buttonClick".
This is a basic function that prints a simple line. Next, we add the button to the window by
calling the window object's add method.
The GtkTooltips class allows you to add tooltips to widgets. The code is
very simple, so let's go over it:
This creates a new instance of the GtkTooltips class. We then call three methods on the object to set up
the tooltip for out newly created GtkButton. These methods are called our: set_delay, set_tip, and enable.
The set_delay method sets the delay before the tip is displayed to the user. The method takes an
interger as an argument which determines the delay time measured in milliseconds. The next method,
set_tip, sets the actual text that will be displayed.
The set_tip takes these arguments. The first
argument associates the tip to a particular widget. The second argument is the text that will be
displayed to the user when the user activates the tip via a mouse hover. The third argument is used to
set the "private tip", which is used in conjunction with the GtkTipsQuery class.
The last method called on the tooltip object is the enable method. This enables or activates this tooltip so that it can be
presented to the user.
We finish this all up with the following code:
These last to lines of code finish off the sample example program. The last method called on the
window object is the show_all method that is derived from the GtkWidget class.
The show_all method displays the widget and all the children of this widget. I definitely recommend checking out the
GtkWidget::show_all and GtkWidget::show
methods for additional information on when to use them.
For this simple example, it is sufficient to call the show_all method to display the window and the
button widget. Last, we call the Gtk function main. The main function gives you Gtk gui application
a loop process that allows it to continue handling user input until the application is terminated.
As you can see from this simple example, that creating quality gui based PHP applications is fast and
straight forward. As you get started with your own PHP-GTK applications, I would suggest reviewing the
Gtk classes and functions. The PHP-GTK site has great documentation and provides many great links to other
resources and applications that others have written using PHP-GTK.