]> Pileus Git - ~andy/gtk/blob - examples/hello-world.c
Use gtk_separator_new() instead gtk_[v|h]separator_new()
[~andy/gtk] / examples / hello-world.c
1 #include <gtk/gtk.h>
2
3 /* This is a callback function. The data arguments are ignored
4  * in this example. More on callbacks below. */
5 static void
6 print_hello (GtkWidget *widget,
7              gpointer   data)
8 {
9   g_print ("Hello World\n");
10 }
11
12 static gboolean
13 on_delete_event (GtkWidget *widget,
14                  GdkEvent  *event,
15                  gpointer   data)
16 {
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.
20    *
21    * This is useful for popping up 'are you sure you want to quit?'
22    * type dialogs.
23    */
24
25   g_print ("delete event occurred\n");
26
27   return TRUE;
28 }
29
30 int
31 main (int   argc,
32       char *argv[])
33 {
34   /* GtkWidget is the storage type for widgets */
35   GtkWidget *window;
36   GtkWidget *button;
37
38   /* This is called in all GTK applications. Arguments are parsed
39    * from the command line and are returned to the application.
40    */
41   gtk_init (&argc, &argv);
42
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");
46
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.
51    *
52    * The data passed to the callback function is NULL and is ignored
53    * in the callback function.
54    */
55   g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);
56
57   /* Here we connect the "destroy" event to the gtk_main_quit() function.
58    *
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.
61    */
62   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
63
64   /* Sets the border width of the window. */
65   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
66
67   /* Creates a new button with the label "Hello World". */
68   button = gtk_button_new_with_label ("Hello World");
69
70   /* When the button receives the "clicked" signal, it will call the
71    * function print_hello() passing it NULL as its argument.
72    *
73    * The print_hello() function is defined above.
74    */
75   g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
76
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.
82    */
83   g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
84
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
87    */
88   gtk_container_add (GTK_CONTAINER (window), button);
89
90   /* The final step is to display this newly created widget... */
91   gtk_widget_show (button);
92
93   /* ... and the window */
94   gtk_widget_show (window);
95
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.
99    */
100   gtk_main ();
101
102   return 0;
103 }