]> Pileus Git - ~andy/gtk/blob - examples/helloworld2/helloworld2.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / examples / helloworld2 / helloworld2.c
1
2 #include <config.h>
3 #include <gtk/gtk.h>
4
5 /* Our new improved callback.  The data passed to this function
6  * is printed to stdout. */
7 void callback( GtkWidget *widget,
8                gpointer   data )
9 {
10     g_print ("Hello again - %s was pressed\n", (gchar *) data);
11 }
12
13 /* another callback */
14 gint delete_event( GtkWidget *widget,
15                    GdkEvent  *event,
16                    gpointer   data )
17 {
18     gtk_main_quit ();
19     return FALSE;
20 }
21
22 int main( int   argc,
23           char *argv[] )
24 {
25     /* GtkWidget is the storage type for widgets */
26     GtkWidget *window;
27     GtkWidget *button;
28     GtkWidget *box1;
29
30     /* This is called in all GTK applications. Arguments are parsed
31      * from the command line and are returned to the application. */
32     gtk_init (&argc, &argv);
33
34     /* Create a new window */
35     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
36
37     /* This is a new call, which just sets the title of our
38      * new window to "Hello Buttons!" */
39     gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");
40
41     /* Here we just set a handler for delete_event that immediately
42      * exits GTK. */
43     g_signal_connect (G_OBJECT (window), "delete_event",
44                       G_CALLBACK (delete_event), NULL);
45
46     /* Sets the border width of the window. */
47     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
48
49     /* We create a box to pack widgets into.  This is described in detail
50      * in the "packing" section. The box is not really visible, it
51      * is just used as a tool to arrange widgets. */
52     box1 = gtk_hbox_new (FALSE, 0);
53
54     /* Put the box into the main window. */
55     gtk_container_add (GTK_CONTAINER (window), box1);
56
57     /* Creates a new button with the label "Button 1". */
58     button = gtk_button_new_with_label ("Button 1");
59     
60     /* Now when the button is clicked, we call the "callback" function
61      * with a pointer to "button 1" as its argument */
62     g_signal_connect (G_OBJECT (button), "clicked",
63                       G_CALLBACK (callback), (gpointer) "button 1");
64
65     /* Instead of gtk_container_add, we pack this button into the invisible
66      * box, which has been packed into the window. */
67     gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
68
69     /* Always remember this step, this tells GTK that our preparation for
70      * this button is complete, and it can now be displayed. */
71     gtk_widget_show (button);
72
73     /* Do these same steps again to create a second button */
74     button = gtk_button_new_with_label ("Button 2");
75
76     /* Call the same callback function with a different argument,
77      * passing a pointer to "button 2" instead. */
78     g_signal_connect (G_OBJECT (button), "clicked",
79                       G_CALLBACK (callback), (gpointer) "button 2");
80
81     gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
82
83     /* The order in which we show the buttons is not really important, but I
84      * recommend showing the window last, so it all pops up at once. */
85     gtk_widget_show (button);
86
87     gtk_widget_show (box1);
88
89     gtk_widget_show (window);
90     
91     /* Rest in gtk_main and wait for the fun to begin! */
92     gtk_main ();
93
94     return 0;
95 }