GTK+ 2.0 Tutorial
<<< Previous | Getting Started | Next >>>

# Events

In addition to the signal mechanism described above, there is a set of events that reflect the X event mechanism. Callbacks may also be attached to these events. These events are:

- event
- button_press_event
- button_release_event
- scroll_event
- motion_notify_event
- delete_event
- destroy_event
- expose_event
- key_press_event
- key_release_event
- enter_notify_event
- leave_notify_event
- configure_event
- focus_in_event
- focus_out_event
- map_event
- unmap_event
- property_notify_event
- selection_clear_event
- selection_request_event
- selection_notify_event
- proximity_in_event
- proximity_out_event
- visibility_notify_event
- client_event
- no_expose_event
- window_state_event

In order to connect a callback function to one of these events you use the function g_signal_connect(), as described above, using one of the above event names as the `name` parameter. The callback function for events has a slightly different form than that for signals:

    gint callback_func( GtkWidget *widget,
                        GdkEvent  *event,
                        gpointer   callback_data );

GdkEvent is a C `union` structure whose type will depend upon which of the above events has occurred. In order for us to tell which event has been issued each of the possible alternatives has a `type` member that reflects the event being issued. The other components of the event structure will depend upon the type of the event. Possible values for the type are:

      GDK_NOTHING
      GDK_DELETE
      GDK_DESTROY
      GDK_EXPOSE
      GDK_MOTION_NOTIFY
      GDK_BUTTON_PRESS
      GDK_2BUTTON_PRESS
      GDK_3BUTTON_PRESS
      GDK_BUTTON_RELEASE
      GDK_KEY_PRESS
      GDK_KEY_RELEASE
      GDK_ENTER_NOTIFY
      GDK_LEAVE_NOTIFY
      GDK_FOCUS_CHANGE
      GDK_CONFIGURE
      GDK_MAP
      GDK_UNMAP
      GDK_PROPERTY_NOTIFY
      GDK_SELECTION_CLEAR
      GDK_SELECTION_REQUEST
      GDK_SELECTION_NOTIFY
      GDK_PROXIMITY_IN
      GDK_PROXIMITY_OUT
      GDK_DRAG_ENTER
      GDK_DRAG_LEAVE
      GDK_DRAG_MOTION
      GDK_DRAG_STATUS
      GDK_DROP_START
      GDK_DROP_FINISHED
      GDK_CLIENT_EVENT
      GDK_VISIBILITY_NOTIFY
      GDK_NO_EXPOSE
      GDK_SCROLL
      GDK_WINDOW_STATE
      GDK_SETTING

So, to connect a callback function to one of these events we would use something like:

    g_signal_connect (G_OBJECT (button), "button_press_event",
                      G_CALLBACK (button_press_callback), NULL);

This assumes that `button` is a Button widget. Now, when the mouse is over the button and a mouse button is pressed, the function button_press_callback() will be called. This function may be declared as:

    static gboolean button_press_callback( GtkWidget      *widget,
                                           GdkEventButton *event,
                                           gpointer        data );

Note that we can declare the second argument as type `GdkEventButton` as we know what type of event will occur for this function to be called.

The value returned from this function indicates whether the event should be propagated further by the GTK event handling mechanism. Returning TRUE indicates that the event has been handled, and that it should not propagate further. Returning FALSE continues the normal event handling. See the section on Advanced Event and Signal Handling for more details on this propagation process.

For details on the GdkEvent data types, see the appendix entitled GDK Event Types.

The GDK selection and drag-and-drop APIs also emit a number of events which are reflected in GTK by the signals. See Signals on the source widget and Signals on the destination widget for details on the signatures of the callback functions for these signals:

- selection_received
- selection_get
- drag_begin_event
- drag_end_event
- drag_data_delete
- drag_motion
- drag_drop
- drag_data_get
- drag_data_received

<<< Previous | Home | Next >>>
Theory of Signals and Callbacks | Up | Stepping Through Hello World