Home arrow PHP arrow Page 7 - 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 - 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:

<?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();

?>


Run reverse.php. When you click on the button, the text in the text box will be reversed, like this:

Our reverse.php example in action

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();

$window->add($box);


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:

GtkObject

|-- GtkWidget

|---- GtkContainer

|------ GtkBox

|-------- GtkVBox

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:

// 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);


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());

$theEntry->set_text($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.
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