Chapter 13. Menu Widget

Table of Contents

1. Manual Menu Creation
2. Manual Menu Example
3. Using ItemFactory
3.1. ItemFactory entries
3.2. Creating an ItemFactory
3.3. Making use of the menu and its menu items
4. Item Factory Example

There are two ways to create menus: there's the easy way, and there's the hard way. Both have their uses, but you can usually use the Itemfactory (the easy way). The "hard" way is to create all the menus using the calls directly. The easy way is to use the gtk_item_factory calls. This is much simpler, but there are advantages and disadvantages to each approach.

The Itemfactory is much easier to use, and to add new menus to, although writing a few wrapper functions to create menus using the manual method could go a long way towards usability. With the Itemfactory, it is not possible to add images or the character '/' to the menus.

1. Manual Menu Creation

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:

  • a menu item, which is what the user wants to select, e.g., "Save"
  • a menu, which acts as a container for the menu items, and
  • a menubar, which is a container for each of the individual menus.

This is slightly complicated by the fact that menu item widgets are used for two different things. They are both the widgets that are packed into the menu, and the widget that is packed into the menubar, which, when selected, activates the menu.

Let's look at the functions that are used to create menus and menubars. This first function is used to create a new menubar.