Public Member Functions

Wt::WPopupMenu Class Reference

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

#include <Wt/WPopupMenu>

Inherits Wt::WCompositeWidget.

List of all members.

Public Member Functions

 WPopupMenu ()
 Creates a new popup menu.
WPopupMenuItemaddItem (const WString &text)
 Adds an item with given text.
WPopupMenuItemaddItem (const std::string &iconPath, const WString &text)
 Adds an item with given icon and text.
template<class T , class V >
WPopupMenuItemaddItem (const WString &text, T *target, void(V::*method)())
 Adds an item with given text, and specify a slot method to be called when the item is triggered.
template<class T , class V >
WPopupMenuItemaddItem (const std::string &iconPath, const WString &text, T *target, void(V::*method)())
 Adds an item with given text and icon, and specify a slot method to be called when activated.
WPopupMenuItemaddMenu (const WString &text, WPopupMenu *menu)
 Adds a submenu, with given text.
WPopupMenuItemaddMenu (const std::string &iconPath, const WString &text, WPopupMenu *menu)
 Adds a submenu, with given icon and text.
void add (WPopupMenuItem *item)
 Adds a menu item.
void addSeparator ()
 Adds a separator to the 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.
WPopupMenuItemexec (const WPoint &point)
 Executes the the popup at a position.
WPopupMenuItemexec (const WMouseEvent &event)
 Executes the the popup at the location of a mouse event.
WPopupMenuItemexec (WWidget *location, Orientation orientation=Vertical)
 Executes the popup besides a widget.
WPopupMenuItemresult () const
 Returns the last triggered menu item.
virtual void setHidden (bool hidden)
 Sets whether the widget is hidden.
SignalaboutToHide ()
 Signal emitted when the popup is hidden.

Detailed Description

A menu presented in a popup window.

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.
 }

CSS

A WPopupMenu has the Wt-popupmenu style class. The look can be overridden using the following style class selectors:

.Wt-popupmenu .Wt-item, .Wt-popupmenu .Wt-selected : item
.Wt-popupmenu .Wt-selected                         : selected item
.Wt-popupmenu .Wt-separator                        : separator

A snapshot of the WPopupMenu:

WPopupMenu-default-1.png

WPopupMenu example (default)

WPopupMenu-polished-1.png

WPopupMenu example (polished)

See also:
WPopupMenuItem

Constructor & Destructor Documentation

Wt::WPopupMenu::WPopupMenu (  ) 

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 (  )  [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.

void Wt::WPopupMenu::add ( WPopupMenuItem item  ) 

Adds a menu item.

Adds an item to the popup menu.

WPopupMenuItem * Wt::WPopupMenu::addItem ( const std::string &  iconPath,
const WString text 
)

Adds an item with given icon and text.

Adds an item to the menu with given text and icon, and returns the corresponding item object.

Note:
The icon should have a width of 16 pixels.
See also:
add(WPopupMenuItem *)
template<class T , class V >
WPopupMenuItem * Wt::WPopupMenu::addItem ( const std::string &  iconPath,
const WString text,
T *  target,
void(V::*)()  method 
)

Adds 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.

Note:
The icon should have a width of 16 pixels.
See also:
add(WPopupMenuItem *)
WPopupMenuItem * Wt::WPopupMenu::addItem ( const WString text  ) 

Adds an item with given text.

Adds an item to the menu with given text, and returns the corresponding item object.

See also:
add(WPopupMenuItem *)
template<class T , class V >
WPopupMenuItem * Wt::WPopupMenu::addItem ( const WString text,
T *  target,
void(V::*)()  method 
)

Adds 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.

See also:
add(WPopupMenuItem *)
WPopupMenuItem * Wt::WPopupMenu::addMenu ( const WString text,
WPopupMenu menu 
)

Adds a submenu, with given text.

Adds an item with text text, that leads to a submenu menu.

See also:
add(WPopupMenuItem *)
WPopupMenuItem * Wt::WPopupMenu::addMenu ( const std::string &  iconPath,
const WString text,
WPopupMenu menu 
)

Adds a submenu, with given icon and text.

Adds an item with given text and icon, that leads to a submenu menu.

See also:
add(WPopupMenuItem *)
void Wt::WPopupMenu::addSeparator (  ) 

Adds a separator to the menu.

Adds a separator the popup menu. The separator is an empty div with style-class "separator".

WPopupMenuItem * 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()
WPopupMenuItem * 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&)
WPopupMenuItem * 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 ( WWidget location,
Orientation  orientation = Vertical 
)

Shows the popup besides a widget.

See also:
positionAt(), popup(const WPointF&)
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()
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().

See also:
hide(), show()

Reimplemented from Wt::WCompositeWidget.


Generated on Mon Nov 29 2010 08:03:19 for Wt by doxygen 1.7.1