Gtk4: Example of gtk_popover_menu_bar_add_child

Hello world !
I am looking for an example of the utilization of the function:
In particular how to setup what the documentation is calling the ‘id’
So far I could not find anything on the web.

I understand that I need to use the ‘custom’ attribute, so I set it up using:

g_menu_item_set_attribute (item, “custom”, “s”, “target”, NULL);

Then I try to insert the new widget using:

gtk_popover_menu_bar_add_child (the_menu_bar, widget, “target”);

But the widget never shows in my menu, and I do not know what to do …

Thanks in advance for your help.

Still looking for help here,
Just for information, I ensured in my code that the custom ‘id’ used to create the menu item is the same that the one I use to try to insert the new widget:

    gchar * custom = "target";
    g_print ("Custom at:: %s\n", custom);
    g_menu_item_set_attribute (item, "custom", "s", custom, NULL);
    GVariant * cust = g_menu_item_get_attribute_value (item, "custom",  g_variant_type_new("s"));
    if (cust) g_print ("item custom is:: %s\n", g_variant_get_string  (cust, NULL));

The previous piece of code actually prints properly the insert custom “target”.

I read the GTK code source for the gtk_popover_menu_bar_add_child function, and following the code I figured that the function gtk_menu_section_box_add_custom is supposed to insert the widget in the menu:

gtk_menu_section_box_add_custom (GtkPopoverMenu *popover,
                                 GtkWidget      *child,
                                 const char     *id)
  GtkWidget *stack;
  GtkMenuSectionBox *box;
  GtkWidget *slot;

  stack = gtk_popover_menu_get_stack (popover);
  box = GTK_MENU_SECTION_BOX (gtk_stack_get_child_by_name (GTK_STACK (stack), "main"));
  if (box == NULL)
    return FALSE;

  slot = (GtkWidget *)g_hash_table_lookup (box->custom_slots, id);

  if (slot == NULL)
    return FALSE;

  if (gtk_widget_get_first_child (slot))
    return FALSE;

  gtk_widget_insert_before (child, slot, NULL);
  return TRUE;

I wish I could run gdb on that part of the code to try to see what’s to read in box and the custom_slots property, only I can’t and there is no documentation available for these objects (GtkMenuSectionBox…) in the main doc of the GTK4 API …

Any help would be much appreciated.

Edit: it seems that as the ‘custom’ attribute is an issue here, I verified that as soon as I setup a g_menu_item with a custom attribute using:

g_menu_item_set_attribute (item, “custom”, “s”, “target”, NULL);

or similarly

g_menu_item_set_attribute_value (item, “custom”, g_variant_new_string (“target”));

Then the menu item does not even appear in the menu at runtime … if any one could help, again that would be much appreciated.

@ebassi , @fmuellner I am sorry to directly ask for help here but I am at a lost about this issue,
you were already kind enough to help me over the past few months to migrate my app from GTK3 to GTK4 :wink:
Also I am sure that you will know who to direct my request to in case you could not help me … this is the last tricky/complicated issue I have to complete the GTK3 to GTK4 migration of my program, and I am also afraid that it could somehow be blocking it.

I understand that GTK4 prefers to read menus from an XML file, I find out an example of gtk_popover_menu_bar_add_child in the GtkWidget factory, where the function calls the custom ID defined in the ui file … it works in that case but not directly in the code as I detailed it above.

Only I cannot use this ‘ui file’ method, the menus I create in my app are dynamical in the sense that the number of menu items and submenus depends on the property of the project loaded (the number of chemical species and the physico-chemical properties of the model that actually is opened … and it can even change when adjusting the calculation parameters … I am chemist/physicist) not to mention that many projects can be opened simultaneously … so the way I see it, I only have the option to code the menus.

After giving some thoughts to my own question I figured that I do not really need the gtk_popover_menu_bar_add_child function but only to be able to find a way to retrieve the GtkWidget that was created using a particular g_menu_item … and I do not know how to do it … so my question to you is can I do this or not, and if yes how ?

Another bad idea would be to write the ui file for each model with the appropriate elements and then load it like for the Widget factory … but that does not seem to be the good choice.

You can download a GTK3 demo version and examples of my app here to understand what I mean for the menus (of the 3D window):

Thanks in advance for your help.
Best regards.


the menu support is yet not fully compatible with GTK3:

  1. Can’t create a menu with anonymous function dynamic
    menu with action is supported only.

  2. Can’t append menu items into the menu dynamically:
    it needs an empty menu item with the name attribute: custom

In my app, menu / clipboard / position are the big stones on the way to porting the app to GTK4.

Dear @taozuhong,
thank you for your reply, I must confess that I need to clarify things a little bit more.
In particular your point 2. because the gtk_popover_menu_bar_add_child function should be the way to do it, but … but … I even try to create an empty menu item with a single and only custom attribute:

 GMenuItem * item = g_menu_item_new (NULL, NULL);
 g_menu_item_set_attribute (item, "custom", "s", custom_id, NULL);

I though that this would reproduce what I saw in the widget factory but still this does not work when I try to use the gtk_popover_menu_bar_add_child targeting the custom_id object.

So I have a some final questions to the dev team then, I though maybe @ebassi or @fmuellner, or anyone else that I do not know about, could clarify that for me:

  1. is it worth it to spend some time working on a GTK4 version of my app as of today (considering that I need some dynamical menu functions to be available ?)

  2. Will these kind of menu functions be available latter on in GTK4, I am not in a position to discuss whether or not it should be the case, but considering that I really need it, It would help me decide if I can keep using GTK, and I would like to, or if I should start thinking about using another API.

Again thanks in advance for your reply.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.