+ Reply to Thread
Results 1 to 5 of 5

Thread: Beginning PHP-GTK: Signals

  1. #1
    Jordan Guest

    Beginning PHP-GTK: Signals


    Before you begin...
    You should know that this tutorial builds off the first tutorial here. You should understand everything in that tutorial before continuing with this tutorial.

    Introduction to "Beginning PHP-GTK Part 2"
    In the last part of this series tutorial you created a basic GUI interface using PHP-GTK. At the end of the tutorial, the application we made looked like this:



    If you don't have the code, go back and fetch it. We will be using that code during this tutorial.

    Signals
    Signals are notifications fired by the user or internal coding. The PHP-GTK manual puts it best:

    Signals are notifications emitted by widgets.

    When programming Graphical User Interfaces (GUI), it is often necessary to respond to actions performed by the user or initiated within the program itself. GNOME and GTK+ do this via the use of signals. Signals are used to let the program know that something happened. This might be, for example, a user clicking on a GtkButton, or a change being made to a GtkAdjustment value.
    We use signals to respond to the user. An event driven GUI such as the one we are building would be useless without them.

    Signal Callback Functions
    In order to react to a signal, it must be connected to a callback function. A callback function is any PHP function or class method. You connect a signal to a function using connect().

    Constructor:
    Code:
    int connect(string signal, string function, [mixed custom_data ]);
    Signal: Textual name of the signal (IE: Clicked)
    function: Name of the function (callback) to be called
    custom_data: optional, used to pass extra data to the callback function (such as params)

    Connecting a signal is easy. First, lets create a stop and start button method. In the CodeCallGtk class, add these methods:

    Code:
        /**
         * This is the method that is executed when 
         * the stop "button" on the GtkWindow is clicked.
         * 
         */
        
    public function stopButtonClick() {
                
    /*
                 * Create a messagebox
                 */
                
    $dialog = new GtkMessageDialog($thisGtk::DIALOG_MODALGtk::MESSAGE_QUESTIONGtk::BUTTONS_YES_NO"");
                
    $dialog->set_markup(
                    
    "<span foreground='red'>Do you really want to stop?</span>"
                
    );
                
                
    /*
                 * Display the message
                 */
                
    $response $dialog->run();
                
    $dialog->destroy();
        }

        
    /**
         * This is the method that is executed when 
         * the start "button" on the GtkWindow is clicked.
         * 
         */
        
    public function startButtonClick() {
            
    // Place holder for now
        

    The first method (stopButtonClick()) creates a messagebox with two buttons, yes and no. The second method does nothing during this tutorial.

    Connecting the Signal
    As mentioned earlier, connecting the signal is done with connect(). There is also connect_after(), connect_object() and connect_object_after but they are beyond the scope of this tutorial.

    In the __construct() of CodeCallGtk (before show_all()), connect the buttons "clicked" signal with the functions we added earlier:

    Code:
            /*
             * Connect our buttons
             */
             
    $btnStart->connect('clicked', array($this,'startButtonClick'));
             
    $btnStop->connect('clicked', array($this,'stopButtonClick')); 
    You should end up with this code:

    Code:
    <?php

    class CodeCallGtk extends GtkWindow {
        
        
    /**
         * FirstGTK constructor. Actually creates the
         * window
         *
         * @return FirstGtk
         */
        
    public function __construct() {
            
    /*
             * Call the parent constructor
             */
            
    parent::__construct();
            
            
    /*
             * Set the size of the new GTK Window
             * Set the Title
             * Connect the destroy/exit event to a function
             */
            
    $this->set_default_size(400,100);
            
    $this->set_title('CodeCall.net PHP-GTK GUI Tutorial');
            
    $this->connect_simple('destroy', array('gtk''main_quit'));
            
            
    /*
             * Create a virticle and horizontal box
             * container to hold our widgets
             */
            
    $table = new GtkTable(4,2);
            
            
    /*
             * Create Label
             */
            
    $lblHost   = new GtkLabel("Host:");
            
            
    /*
             * Create an entry for entering 
             * host to access
             */
            
    $txtEntry = new GtkEntry(); 
            
            
    /*
             * Create two buttons, one for starting
             * and another for stopping
             */
            
    $btnStart = new GtkButton('Start');
            
    $btnStop  = new GtkButton('Stop');
            
            
    /*
             * Connect our buttons
             */
             
    $btnStart->connect('clicked', array($this,'startButtonClick'));
             
    $btnStop->connect('clicked', array($this,'stopButtonClick'));
             
            
            
            
    /*
             * Attach widgets to the table
             */
            
    $table->attach($lblHost0101); // Column 1, Row 1
            
    $table->attach($txtEntry1401); // Comumn 2-4, Row 1
            
    $table->attach($btnStop2312); // Column 3, Row 2
            
    $table->attach($btnStart3412); // Column 4, Row 2
            
            /*
              * Add the table to the GTK Window
              */
            
    $this->add($table);



             
    /*
             * This will realize and show
             * all windows from child to parent.
             */
            
    $this->show_all();
        }
        
        
    /**
         * This is the method that is executed when 
         * the stop "button" on the GtkWindow is clicked.
         * 
         */
        
    public function stopButtonClick() {
                
    /*
                 * Create a messagebox
                 */
                
    $dialog = new GtkMessageDialog($thisGtk::DIALOG_MODALGtk::MESSAGE_QUESTIONGtk::BUTTONS_YES_NO"");
                
    $dialog->set_markup(
                    
    "<span foreground='red'>Do you really want to stop?</span>"
                
    );
                
                
    /*
                 * Display the message
                 */
                
    $response $dialog->run();
                echo 
    $response;
                
    $dialog->destroy();
        }
        
        
    /**
         * This is the method that is executed when 
         * the start "button" on the GtkWindow is clicked.
         * 
         */
        
    public function startButtonClick() {
            
    // Place holder for now
        
    }
    }

    if (!@
    $GLOBALS['framework']) {
        new 
    CodeCallGtk();
        
    Gtk::main();
    }
    Execute the program and click the stop button. You should see this message dialog:



    Clicking the Start button currently does nothing.

    Part 2 Conclusion
    This was a very simple tutorial covering only signals in PHP-GTK. Signals are important because they allow you, the PHP-GTK programmer, to react to user events. Without them, event based GUIs such as this one would be useless.

    << Previous Part 1 | Continue to Part 3 >>
    ------
    Attached Thumbnails Attached Thumbnails Beginning PHP-GTK: Signals-gtk_message_dialog.jpg  
    Last edited by Jordan; 07-17-2009 at 04:48 AM.

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Beginning PHP-GTK: Signals

    Your making these too fast! lol I have not even gotten to the first one yet

  4. #3
    Jordan Guest

    Re: Beginning PHP-GTK: Signals

    Part 3 probably will not come out until later next week so you have some time.

  5. #4
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    Re: Beginning PHP-GTK: Signals

    /**
    * This is the method that is executed when
    * the stop "button" on the GtkWindow is clicked.
    *
    */
    public function startButtonClick() {
    // Place holder for now
    }
    Shouldn't that say when the "start" button is clicked? Either way it is very nice once again!

  6. #5
    Jordan Guest

    Re: Beginning PHP-GTK: Signals

    Whoops. Yup. Thanks. Fixed!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginning PHP
    By Xav in forum PHP Development
    Replies: 110
    Last Post: 02-16-2011, 04:27 AM
  2. beginning C#
    By mastrgamr in forum C# Programming
    Replies: 8
    Last Post: 12-27-2009, 06:44 AM
  3. Signals Programming
    By Yoshimo in forum C and C++
    Replies: 1
    Last Post: 11-02-2009, 09:27 AM
  4. Beginning C++
    By PlayaSkater in forum C and C++
    Replies: 17
    Last Post: 07-21-2008, 09:25 AM
  5. The beginning!
    By Jaan in forum PHP Tutorials
    Replies: 1
    Last Post: 02-20-2007, 05:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts