Initial version of CrossReport program.
[fedora-mingw.git] / gtk2 / test2.c
1 /* Two-button dialog from
2  * http://library.gnome.org/devel/gtk-tutorial/stable/x345.html
3  */
4
5 #include <gtk/gtk.h>
6
7 /* Our new improved callback.  The data passed to this function
8  * is printed to stdout. */
9 static void callback( GtkWidget *widget,
10                       gpointer   data )
11 {
12     g_print ("Hello again - %s was pressed\n", (gchar *) data);
13 }
14
15 /* another callback */
16 static gboolean delete_event( GtkWidget *widget,
17                               GdkEvent  *event,
18                               gpointer   data )
19 {
20     gtk_main_quit ();
21     return FALSE;
22 }
23
24 int main( int   argc,
25           char *argv[] )
26 {
27     /* GtkWidget is the storage type for widgets */
28     GtkWidget *window;
29     GtkWidget *button;
30     GtkWidget *box1;
31
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);
35
36     /* Create a new window */
37     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
38
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!");
42
43     /* Here we just set a handler for delete_event that immediately
44      * exits GTK. */
45     g_signal_connect (G_OBJECT (window), "delete_event",
46                       G_CALLBACK (delete_event), NULL);
47
48     /* Sets the border width of the window. */
49     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
50
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);
55
56     /* Put the box into the main window. */
57     gtk_container_add (GTK_CONTAINER (window), box1);
58
59     /* Creates a new button with the label "Button 1". */
60     button = gtk_button_new_with_label ("Button 1");
61     
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");
66
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);
70
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);
74
75     /* Do these same steps again to create a second button */
76     button = gtk_button_new_with_label ("Button 2");
77
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");
82
83     gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
84
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);
88
89     gtk_widget_show (box1);
90
91     gtk_widget_show (window);
92     
93     /* Rest in gtk_main and wait for the fun to begin! */
94     gtk_main ();
95
96     return 0;
97 }