]> Pileus Git - ~andy/gtk/blob - examples/notebook/notebook.c
gtk: Add gtk_widget_queue_draw_region()
[~andy/gtk] / examples / notebook / notebook.c
1
2 #include <stdio.h>
3 #include <gtk/gtk.h>
4
5 /* This function rotates the position of the tabs */
6 static void rotate_book( GtkButton   *button,
7                          GtkNotebook *notebook )
8 {
9     gtk_notebook_set_tab_pos (notebook, (notebook->tab_pos + 1) % 4);
10 }
11
12 /* Add/Remove the page tabs and the borders */
13 static void tabsborder_book( GtkButton   *button,
14                              GtkNotebook *notebook )
15 {
16     gint tval = FALSE;
17     gint bval = FALSE;
18     if (notebook->show_tabs == 0)
19             tval = TRUE;
20     if (notebook->show_border == 0)
21             bval = TRUE;
22
23     gtk_notebook_set_show_tabs (notebook, tval);
24     gtk_notebook_set_show_border (notebook, bval);
25 }
26
27 /* Remove a page from the notebook */
28 static void remove_book( GtkButton   *button,
29                          GtkNotebook *notebook )
30 {
31     gint page;
32
33     page = gtk_notebook_get_current_page (notebook);
34     gtk_notebook_remove_page (notebook, page);
35     /* Need to refresh the widget --
36      This forces the widget to redraw itself. */
37     gtk_widget_queue_draw (GTK_WIDGET (notebook));
38 }
39
40 static gboolean delete( GtkWidget *widget,
41                         GtkWidget *event,
42                         gpointer   data )
43 {
44     gtk_main_quit ();
45     return FALSE;
46 }
47
48 int main( int argc,
49           char *argv[] )
50 {
51     GtkWidget *window;
52     GtkWidget *button;
53     GtkWidget *table;
54     GtkWidget *notebook;
55     GtkWidget *frame;
56     GtkWidget *label;
57     GtkWidget *checkbutton;
58     int i;
59     char bufferf[32];
60     char bufferl[32];
61
62     gtk_init (&argc, &argv);
63
64     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
65
66     g_signal_connect (window, "delete-event",
67                       G_CALLBACK (delete), NULL);
68
69     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
70
71     table = gtk_table_new (3, 6, FALSE);
72     gtk_container_add (GTK_CONTAINER (window), table);
73
74     /* Create a new notebook, place the position of the tabs */
75     notebook = gtk_notebook_new ();
76     gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
77     gtk_table_attach_defaults (GTK_TABLE (table), notebook, 0, 6, 0, 1);
78     gtk_widget_show (notebook);
79
80     /* Let's append a bunch of pages to the notebook */
81     for (i = 0; i < 5; i++) {
82         sprintf(bufferf, "Append Frame %d", i + 1);
83         sprintf(bufferl, "Page %d", i + 1);
84
85         frame = gtk_frame_new (bufferf);
86         gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
87         gtk_widget_set_size_request (frame, 100, 75);
88         gtk_widget_show (frame);
89
90         label = gtk_label_new (bufferf);
91         gtk_container_add (GTK_CONTAINER (frame), label);
92         gtk_widget_show (label);
93
94         label = gtk_label_new (bufferl);
95         gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label);
96     }
97
98     /* Now let's add a page to a specific spot */
99     checkbutton = gtk_check_button_new_with_label ("Check me please!");
100     gtk_widget_set_size_request (checkbutton, 100, 75);
101     gtk_widget_show (checkbutton);
102
103     label = gtk_label_new ("Add page");
104     gtk_notebook_insert_page (GTK_NOTEBOOK (notebook), checkbutton, label, 2);
105
106     /* Now finally let's prepend pages to the notebook */
107     for (i = 0; i < 5; i++) {
108         sprintf (bufferf, "Prepend Frame %d", i + 1);
109         sprintf (bufferl, "PPage %d", i + 1);
110
111         frame = gtk_frame_new (bufferf);
112         gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
113         gtk_widget_set_size_request (frame, 100, 75);
114         gtk_widget_show (frame);
115
116         label = gtk_label_new (bufferf);
117         gtk_container_add (GTK_CONTAINER (frame), label);
118         gtk_widget_show (label);
119
120         label = gtk_label_new (bufferl);
121         gtk_notebook_prepend_page (GTK_NOTEBOOK (notebook), frame, label);
122     }
123
124     /* Set what page to start at (page 4) */
125     gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 3);
126
127     /* Create a bunch of buttons */
128     button = gtk_button_new_with_label ("close");
129     g_signal_connect_swapped (button, "clicked",
130                               G_CALLBACK (delete), NULL);
131     gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 1, 2);
132     gtk_widget_show (button);
133
134     button = gtk_button_new_with_label ("next page");
135     g_signal_connect_swapped (button, "clicked",
136                               G_CALLBACK (gtk_notebook_next_page),
137                               notebook);
138     gtk_table_attach_defaults (GTK_TABLE (table), button, 1, 2, 1, 2);
139     gtk_widget_show (button);
140
141     button = gtk_button_new_with_label ("prev page");
142     g_signal_connect_swapped (button, "clicked",
143                               G_CALLBACK (gtk_notebook_prev_page),
144                               notebook);
145     gtk_table_attach_defaults (GTK_TABLE (table), button, 2, 3, 1, 2);
146     gtk_widget_show (button);
147
148     button = gtk_button_new_with_label ("tab position");
149     g_signal_connect (button, "clicked",
150                       G_CALLBACK (rotate_book),
151                       notebook);
152     gtk_table_attach_defaults (GTK_TABLE (table), button, 3, 4, 1, 2);
153     gtk_widget_show (button);
154
155     button = gtk_button_new_with_label ("tabs/border on/off");
156     g_signal_connect (button, "clicked",
157                       G_CALLBACK (tabsborder_book),
158                       notebook);
159     gtk_table_attach_defaults (GTK_TABLE (table), button, 4, 5, 1, 2);
160     gtk_widget_show (button);
161
162     button = gtk_button_new_with_label ("remove page");
163     g_signal_connect (button, "clicked",
164                       G_CALLBACK (remove_book),
165                       notebook);
166     gtk_table_attach_defaults (GTK_TABLE (table), button, 5, 6, 1, 2);
167     gtk_widget_show (button);
168
169     gtk_widget_show (table);
170     gtk_widget_show (window);
171
172     gtk_main ();
173
174     return 0;
175 }