📄 gtk_tut-10.html
字号:
they all stay in active state, preventing us from switching between them).<P><BLOCKQUOTE><CODE><PRE> /* here we have just a simple toggle button */ iconw = gtk_pixmap_new ( icon, mask ); tooltips_button = gtk_toolbar_append_element(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_CHILD_TOGGLEBUTTON, NULL, "Tooltips", "Toolbar with or without tips", "Private", iconw, GTK_SIGNAL_FUNC (toggle_event), toolbar); gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) ); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tooltips_button),TRUE);</PRE></CODE></BLOCKQUOTE><P>A toggle button can be created in the obvious way (if one knows how to createradio buttons already).<P><BLOCKQUOTE><CODE><PRE> /* to pack a widget into toolbar, we only have to * create it and append it with an appropriate tooltip */ entry = gtk_entry_new (); gtk_toolbar_append_widget( GTK_TOOLBAR (toolbar), entry, "This is just an entry", "Private" ); /* well, it isn't created within thetoolbar, so we must still show it */ gtk_widget_show ( entry );</PRE></CODE></BLOCKQUOTE><P>As you see, adding any kind of widget to a toolbar is simple. Theone thing you have to remember is that this widget must be shown manually(contrary to other items which will be shown together with the toolbar).<P><BLOCKQUOTE><CODE><PRE> /* that's it ! let's show everything. */ gtk_widget_show ( toolbar ); gtk_widget_show (handlebox); gtk_widget_show ( dialog ); /* rest in gtk_main and wait for the fun to begin! */ gtk_main (); return 0;}</PRE></CODE></BLOCKQUOTE><P>So, here we are at the end of toolbar tutorial. Of course, to appreciateit in full you need also this nice XPM icon, so here it is:<P><BLOCKQUOTE><CODE><PRE>/* XPM */static char * gtk_xpm[] = {"32 39 5 1",". c none","+ c black","@ c #3070E0","# c #F05050","$ c #35E035","................+...............","..............+++++.............","............+++++@@++...........","..........+++++@@@@@@++.........","........++++@@@@@@@@@@++........","......++++@@++++++++@@@++.......",".....+++@@@+++++++++++@@@++.....","...+++@@@@+++@@@@@@++++@@@@+....","..+++@@@@+++@@@@@@@@+++@@@@@++..",".++@@@@@@+++@@@@@@@@@@@@@@@@@@++",".+#+@@@@@@++@@@@+++@@@@@@@@@@@@+",".+##++@@@@+++@@@+++++@@@@@@@@$@.",".+###++@@@@+++@@@+++@@@@@++$$$@.",".+####+++@@@+++++++@@@@@+@$$$$@.",".+#####+++@@@@+++@@@@++@$$$$$$+.",".+######++++@@@@@@@++@$$$$$$$$+.",".+#######+##+@@@@+++$$$$$$@@$$+.",".+###+++##+##+@@++@$$$$$$++$$$+.",".+###++++##+##+@@$$$$$$$@+@$$@+.",".+###++++++#+++@$$@+@$$@++$$$@+.",".+####+++++++#++$$@+@$$++$$$$+..",".++####++++++#++$$@+@$++@$$$$+..",".+#####+++++##++$$++@+++$$$$$+..",".++####+++##+#++$$+++++@$$$$$+..",".++####+++####++$$++++++@$$$@+..",".+#####++#####++$$+++@++++@$@+..",".+#####++#####++$$++@$$@+++$@@..",".++####++#####++$$++$$$$$+@$@++.",".++####++#####++$$++$$$$$$$$+++.",".+++####+#####++$$++$$$$$$$@+++.","..+++#########+@$$+@$$$$$$+++...","...+++########+@$$$$$$$$@+++....",".....+++######+@$$$$$$$+++......","......+++#####+@$$$$$@++........",".......+++####+@$$$$+++.........",".........++###+$$$@++...........","..........++##+$@+++............","...........+++++++..............",".............++++..............."};</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss10.12">10.12 Notebooks</A></H2><P>The NoteBook Widget is a collection of 'pages' that overlap eachother, each page contains different information. This widget hasbecome more common lately in GUI programming, and it is a good way toshow blocks of similar information that warrant separation in theirdisplay.<P>The first function call you will need to know, as you can probablyguess by now, is used to create a new notebook widget.<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_notebook_new( void );</PRE></CODE></BLOCKQUOTE><P>Once the notebook has been created, there are a number of functionsthat operate on the notebook widget. Let's look at them individually.<P>The first one we will look at is how to position the page indicators.These page indicators or 'tabs' as they are referred to, can bepositioned in four ways: top, bottom, left, or right.<P><BLOCKQUOTE><CODE><PRE>void gtk_notebook_set_tab_pos( GtkNotebook *notebook, GtkPositionType pos );</PRE></CODE></BLOCKQUOTE><P>GtkPostionType will be one of the following, which are pretty selfexplanatory:<UL><LI> GTK_POS_LEFT</LI><LI> GTK_POS_RIGHT</LI><LI> GTK_POS_TOP</LI><LI> GTK_POS_BOTTOM</LI></UL><P>GTK_POS_TOP is the default.<P>Next we will look at how to add pages to the notebook. There are threeways to add pages to the NoteBook. Let's look at the first twotogether as they are quite similar.<P><BLOCKQUOTE><CODE><PRE>void gtk_notebook_append_page( GtkNotebook *notebook, GtkWidget *child, GtkWidget *tab_label );void gtk_notebook_prepend_page( GtkNotebook *notebook, GtkWidget *child, GtkWidget *tab_label );</PRE></CODE></BLOCKQUOTE><P>These functions add pages to the notebook by inserting them from theback of the notebook (append), or the front of the notebook (prepend).<CODE>child</CODE> is the widget that is placed within the notebook page, and<CODE>tab_label</CODE> is the label for the page being added. The <CODE>child</CODE>widget must be created separately, and is typically a set of optionssetout witin one of the other container widgets, such as a table.<P>The final function for adding a page to the notebook contains all ofthe properties of the previous two, but it allows you to specify whatposition you want the page to be in the notebook.<P><BLOCKQUOTE><CODE><PRE>void gtk_notebook_insert_page( GtkNotebook *notebook, GtkWidget *child, GtkWidget *tab_label, gint position );</PRE></CODE></BLOCKQUOTE><P>The parameters are the same as _append_ and _prepend_ except itcontains an extra parameter, <CODE>position</CODE>. This parameter is used tospecify what place this page will be inserted into.<P>Now that we know how to add a page, lets see how we can remove a pagefrom the notebook.<P><BLOCKQUOTE><CODE><PRE>void gtk_notebook_remove_page( GtkNotebook *notebook, gint page_num );</PRE></CODE></BLOCKQUOTE><P>This function takes the page specified by <CODE>page_num</CODE> and removes itfrom the widget pointed to by <CODE>notebook</CODE>.<P>To find out what the current page is in a notebook use the function:<P><BLOCKQUOTE><CODE><PRE>gint gtk_notebook_get_current_page( GtkNotebook *notebook );</PRE></CODE></BLOCKQUOTE><P>These next two functions are simple calls to move the notebook pageforward or backward. Simply provide the respective function call withthe notebook widget you wish to operate on. Note: when the NoteBook iscurrently on the last page, and gtk_notebook_next_page is called, thenotebook will wrap back to the first page. Likewise, if the NoteBookis on the first page, and gtk_notebook_prev_page is called, thenotebook will wrap to the last page.<P><BLOCKQUOTE><CODE><PRE>void gtk_notebook_next_page( GtkNoteBook *notebook );void gtk_notebook_prev_page( GtkNoteBook *notebook );</PRE></CODE></BLOCKQUOTE><P>This next function sets the 'active' page. If you wish the notebook tobe opened to page 5 for example, you would use this function. Withoutusing this function, the notebook defaults to the first page.<P><BLOCKQUOTE><CODE><PRE>void gtk_notebook_set_page( GtkNotebook *notebook, gint page_num );</PRE></CODE></BLOCKQUOTE><P>The next two functions add or remove the notebook page tabs and thenotebook border respectively.<P><BLOCKQUOTE><CODE><PRE>void gtk_notebook_set_show_tabs( GtkNotebook *notebook, gboolean show_tabs);void gtk_notebook_set_show_border( GtkNotebook *notebook, gboolean show_border );</PRE></CODE></BLOCKQUOTE><P>The next function is useful when the you have a large number of pages,and the tabs don't fit on the page. It allows the tabs to be scrolledthrough using two arrow buttons.<P><BLOCKQUOTE><CODE><PRE>void gtk_notebook_set_scrollable( GtkNotebook *notebook, gboolean scrollable );</PRE></CODE></BLOCKQUOTE><P><CODE>show_tabs</CODE>, <CODE>show_border</CODE> and <CODE>scrollable</CODE> can be eitherTRUE or FALSE.<P>Now lets look at an example, it is expanded from the testgtk.c codethat comes with the GTK distribution. This small program creates awindow with a notebook and six buttons. The notebook contains 11pages, added in three different ways, appended, inserted, andprepended. The buttons allow you rotate the tab positions, add/removethe tabs and border, remove a page, change pages in both a forward andbackward manner, and exit the program.<P><BLOCKQUOTE><CODE><PRE>/* example-start notebook notebook.c */#include <gtk/gtk.h>/* This function rotates the position of the tabs */void rotate_book (GtkButton *button, GtkNotebook *notebook){ gtk_notebook_set_tab_pos (notebook, (notebook->tab_pos +1) %4);}/* Add/Remove the page tabs and the borders */void tabsborder_book (GtkButton *button, GtkNotebook *notebook){ gint tval = FALSE; gint bval = FALSE; if (notebook->show_tabs == 0) tval = TRUE; if (notebook->show_border == 0) bval = TRUE; gtk_notebook_set_show_tabs (notebook, tval); gtk_notebook_set_show_border (notebook, bval);}/* Remove a page from the notebook */void remove_book (GtkButton *button, GtkNotebook *notebook){ gint page; page = gtk_notebook_get_current_page(notebook); gtk_notebook_remove_page (notebook, page); /* Need to refresh the widget -- This forces the widget to redraw itself. */ gtk_widget_draw(GTK_WIDGET(notebook), NULL);}void delete (GtkWidget *widget, GtkWidget *event, gpointer data){ gtk_main_quit ();}int main (int argc, char *argv[]){ GtkWidget *window; GtkWidget *button; GtkWidget *table; GtkWidget *notebook; GtkWidget *frame; GtkWidget *label; GtkWidget *checkbutton; int i; char bufferf[32]; char bufferl[32]; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_signal_connect (GTK_OBJECT (window), "delete_event", GTK_SIGNAL_FUNC (delete), NULL); gtk_container_set_border_width (GTK_CONTAINER (window), 10); table = gtk_table_new(3,6,FALSE); gtk_container_add (GTK_CONTAINER (window), table); /* Create a new notebook, place the position of the tabs */ notebook = gtk_notebook_new (); gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP); gtk_table_attach_defaults(GTK_TABLE(table), notebook, 0,6,0,1); gtk_widget_show(notebook); /* Lets append a bunch of pages to the notebook */ for (i=0; i < 5; i++) { sprintf(bufferf, "Append Frame %d", i+1); sprintf(bufferl, "Page %d", i+1); frame = gtk_frame_new (bufferf); gtk_container_set_border_width (GTK_CONTAINER (frame), 10); gtk_widget_set_usize (frame, 100, 75); gtk_widget_show (frame); label = gtk_label_new (bufferf); gtk_container_add (GTK_CONTAINER (frame), label); gtk_widget_show (label); label = gtk_label_new (bufferl); gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label); } /* Now lets add a page to a specific spot */ checkbutton = gtk_check_button_new_with_label ("Check me please!"); gtk_widget_set_usize(checkbutton, 100, 75); gtk_widget_show (checkbutton); label = gtk_label_new ("Add page"); gtk_notebook_insert_page (GTK_NOTEBOOK (notebook), checkbutton, label, 2); /* Now finally lets prepend pages to the notebook */ for (i=0; i < 5; i++) { sprintf(bufferf, "Prepend Frame %d", i+1); sprintf(bufferl, "PPage %d", i+1); frame = gtk_frame_new (bufferf); gtk_container_set_border_width (GTK_CONTAINER (frame), 10); gtk_widget_set_usize (frame, 100, 75); gtk_widget_show (frame); label = gtk_label_new (bufferf); gtk_container_add (GTK_CONTAINER (frame), label); gtk_widget_show (label); label = gtk
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -