5 /* Our new improved callback. The data passed to this function
6 * is printed to stdout. */
7 void callback( GtkWidget *widget,
10 g_print ("Hello again - %s was pressed\n", (gchar *) data);
13 /* another callback */
14 gint delete_event( GtkWidget *widget,
25 /* GtkWidget is the storage type for widgets */
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);
34 /* Create a new window */
35 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
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!");
41 /* Here we just set a handler for delete_event that immediately
43 g_signal_connect (G_OBJECT (window), "delete_event",
44 G_CALLBACK (delete_event), NULL);
46 /* Sets the border width of the window. */
47 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
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);
54 /* Put the box into the main window. */
55 gtk_container_add (GTK_CONTAINER (window), box1);
57 /* Creates a new button with the label "Button 1". */
58 button = gtk_button_new_with_label ("Button 1");
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");
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);
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);
73 /* Do these same steps again to create a second button */
74 button = gtk_button_new_with_label ("Button 2");
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");
81 gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
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);
87 gtk_widget_show (box1);
89 gtk_widget_show (window);
91 /* Rest in gtk_main and wait for the fun to begin! */