Wt: Wt::WPopupMenu Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions
Wt::WPopupMenu Class Reference

A menu presented in a popup window. More...

`#include <Wt/WPopupMenu>`

Inheritance diagram for Wt::WPopupMenu:
[legend]

List of all members.

## Public Member Functions
| WPopupMenu (WStackedWidget *contentsStack=0)
| Creates a new popup menu.
void | popup (const WPoint &point)
| Shows the the popup at a position.
void | popup (const WMouseEvent &event)
| Shows the the popup at the location of a mouse event.
void | popup (WWidget *location, Orientation orientation=Vertical)
| Shows the popup besides a widget.
WMenuItem * | exec (const WPoint &point)
| Executes the the popup at a position.
WMenuItem * | exec (const WMouseEvent &event)
| Executes the the popup at the location of a mouse event.
WMenuItem * | exec (WWidget *location, Orientation orientation=Vertical)
| Executes the popup besides a widget.
WMenuItem * | result () const
| Returns the last triggered menu item.
virtual void | setHidden (bool hidden, const WAnimation &animation=WAnimation())
| Hides or shows the widget.
virtual void | setMaximumSize (const WLength &width, const WLength &height)
| Sets a maximum size.
virtual void | setMinimumSize (const WLength &width, const WLength &height)
| Sets a minimum size.
Signal & | aboutToHide ()
| Signal emitted when the popup is hidden.
Signal< WMenuItem * > & | triggered ()
| Signal emitted when an item is selected.
void | setAutoHide (bool enabled, int autoHideDelay=0)
| Configure auto-hide when the mouse leaves the menu.

## Detailed Description

A menu presented in a popup window.

The menu implements a typical context menu, with support for submenu's. It is a specialized WMenu from which it inherits most of the API.

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 hitting Escape 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.

As with WDialog, there are two ways of using the menu. The simplest way is to use one of the synchronous exec() methods, which starts a reentrant event loop and waits until the user cancelled the popup menu (by hitting Escape or clicking elsewhere), or selected an item.

Alternatively, you can use one of the popup() methods to show the menu and listen to the triggered signal where you read the result().

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

- Either you use the WMenuItem itself to identify the action, perhaps by specialization or simply by binding custom data using WMenuItem::setData().
- You can bind a separate method to each item's WMenuItem::triggered signal.

Usage example:

     // Create a menu with some items
     WPopupMenu popup;
     popup.addItem("icons/item1.gif", "Item 1");
     popup.addItem("Item 2")->setCheckable(true);
     popup.addItem("Item 3");
     popup.addSeparator();
     popup.addItem("Item 4");
     popup.addSeparator();
     popup.addItem("Item 5");
     popup.addItem("Item 6");
     popup.addSeparator();

     WPopupMenu *subMenu = new WPopupMenu();
     subMenu->addItem("Sub Item 1");
     subMenu->addItem("Sub Item 2");
     popup.addMenu("Item 7", subMenu);

     WMenuItem *item = popup.exec(event);

     if (item) {
       // ... do associated action.
     }

A snapshot of the WPopupMenu:

WPopupMenu example (default)
WPopupMenu example (polished)

See also:
  WMenuItem

## Constructor & Destructor Documentation

Wt::WPopupMenu::WPopupMenu | ( | WStackedWidget * | contentsStack = `0` | ) |

Creates a new popup menu.

The menu is hidden, by default, and must be shown using popup() or exec().

## Member Function Documentation

Signal& Wt::WPopupMenu::aboutToHide | ( |  | ) |

Signal emitted when the popup is hidden.

Unlike the itemSelected() signal, aboutToHide() is only emitted by the toplevel popup menu (and not by submenus), and is also emitted when no item was selected.

You can use result() to get the selected item, which may be `0`.

See also:
  triggered(), itemSelected()

WMenuItem * Wt::WPopupMenu::exec | ( | const WPoint & | point | ) |

Executes the the popup at a position.

Displays the popup at a point with document coordinates `p`, using popup(), and the waits until a menu item is selected, or the menu is cancelled.

