Article

Build Cross-Platform Windowed Apps with PHP

Page: 1 2 3 4 5 6 7 8 Next

Registering Multiple Callback Functions

One of the great things about PHP-GTK is that you can register multiple callback functions for any signal. This can come in handy if you need to perform multiple tasks when a button is clicked, a window is closed, a tree is expanded, etc.

Let's take a quick look at how to register multiple callback functions for the destroy signal of the window we created in window.php:

// Set a callback function for the destroy signal    
$window->connect("destroy", "function1");    
$window->connect("destroy", "killwin");    
   
function function1()    
{    
echo("This is some output before the killwin function is called.\n");    
}    
   
function killwin()    
{    
echo("Window Destroyed!\n");    
gtk::main_quit();    
}

As you can see, I've added another call to our windows connect function, specifying that it should also call the function1 function when it is destroyed. I've placed the call to connect("destroy", "function1") before the call to connect("destroy", "killwin"). This is an important point to remember: the order in which you register the callback functions is the order in which they will be executed.

The output to my console window from running window.php with the new callback handler and function looks like this:

C:\>php -q window.php    
This is some output before the killwin function is called.    
Window Destroyed!

Adding GTK Widgets to Our Window

So, we've created a basic PHP-GTK window and set up 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:

<?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, 50);    
$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();    
}    
   
// Add a GtkVBox class to our window    
$box = &new GtkVBox();    
$window->add($box);    
   
// Add a GtkEntry class to our window    
$entry = &new GtkEntry();    
$entry->set_text("Hello World");    
$box->pack_start($entry);    
   
// Add a GtkButton class to our window    
$button = &new GtkButton("Reverse Text");    
$button->connect("clicked", "reverseText", $entry);    
$box->pack_start($button);    
   
function reverseText($theButton, $theEntry)    
{    
$text = strrev($theEntry->get_text());    
$theEntry->set_text($text);    
}    
   
// Show the window    
$window->show_all();    
   
// Start the main PHP-GTK listener loop    
gtk::main();    
   
?>

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

Sponsored Links