📄 ch-menuwidget.html
字号:
<HTML><HEAD><TITLE>Menu Widget</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="PREVIOUS"TITLE="Tree Example"HREF="sec-treeexample.html"><LINKREL="NEXT"TITLE="Manual Menu Example"HREF="sec-manualmenuexample.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%"> <tr> <td bgcolor="#FFFFFF" valign=top nowrap> <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A> <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR> </td> <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sec-treeexample.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-manualmenuexample.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CH-MENUWIDGET">Chapter 14. Menu Widget</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT>14.1. <AHREF="ch-menuwidget.html#SEC-MANUALMENUCREATION">Manual Menu Creation</A></DT><DT>14.2. <AHREF="sec-manualmenuexample.html">Manual Menu Example</A></DT><DT>14.3. <AHREF="sec-usingitemfactory.html">Using ItemFactory</A></DT><DT>14.4. <AHREF="sec-itemfactoryexample.html">Item Factory Example</A></DT></DL></DIV><P>There are two ways to create menus: there's the easy way, and there'sthe hard way. Both have their uses, but you can usually use theItemfactory (the easy way). The "hard" way is to create all the menususing the calls directly. The easy way is to use the gtk_item_factorycalls. This is much simpler, but there are advantages anddisadvantages to each approach.</P><P>The Itemfactory is much easier to use, and to add new menus to,although writing a few wrapper functions to create menus using themanual method could go a long way towards usability. With theItemfactory, it is not possible to add images or the character '/' tothe menus.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-MANUALMENUCREATION">14.1. Manual Menu Creation</A></H1><P>In the true tradition of teaching, we'll show you the hard wayfirst. <TTCLASS="LITERAL">:)</TT></P><P>There are three widgets that go into making a menubar and submenus:</P><P></P><UL><LI><P>a menu item, which is what the user wants to select, e.g.,"Save"</P></LI><LI><P>a menu, which acts as a container for the menu items, and</P></LI><LI><P>a menubar, which is a container for each of the individualmenus.</P></LI></UL><P>This is slightly complicated by the fact that menu item widgets areused for two different things. They are both the widgets that arepacked into the menu, and the widget that is packed into the menubar,which, when selected, activates the menu.</P><P>Let's look at the functions that are used to create menus andmenubars. This first function is used to create a new menubar.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget *gtk_menu_bar_new( void );</PRE></TD></TR></TABLE><P>This rather self explanatory function creates a new menubar. You usegtk_container_add to pack this into a window, or the box_packfunctions to pack it into a box - the same as buttons.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget *gtk_menu_new( void );</PRE></TD></TR></TABLE><P>This function returns a pointer to a new menu; it is never actuallyshown (with gtk_widget_show), it is just a container for the menuitems. I hope this will become more clear when you look at theexample below.</P><P>The next two calls are used to create menu items that are packed intothe menu (and menubar).</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget *gtk_menu_item_new( void );</PRE></TD></TR></TABLE><P>and</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget *gtk_menu_item_new_with_label( const char *label );</PRE></TD></TR></TABLE><P>These calls are used to create the menu items that are to bedisplayed. Remember to differentiate between a "menu" as created withgtk_menu_new and a "menu item" as created by the gtk_menu_item_newfunctions. The menu item will be an actual button with an associatedaction, whereas a menu will be a container holding menu items.</P><P>The gtk_menu_new_with_label and gtk_menu_new functions are just asyou'd expect after reading about the buttons. One creates a new menuitem with a label already packed into it, and the other just creates ablank menu item.</P><P>Once you've created a menu item you have to put it into a menu. Thisis done using the function gtk_menu_append. In order to capture whenthe item is selected by the user, we need to connect to the<TTCLASS="LITERAL">activate</TT> signal in the usual way. So, if we wanted to create astandard <TTCLASS="LITERAL">File</TT> menu, with the options <TTCLASS="LITERAL">Open</TT>, <TTCLASS="LITERAL">Save</TT>, and<TTCLASS="LITERAL">Quit</TT>, the code would look something like:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> file_menu = gtk_menu_new (); /* Don't need to show menus */ /* Create the menu items */ open_item = gtk_menu_item_new_with_label ("Open"); save_item = gtk_menu_item_new_with_label ("Save"); quit_item = gtk_menu_item_new_with_label ("Quit"); /* Add them to the menu */ gtk_menu_append (GTK_MENU (file_menu), open_item); gtk_menu_append (GTK_MENU (file_menu), save_item); gtk_menu_append (GTK_MENU (file_menu), quit_item); /* Attach the callback functions to the activate signal */ gtk_signal_connect_object (GTK_OBJECT (open_item), "activate", GTK_SIGNAL_FUNC (menuitem_response), (gpointer) "file.open"); gtk_signal_connect_object (GTK_OBJECT (save_item), "activate", GTK_SIGNAL_FUNC (menuitem_response), (gpointer) "file.save"); /* We can attach the Quit menu item to our exit function */ gtk_signal_connect_object (GTK_OBJECT (quit_item), "activate", GTK_SIGNAL_FUNC (destroy), (gpointer) "file.quit"); /* We do need to show menu items */ gtk_widget_show (open_item); gtk_widget_show (save_item); gtk_widget_show (quit_item);</PRE></TD></TR></TABLE><P>At this point we have our menu. Now we need to create a menubar and amenu item for the <TTCLASS="LITERAL">File</TT> entry, to which we add our menu. The codelooks like this:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> menu_bar = gtk_menu_bar_new (); gtk_container_add (GTK_CONTAINER (window), menu_bar); gtk_widget_show (menu_bar); file_item = gtk_menu_item_new_with_label ("File"); gtk_widget_show (file_item);</PRE></TD></TR></TABLE><P>Now we need to associate the menu with <TTCLASS="LITERAL">file_item</TT>. This is donewith the function</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_menu_item_set_submenu( GtkMenuItem *menu_item, GtkWidget *submenu );</PRE></TD></TR></TABLE><P>So, our example would continue with</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> gtk_menu_item_set_submenu (GTK_MENU_ITEM (file_item), file_menu);</PRE></TD></TR></TABLE><P>All that is left to do is to add the menu to the menubar, which isaccomplished using the function</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_menu_bar_append( GtkMenuBar *menu_bar, GtkWidget *menu_item );</PRE></TD></TR></TABLE><P>which in our case looks like this:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> gtk_menu_bar_append (GTK_MENU_BAR (menu_bar), file_item);</PRE></TD></TR></TABLE><P>If we wanted the menu right justified on the menubar, such as helpmenus often are, we can use the following function (again on<TTCLASS="LITERAL">file_item</TT> in the current example) before attaching it to themenubar.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_menu_item_right_justify( GtkMenuItem *menu_item );</PRE></TD></TR></TABLE><P>Here is a summary of the steps needed to create a menu bar with menusattached:</P><P></P><UL><LI><P> Create a new menu using gtk_menu_new()</P></LI><LI><P> Use multiple calls to gtk_menu_item_new() for each item youwish to have on your menu. And use gtk_menu_append() to put each ofthese new items on to the menu.</P></LI><LI><P> Create a menu item using gtk_menu_item_new(). This will be theroot of the menu, the text appearing here will be on the menubaritself.</P></LI><LI><P>Use gtk_menu_item_set_submenu() to attach the menu to the rootmenu item (the one created in the above step).</P></LI><LI><P> Create a new menubar using gtk_menu_bar_new. This step onlyneeds to be done once when creating a series of menus on one menu bar.</P></LI><LI><P> Use gtk_menu_bar_append() to put the root menu onto the menubar.</P></LI></UL><P>Creating a popup menu is nearly the same. The difference is that themenu is not posted "automatically" by a menubar, but explicitly bycalling the function gtk_menu_popup() from a button-press event, forexample. Take these steps:</P><P></P><UL><LI><P>Create an event handling function. It needs to have theprototype</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">static gint handler (GtkWidget *widget, GdkEvent *event);</PRE></TD></TR></TABLE><P>and it will use the event to find out where to pop up the menu.</P></LI><LI><P>In the event handler, if the event is a mouse button press,treat <TTCLASS="LITERAL">event</TT> as a button event (which it is) and use it asshown in the sample code to pass information to gtk_menu_popup().</P></LI><LI><P>Bind that event handler to a widget with</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> gtk_signal_connect_object (GTK_OBJECT (widget), "event", GTK_SIGNAL_FUNC (handler), GTK_OBJECT (menu));</PRE></TD></TR></TABLE><P>where <TTCLASS="LITERAL">widget</TT> is the widget you are binding to,<TTCLASS="LITERAL">handler</TT> is the handling function, and <TTCLASS="LITERAL">menu</TT> is a menucreated with gtk_menu_new(). This can be a menu which is also postedby a menu bar, as shown in the sample code.</P></LI></UL></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-treeexample.html"><<< Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-manualmenuexample.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Tree Example</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"> </TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Manual Menu Example</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -