Chapter 12. Menus and Toolbars

Table of Contents

There are specific APIs for Menus and toolbars, but you should usually deal with them together, using the UIManager to define Actions which you can then arrange in menus 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 RefPtrs.

[Note] Note

GtkAction, GtkActionGroup and GtkUIManager are deprecated in GTK+ from version 3.10. The corresponding classes in gtkmm will also be deprecated in the future. The examples in this chapter use Gio::SimpleAction, Gio::SimpleActionGroup and Gtk::Builder instead.

Actions

First create the Actions 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.

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", "_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", "_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.