#include <Wt/WPopupMenu>
Public Member Functions | |
WPopupMenu () | |
Create a new popup menu. | |
WPopupMenuItem * | addItem (const WString &text) |
Add an item with given text. | |
WPopupMenuItem * | addItem (const std::string &iconPath, const WString &text) |
Add an item with given icon and text. | |
template<class T, class V> | |
WPopupMenuItem * | addItem (const WString &text, T *target, void(V::*method)()) |
Add an item with given text, and specify a slot method to be called when the item is triggered. | |
template<class T, class V> | |
WPopupMenuItem * | addItem (const std::string &iconPath, const WString &text, T *target, void(V::*method)()) |
Add an item with given text and icon, and specify a slot method to be called when activated. | |
WPopupMenuItem * | addMenu (const WString &text, WPopupMenu *menu) |
Add a submenu, with given text. | |
WPopupMenuItem * | addMenu (const std::string &iconPath, const WString &text, WPopupMenu *menu) |
Add a submenu, with given icon and text. | |
void | add (WPopupMenuItem *item) |
Add a menu item. | |
void | addSeparator () |
Add a separator to the menu. | |
void | popup (const WPoint &point) |
Show the the popup at a position. | |
void | popup (const WMouseEvent &event) |
Show the the popup at the location of a mouse event. | |
WPopupMenuItem * | exec (const WPoint &point) |
Execute the the popup at a position. | |
WPopupMenuItem * | exec (const WMouseEvent &event) |
Execute the the popup at the location of a mouse event. | |
WPopupMenuItem * | result () const |
Returns the last triggered menu item. | |
virtual void | setHidden (bool hidden) |
Sets whether the widget is hidden. | |
Signal & | aboutToHide () |
Signal emitted when the popup is hidden. |
The menu implements a typical context menu, with support for submenu's. It 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 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.
Similar in use as WDialog, there are two ways of using the menu. The simplest way is to use one of the exec() methods, to use a reentrant event loop and wait 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 aboutToHide signal where you read the result().
You have several options to react to the selection of an item:
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); WPopupMenuItem *item = popup.exec(event); if (item) { // ... do associated action. }
The menu implementation does not provide any style. You can style the menu using CSS.
For example:
div.Wt-popupmenu { background: white; color: black; border: 1px solid #666666; z-index: 200; cursor: default; } div.Wt-popupmenu .notselected, div.Wt-popupmenu .selected { padding: 2px 0px; } div.Wt-popupmenu .selected { background: blue; color: white; } div.Wt-popupmenu .separator { border-top: 1px solid #CCCCCC; border-bottom: 1px solid #DDDDDD; margin: 0px 3px; }
Wt::WPopupMenu::WPopupMenu | ( | ) |
WPopupMenuItem * Wt::WPopupMenu::addItem | ( | const WString & | text | ) |
Add an item with given text.
Adds an item to the menu with given text, and returns the corresponding item object.
WPopupMenuItem * Wt::WPopupMenu::addItem | ( | const std::string & | iconPath, | |
const WString & | text | |||
) |
Add an item with given icon and text.
Adds an item to the menu with given text and icon, and returns the corresponding item object.
WPopupMenuItem * Wt::WPopupMenu::addItem | ( | const WString & | text, | |
T * | target, | |||
void(V::*)() | method | |||
) | [inline] |
Add an item with given text, and specify a slot method to be called when the item is triggered.
The target and method
are connected to the WPopupMenuItem::triggered signal.
WPopupMenuItem * Wt::WPopupMenu::addItem | ( | const std::string & | iconPath, | |
const WString & | text, | |||
T * | target, | |||
void(V::*)() | method | |||
) | [inline] |
Add an item with given text and icon, and specify a slot method to be called when activated.
The target and method
are connected to the WPopupMenuItem::triggered signal.
WPopupMenuItem * Wt::WPopupMenu::addMenu | ( | const WString & | text, | |
WPopupMenu * | menu | |||
) |
Add a submenu, with given text.
Adds an item with text text
, that leads to a submenu menu
.
WPopupMenuItem * Wt::WPopupMenu::addMenu | ( | const std::string & | iconPath, | |
const WString & | text, | |||
WPopupMenu * | menu | |||
) |
Add a submenu, with given icon and text.
Adds an item with given text and icon, that leads to a submenu menu
.
void Wt::WPopupMenu::add | ( | WPopupMenuItem * | item | ) |
Add a menu item.
Adds an item to the popup menu.
void Wt::WPopupMenu::addSeparator | ( | ) |
Add a separator to the menu.
Adds a separator the popup menu. The separator is an empty div with style-class "separator".
void Wt::WPopupMenu::popup | ( | const WPoint & | point | ) |
Show 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.
void Wt::WPopupMenu::popup | ( | const WMouseEvent & | event | ) |
Show 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.
WPopupMenuItem * Wt::WPopupMenu::exec | ( | const WPoint & | point | ) |
WPopupMenuItem * Wt::WPopupMenu::exec | ( | const WMouseEvent & | event | ) |
Execute 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.
WPopupMenuItem* Wt::WPopupMenu::result | ( | ) | const [inline] |
Returns the last triggered menu item.
The result is 0
when the user cancelled the popup menu.
void Wt::WPopupMenu::setHidden | ( | bool | hidden | ) | [virtual] |
Sets whether the widget is hidden.
Hides or show the widget (including all its descendant widgets). setHidden(false) will show this widget and all descendant widgets that are not hidden. 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.
Signal& Wt::WPopupMenu::aboutToHide | ( | ) | [inline] |
Signal emitted when the popup is hidden.
This signal is emitted when the popup is hidden, either because an item was selected, or when the menu was cancelled.
You can use result() to get the selected item.