1 /* Two-button dialog from
2 * http://library.gnome.org/devel/gtk-tutorial/stable/x345.html
7 /* Our new improved callback. The data passed to this function
8 * is printed to stdout. */
9 static void callback( GtkWidget *widget,
12 g_print ("Hello again - %s was pressed\n", (gchar *) data);
15 /* another callback */
16 static gboolean delete_event( GtkWidget *widget,
27 /* GtkWidget is the storage type for widgets */
32 /* This is called in all GTK applications. Arguments are parsed
33 * from the command line and are returned to the application. */
34 gtk_init (&argc, &argv);
36 /* Create a new window */
37 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
39 /* This is a new call, which just sets the title of our
40 * new window to "Hello Buttons!" */
41 gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");
43 /* Here we just set a handler for delete_event that immediately
45 g_signal_connect (G_OBJECT (window), "delete_event",
46 G_CALLBACK (delete_event), NULL);
48 /* Sets the border width of the window. */
49 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
51 /* We create a box to pack widgets into. This is described in detail
52 * in the "packing" section. The box is not really visible, it
53 * is just used as a tool to arrange widgets. */
54 box1 = gtk_hbox_new (FALSE, 0);
56 /* Put the box into the main window. */
57 gtk_container_add (GTK_CONTAINER (window), box1);
59 /* Creates a new button with the label "Button 1". */
60 button = gtk_button_new_with_label ("Button 1");
62 /* Now when the button is clicked, we call the "callback" function
63 * with a pointer to "button 1" as its argument */
64 g_signal_connect (G_OBJECT (button), "clicked",
65 G_CALLBACK (callback), (gpointer) "button 1");
67 /* Instead of gtk_container_add, we pack this button into the invisible
68 * box, which has been packed into the window. */
69 gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
71 /* Always remember this step, this tells GTK that our preparation for
72 * this button is complete, and it can now be displayed. */
73 gtk_widget_show (button);
75 /* Do these same steps again to create a second button */
76 button = gtk_button_new_with_label ("Button 2");
78 /* Call the same callback function with a different argument,
79 * passing a pointer to "button 2" instead. */
80 g_signal_connect (G_OBJECT (button), "clicked",
81 G_CALLBACK (callback), (gpointer) "button 2");
83 gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
85 /* The order in which we show the buttons is not really important, but I
86 * recommend showing the window last, so it all pops up at once. */
87 gtk_widget_show (button);
89 gtk_widget_show (box1);
91 gtk_widget_show (window);
93 /* Rest in gtk_main and wait for the fun to begin! */