Returns the selected menu (or sub-menu) item, or `0` if the user cancelled the menu.

See also:
  popup()

WMenuItem * Wt::WPopupMenu::exec | ( | const WMouseEvent & | event | ) |

Executes the the popup at the location of a mouse event.

This is a convenience method for exec(const WPoint& p) that uses the event's document coordinates.

See also:
  exec(const WPoint&)

WMenuItem * Wt::WPopupMenu::exec | ( | WWidget * | location,
|  | Orientation | orientation = `Vertical`
| ) |  |

Executes the popup besides a widget.

See also:
  positionAt(), popup(const WPointF&)

void Wt::WPopupMenu::popup | ( | const WPoint & | point | ) |

Shows the the popup at a position.

Displays the popup at a point with document coordinates `point`. The positions intelligent, and will chose one of the four menu corners to correspond to this point so that the popup menu is completely visible within the window.

See also:
  exec()

void Wt::WPopupMenu::popup | ( | const WMouseEvent & | event | ) |

Shows the the popup at the location of a mouse event.

This is a convenience method for popup(const WPoint&) that uses the event's document coordinates.

See also:
  popup(const WPoint& p), WMouseEvent::document()

void Wt::WPopupMenu::popup | ( | WWidget * | location,
|  | Orientation | orientation = `Vertical`
| ) |  |

Shows the popup besides a widget.

See also:
  positionAt(), popup(const WPointF&)

WMenuItem* Wt::WPopupMenu::result | ( |  | ) | const

Returns the last triggered menu item.

The result is `0` when the user cancelled the popup menu.

void Wt::WPopupMenu::setAutoHide | ( | bool | enabled,
|  | int | autoHideDelay = `0`
| ) |  |

Configure auto-hide when the mouse leaves the menu.

If `enabled`, The popup menu will be hidden when the mouse leaves the menu for longer than `autoHideDelay` (milliseconds). The popup menu result will be 0, as if the user cancelled.

By default, this option is disabled.

void Wt::WPopupMenu::setHidden | ( | bool | hidden,
|  | const WAnimation & | animation = `WAnimation()`
| ) |  | `[virtual]`

Hides or shows the widget.

Hides or show the widget (including all its descendant widgets). When setting `hidden` = `false`, this widget and all descendant widgets that are not hidden will be shown. A widget is only visible if it and all its ancestors in the widget tree are visible, which may be checked using isVisible().

Reimplemented from Wt::WCompositeWidget.

void Wt::WPopupMenu::setMaximumSize | ( | const WLength & | width,
|  | const WLength & | height
| ) |  | `[virtual]`

Sets a maximum size.

Specifies a maximum size for this widget, setting CSS `max-width` and `max-height` properties.

The default the maximum width and height are WLength::Auto, indicating no maximum size. A WLength::Percentage size should not be used, as this is (in virtually all cases) undefined behaviour.

When the widget is a container widget that contains a layout manager, then setting a maximum size will have the effect of letting the size of the container to reflect the preferred size of the contents (rather than constraining the size of the children based on the size of the container), up to the specified maximum size.

See also:
  resize(), setMinimumSize()

Reimplemented from Wt::WCompositeWidget.

void Wt::WPopupMenu::setMinimumSize | ( | const WLength & | width,
|  | const WLength & | height
| ) |  | `[virtual]`

Sets a minimum size.

Specifies a minimum size for this widget, setting CSS `min-width` and `min-height` properties.

The default minimum width and height is 0. The special value WLength::Auto indicates that the initial width is used as minimum size. A WLength::Percentage size should not be used, as this is (in virtually all cases) undefined behaviour.

When the widget is inserted in a layout manager, then the minimum size will be taken into account.

See also:
  resize(), minimumWidth(), minimumHeight()

Reimplemented from Wt::WCompositeWidget.

Signal<WMenuItem *>& Wt::WPopupMenu::triggered | ( |  | ) |

Signal emitted when an item is selected.

Unlike the itemSelected() signal, triggered() is only emitted by the toplevel popup menu (and not by submenus).

See also:
  aboutToHide(), itemSelected()

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator