4 /* Our new improved callback. The data passed to this function
5 * is printed to stdout. */
6 void callback( GtkWidget *widget,
9 g_print ("Hello again - %s was pressed\n", (gchar *) data);
12 /* another callback */
13 gint delete_event( GtkWidget *widget,
24 /* GtkWidget is the storage type for widgets */
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);
33 /* Create a new window */
34 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
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!");
40 /* Here we just set a handler for delete_event that immediately
42 g_signal_connect (G_OBJECT (window), "delete_event",
43 G_CALLBACK (delete_event), NULL);
45 /* Sets the border width of the window. */
46 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
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);
53 /* Put the box into the main window. */
54 gtk_container_add (GTK_CONTAINER (window), box1);
56 /* Creates a new button with the label "Button 1". */
57 button = gtk_button_new_with_label ("Button 1");
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 (G_OBJECT (button), "clicked",
62 G_CALLBACK (callback), "button 1");
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);
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);
72 /* Do these same steps again to create a second button */
73 button = gtk_button_new_with_label ("Button 2");
75 /* Call the same callback function with a different argument,
76 * passing a pointer to "button 2" instead. */
77 g_signal_connect (G_OBJECT (button), "clicked",
78 G_CALLBACK (callback), "button 2");
80 gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
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);
86 gtk_widget_show (box1);
88 gtk_widget_show (window);
90 /* Rest in gtk_main and wait for the fun to begin! */