3 /* This is a callback function. The data arguments are ignored
4 * in this example. More on callbacks below. */
6 print_hello (GtkWidget *widget,
9 g_print ("Hello World\n");
13 on_delete_event (GtkWidget *widget,
17 /* If you return FALSE in the "delete_event" signal handler,
18 * GTK will emit the "destroy" signal. Returning TRUE means
19 * you don't want the window to be destroyed.
21 * This is useful for popping up 'are you sure you want to quit?'
25 g_print ("delete event occurred\n");
34 /* GtkWidget is the storage type for widgets */
38 /* This is called in all GTK applications. Arguments are parsed
39 * from the command line and are returned to the application.
41 gtk_init (&argc, &argv);
43 /* create a new window, and set its title */
44 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
45 gtk_window_set_title (GTK_WINDOW (window), "Hello");
47 /* When the window emits the "delete-event" signal (which is emitted
48 * by GTK+ in response to an event coming from the window manager,
49 * usually as a result of clicking the "close" window control), we
50 * ask it to call the on_delete_event() function as defined above.
52 * The data passed to the callback function is NULL and is ignored
53 * in the callback function.
55 g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);
57 /* Here we connect the "destroy" event to the gtk_main_quit() function.
59 * This signal is emitted when we call gtk_widget_destroy() on the window,
60 * or if we return FALSE in the "delete_event" callback.
62 g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
64 /* Sets the border width of the window. */
65 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
67 /* Creates a new button with the label "Hello World". */
68 button = gtk_button_new_with_label ("Hello World");
70 /* When the button receives the "clicked" signal, it will call the
71 * function print_hello() passing it NULL as its argument.
73 * The print_hello() function is defined above.
75 g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
77 /* The g_signal_connect_swapped() function will connect the "clicked" signal
78 * of the button to the gtk_widget_destroy() function; instead of calling it
79 * using the button as its argument, it will swap it with the user data
80 * argument. This will cause the window to be destroyed by calling
81 * gtk_widget_destroy() on the window.
83 g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
85 /* This packs the button into the window. A GtkWindow inherits from GtkBin,
86 * which is a special container that can only have one child
88 gtk_container_add (GTK_CONTAINER (window), button);
90 /* The final step is to display this newly created widget... */
91 gtk_widget_show (button);
93 /* ... and the window */
94 gtk_widget_show (window);
96 /* All GTK applications must have a gtk_main(). Control ends here
97 * and waits for an event to occur (like a key press or a mouse event),
98 * until gtk_main_quit() is called.