In the true tradition of teaching, we'll show you the hard way first. :)
There are three widgets that go into making a menubar and submenus:
Let's look at the functions that are used to create menus and menubars. This first function is used to create a new menubar.
MenuBar menu_bar = new MenuBar();This rather self explanatory function creates a new menubar. You use the Add() method to pack this into a window, or the box_pack functions to pack it into a box - the same as buttons.
Menu file_menu = new Menu ();This creates a new Menu object; it is never actually shown (with Show() method), it is just a container for the menu items. I hope this will become more clear when you look at the example below.
The next calls are used to create menu items that are packed into the menu (and menubar).
MenuItem open_item = new MenuItem(); MenuItem open_item = new MenuItem("Open");These calls are used to create the menu items that are to be displayed. Remember to differentiate between a "menu" as created with Menu and a "menu item" as created by the MenuItem functions. The menu item will be an actual button with an associated action, whereas a menu will be a container holding menu items.
The MenuItem("label") and MenuItem() creators are just as you'd expect after reading about the buttons. One creates a new menu item with a label already packed into it, and the other just creates a blank menu item.
Once you've created a menu item you have to put it into a menu. This is done using the method Append of Menu object. In order to capture when the item is selected by the user, we need to connect to the activate signal in the usual way. So, if we wanted to create a standard File menu, with the options Open, Save, and Quit, the code would look something like:
Menu file_menu = new Menu (); /* Don't need to show menus */ /* Create the menu items */ MenuItem open_item = new MenuItem("Open"); MenuItem save_item = new MenuItem("Save"); MenuItem quit_item = new MenuItem("Quit"); /* Add them to the menu */ file_menu.Append (open_item); file_menu.Append (save_item); file_menu.Append (quit_item); /* Attach the callback functions to the activate signal */ open_item.Activated += new EventHandler (menuitem_response); save_item.Activated += new EventHandler (menuitem_response); quit_item.Activated += new EventHandler (menuitem_response); /* We do need to show menu items */ open_item.Show(); save_item.Show(); quit_item.Show();At this point we have our menu. Now we need to create a menubar and a menu item for the File entry, to which we add our menu. The code looks like this:
MenuBar menu_bar = new MenuBar(); window.Add (menu_bar); menu_bar.Show(); MenuItem file_item = new MenuItem("File"); file_item.Show();Now we need to associate the menu with file_item. This is done with the MenuItem's Submenu property. So, our example would continue with
file_item.Submenu = file_menu;All that is left to do is to add the menu to the menubar, which is accomplished using the MenuBar's Append method
public void Append (Gtk.Widget child);which in our case looks like this:
menu_bar.Append (file_item);If we wanted the menu right justified on the menubar, such as help menus often are, we can use the RightJustified property (again on file_item in the current example) before attaching it to the menubar.
public bool RightJustified { set; get; };Here is a summary of the steps needed to create a menu bar with menus attached:
Create a new Menu object
Create a new MenuItem object for each item you wish to have on your menu. And use Menu's Append() method to put each of these new items on to the menu.
Create a new MenuItem. This will be the root of the menu, the text appearing here will be on the menubar itself.
Use the Submenu property of MenuItem to attach the menu to the root menu item (the one created in the above step).
Create a new MenuBar. This step only needs to be done once when creating a series of menus on one menu bar.
Use the MenuBar's Append() method to put the root menu onto the menubar.
Creating a popup menu is nearly the same. The difference is that the menu is not posted "automatically" by a menubar, but explicitly by calling the Menu's Popup() method from a button-press event, for example. Take these steps:
Create an event handling function. It needs to have the prototype
public delegate void EventHandler(object sender, EventArgs e);and bind it to the Button's Pressed event with
button.Pressed += new EventHandler (button_press);
So, in our EventHandler function for Pressed will call the Popup method like this:
menu.Popup(null, null, null, IntPtr.Zero, 0, 0);
This can be a menu which is also posted by a menu bar, as shown in the sample code.