]> Pileus Git - ~andy/gtk/blob - examples/helloworld/helloworld.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / examples / helloworld / helloworld.c
1
2 #include <config.h>
3 #include <gtk/gtk.h>
4
5 /* This is a callback function. The data arguments are ignored
6  * in this example. More on callbacks below. */
7 void hello( GtkWidget *widget,
8             gpointer   data )
9 {
10     g_print ("Hello World\n");
11 }
12
13 gint 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      * This is useful for popping up 'are you sure you want to quit?'
21      * type dialogs. */
22
23     g_print ("delete event occurred\n");
24
25     /* Change TRUE to FALSE and the main window will be destroyed with
26      * a "delete_event". */
27
28     return TRUE;
29 }
30
31 /* Another callback */
32 void destroy( GtkWidget *widget,
33               gpointer   data )
34 {
35     gtk_main_quit ();
36 }
37
38 int main( int   argc,
39           char *argv[] )
40 {
41     /* GtkWidget is the storage type for widgets */
42     GtkWidget *window;
43     GtkWidget *button;
44     
45     /* This is called in all GTK applications. Arguments are parsed
46      * from the command line and are returned to the application. */
47     gtk_init (&argc, &argv);
48     
49     /* create a new window */
50     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
51     
52     /* When the window is given the "delete_event" signal (this is given
53      * by the window manager, usually by the "close" option, or on the
54      * titlebar), we ask it to call the delete_event () function
55      * as defined above. The data passed to the callback
56      * function is NULL and is ignored in the callback function. */
57     g_signal_connect (G_OBJECT (window), "delete_event",
58                       G_CALLBACK (delete_event), NULL);
59     
60     /* Here we connect the "destroy" event to a signal handler.  
61      * This event occurs when we call gtk_widget_destroy() on the window,
62      * or if we return FALSE in the "delete_event" callback. */
63     g_signal_connect (G_OBJECT (window), "destroy",
64                       G_CALLBACK (destroy), NULL);
65     
66     /* Sets the border width of the window. */
67     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
68     
69     /* Creates a new button with the label "Hello World". */
70     button = gtk_button_new_with_label ("Hello World");
71     
72     /* When the button receives the "clicked" signal, it will call the
73      * function hello() passing it NULL as its argument.  The hello()
74      * function is defined above. */
75     g_signal_connect (G_OBJECT (button), "clicked",
76                       G_CALLBACK (hello), NULL);
77     
78     /* This will cause the window to be destroyed by calling
79      * gtk_widget_destroy(window) when "clicked".  Again, the destroy
80      * signal could come from here, or the window manager. */
81     g_signal_connect_swapped (G_OBJECT (button), "clicked",
82                               G_CALLBACK (gtk_widget_destroy),
83                               G_OBJECT (window));
84     
85     /* This packs the button into the window (a gtk container). */
86     gtk_container_add (GTK_CONTAINER (window), button);
87     
88     /* The final step is to display this newly created widget. */
89     gtk_widget_show (button);
90     
91     /* and the window */
92     gtk_widget_show (window);
93     
94     /* All GTK applications must have a gtk_main(). Control ends here
95      * and waits for an event to occur (like a key press or
96      * mouse event). */
97     gtk_main ();
98     
99     return 0;
100 }