Revisited: Building Cross Platform GUI Apps With PHP-GTK - Adding GTK widgets to our window (Page 7 of 10 )
So, we've created a basic PHP-GTK window and setup the GTK loop, but what good is our application when we can't even interact with it? Let's add a text box and button to our window. When we click on the button, any text in the text box will be reversed using PHP's strrev function.
Before we look at the code to do this, we need to know about the GtkEntry and GtkButton classes. The GtkEntry class is a single lined text box that allows us to enter, copy, paste, and delete text from it. The GtkButton class is a button that can be clicked, double clicked, etc. Both the GtkEntry and GtkButton classes support the connect event to register callback functions.
Create a new file called reverse.php and enter the following code into it:
Run reverse.php. When you click on the button, the text in the text box will be reversed, like this:
It's time to introduce a new concept regarding PHP-GTK. Put simply, PHP-GTK doesn’t support absolute positioning of widgets, meaning that we can't add a text box 10 pixels from the left of a window and 20 pixels down. PHP-GTK only supports relative positioning, meaning that we must define areas into which widgets can be placed.
The GtkHBox and GtkVBox classes create horizontal and vertical boxes that act as containers for other widgets. In our example above, we create a new GtkVBox object, adding it to our window, like this:
// Add a GtkVBox class to our window
$box = &new GtkVBox();
All GTK widgets that are containers (i.e. can hold other widgets) expose the add function, which accepts one parameter: a widget. This widget is then displayed within that container.
The GtkVBox widget inherits many of its functions from its base classes. All GTK widgets are derived from base classes, meaning that most of the GTK widgets share similar functionality. Here's the hierarchy of the GtkVBox class:
The GtkBox class exposes a pack_start function. This function is used to add a widget to a GtkBox class or derived class. In our example, we create a new GtkEntry and GtkButton class, adding them to our GtkVBox object using its pack_start function, like this:
As you can see in our example above, we register a callback function for the "clicked" signal of our button. Remember back to our first window.php example where our call to connect contained only two arguments. In our example above, we specify a third parameter, which is our GtkEntry widget, $entry. The callback function that we specify is reverseText, which looks like this:
function reverseText($theButton, $theEntry)
$text = strrev($theEntry->get_text());
The reverseText function accepts two arguments: the widget that emitted the signal (which in our case is the button) as well as a custom argument (which is our GtkEntry widget). The first argument, $theButton is passed in implicitly by PHP-GTK, and we have manually passed in the second argument when we defined our callback function for the buttons "clicked" signal.
Our reverseText function gets the text from our GtkEntry object by calling its get_text function. This text is reversed using PHP's strrev function, and is the put back into our GtkEntry object by calling its set_text function.