Gtk+ events,GTK+ signals

GTK+ Evenits
GTK+ library is an event driven system. All GUI applications are event driven. The applications start a main loop, which continuously checks for newly generated events. If there is no event, the application waits and does nothing. In GTK+ an event is a message from the X server. When the event reaches a wiget, it may react to this event by emitting a signal. The GTK+ programmer can connect a specific callback to a signal. The callback is a handler function, that reacts to a signal.

#include 
void button_clicked(GtkWidget *widget, gpointer data)
{
g_print("clicked\n");
}
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *fixed;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GtkButton");
gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), fixed);
button = gtk_button_new_with_label("Click");
gtk_fixed_put(GTK_FIXED(fixed), button, 50, 50);
gtk_widget_set_size_request(button, 80, 35);
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(button_clicked), NULL);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
In our application, we have two signals. The clicked signal and the destroy signal.
g_signal_connect(G_OBJECT(button), "clicked", 
G_CALLBACK(button_clicked), NULL);
We use the g_signal_connect() function to connect the clicked signal to the button_clicked() callback.

 void button_clicked(GtkWidget *widget, gpointer data)
{
g_print("clicked\n");
}
The callback will pring "clicked" text to the console. The first parameter of the callback function is the object, which emitted the signal. In our case it is the Click button. The second parameter is optional. We may send some data to the callback. In our case, we did not send any data. We provided a NULL parameter in the g_signal_connect() function.

g_signal_connect(G_OBJECT(window), "destroy", 
G_CALLBACK(gtk_main_quit), NULL);
If we press on the x button located in the upper right corner of the titlebar, or we press Atl + F4, a destroy signal is emitted. We call the gtk_main_quit() function, which will terminate the application.

No comments:

 
Top Blogs