Signal Handling

Table of Contents

Introduction
Signals
Callbacks and Signal Handlers
Connecting the two
Events
Summary

Introduction

This tutorial will deal with the concept of signal handling in PHP-GTK 2 which is an important and fundamental, since this is what makes your application "tick"!.

You might have noticed that at the end of every PHP-GTK 2 program we write this statement:
Gtk::main();
What this means is that an infinite loop is started which waits for something to occur. Obviously you do not want to just create, display widgets and quit! This loop is what keeps that from happening and is the key to keep your application running. Conversely, the statement:
Gtk::main_quit();
exits from that loop. We use this whenever we really want to stop our application.

First, it will be useful to define some terms that we will use frequently.

  • Action: This is something that occurs. For example, the user clicking on a button counts as an action.
  • Signal: This is a notification generated whenever an event occurs. This is the way your application knows that an action has just occurred.
  • Callback: A function or method that is invoked by some other piece of code.
  • Signal Handler: A callback that is invoked in response to a signal being emitted. Here is where you place the code that will handle the signal.
  • Events: These are a continuous stream of impulses that communicate low-level changes the environment of the underlying windowing system.

Signal handling basically consists of connecting a signal to its signal handler, and of course, writing the signal handler itself. The job of generating the signal whenever an action occurs is taken care by PHP-GTK 2. Another thing to note is that signal generation does not guarantee that the user has performed some action. It is possible to manually generate a signal, and PHP-GTK 2 generates signals internally all the time.

A common instance of a signal being internally generated and used is when GtkAdjustment emits the "value-changed" for a GtkProgressBar to use it. Hence, some signals have a default handler already inbuilt, which will be executed whether or not you manually connect a signal handler to that signal.

Most of the times however, if you want something to happen when a signal is generated, you need to create a signal handler for it.