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