Menyrad och verktygsfält

Härnäst ska du skapa en Gtk::Builder. Vid detta tillfälle är det också en bra idé att säga till programmet att svara på tangentbordsgenvägar genom att använda Gtk::Application::set_accel_for_action().

Till exempel,

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.glade");

Här anger vi namnen för menyobjekten som de kommer ses av användare i menyn. Det är därför här som du ska göra strängar översättbara genom att lägga till 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);