PHP
  Home arrow PHP arrow Page 4 - Programming with PHP and GTK, Part 1
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
PHP

Programming with PHP and GTK, Part 1
By: Vinu Thomas
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 40
    2004-08-16


    Table of Contents:
  • Programming with PHP and GTK, Part 1
  • Installing PHP-GTK
  • A Test Run
  • A PHP-GTK Window
  • Widgets
  • A Simple Program

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Programming with PHP and GTK, Part 1 - A PHP-GTK Window
    ( Page 4 of 6 )

    Making a window

    Let's start off with PHP-GTK programming by making a simple window. Before you start any PHP-GTK code, you'll need to load the GTK extensions for PHP. This is achieved by using the following code:

    if (!extension_loaded('gtk')) {
    dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
    }

    This snippet of code should be present at the start of all your programs using the PHP-GTK extension. What this code does is to load the GTK extension if it's not already loaded.

    Now let's get to the code to create a window. Let's create a window with the Title "First Window" and save the code as window.php

    <?
    if (!extension_loaded('gtk')) {
    dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
    }

    //function to handle destroy signal
    function killwindow()
     {
            Gtk::main_quit();
      }

    // Create a new Window
    $window = &new GtkWindow();
    $window->set_title("First Window");
    $window->set_usize(200, 60);

    //Callback for destroy signal
    $window->connect("destroy", "killwindow");

    // Show the window
    $window->show_all();

    // Start the main PHP-GTK listener loop
    gtk::main();
    ?>

    Does this look cryptic? It's quite easy to understand. Let's look at the following snippet of code:

    $window = &new GtkWindow();
    $window->set_title("First Window");
    $window->set_usize(200, 60);

    The first line $window = &new GtkWindow(); creates a new instance of a GTK Window. The next two lines sets the window properties.

    window->set_title allows you to set the Window's Title, which is the text in the Title Bar.

    window->set_usize allows you to set the size of the window. In this example, we are creating a 200x600 window.

    Now we come to the the callback function for signal handling. The following line allows you to capture the destroy signal for the window.

    //Callback for destroy signal
    $window->connect("destroy", "killwindow");

    Now what's a signal? In GTK, any action you take on a components, generates a signal. We need to trap these signals to respond properly to the action. In our window we need to trap the destroy signal, which will be generated when the window is closed. In the above code, we are telling the program to call the function killwindow, when the destroy signal is generated.

    function killwindow()
     {
            Gtk::main_quit();
      }

    The function killwindow() is used to respond to the destroy signal. Gtk::main_quit() tells GTK to close the program. This is an important step to shut down the program. If you do not call Gtk::main_quit() to shutdown the program, the window might disappear when you close the it, but the application will still be running in the background.

    $window->show_all();
    gtk::main();

    These two lines are important to your code. The first line $window->show_all(); tells the program to display your window. If you do not call this code, the window will not appear on screen. The next line, gtk::main(); is the GTK event loop. This loop keeps running till Gtk::main_quit() is called.

    Now when you run this program window.php, you should get an output like this:

    PHP and GTK+



     
     
    >>> More PHP Articles          >>> More By Vinu Thomas
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek