📄 sec-manualmenuexample.html
字号:
<HTML><HEAD><TITLE>Manual Menu Example</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="UP"TITLE="Menu Widget"HREF="ch-menuwidget.html"><LINKREL="PREVIOUS"TITLE="Menu Widget"HREF="ch-menuwidget.html"><LINKREL="NEXT"TITLE="Using ItemFactory"HREF="sec-usingitemfactory.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="SECT1"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="ch-menuwidget.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 14. Menu Widget</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-usingitemfactory.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-MANUALMENUEXAMPLE">14.2. Manual Menu Example</A></H1><P>That should about do it. Let's take a look at an example to help clarify.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start menu menu.c */#include <stdio.h>#include <gtk/gtk.h>static gint button_press (GtkWidget *, GdkEvent *);static void menuitem_response (gchar *);int main( int argc, char *argv[] ){ GtkWidget *window; GtkWidget *menu; GtkWidget *menu_bar; GtkWidget *root_menu; GtkWidget *menu_items; GtkWidget *vbox; GtkWidget *button; char buf[128]; int i; gtk_init (&argc, &argv); /* create a new window */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_widget_set_usize (GTK_WIDGET (window), 200, 100); gtk_window_set_title (GTK_WINDOW (window), "GTK Menu Test"); gtk_signal_connect (GTK_OBJECT (window), "delete_event", (GtkSignalFunc) gtk_main_quit, NULL); /* Init the menu-widget, and remember -- never * gtk_show_widget() the menu widget!! * This is the menu that holds the menu items, the one that * will pop up when you click on the "Root Menu" in the app */ menu = gtk_menu_new (); /* Next we make a little loop that makes three menu-entries for "test-menu". * Notice the call to gtk_menu_append. Here we are adding a list of * menu items to our menu. Normally, we'd also catch the "clicked" * signal on each of the menu items and setup a callback for it, * but it's omitted here to save space. */ for (i = 0; i < 3; i++) { /* Copy the names to the buf. */ sprintf (buf, "Test-undermenu - %d", i); /* Create a new menu-item with a name... */ menu_items = gtk_menu_item_new_with_label (buf); /* ...and add it to the menu. */ gtk_menu_append (GTK_MENU (menu), menu_items); /* Do something interesting when the menuitem is selected */ gtk_signal_connect_object (GTK_OBJECT (menu_items), "activate", GTK_SIGNAL_FUNC (menuitem_response), (gpointer) g_strdup (buf)); /* Show the widget */ gtk_widget_show (menu_items); } /* This is the root menu, and will be the label * displayed on the menu bar. There won't be a signal handler attached, * as it only pops up the rest of the menu when pressed. */ root_menu = gtk_menu_item_new_with_label ("Root Menu"); gtk_widget_show (root_menu); /* Now we specify that we want our newly created "menu" to be the menu * for the "root menu" */ gtk_menu_item_set_submenu (GTK_MENU_ITEM (root_menu), menu); /* A vbox to put a menu and a button in: */ vbox = gtk_vbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (window), vbox); gtk_widget_show (vbox); /* Create a menu-bar to hold the menus and add it to our main window */ menu_bar = gtk_menu_bar_new (); gtk_box_pack_start (GTK_BOX (vbox), menu_bar, FALSE, FALSE, 2); gtk_widget_show (menu_bar); /* Create a button to which to attach menu as a popup */ button = gtk_button_new_with_label ("press me"); gtk_signal_connect_object (GTK_OBJECT (button), "event", GTK_SIGNAL_FUNC (button_press), GTK_OBJECT (menu)); gtk_box_pack_end (GTK_BOX (vbox), button, TRUE, TRUE, 2); gtk_widget_show (button); /* And finally we append the menu-item to the menu-bar -- this is the * "root" menu-item I have been raving about =) */ gtk_menu_bar_append (GTK_MENU_BAR (menu_bar), root_menu); /* always display the window as the last step so it all splashes on * the screen at once. */ gtk_widget_show (window); gtk_main (); return(0);}/* Respond to a button-press by posting a menu passed in as widget. * * Note that the "widget" argument is the menu being posted, NOT * the button that was pressed. */static gint button_press( GtkWidget *widget, GdkEvent *event ){ if (event->type == GDK_BUTTON_PRESS) { GdkEventButton *bevent = (GdkEventButton *) event; gtk_menu_popup (GTK_MENU (widget), NULL, NULL, NULL, NULL, bevent->button, bevent->time); /* Tell calling code that we have handled this event; the buck * stops here. */ return TRUE; } /* Tell calling code that we have not handled this event; pass it on. */ return FALSE;}/* Print a string when a menu item is selected */static void menuitem_response( gchar *string ){ printf ("%s\n", string);}/* example-end */</PRE></TD></TR></TABLE><P>You may also set a menu item to be insensitive and, using an acceleratortable, bind keys to menu functions.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="ch-menuwidget.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-usingitemfactory.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Menu Widget</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-menuwidget.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Using ItemFactory</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -