[gtkmm] Building popups on the fly



Hello,

I need to build a popup menu on the fly, with values that change
depending on which item the popup has been invoked on.

My original idea was to work like this:

void Foo::invokePopup(...)
{
	Gtk::Menu itemPopup;
	Gtk::Menu submenu;

	for (something)
		itemPopup.items().push_back(Gtk::Menu_Helpers::MenuElem(...));
	for (somethingElse)
		submenu.items().push_back(Gtk::Menu_Helpers::MenuElem(...));

	itemPopup.items().push_back(Gtk::Menu_Helpers::MenuElem("Submenu...", submenu));

	itemPopup.popup(e->button.button, e->button.time);
}

However, the popup() method returns immediately, causing itemPopup and
submenu to be deallocated and the application to segfault or, if I move
itemPopup elsewhere and clear() its items() at the beginning of
invokePopup, the submenu deallocates and appears empty.

Should I do some funky bookkeeping of submenus (and then get mad at when
to deallocate them: before itemPopup().items().clear() or later?  I seem
to get segfaults either way) or is there a saner way to do all this?

So far I resolved like this:

class Foo
{
	Gtk::Menu itemPopup;
	invokePopup(...);
}

void invokePopup(...)
{
	Gtk::Menu* submenu = new Gtk::Menu();
	submenu->set_managed();
	
	itemPopup.items().clear();
	
	for (something)
		itemPopup.items().push_back(Gtk::Menu_Helpers::MenuElem(...));
	for (somethingElse)
		submenu->items().push_back(Gtk::Menu_Helpers::MenuElem(...));

	itemPopup.items().push_back(Gtk::Menu_Helpers::MenuElem("Submenu...", *submenu));

	itemPopup.popup(e->button.button, e->button.time);
}

however, it means that all the "submenu" that are created keep living at
least until itemPopup is destroyed (which means, at the end of the
application: a memory leak).  Or clear() also deallocates managed
widgets?  Anything better?

Ciao,

Enrico

P.S.
Referring to the debian libgtkmm2.0-doc, section 10.2 of the book say
"Gtk::Menu contain child Gtk::MenuItems. MenuItems can, in turn, contain
child Menus, to create sub menus." but don't tell how to create
submenus.  The whole documentation, instead, seems to contain no
direct reference to Gtk::Menu_Helpers namespace (its elements are
included in the "Menu classes" part, but the Menu_Helpers namespace is
not explicitly mentioned.
 
--
GPG key: 1024D/797EBFAB 2000-12-05 Enrico Zini <enrico debian org>

Attachment: signature.asc
Description: Digital signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]