Chapter 11. Menus and Toolbars

## Chapter 11. Menus and Toolbars

There are specific APIs for Menus and toolbars, but you should usually deal with them together, using the `UIManager` to define `Action`s which you can then arrange in menu and toolbars. In this way you can handle activation of the action instead of responding to the menu and toolbar items separately. And you can enable or disable both the menu and toolbar item via the action.

This involves the use of the `Gtk::ActionGroup`, `Gtk::Action`, and `UIManager` classes, all of which should be instantiated via their `create()` methods, which return `RefPtr`s.

## Actions

First create the `Action`s and add them to an `ActionGroup`, with `ActionGroup::add()`.

The arguments to `Action::create()` specify the action's name and how it will appear in menus and toolbars. Use stock items where possible so that you don't need to specify the label, accelerator, icon, and tooltips, and so you can use pre-existing translations.

You can also specify a signal handler when calling `ActionGroup::add()`. This signal handler will be called when the action is activated via either a menu item or a toolbar button.

Note that you must specify actions for sub menus as well as menu items.

For instance:

    m_refActionGroup = Gtk::ActionGroup::create();

    m_refActionGroup->add( Gtk::Action::create("MenuFile", "_File") );
    m_refActionGroup->add( Gtk::Action::create("New", Gtk::Stock::NEW),
      sigc::mem_fun(*this, &ExampleWindow::on_action_file_new) );
    m_refActionGroup->add( Gtk::Action::create("ExportData", "Export Data"),
      sigc::mem_fun(*this, &ExampleWindow::on_action_file_open) );
    m_refActionGroup->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
      sigc::mem_fun(*this, &ExampleWindow::on_action_file_quit) );

Note that this is where we specify the names of the actions as they will be seen by users in menus and toolbars. Therefore, this is where you should make strings translatable, by putting them inside the _() macro. When we use the Gtk::Stock items, of course, translations are automatically available.

Examples |  | UIManager