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