fossfreedom
fossfreedom

Reputation: 3073

Is there an equivalent way to load a GMenu from XML?

I've been using Glade to create my Application menus in the menu-bar for Gtk3.4/3.6 based apps like this:

<object class="GtkMenu" id="popup_menu">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <object class="GtkMenuItem" id="play_album_menu_item">
        <property name="use_action_appearance">False</property>
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="tooltip_text" translatable="yes">Play this album</property>
        <property name="label" translatable="yes">Play Album</property>
        <property name="use_underline">True</property>
        <signal name="activate" handler="play_album_menu_item_callback" swapped="no"/>
      </object>
    </child>

However, the latest Gnome-based GTK3.8 applications have moved away from GtkMenu/UI-Manager based menu creation and now used GMenu and GMenuItem based creation.

I've seen Python code-examples that explain how to individually code menu-items.

However I'm trying to save time/increase maintainability & readability by trying to use a similar Glade XML based menu creation process to:

  1. Create the GMenu
  2. Create a list of GMenuItems
  3. Set various properties such as sensitivity, tooltips
  4. Connect a signal event

Previously I simply did stuff like:

ui = Gtk.Builder()
ui.add_from_file("menufile.xml")
ui.connect_signals(self)
popup_menu = ui.get_object('popup_menu')

etc.etc.

Is this possible with GMenu and GMenuItems?

If so, can anyone point me in the correct direction with example Python code samples or links to existing Python code?

Upvotes: 1

Views: 762

Answers (1)

murrayc
murrayc

Reputation: 2123

For C, the bloatpad example shows the new XML format that can be used in GtkBuilder files: https://git.gnome.org/browse/gtk+/tree/examples/bloatpad.c

However, you have to write these by hand. Glade does not yet let you edit GMenus and will just silently remove nodes.

Upvotes: 1

Related Questions