1 /* This file extracted from the GTK tutorial. */
7 /* Our new improved callback. The data passed to this function is printed
9 void callback (GtkWidget *widget, gpointer *data)
11 g_print ("Hello again - %s was pressed\n", (char *) data);
14 /* another callback */
15 void delete_event (GtkWidget *widget, GdkEvent *event, gpointer *data)
20 int main (int argc, char *argv[])
22 /* GtkWidget is the storage type for widgets */
27 /* this is called in all GTK applications. arguments are parsed from
28 * the command line and are returned to the application. */
29 gtk_init (&argc, &argv);
31 /* create a new window */
32 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
34 /* this is a new call, this just sets the title of our
35 * new window to "Hello Buttons!" */
36 gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");
38 /* Here we just set a handler for delete_event that immediately
40 gtk_signal_connect (GTK_OBJECT (window), "delete_event",
41 GTK_SIGNAL_FUNC (delete_event), NULL);
44 /* sets the border width of the window. */
45 gtk_container_border_width (GTK_CONTAINER (window), 10);
47 /* we create a box to pack widgets into. this is described in detail
48 * in the "packing" section below. The box is not really visible, it
49 * is just used as a tool to arrange widgets. */
50 box1 = gtk_hbox_new(FALSE, 0);
52 /* put the box into the main window. */
53 gtk_container_add (GTK_CONTAINER (window), box1);
55 /* creates a new button with the label "Button 1". */
56 button = gtk_button_new_with_label ("Button 1");
58 /* Now when the button is clicked, we call the "callback" function
59 * with a pointer to "button 1" as it's argument */
60 gtk_signal_connect (GTK_OBJECT (button), "clicked",
61 GTK_SIGNAL_FUNC (callback), (gpointer) "button 1");
63 /* instead of gtk_container_add, we pack this button into the invisible
64 * box, which has been packed into the window. */
65 gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
67 /* always remember this step, this tells GTK that our preparation for
68 * this button is complete, and it can be displayed now. */
69 gtk_widget_show(button);
71 /* do these same steps again to create a second button */
72 button = gtk_button_new_with_label ("Button 2");
74 /* call the same callback function with a different argument,
75 * passing a pointer to "button 2" instead. */
76 gtk_signal_connect (GTK_OBJECT (button), "clicked",
77 GTK_SIGNAL_FUNC (callback), (gpointer) "button 2");
79 gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
81 /* The order in which we show the buttons is not really important, but I
82 * recommend showing the window last, so it all pops up at once. */
83 gtk_widget_show(button);
85 gtk_widget_show(box1);
87 gtk_widget_show (window);
89 /* rest in gtk_main and wait for the fun to begin! */