I just can not figure out the signatures of callback functions that are associated with certain signals.
At first I thought that each type of event has its own function signature, but after digging in various sources, I met different types of functions for the same signals.
Then I tried changing the signatures - I added parameters of different types to the function ...
And no warnings or failures have arisen.
How does this magic work?
Ps. Here is an example of the code that was requested in the comments:
#include <stdio.h> #include <stdlib.h> #include <gtk/gtk.h> void func_1(void) { abort(); } gint func_2(void) { abort(); return 1; } // _int и _data можно менять местами, GTK при помощи какой-то магии // использует _data правильно. // Плюс, параметра _int вообще не должно быть среди параметров функции. gboolean func_3(GtkWidget *_widget, gint _int, gpointer _data) { gtk_button_set_label(GTK_BUTTON(_widget), _data); return TRUE; } int main(int argc, char **argv) { gtk_init(&argc, &argv); GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); GtkWidget *button = gtk_button_new_with_label("magic"); gtk_container_add(GTK_CONTAINER(window), button); //g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(func_1), NULL); //g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(func_2), NULL); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(func_3), "reset"); gtk_widget_show_all(window); gtk_main(); return 0; } There are no mistakes. All three callback options work correctly.
The third variant of the function works even under the condition that the return value is not void, but the left argument is in the position in the argument list of the function.
gtk_signal_connectis a deprecated function and should not be used anymore. - AnT