Vyskakovací nabídky

Nabídky Menu jsou normálně jen přidané do okna, ale mohou být také jen dočasně zobrazené na základě kliknutí myší. Například kontextová nabídka se může zobrazit, když uživatel klikne druhým tlačítkem myši.

Například:

Glib::ustring ui_info =
  "<interface>"
  "  <menu id='menu-examplepopup'>"
  "    <section>"
  "      <item>"
  "        <attribute name='label' translatable='yes'>Edit</attribute>"
  "        <attribute name='action'>examplepopup.edit</attribute>"
  "      </item>"
  "      <item>"
  "        <attribute name='label' translatable='yes'>Process</attribute>"
  "        <attribute name='action'>examplepopup.process</attribute>"
  "      </item>"
  "      <item>"
  "        <attribute name='label' translatable='yes'>Remove</attribute>"
  "        <attribute name='action'>examplepopup.remove</attribute>"
  "      </item>"
  "    </section>"
  "  </menu>"
  "</interface>";

m_refBuilder->add_from_string(ui_info);

auto gmenu = m_refBuilder->get_object<Gio::Menu>("menu-examplepopup");
m_pMenuPopup = std::make_unique<Gtk::Menu>(gmenu);

K zobrazení vyskakovací nabídky použijte metodu popup() třídy Gtk::Menu, poskytněte ji identifikátor tlačítka a čas aktivace tak, jak to dělá signál button_press_event, který stejně budete muset obsluhovat. Například:

bool ExampleWindow::on_button_press_event(GdkEventButton* event)
{
  if( (event->type == GDK_BUTTON_PRESS) && (event->button == 3) )
  {
    if(!m_pMenuPopup->get_attach_widget())
      m_pMenuPopup->attach_to_widget(*this);

    m_pMenuPopup->popup(event->button, event->time);
    return true; //It has been handled.
  }
  else
    return false;
}