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

Programming with PHP and GTK, Part 1
By: Vinu Thomas
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 39
    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:
      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


    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


       · I liked the article, simple and to the point. However, the reason for making the...
       · that you can create distributable applications without having to have those the run...
       · nice article, but...it's to basic :)...all these things can anyone read out of the...
       · GTK is GIMP Tool Kit, not GNOME Tool Kit...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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