bergentroll
bergentroll

Reputation: 127

gtkmm: how to create popover menu without builder?

I am trying to make GTK3 application with C++. Because it is my first gtkmm app and it is really small, I am avoiding builder and placing widgets with plain code.

I have such snippet for titlebar's menu button:

Gtk::MenuButton mbtn;
Gtk::Menu menu;
Gtk::MenuItem mnitSettings {"Settings"};
Gtk::MenuItem mnitAbout {"About"};
mbtn.set_image_from_icon_name("open-menu-symbolic");
menu.append(mnitSettings);
menu.append(mnitAbout);
menu.show_all();
mbtn.set_popup(menu);

It works fine, but I noticed that most GTK3 applications have some kind of Gtk::Popover for button's menu, which have transition animation and pointing arrow on it's edge. For my sadness, most GTK3 applications use builder, so I can not understand how to do the trick.

There is Gtk::MenuButton::set_popover(Gtk::Popover &), but I failed to add my menu to popover wrapper (I've got "Attempting to add a widget with type gtkmm__GtkMenu to a container of type gtkmm__GtkPopover, but the widget is already inside a container of type GtkWindow" warning).

How could popover menu be achieved in this case?

Upvotes: 5

Views: 1060

Answers (1)

bergentroll
bergentroll

Reputation: 127

I just figured out how to.

Gtk::MenuButton mbtn;
Glib::RefPtr<Gio::Menu> menu = Gio::Menu::create();
menu->append("Settings", "app.settings");
menu->append("About", "app.about");
mbtn.set_menu_model(menu);

Actions can be attached with:

app->add_action("settings", glibc::ptr_fun(&some_useful_func));

Upvotes: 5

Related Questions