How to avoid this allocating size Gtk-Warning ? (GtkPopoverMenu)

This code,

Gtk::PopoverMenu * popover_menu = 
	new Gtk::PopoverMenu (get_contextual_menu());

popover_menu->set_has_arrow (false);
popover_menu->set_position (Gtk::PositionType::BOTTOM);
popover_menu->set_pointing_to ({(gint) _x, (gint) _y, 0, 0});

popover_menu->set_parent (*this);
popover_menu->popup();

throws this Gtk-Warning:

Gtk-WARNING **: Allocating size to GtkPopoverMenu without calling gtk_widget_measure(). How does the code know the size to allocate?

How to avoid it ?

Whatever widget *this is needs to be aware of the popover menu and allocate a size to it. What widget would that be?

See for example the sources of GtkText: https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gtk/gtktext.c#L2514 - you need to call gtk_popover_present in the size_allocate implementation of that widget.

‘*this’ is just a Gtk::ScrolledWindow (with a Gtk::ColumnView inside)

If I didn’t misunderstood, I need to call, popover_menu->size_allocate (…) before, but how could the size be calculated ?

Is there a general/automatic method/process that tells the system to compute the sizes ?

You are generally right and the way to compute the size is generally through gtk_widget_measure() but in the case of a popover, the call to gtk_popover_present should be enough.

I changed the code in order to call the present() method in a code, but the warning remains.

Gtk::PopoverMenu * popover_menu =
	new Gtk::PopoverMenu (get_contextual_menu());

popover_menu->set_parent (*this);

popover_menu->set_has_arrow (false);
popover_menu->set_position (Gtk::PositionType::BOTTOM);

popover_menu->set_pointing_to ((Gdk::Rectangle){(gint) _x, (gint) _y, 1, 1});

popover_menu->present(); // <- I added this line
popover_menu->popup();

No, look at the code I linked. It’s called in the parent widget’s size_allocate implementation.

Sorry,

I’m very confused. I need another example or maybe another explanation.

Gtk::Widget::present() must be called or not ?.. from which object ? from a Gtk::PopoverMenu ? or from Gtk::ScrolledWindow (which is the parent of Gtk::PopoverMenu)…

And if popup (Gtk::PopupMenu) sizes should be passed… how could get them ?.. and how (with which method) should be passed into ?

You can’t add popovers to a random widget such as a scrolled window. You need a custom widget that’s aware of the popover OR the widget needs to be using a layout manager - then it’s automatic. GtkScrolledWindow doesn’t use one, and it’s not a custom widget you can override allocation for, so this can’t work.

Here’s an example of how to implement it manually:

static void
my_widget_size_allocate (GtkWidget *widget,
                         int        width,
                         int        height,
                         int        baseline)
{
  MyWidget *self = MY_WIDGET (widget);

  gtk_popover_present (self->popover);

  ...
}

...

  klass->size_allocate = my_widget_size_allocate;

This is C example, no idea how it looks like with gtkmm. But you probably just want to take something like GtkBox - it’s using a layout manager and popovers will work with it.

Another option is to create you custom widget by inheriting from GtkWidget and use a GtkBinLayout as the layout manager; then you can add the ScrolledWindow and the Popover as children of your widget.

Every custom widget using a layout manager can handle popovers automatically; if you don’t use a layout manager, then you’ll need to deal with popovers in your own size_allocate() virtual function implementation.

The hints provided by Alexander Mikhaylenko and Emmanuele Bassi are always very appreciated…

But since I’m using C++/Gtkmm, I’m still trying to put them all together.

Concerning to Alexander Mikhaylenko’s, I think that the related method in Gtkmm should be Gtk::Widget::size_allocate_vfunc (int witdh, int height, int baseline), but I don’t know how to implement it properly.

The Emmuele Bassi’s seems to be not possible, almost from Gtk::Widget, while there is no defined method like set_child(…).

The Gtkmm team does a good job, but the documentation has some defects. Why This Example Code is not yet updated ?

I’m sorry, I can’t help you with gtkmm.

There’s no gtk_widget_set_child(), but there’s gtk_widget_set_parent().

You’re supposed to create your own widget class, inherit from GtkWidget, and inside the instance initialisation function you should create the scrolled window widget and set its parent to your custom widget.

Thanks, that’s it !!

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