1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
10
TITLE="GTK+ 2.0 Tutorial"
11
HREF="book1.html"><LINK
13
TITLE="Container Widgets"
14
HREF="c1228.html"><LINK
17
HREF="x1404.html"><LINK
20
HREF="c1501.html"></HEAD
31
SUMMARY="Header navigation table"
40
>GTK+ 2.0 Tutorial</TH
50
><<< Previous</A
56
>Container Widgets</TD
64
>Next >>></A
80
>The NoteBook Widget is a collection of "pages" that overlap each
81
other, each page contains different information with only one page
82
visible at a time. This widget has become more common lately in GUI
83
programming, and it is a good way to show blocks of similar
84
information that warrant separation in their display.</P
86
>The first function call you will need to know, as you can probably
87
guess by now, is used to create a new notebook widget.</P
95
CLASS="PROGRAMLISTING"
96
>GtkWidget *gtk_notebook_new( void );</PRE
101
>Once the notebook has been created, there are a number of functions
102
that operate on the notebook widget. Let's look at them individually.</P
104
>The first one we will look at is how to position the page indicators.
105
These page indicators or "tabs" as they are referred to, can be
106
positioned in four ways: top, bottom, left, or right.</P
114
CLASS="PROGRAMLISTING"
115
>void gtk_notebook_set_tab_pos( GtkNotebook *notebook,
116
GtkPositionType pos );</PRE
121
>GtkPositionType will be one of the following, which are pretty self
130
CLASS="PROGRAMLISTING"
144
>Next we will look at how to add pages to the notebook. There are three
145
ways to add pages to the NoteBook. Let's look at the first two
146
together as they are quite similar.</P
154
CLASS="PROGRAMLISTING"
155
>void gtk_notebook_append_page( GtkNotebook *notebook,
157
GtkWidget *tab_label );
159
void gtk_notebook_prepend_page( GtkNotebook *notebook,
161
GtkWidget *tab_label );</PRE
166
>These functions add pages to the notebook by inserting them from the
167
back of the notebook (append), or the front of the notebook (prepend).
171
> is the widget that is placed within the notebook page, and
175
> is the label for the page being added. The <TT
179
widget must be created separately, and is typically a set of options
180
setup witin one of the other container widgets, such as a table.</P
182
>The final function for adding a page to the notebook contains all of
183
the properties of the previous two, but it allows you to specify what
184
position you want the page to be in the notebook.</P
192
CLASS="PROGRAMLISTING"
193
>void gtk_notebook_insert_page( GtkNotebook *notebook,
195
GtkWidget *tab_label,
196
gint position );</PRE
201
>The parameters are the same as _append_ and _prepend_ except it
202
contains an extra parameter, <TT
205
>. This parameter is used to
206
specify what place this page will be inserted into the first page
207
having position zero.</P
209
>Now that we know how to add a page, lets see how we can remove a page
210
from the notebook.</P
218
CLASS="PROGRAMLISTING"
219
>void gtk_notebook_remove_page( GtkNotebook *notebook,
220
gint page_num );</PRE
225
>This function takes the page specified by <TT
229
from the widget pointed to by <TT
234
>To find out what the current page is in a notebook use the function:</P
242
CLASS="PROGRAMLISTING"
243
>gint gtk_notebook_get_current_page( GtkNotebook *notebook );</PRE
248
>These next two functions are simple calls to move the notebook page
249
forward or backward. Simply provide the respective function call with
250
the notebook widget you wish to operate on. Note: When the NoteBook is
251
currently on the last page, and gtk_notebook_next_page() is called, the
252
notebook will wrap back to the first page. Likewise, if the NoteBook
253
is on the first page, and gtk_notebook_prev_page() is called, the
254
notebook will wrap to the last page.</P
262
CLASS="PROGRAMLISTING"
263
>void gtk_notebook_next_page( GtkNoteBook *notebook );
265
void gtk_notebook_prev_page( GtkNoteBook *notebook );</PRE
270
>This next function sets the "active" page. If you wish the notebook to
271
be opened to page 5 for example, you would use this function. Without
272
using this function, the notebook defaults to the first page.</P
280
CLASS="PROGRAMLISTING"
281
>void gtk_notebook_set_current_page( GtkNotebook *notebook,
282
gint page_num );</PRE
287
>The next two functions add or remove the notebook page tabs and the
288
notebook border respectively.</P
296
CLASS="PROGRAMLISTING"
297
>void gtk_notebook_set_show_tabs( GtkNotebook *notebook,
298
gboolean show_tabs );
300
void gtk_notebook_set_show_border( GtkNotebook *notebook,
301
gboolean show_border );</PRE
306
>The next function is useful when the you have a large number of pages,
307
and the tabs don't fit on the page. It allows the tabs to be scrolled
308
through using two arrow buttons.</P
316
CLASS="PROGRAMLISTING"
317
>void gtk_notebook_set_scrollable( GtkNotebook *notebook,
318
gboolean scrollable );</PRE
335
>Now let's look at an example, it is expanded from the
340
that comes with the GTK distribution. This small program creates a
341
window with a notebook and six buttons. The notebook contains 11
342
pages, added in three different ways, appended, inserted, and
343
prepended. The buttons allow you rotate the tab positions, add/remove
344
the tabs and border, remove a page, change pages in both a forward and
345
backward manner, and exit the program.</P
348
CLASS="INLINEMEDIAOBJECT"
350
SRC="images/notebook.png"></SPAN
359
CLASS="PROGRAMLISTING"
360
> #include <stdio.h>
361
#include <gtk/gtk.h>
363
/* This function rotates the position of the tabs */
364
static void rotate_book( GtkButton *button,
365
GtkNotebook *notebook )
367
gtk_notebook_set_tab_pos (notebook, (notebook->tab_pos + 1) % 4);
370
/* Add/Remove the page tabs and the borders */
371
static void tabsborder_book( GtkButton *button,
372
GtkNotebook *notebook )
376
if (notebook->show_tabs == 0)
378
if (notebook->show_border == 0)
381
gtk_notebook_set_show_tabs (notebook, tval);
382
gtk_notebook_set_show_border (notebook, bval);
385
/* Remove a page from the notebook */
386
static void remove_book( GtkButton *button,
387
GtkNotebook *notebook )
391
page = gtk_notebook_get_current_page (notebook);
392
gtk_notebook_remove_page (notebook, page);
393
/* Need to refresh the widget --
394
This forces the widget to redraw itself. */
395
gtk_widget_queue_draw (GTK_WIDGET (notebook));
398
static gboolean delete( GtkWidget *widget,
415
GtkWidget *checkbutton;
420
gtk_init (&argc, &argv);
422
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
424
g_signal_connect (G_OBJECT (window), "delete_event",
425
G_CALLBACK (delete), NULL);
427
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
429
table = gtk_table_new (3, 6, FALSE);
430
gtk_container_add (GTK_CONTAINER (window), table);
432
/* Create a new notebook, place the position of the tabs */
433
notebook = gtk_notebook_new ();
434
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
435
gtk_table_attach_defaults (GTK_TABLE (table), notebook, 0, 6, 0, 1);
436
gtk_widget_show (notebook);
438
/* Let's append a bunch of pages to the notebook */
439
for (i = 0; i < 5; i++) {
440
sprintf(bufferf, "Append Frame %d", i + 1);
441
sprintf(bufferl, "Page %d", i + 1);
443
frame = gtk_frame_new (bufferf);
444
gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
445
gtk_widget_set_size_request (frame, 100, 75);
446
gtk_widget_show (frame);
448
label = gtk_label_new (bufferf);
449
gtk_container_add (GTK_CONTAINER (frame), label);
450
gtk_widget_show (label);
452
label = gtk_label_new (bufferl);
453
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label);
456
/* Now let's add a page to a specific spot */
457
checkbutton = gtk_check_button_new_with_label ("Check me please!");
458
gtk_widget_set_size_request (checkbutton, 100, 75);
459
gtk_widget_show (checkbutton);
461
label = gtk_label_new ("Add page");
462
gtk_notebook_insert_page (GTK_NOTEBOOK (notebook), checkbutton, label, 2);
464
/* Now finally let's prepend pages to the notebook */
465
for (i = 0; i < 5; i++) {
466
sprintf (bufferf, "Prepend Frame %d", i + 1);
467
sprintf (bufferl, "PPage %d", i + 1);
469
frame = gtk_frame_new (bufferf);
470
gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
471
gtk_widget_set_size_request (frame, 100, 75);
472
gtk_widget_show (frame);
474
label = gtk_label_new (bufferf);
475
gtk_container_add (GTK_CONTAINER (frame), label);
476
gtk_widget_show (label);
478
label = gtk_label_new (bufferl);
479
gtk_notebook_prepend_page (GTK_NOTEBOOK (notebook), frame, label);
482
/* Set what page to start at (page 4) */
483
gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 3);
485
/* Create a bunch of buttons */
486
button = gtk_button_new_with_label ("close");
487
g_signal_connect_swapped (G_OBJECT (button), "clicked",
488
G_CALLBACK (delete), NULL);
489
gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 1, 2);
490
gtk_widget_show (button);
492
button = gtk_button_new_with_label ("next page");
493
g_signal_connect_swapped (G_OBJECT (button), "clicked",
494
G_CALLBACK (gtk_notebook_next_page),
495
G_OBJECT (notebook));
496
gtk_table_attach_defaults (GTK_TABLE (table), button, 1, 2, 1, 2);
497
gtk_widget_show (button);
499
button = gtk_button_new_with_label ("prev page");
500
g_signal_connect_swapped (G_OBJECT (button), "clicked",
501
G_CALLBACK (gtk_notebook_prev_page),
502
G_OBJECT (notebook));
503
gtk_table_attach_defaults (GTK_TABLE (table), button, 2, 3, 1, 2);
504
gtk_widget_show (button);
506
button = gtk_button_new_with_label ("tab position");
507
g_signal_connect (G_OBJECT (button), "clicked",
508
G_CALLBACK (rotate_book),
509
(gpointer) notebook);
510
gtk_table_attach_defaults (GTK_TABLE (table), button, 3, 4, 1, 2);
511
gtk_widget_show (button);
513
button = gtk_button_new_with_label ("tabs/border on/off");
514
g_signal_connect (G_OBJECT (button), "clicked",
515
G_CALLBACK (tabsborder_book),
516
(gpointer) notebook);
517
gtk_table_attach_defaults (GTK_TABLE (table), button, 4, 5, 1, 2);
518
gtk_widget_show (button);
520
button = gtk_button_new_with_label ("remove page");
521
g_signal_connect (G_OBJECT (button), "clicked",
522
G_CALLBACK (remove_book),
523
(gpointer) notebook);
524
gtk_table_attach_defaults (GTK_TABLE (table), button, 5, 6, 1, 2);
525
gtk_widget_show (button);
527
gtk_widget_show (table);
528
gtk_widget_show (window);
538
>I hope this helps you on your way with creating notebooks for your
546
SUMMARY="Footer navigation table"
559
><<< Previous</A
577
>Next >>></A
b'\\ No newline at end of file'