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

    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 1 hosted by Hostway
    Stay green...Green IT