The first thing to do is loading the .glade file from the previous section. GladeXML constructor takes the file path as first parameter, so all we need to do is this:
Example 4.2. Loading the .glade file
<?php //Create a new glade instance, load the // widgets from the file passed as parameter $glade = new GladeXML('helloglade.glade'); //Start the main loop Gtk::main(); ?> |
When running the script, you will note that the window with the button appears on your screen, but doesn't react to anything other than closing the window. Even there, the window is destroyed but your script continues to run - clearly a case of missing signal connections.
Next, we will just connect the signals as we know it: Call connect or connect_simple on the widget object. To get the object, just use the get_widget() method and pass the widget's name (id) to it. Then do the job as usual:
Example 4.3. Getting and connecting the widgets
<?php //Create a new glade instance, load the // widgets from the file passed as parameter //We use the absolute file path as it is not uncommon // that the application is run from a different working directory $glade = new GladeXML(dirname(__FILE__) . '/helloglade.glade'); //Nothing happened when you clicked the button or closed // the window with Step 1's code. //Here we manually connect the widget signals as you know it $window = $glade->get_widget('wndClose'); $window->connect_simple('destroy', array('Gtk', 'main_quit')); //Again, get the widget object and connect the clicked signal $button = $glade->get_widget('btnClose'); $button->connect_simple('clicked', 'onClickButton'); //This method is called when the button is clicked function onClickButton() { echo "button clicked!\r\n"; Gtk::main_quit(); } //Start the main loop Gtk::main(); ?> |
You probably noted the <signal> tags in the .glade file - it is possible to define signal handlers directly in the file. All we have to do is telling Glade to establish the connections by calling signal_autoconnect() .
You can either define plain function names as handler that will be called when an event occurs, or use a special notation for static method calls by separating class name and method name with double colons as in Classname::methodName.
Example 4.4. Using signal_autoconnect
<?php //Create a new glade instance, load the // widgets from the file passed as parameter //We use the absolute file path as it is not uncommon // that the application is run from a different working directory $glade = new GladeXML(dirname(__FILE__) . '/helloglade.glade'); //Let glade do all the signal connections we specified in the file $glade->signal_autoconnect(); //This method is called when the button is clicked function onClickButton() { echo "button clicked!\r\n"; Gtk::main_quit(); } //Start the main loop Gtk::main(); ?> |
Just connecting to plain functions or static methods doesn't really fulfill the needs of a good programmer. To get away from spaghetti code, we need to be able to connect signals to methods of objects.
Doing that is really simple: Just use signal_autoconnect_instance() with the object as first parameter instead of using signal_autoconnect() :
Example 4.5. Using signal_autoconnect_instance
<?php //Here we use an object and connect all the // signals to *object methods* instead of // functions class MyClass { //This method is called when the button is clicked function onClickButton() { echo "MyClass->onClickButton!\r\n"; Gtk::main_quit(); } function staticMethod() { echo "MyClass::staticMethod()\r\n"; } } $glade = new GladeXML(dirname(__FILE__) . '/helloglade.glade'); //Let glade do all the signal connections we specified in the file // but this time, connect to the object methods $myClassInstance = new MyClass(); $glade->signal_autoconnect_instance($myClassInstance); //Start the main loop Gtk::main(); ?> |