Re: [gtkmm] Building popups on the fly



Hi!

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

I had the same problem, but it was relatively easy to accomplish:

This is my header file for the popup menu class:

--------8<-------- snip --------8<--------

class cPopupMenu : public Gtk::Menu
{
  private:
    void delete_me (void); /* Methode for deleting. */

  public:
    cPopupMenu (...);
};

--------8<-------- snip --------8<--------

The important parts of the .cpp file look like this:

--------8<-------- snip --------8<--------

cPopupMenu::cPopupMenu (...)
{
  /* Build your menu here! */
  // [...]
  this->show_all();
  this->signal_selection_done().connect(SigC::slot(*this,
      &cPopupMenu::delete_me));
}

void
cPopupMenu::delete_me (void)
{
  delete this;
}

--------8<-------- snip --------8<--------

Use the contructor to build your menu. The important thing is, that
`signal_selection_done()? will always be called when the popup menu
disappears. Then you are able to popup your menu with the following code:

--------8<-------- snip --------8<--------

bool
cGuiMain::button_pressed (GdkEventButton* event)
{
  if (event->type != GDK_BUTTON_PRESS || event->button != 3)
    return false; /* Button has not been handled. */

  cPopupMenu* menu = new cPopupMenu(...);
  menu->popup(event->button, event->time);
  return true; /* The button has been handled. */
}

--------8<-------- snip --------8<--------

You have to create the popup menu in the heap, of corse. Otherwise it will
go out of scope at the end of your code block.

> "Gtk::Menu contain child Gtk::MenuItems. MenuItems can, in turn, contain
> child Menus, to create sub menus."

Thats easy. Just create a normal menu item and then call
`my_menuitem->set_submenu(...);? and pass another menu (with menuitems).
Don't forget to `show()? all these items ;-)

Regards,
Simon Fuhrmann <Nightslayer at gmx.de>




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