A popup menu is a menu presented in a popup window. The menu implements a typical context menu, with support for submenu's.

WPopupMenu is not to be confused with WMenu which implements an always-visible navigation menu for a web application.

When initially created, the menu is invisible, until popup() or exec() is called. Then, the menu will remain visible until an item is selected, or the user cancels the menu (by pressing the Escape key or clicking elsewhere).

The implementation assumes availability of JavaScript to position the menu at the current mouse position and provide feed-back of the currently selected item.

WPopupMenu is similar in use to WDialog. So there are two ways of using the menu. The simplest way is to use one of the exec() methods, apply a reentrant event loop and wait until the user selected an item or cancelled the popup menu (by pressing Escape or clicking elsewhere).

Preferably, use one of the popup() methods to show the menu and listen to the aboutToHide signal where you read the result() (the last triggered menu item). If you listen to the aboutToHide signal of a parent popup it will also be triggered by its child popups.

You have several options to react to the selection of an item:

  • You can use the WMenuItem itself to identify the action, perhaps by specialization or by simply by binding custom data using WMenuItem.setData().
  • You can use a lambda function to connect to each of the listeners. In Java, you can use boost.bind() to bind parameters that identify the selected item.
  • You can bind a separate method to each item's WMenuItem.triggered signal.

Example
source
  void Popup() {
    WContainerWidget container = new WContainerWidget();
    WPopupMenu popupPtr = new WPopupMenu();
    WPopupMenu popup = popupPtr;
    final WText status = new WText();
    status.setMargin(new WLength(10), EnumSet.of(Side.Left, Side.Right));
    final WText out = new WText();
    popup
        .addItem("Connect")
        .triggered()
        .addListener(
            this,
            () -> {
              out.setText("<p>Connecting...</p>");
            });
    popup
        .addItem("Disconnect")
        .triggered()
        .addListener(
            this,
            () -> {
              out.setText("<p>You are disconnected now.</p>");
            });
    popup.addSeparator();
    popup
        .addItem("icons/house.png", "I'm home")
        .triggered()
        .addListener(
            this,
            () -> {
              out.setText("");
            });
    final WMenuItem item = popup.addItem("Don't disturb");
    item.setCheckable(true);
    item.triggered()
        .addListener(
            this,
            () -> {
              out.setText(
                  new WString("<p>{1} item is {2}.</p>")
                      .arg(item.getText())
                      .arg(item.isChecked() ? "checked" : "unchecked"));
            });
    popup.addSeparator();
    WPopupMenu subMenuPtr = new WPopupMenu();
    WPopupMenu subMenu = subMenuPtr;
    subMenu
        .addItem("Contents")
        .triggered()
        .addListener(
            this,
            () -> {
              out.setText("<p>This could be a link to /contents.html.</p>");
            });
    subMenu
        .addItem("Index")
        .triggered()
        .addListener(
            this,
            () -> {
              out.setText("<p>This could be a link to /index.html.</p>");
            });
    subMenu.addSeparator();
    subMenu
        .addItem("About")
        .triggered()
        .addListener(
            this,
            () -> {
              final WMessageBox messageBox =
                  new WMessageBox(
                      "About",
                      "<p>This is a program to make connections.</p>",
                      Icon.Information,
                      EnumSet.of(StandardButton.Ok));
              messageBox.show();
              messageBox
                  .buttonClicked()
                  .addListener(
                      this,
                      () -> {
                        if (messageBox != null) messageBox.remove();
                      });
            });
    popup.addMenu("Help", subMenuPtr);
    WPushButton button = new WPushButton((WContainerWidget) container);
    button.setMenu(popupPtr);
    popup
        .itemSelected()
        .addListener(
            this,
            (WMenuItem selectedItem) -> {
              status.setText(new WString("Selected menu item: {1}.").arg(selectedItem.getText()));
            });
    container.addWidget(status);
    container.addWidget(out);
  }

Top