📄 gtk_tut-10.html
字号:
gtk_container_set_border_width (GTK_CONTAINER (hbox), 10); gtk_container_add (GTK_CONTAINER (frame_vert), hbox); gtk_box_pack_start (GTK_BOX (hbox), create_bbox (FALSE, "Spread (spacing 5)", 5, 85, 20, GTK_BUTTONBOX_SPREAD), TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (hbox), create_bbox (FALSE, "Edge (spacing 30)", 30, 85, 20, GTK_BUTTONBOX_EDGE), TRUE, TRUE, 5); gtk_box_pack_start (GTK_BOX (hbox), create_bbox (FALSE, "Start (spacing 20)", 20, 85, 20, GTK_BUTTONBOX_START), TRUE, TRUE, 5); gtk_box_pack_start (GTK_BOX (hbox), create_bbox (FALSE, "End (spacing 20)", 20, 85, 20, GTK_BUTTONBOX_END), TRUE, TRUE, 5); gtk_widget_show_all (window); /* Enter the event loop */ gtk_main (); return(0);}/* example-end */</PRE></CODE></BLOCKQUOTE><P> <H2><A NAME="ss10.11">10.11 Toolbar</A></H2><P>Toolbars are usually used to group some number of widgets in order tosimplify customization of their look and layout. Typically a toolbarconsists of buttons with icons, labels and tooltips, but any otherwidget can also be put inside a toolbar. Finally, items can bearranged horizontally or vertically and buttons can be displayed withicons, labels or both.<P>Creating a toolbar is (as one may already suspect) done with thefollowing function:<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_toolbar_new( GtkOrientation orientation, GtkToolbarStyle style );</PRE></CODE></BLOCKQUOTE><P>where orientation may be one of:<P><BLOCKQUOTE><CODE><PRE> GTK_ORIENTATION_HORIZONTAL GTK_ORIENTATION_VERTICAL</PRE></CODE></BLOCKQUOTE><P>and style one of:<P><BLOCKQUOTE><CODE><PRE> GTK_TOOLBAR_TEXT GTK_TOOLBAR_ICONS GTK_TOOLBAR_BOTH</PRE></CODE></BLOCKQUOTE><P>The style applies to all the buttons created with the `item' functions(not to buttons inserted into toolbar as separate widgets).<P>After creating a toolbar one can append, prepend and insert items(that means simple buttons) into the toolbar. To describe an item weneed a label text, a tooltip text, a private tooltip text, an icon forthe button and a callback function for it. For example, to append orprepend an item you may use the following functions:<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_toolbar_append_item( GtkToolbar *toolbar, const char *text, const char *tooltip_text, const char *tooltip_private_text, GtkWidget *icon, GtkSignalFunc callback, gpointer user_data );GtkWidget *gtk_toolbar_prepend_item( GtkToolbar *toolbar, const char *text, const char *tooltip_text, const char *tooltip_private_text, GtkWidget *icon, GtkSignalFunc callback, gpointer user_data );</PRE></CODE></BLOCKQUOTE><P>If you want to use gtk_toolbar_insert_item, the only additionalparameter which must be specified is the position in which the itemshould be inserted, thus:<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_toolbar_insert_item( GtkToolbar *toolbar, const char *text, const char *tooltip_text, const char *tooltip_private_text, GtkWidget *icon, GtkSignalFunc callback, gpointer user_data, gint position );</PRE></CODE></BLOCKQUOTE><P>To simplify adding spaces between toolbar items, you may use thefollowing functions:<P><BLOCKQUOTE><CODE><PRE>void gtk_toolbar_append_space( GtkToolbar *toolbar );void gtk_toolbar_prepend_space( GtkToolbar *toolbar );void gtk_toolbar_insert_space( GtkToolbar *toolbar, gint position ); </PRE></CODE></BLOCKQUOTE><P>While the size of the added space can be set globally for awhole toolbar with the function:<P><BLOCKQUOTE><CODE><PRE>void gtk_toolbar_set_space_size( GtkToolbar *toolbar, gint space_size) ;</PRE></CODE></BLOCKQUOTE><P>If it's required, the orientation of a toolbar and its style can bechanged `on the fly' using the following functions:<P><BLOCKQUOTE><CODE><PRE>void gtk_toolbar_set_orientation( GtkToolbar *toolbar, GtkOrientation orientation );void gtk_toolbar_set_style( GtkToolbar *toolbar, GtkToolbarStyle style );void gtk_toolbar_set_tooltips( GtkToolbar *toolbar, gint enable );</PRE></CODE></BLOCKQUOTE><P>Where <CODE>orientation</CODE> is one of GTK_ORIENTATION_HORIZONTAL orGTK_ORIENTATION_VERTICAL. The <CODE>style</CODE> is used to set appearance ofthe toolbar items by using one of GTK_TOOLBAR_ICONS, GTK_TOOLBAR_TEXTor GTK_TOOLBAR_BOTH.<P>To show some other things that can be done with a toolbar, let's takethe following program (we'll interrupt the listing with someadditional explanations):<P><BLOCKQUOTE><CODE><PRE>#include <gtk/gtk.h>#include "gtk.xpm"/* This function is connected to the Close button or * closing the window from the WM */void delete_event (GtkWidget *widget, GdkEvent *event, gpointer data){ gtk_main_quit ();}</PRE></CODE></BLOCKQUOTE><P>The above beginning seems for sure familiar to you if it's not your firstGTK program. There is one additional thing though, we include a nice XPMpicture to serve as an icon for all of the buttons.<P><BLOCKQUOTE><CODE><PRE>GtkWidget* close_button; /* This button will emit signal to close * application */GtkWidget* tooltips_button; /* to enable/disable tooltips */GtkWidget* text_button, * icon_button, * both_button; /* radio buttons for toolbar style */GtkWidget* entry; /* a text entry to show packing any widget into * toolbar */</PRE></CODE></BLOCKQUOTE><P>In fact not all of the above widgets are needed here, but to make thingsclearer I put them all together.<P><BLOCKQUOTE><CODE><PRE>/* that's easy... when one of the buttons is toggled, we just * check which one is active and set the style of the toolbar * accordingly * ATTENTION: our toolbar is passed as data to callback ! */void radio_event (GtkWidget *widget, gpointer data){ if (GTK_TOGGLE_BUTTON (text_button)->active) gtk_toolbar_set_style(GTK_TOOLBAR ( data ), GTK_TOOLBAR_TEXT); else if (GTK_TOGGLE_BUTTON (icon_button)->active) gtk_toolbar_set_style(GTK_TOOLBAR ( data ), GTK_TOOLBAR_ICONS); else if (GTK_TOGGLE_BUTTON (both_button)->active) gtk_toolbar_set_style(GTK_TOOLBAR ( data ), GTK_TOOLBAR_BOTH);}/* even easier, just check given toggle button and enable/disable * tooltips */void toggle_event (GtkWidget *widget, gpointer data){ gtk_toolbar_set_tooltips (GTK_TOOLBAR ( data ), GTK_TOGGLE_BUTTON (widget)->active );}</PRE></CODE></BLOCKQUOTE><P>The above are just two callback functions that will be called whenone of the buttons on a toolbar is pressed. You should already befamiliar with things like this if you've already used toggle buttons (andradio buttons).<P><BLOCKQUOTE><CODE><PRE>int main (int argc, char *argv[]){ /* Here is our main window (a dialog) and a handle for the handlebox */ GtkWidget* dialog; GtkWidget* handlebox; /* Ok, we need a toolbar, an icon with a mask (one for all of the buttons) and an icon widget to put this icon in (but we'll create a separate widget for each button) */ GtkWidget * toolbar; GdkPixmap * icon; GdkBitmap * mask; GtkWidget * iconw; /* this is called in all GTK application. */ gtk_init (&argc, &argv); /* create a new window with a given title, and nice size */ dialog = gtk_dialog_new (); gtk_window_set_title ( GTK_WINDOW ( dialog ) , "GTKToolbar Tutorial"); gtk_widget_set_usize( GTK_WIDGET ( dialog ) , 600 , 300 ); GTK_WINDOW ( dialog ) ->allow_shrink = TRUE; /* typically we quit if someone tries to close us */ gtk_signal_connect ( GTK_OBJECT ( dialog ), "delete_event", GTK_SIGNAL_FUNC ( delete_event ), NULL); /* we need to realize the window because we use pixmaps for * items on the toolbar in the context of it */ gtk_widget_realize ( dialog ); /* to make it nice we'll put the toolbar into the handle box, * so that it can be detached from the main window */ handlebox = gtk_handle_box_new (); gtk_box_pack_start ( GTK_BOX ( GTK_DIALOG(dialog)->vbox ), handlebox, FALSE, FALSE, 5 );</PRE></CODE></BLOCKQUOTE><P>The above should be similar to any other GTK application. Justinitialization of GTK, creating the window etc.. There is only onething that probably needs some explanation: a handle box. A handle boxis just another box that can be used to pack widgets in to. Thedifference between it and typical boxes is that it can be detachedfrom a parent window (or, in fact, the handle box remains in theparent, but it is reduced to a very small rectangle, while all of itscontents are reparented to a new freely floating window). It isusually nice to have a detachable toolbar, so these two widgets occurtogether quite often.<P><BLOCKQUOTE><CODE><PRE> /* toolbar will be horizontal, with both icons and text, and * with 5pxl spaces between items and finally, * we'll also put it into our handlebox */ toolbar = gtk_toolbar_new ( GTK_ORIENTATION_HORIZONTAL, GTK_TOOLBAR_BOTH ); gtk_container_set_border_width ( GTK_CONTAINER ( toolbar ) , 5 ); gtk_toolbar_set_space_size ( GTK_TOOLBAR ( toolbar ), 5 ); gtk_container_add ( GTK_CONTAINER ( handlebox ) , toolbar ); /* now we create icon with mask: we'll reuse it to create * icon widgets for toolbar items */ icon = gdk_pixmap_create_from_xpm_d ( dialog->window, &mask, &dialog->style->white, gtk_xpm );</PRE></CODE></BLOCKQUOTE><P>Well, what we do above is just a straight-forward initialization ofthe toolbar widget and creation of a GDK pixmap with its mask. If youwant to know something more about using pixmaps, refer to GDKdocumentation or to the <A HREF="gtk_tut-9.html#sec_Pixmaps">Pixmaps</A> sectionearlier in this tutorial.<P><BLOCKQUOTE><CODE><PRE> /* our first item is <close> button */ iconw = gtk_pixmap_new ( icon, mask ); /* icon widget */ close_button = gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */ "Close", /* button label */ "Closes this app", /* this button's tooltip */ "Private", /* tooltip private info */ iconw, /* icon widget */ GTK_SIGNAL_FUNC (delete_event), /* a signal */ NULL ); gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) ); /* space after item */</PRE></CODE></BLOCKQUOTE><P>In the above code you see the simplest case: adding a button totoolbar. Just before appending a new item, we have to construct apixmap widget to serve as an icon for this item; this step will haveto be repeated for each new item. Just after the item we also add aspace, so the following items will not touch each other. As you seegtk_toolbar_append_item returns a pointer to our newly created buttonwidget, so that we can work with it in the normal way.<P><BLOCKQUOTE><CODE><PRE> /* now, let's make our radio buttons group... */ iconw = gtk_pixmap_new ( icon, mask ); icon_button = gtk_toolbar_append_element( GTK_TOOLBAR(toolbar), GTK_TOOLBAR_CHILD_RADIOBUTTON, /* a type of element */ NULL, /* pointer to widget */ "Icon", /* label */ "Only icons in toolbar", /* tooltip */ "Private", /* tooltip private string */ iconw, /* icon */ GTK_SIGNAL_FUNC (radio_event), /* signal */ toolbar); /* data for signal */ gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );</PRE></CODE></BLOCKQUOTE><P>Here we begin creating a radio buttons group. To do this we usegtk_toolbar_append_element. In fact, using this function one can alsoadd simple items or even spaces (type = GTK_TOOLBAR_CHILD_SPACE orGTK_TOOLBAR_CHILD_BUTTON). In the above case we start creating a radiogroup. In creating other radio buttons for this group a pointer to theprevious button in the group is required, so that a list of buttonscan be easily constructed (see the section on <A HREF="gtk_tut-6.html#sec_Radio_Buttons">Radio Buttons</A> earlier in thistutorial).<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 set the state of one of the buttons manually (otherwise
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -