3 /* This is a callback function. The data arguments are ignored
4 * in this example. More on callbacks below.
7 print_hello (GtkWidget *widget,
10 g_print ("Hello World\n");
14 on_delete_event (GtkWidget *widget,
18 /* If you return FALSE in the "delete_event" signal handler,
19 * GTK will emit the "destroy" signal. Returning TRUE means
20 * you don't want the window to be destroyed.
22 * This is useful for popping up 'are you sure you want to quit?'
26 g_print ("delete event occurred\n");
35 /* GtkWidget is the storage type for widgets */
39 /* This is called in all GTK applications. Arguments are parsed
40 * from the command line and are returned to the application.
42 gtk_init (&argc, &argv);
44 /* create a new window, and set its title */
45 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
46 gtk_window_set_title (GTK_WINDOW (window), "Hello");
48 /* When the window emits the "delete-event" signal (which is emitted
49 * by GTK+ in response to an event coming from the window manager,
50 * usually as a result of clicking the "close" window control), we
51 * ask it to call the on_delete_event() function as defined above.
53 * The data passed to the callback function is NULL and is ignored
54 * in the callback function.
56 g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);
58 /* Here we connect the "destroy" event to the gtk_main_quit() function.
60 * This signal is emitted when we call gtk_widget_destroy() on the window,
61 * or if we return FALSE in the "delete_event" callback.
63 g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
65 /* Sets the border width of the window. */
66 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
68 /* Creates a new button with the label "Hello World". */
69 button = gtk_button_new_with_label ("Hello World");
71 /* When the button receives the "clicked" signal, it will call the
72 * function print_hello() passing it NULL as its argument.
74 * The print_hello() function is defined above.
76 g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
78 /* The g_signal_connect_swapped() function will connect the "clicked" signal
79 * of the button to the gtk_widget_destroy() function; instead of calling it
80 * using the button as its argument, it will swap it with the user data
81 * argument. This will cause the window to be destroyed by calling
82 * gtk_widget_destroy() on the window.
84 g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
86 /* This packs the button into the window. A GtkWindow inherits from GtkBin,
87 * which is a special container that can only have one child
89 gtk_container_add (GTK_CONTAINER (window), button);
91 /* The final step is to display this newly created widget... */
92 gtk_widget_show (button);
94 /* ... and the window */
95 gtk_widget_show (window);
97 /* All GTK applications must have a gtk_main(). Control ends here
98 * and waits for an event to occur (like a key press or a mouse event),
99 * until gtk_main_quit() is called.