Question

The following code snippet is from the GTK+ 3 tutorial from GNOME given here.

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static gboolean
on_delete_event (GtkWidget *widget,
                 GdkEvent  *event,
                 gpointer   data)
{
  g_print ("delete event occurred\n");
  return TRUE;
}

The program is very simple, and it only has a toplevel window and a button. And this is how the callbacks have been connected:

 g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);
 g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
 g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
 g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);

My question is regarding the arguments we pass to the callback functions. Why is it that in the on_delete_event handler we pass the second argument GdkEvent* data?

Alternatively, why do we pass no such argument to the first callback function. What is the use of the GdkEvent parameter in this scenario?

I'm sorry if the question shows lack of research, but for me neither the tutorial, nor the resource on event structures was clear enough in describing callbacks.

Was it helpful?

Solution

Signals have different signatures, just like functions have different signatures.

In the example above: The event family of signals on the GtkWidget class have, usually, associated a GdkEvent instance that details the event as received from the windowing system.

Still in the example above: The clicked signal is emitted by the GtkButton in response to a sequence of events: a button press, followed by a button release within the responsive area of the widget itself (that is, if you press the pointer button on the GtkButton widget, then you move the pointer outside the button widget and release the pointer button, and the GtkButton widget will not emit the clicked signal). For this reason, there is no GdkEvent instance associated to it.

It usually helps, when trying to understand them, to consider GObject signals as named lists of functions that can be invoked by a particular instance of a type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top