Menubar and Toolbar
| Chapter 15. Menus and Toolbars |

## Menubar and Toolbar

Next you should create a `Gtk::Builder`. At this point is also a good idea to tell the application to respond to keyboard shortcuts, by using `Gtk::Application::set_accel_for_action()`.

For instance,

    m_refBuilder = Gtk::Builder::create();

    app->set_accel_for_action("example.new", "<Primary>n");
    app->set_accel_for_action("example.quit", "<Primary>q");
    app->set_accel_for_action("example.copy", "<Primary>c");
    app->set_accel_for_action("example.paste", "<Primary>v");

Then, you can define the actual visible layout of the menus and toolbars, and add the UI layout to the `Builder`. This "ui string" uses an XML format, in which you should mention the names of the actions that you have already created. For instance:

    Glib::ustring ui_info =
      "<interface>"
      "  <menu id='menubar'>"
      "    <submenu>"
      "      <attribute name='label' translatable='yes'>_File</attribute>"
      "      <section>"
      "        <item>"
      "          <attribute name='label' translatable='yes'>_New</attribute>"
      "          <attribute name='action'>example.new</attribute>"
      "        </item>"
      "      </section>"
      "      <section>"
      "        <item>"
      "          <attribute name='label' translatable='yes'>_Quit</attribute>"
      "          <attribute name='action'>example.quit</attribute>"
      "        </item>"
      "      </section>"
      "    </submenu>"
      "    <submenu>"
      "      <attribute name='label' translatable='yes'>_Edit</attribute>"
      "      <item>"
      "        <attribute name='label' translatable='yes'>_Copy</attribute>"
      "        <attribute name='action'>example.copy</attribute>"
      "      </item>"
      "      <item>"
      "        <attribute name='label' translatable='yes'>_Paste</attribute>"
      "        <attribute name='action'>example.paste</attribute>"
      "      </item>"
      "    </submenu>"
      "  </menu>"
      "</interface>";

    m_refBuilder->add_from_string(ui_info);
    m_refBuilder->add_from_resource("/toolbar/toolbar.ui");

This is where we specify the names of the menu items as they will be seen by users in the menu. Therefore, this is where you should make strings translatable, by adding `translatable='yes'`.

To instantiate a `Gtk::PopoverMenuBar` and toolbar (a horizontal `Gtk::Box`) which you can actually show, you should use the `Builder::get_object()` and `Builder::get_widget()` methods, and then add the widgets to a container. For instance:

    auto gmenu = m_refBuilder->get_object<Gio::Menu>("menubar");
    auto pMenuBar = Gtk::make_managed<Gtk::PopoverMenuBar>(gmenu);
    m_Box.append(*pMenuBar);

    auto toolbar = m_refBuilder->get_widget<Gtk::Box>("toolbar");
    m_Box.append(*toolbar);

Chapter 15. Menus and Toolbars |  | Popup Menus