GTK2 signals

[ Start > PikeTutorials > GTK2 signals ] [ Edit this Page | Show Page Versions | Show Raw Source ]

Pike GTK2 signals

The best place to look for information on GTK signals is the original documentation for the C GTK library. Pike follows the C library very closely, with a few important differences.

Setting up signal handlers is easy. The following example connects a Pike function, do_click(), to the 'clicked' signal of a GTK2.Button.

GTK2.Button b = GTK2.Button( "Hello" );
b->signal_connect( "clicked", do_click );

This function (signal_connect) takes an optional third argument of any type. This is for any extra data that you wish to pass on to the signal handler when it is called.

The original GTK docs give the function signature of this handler as:

void user_function ( GtkButton* button, gpointer user_data )

This translates into Pike as:

void user_function( GTK2.Button button, mixed user_data )

Note that, in Pike, both parameters are optional. This is the standard format for all signal handlers: the first argument is the object that emitted the signal; and the second argument is any value that was specified by the user when first connecting the signal.

Sometimes signal handlers are a little more complex. An example of this is the 'key_press_event' signal. The C function signature for keypress handlers is:

gboolean user_function( GtkWidget* widget, GdkEvent* event, gpointer user_data )

In Pike, this would be written as:

int user_function( GTK2.Widget widget, GTK2.GdkEvent event, mixed user_data )

All the interesting information here is stored in the GTK2.GdkEvent, but to access that information you first need to cast this object to a mapping. Here's an example keypress handler and some possible output when it's called.

int handle_key_press( GTK2.Widget widget, GTK2.GdkEvent event )
    mapping m = (mapping) event;
    write( "m=%On", m );
    // Propagate the event.
    return 0;


m=([ /* 5 elements */
  "data": "k",
  "keyval": 107,
  "state": 0,
  "time": 3577686,
  "type": "key_press"

The mapping keys correspond exactly to the variable names of the original C GdkEventKey structure members, although some of the original structure's members are absent.


Written by Matthew Clarke.

Powered by PikeWiki2 | Copyright © 2004 - 2009 | Pike is a trademark of Department of Computer and Information Science, Linköping University