Home arrow PHP arrow Page 5 - Revisited: Building Cross Platform GUI Apps With PHP-GTK
PHP

Revisited: Building Cross Platform GUI Apps With PHP-GTK


PHP is not just a web-based scripting language. Did you know that you can also build complete, cross-platform, windowed applications with PHP and MySQL? If not, read on...

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 19
November 17, 2002
TABLE OF CONTENTS:
  1. · Revisited: Building Cross Platform GUI Apps With PHP-GTK
  2. · What is PHP-GTK?
  3. · Downloading and installing PHP-GTK
  4. · Installing PHP-GTK for Linux
  5. · Building our first PHP-GTK app
  6. · Registering multiple callback functions
  7. · Adding GTK widgets to our window
  8. · Other PHP-GTK widgets
  9. · A PHP-GTK database app
  10. · Conclusion

print this article
SEARCH DEVARTICLES

Revisited: Building Cross Platform GUI Apps With PHP-GTK - Building our first PHP-GTK app
(Page 5 of 10 )

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:

Our first PHP-GTK window

If you play around with the window, then you'll notice that you can't resize it and can't 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 now run through the code that 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 even 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, meaning that if we move the mouse over a button for example, then a signal is 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 (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.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials