0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

but assuming that i release within the responsive area,clicked event will be triggered , so why does print_hello not accomodate for this event ,i mean why does that not have a GdkEvent argument?
because the clicked signal is not an event-related signal. :-) you can cause its emission programmatically (in some limited cases) or as a result of an accessibility request. if you want the event structure, you can connect the button-release-event on the button widget.
sorry didn't get it initially , so on_delete_event has a GdkEvent associated with it because we are connecting it to the window as well?
no, GtkWidget::delete-event is a signal emitted in response to an event coming from the windowing system, which means it has a GdkEvent associated with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.