GTK2 signals

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

1 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.

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

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:

{link:void user_function ( GtkButton* button, gpointer user_data )|}

This translates into Pike as:

{code}void user_function( GTK2.Button button, mixed user_data ){code}

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:

{link:gboolean user_function( GtkWidget* widget, GdkEvent* event, gpointer user_data )|}

In Pike, this would be written as:

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

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.

{code}int handle_key_press( GTK2.Widget widget, GTK2.GdkEvent event )
    mapping m = (mapping) event;
    write( "m=%O\n", m );
    // Propagate the event.
    return 0;

{code}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 {link:GdkEventKey|} structure members, although some of the original structure's members are absent.
1.1 References
* {link:GTK+ 2 Reference Manual|}
* {link:Pike GTK2 module reference|}

Written by Matthew Clarke.

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