]> Pileus Git - ~andy/gtk/blob - examples/helloworld2/helloworld2.c
- new section on Events - change all delete_event callbacks to include a
[~andy/gtk] / examples / helloworld2 / helloworld2.c
1 /* This file extracted from the GTK tutorial. */
2
3 /* helloworld2.c */
4
5 #include <gtk/gtk.h>
6    
7 /* Our new improved callback.  The data passed to this function is printed
8  * to stdout. */
9 void callback (GtkWidget *widget, gpointer *data)
10 {  
11     g_print ("Hello again - %s was pressed\n", (char *) data);
12 }
13      
14 /* another callback */
15 void delete_event (GtkWidget *widget, GdkEvent *event, gpointer *data)
16 {    
17     gtk_main_quit ();
18 }
19
20 int main (int argc, char *argv[])
21 {
22     /* GtkWidget is the storage type for widgets */
23     GtkWidget *window;
24     GtkWidget *button;
25     GtkWidget *box1;
26
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);
30
31     /* create a new window */
32     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
33
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!");
37
38     /* Here we just set a handler for delete_event that immediately
39      * exits GTK. */
40     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
41                         GTK_SIGNAL_FUNC (delete_event), NULL);
42
43     
44     /* sets the border width of the window. */
45     gtk_container_border_width (GTK_CONTAINER (window), 10);
46
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);
51
52     /* put the box into the main window. */
53     gtk_container_add (GTK_CONTAINER (window), box1);
54
55     /* creates a new button with the label "Button 1". */
56     button = gtk_button_new_with_label ("Button 1");
57     
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");
62                         
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);
66     
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);
70      
71     /* do these same steps again to create a second button */
72     button = gtk_button_new_with_label ("Button 2");
73     
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");
78     
79     gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
80      
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);
84
85     gtk_widget_show(box1);
86
87     gtk_widget_show (window);
88
89     /* rest in gtk_main and wait for the fun to begin! */
90     gtk_main ();
91      
92     return 0;
93 }
94