]> Pileus Git - ~andy/gtk/blob - examples/scrolledwin/scrolledwin.c
Unset CATOBJEXT so that the macros and Makefiles correctly handle
[~andy/gtk] / examples / scrolledwin / scrolledwin.c
1 /* example-start scrolledwin scrolledwin.c */
2
3 #include <gtk/gtk.h>
4
5 void destroy(GtkWidget *widget, gpointer data)
6 {
7     gtk_main_quit();
8 }
9
10 int main (int argc, char *argv[])
11 {
12     static GtkWidget *window;
13     GtkWidget *scrolled_window;
14     GtkWidget *table;
15     GtkWidget *button;
16     char buffer[32];
17     int i, j;
18     
19     gtk_init (&argc, &argv);
20     
21     /* Create a new dialog window for the scrolled window to be
22      * packed into. A dialog is just like a normal window except it has a 
23      * vbox and a horizontal separator packed into it. It's just a shortcut
24      * for creating dialogs */
25     window = gtk_dialog_new ();
26     gtk_signal_connect (GTK_OBJECT (window), "destroy",
27                         (GtkSignalFunc) destroy, NULL);
28     gtk_window_set_title (GTK_WINDOW (window), "GtkScrolledWindow example");
29     gtk_container_border_width (GTK_CONTAINER (window), 0);
30     gtk_widget_set_usize(window, 300, 300);
31     
32     /* create a new scrolled window. */
33     scrolled_window = gtk_scrolled_window_new (NULL, NULL);
34     
35     gtk_container_border_width (GTK_CONTAINER (scrolled_window), 10);
36     
37     /* the policy is one of GTK_POLICY AUTOMATIC, or GTK_POLICY_ALWAYS.
38      * GTK_POLICY_AUTOMATIC will automatically decide whether you need
39      * scrollbars, whereas GTK_POLICY_ALWAYS will always leave the scrollbars
40      * there.  The first one is the horizontal scrollbar, the second, 
41      * the vertical. */
42     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
43                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
44     /* The dialog window is created with a vbox packed into it. */                                                              
45     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, 
46                         TRUE, TRUE, 0);
47     gtk_widget_show (scrolled_window);
48     
49     /* create a table of 10 by 10 squares. */
50     table = gtk_table_new (10, 10, FALSE);
51     
52     /* set the spacing to 10 on x and 10 on y */
53     gtk_table_set_row_spacings (GTK_TABLE (table), 10);
54     gtk_table_set_col_spacings (GTK_TABLE (table), 10);
55     
56     /* pack the table into the scrolled window */
57     gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),
58                                            table);
59     gtk_widget_show (table);
60     
61     /* this simply creates a grid of toggle buttons on the table
62      * to demonstrate the scrolled window. */
63     for (i = 0; i < 10; i++)
64        for (j = 0; j < 10; j++) {
65           sprintf (buffer, "button (%d,%d)\n", i, j);
66           button = gtk_toggle_button_new_with_label (buffer);
67           gtk_table_attach_defaults (GTK_TABLE (table), button,
68                                      i, i+1, j, j+1);
69           gtk_widget_show (button);
70        }
71     
72     /* Add a "close" button to the bottom of the dialog */
73     button = gtk_button_new_with_label ("close");
74     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
75                                (GtkSignalFunc) gtk_widget_destroy,
76                                GTK_OBJECT (window));
77     
78     /* this makes it so the button is the default. */
79     
80     GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
81     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area), button, TRUE, TRUE, 0);
82     
83     /* This grabs this button to be the default button. Simply hitting
84      * the "Enter" key will cause this button to activate. */
85     gtk_widget_grab_default (button);
86     gtk_widget_show (button);
87     
88     gtk_widget_show (window);
89     
90     gtk_main();
91     
92     return(0);
93 }
94 /* example-end */