PHP
  Home arrow PHP arrow Page 7 - Revisited: Building Cross Platform GUI App...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Revisited: Building Cross Platform GUI Apps With PHP-GTK
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2002-11-17

    Table of Contents:
  • Revisited: Building Cross Platform GUI Apps With PHP-GTK
  • What is PHP-GTK?
  • Downloading and installing PHP-GTK
  • Installing PHP-GTK for Linux
  • Building our first PHP-GTK app
  • Registering multiple callback functions
  • Adding GTK widgets to our window
  • Other PHP-GTK widgets
  • A PHP-GTK database app
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More PHP Articles
    More By Mitchell Harper


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT