⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtk_tut-10.html

📁 GTK development guide
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</CODE></BLOCKQUOTE>
<P>Here we begin creating a radio buttons group. To do this we use
gtk_toolbar_append_element.  In fact, using this function one can also
+add simple items or even spaces (type = <CODE>GTK_TOOLBAR_CHILD_SPACE</CODE>
or +<CODE>GTK_TOOLBAR_CHILD_BUTTON</CODE>). In the above case we start
creating a radio group. In creating other radio buttons for this group
a pointer to the previous button in the group is required, so that a
list of buttons can be easily constructed (see the section on 
<A HREF="gtk_tut-6.html#sec_Radio_Buttons">Radio Buttons</A> earlier in this
tutorial).
<P>
<BLOCKQUOTE><CODE>
<PRE>
  /* following radio buttons refer to previous ones */
  iconw = gtk_pixmap_new ( icon, mask );
  text_button = 
    gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
                               GTK_TOOLBAR_CHILD_RADIOBUTTON,
                               icon_button,
                               "Text",
                               "Only texts in toolbar",
                               "Private",
                               iconw,
                               GTK_SIGNAL_FUNC (radio_event),
                               toolbar);
  gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );
                                          
  iconw = gtk_pixmap_new ( icon, mask );
  both_button = 
    gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
                               GTK_TOOLBAR_CHILD_RADIOBUTTON,
                               text_button,
                               "Both",
                               "Icons and text in toolbar",
                               "Private",
                               iconw,
                               GTK_SIGNAL_FUNC (radio_event),
                               toolbar);
  gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(both_button),TRUE);
</PRE>
</CODE></BLOCKQUOTE>
<P>In the end we have to set the state of one of the buttons manually
(otherwise 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 create
radio 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. The
one 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 appreciate
it 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 each
other, each page contains different information with only one page
visible at a time. This widget has become more common lately in GUI
programming, and it is a good way to show blocks of similar
information that warrant separation in their display.
<P>The first function call you will need to know, as you can probably
guess 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 functions
that 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 be
positioned 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>GtkPositionType will be one of the following, which are pretty self
explanatory:
<BLOCKQUOTE><CODE>
<PRE>
  GTK_POS_LEFT
  GTK_POS_RIGHT
  GTK_POS_TOP
  GTK_POS_BOTTOM
</PRE>
</CODE></BLOCKQUOTE>
<P><CODE>GTK_POS_TOP</CODE> is the default.
<P>Next we will look at how to add pages to the notebook. There are three
ways to add pages to the NoteBook. Let's look at the first two
together 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 the
back 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 options
setup witin one of the other container widgets, such as a table.
<P>The final function for adding a page to the notebook contains all of
the properties of the previous two, but it allows you to specify what
position 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 it
contains an extra parameter, <CODE>position</CODE>.  This parameter is used to
specify what place this page will be inserted into the first page
having position zero.
<P>Now that we know how to add a page, lets see how we can remove a page
from 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 it
from 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 page
forward or backward. Simply provide the respective function call with
the notebook widget you wish to operate on. Note: When the NoteBook is
currently on the last page, and gtk_notebook_next_page is called, the
notebook will wrap back to the first page. Likewise, if the NoteBook
is on the first page, and gtk_notebook_prev_page is called, the
notebook 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 to
be opened to page 5 for example, you would use this function.  Without
using 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 the
notebook 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 scrolled
through 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 either
TRUE or FALSE.
<P>Now let's look at an example, it is expanded from the testgtk.c code
that comes with the GTK distribution. This small program creates a
window with a notebook and six buttons. The notebook contains 11
pages, added in three different ways, appended, inserted, and
prepended. The buttons allow you rotate the tab positions, add/remove
the tabs and border, remove a page, change pages in both a forward and
backward manner, and exit the program.
<P>
<BLOCKQUOTE><CODE>
<PRE>
/* example-start notebook notebook.c */

#include &lt;stdio.h>
#include &lt;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);
}

gint delete( GtkWidget *widget,
             GtkWidget *event,
             gpointer   data )
{
    gtk_main_quit();
    return(FALSE);
}

int main( int argc,
          char *argv[] )
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *table;
    GtkWidg

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -