Wt::WMenu Class Reference

A widget that shows a menu of options. More...

#include <Wt/WMenu>

Inherits Wt::WCompositeWidget.

List of all members.

Public Member Functions

 WMenu (WStackedWidget *contentsStack, Orientation orientation, WContainerWidget *parent=0)
 Creates a new menu.
 ~WMenu ()
 Destructor.
WMenuItemaddItem (const WString &name, WWidget *contents, WMenuItem::LoadPolicy policy=WMenuItem::LazyLoading)
 Adds an item.
WMenuItemaddItem (WMenuItem *item)
 Adds an item.
void removeItem (WMenuItem *item)
 Removes an item.
void select (WMenuItem *item)
 Selects an item.
void select (int index)
 Selects an item.
Signal< WMenuItem * > & itemSelected ()
 Signal which indicates that a new item was selected.
Signal< WMenuItem * > & itemSelectRendered ()
 Signal which indicates that a new selected item is rendered.
const std::vector< WMenuItem * > & items () const
 Returns the items.
WMenuItemcurrentItem () const
 Returns the currently selected item.
int currentIndex () const
 Returns the index of the currently selected item.
Orientation orientation () const
 Returns the orientation.
void setRenderAsList (bool enable)
 Renders using an HTML list.
bool renderAsList () const
 Returns whether the menu is rendered as an HTML list.
void setInternalPathEnabled (const std::string &basePath="")
 Enables internal paths for items.
bool internalPathEnabled () const
 Returns whether the menu generates internal paths entries.
void setInternalBasePath (const std::string &basePath)
 Sets the internal base path.
const std::string & internalBasePath () const
 Returns the internal base path.

Protected Member Functions

virtual void enableAjax ()
 Progresses to an Ajax-enabled widget.

Detailed Description

A widget that shows a menu of options.

The WMenu widget offers menu navigation in conjunction with a WStackedWidget, where different 'contents' are stacked upon each other. Each choice in the menu (which is implemented as a WMenuItem) corresponds to a tab in the contents stack. The contents stack may contain other items, and could be shared with other WMenu instances. (the old restriction of a dedicated contents stack has been removed since Wt 2.2.1).

Usage example:

 // create the stack where the contents will be located
 Wt::WStackedWidget *contents = new Wt::WStackedWidget(contentsParent);

 // create a menu
 Wt::WMenu *menu = new Wt::WMenu(contents, Wt::Vertical, menuParent);

 // add four items using the default lazy loading policy.
 menu->addItem("Introduction", new Wt::WText("intro"));
 menu->addItem("Download", new Wt::WText("Not yet available"));
 menu->addItem("Demo", new DemoWidget());
 menu->addItem(new Wt::WMenuItem("Demo2", new DemoWidget()));

After contruction, the first entry will be selected. At any time, it is possible to select a particular item using select().

The WMenu implementation offers fine-grained control on how contents should be preloaded. By default, all contents is lazy-loaded, only when needed. To improve response time, an item may also be preloaded (using addItem()). In that case, the item will be loaded in the background, before its first use. In any case, once the contents corresponding to a menu item is loaded, subsequent navigation to it is handled entirely client-side.

The WMenu may participate in the application's internal path, which lets menu items correspond to internal URLs, see setInternalPathEnabled().

The layout of the menu may be Horizontal or Vertical. The look of the items may be defined through style sheets. The default WMenuItem implementation uses two style classes to distinguish between activated and inactivated menu items: item and itemselected. By using CSS nested selectors, a different style may be defined for items in a different menu.

You may customize the rendering and behaviour of menu entries by specializing WMenuItem.

CSS

Styling a menu will be different depending of the rendering mode (table, list). Conventionally, menus like this are styled as a list.

You will want to differentiate between unselected and selected items based on the item and itemselected style classes.

.menu * .item {
  cursor: pointer; cursor: hand;
  color: blue;
  text-decoration: underline;
}

.menu * .itemselected {
  color: blue;
  text-decoration: underline;
  font-weight: bold;  
}
See also:
WMenuItem

Constructor & Destructor Documentation

Wt::WMenu::WMenu ( WStackedWidget contentsStack,
Orientation  orientation,
WContainerWidget parent = 0 
)

Creates a new menu.

Construct a menu to manage the widgets in contentsStack, and sets the menu orientation.

Each menu item will manage a single widget in the contentsStack, making it the current widget when the menu item is activated.


Member Function Documentation

WMenuItem * Wt::WMenu::addItem ( WMenuItem item  ) 

Adds an item.

Adds a menu item. Use this form to add specialized WMenuItem implementations.

See also:
addItem(const WString&, WWidget *, WMenuItem::LoadPolicy)
WMenuItem * Wt::WMenu::addItem ( const WString name,
WWidget contents,
WMenuItem::LoadPolicy  policy = WMenuItem::LazyLoading 
)

Adds an item.

Adds a menu item with given contents, which is added to the menu's associated contents stack.

contents may be 0, in which case no contents in the contents stack is associated with the menu item.

Returns the corresponding WMenuItem.

See also:
addItem(WMenuItem *)
int Wt::WMenu::currentIndex (  )  const [inline]

Returns the index of the currently selected item.

See also:
currentItem(), select(int)
WMenuItem * Wt::WMenu::currentItem (  )  const

Returns the currently selected item.

See also:
currentIndex(), select(WMenuItem *)
void Wt::WMenu::enableAjax (  )  [protected, virtual]

Progresses to an Ajax-enabled widget.

This method is called when the progressive bootstrap method is used, and support for AJAX has been detected. The default behavior will upgrade the widget's event handling to use AJAX instead of full page reloads, and propagate the call to its children.

You may want to reimplement this method if you want to make changes to widget when AJAX is enabled. You should always call the base implementation.

See also:
WApplication::enableAjax()

Reimplemented from Wt::WCompositeWidget.

const std::string& Wt::WMenu::internalBasePath (  )  const [inline]

Returns the internal base path.

The default value is the application's internalPath (WApplication::internalPath()) that was recorded when setInternalPathEnabled() was called, and together with each WMenuItem::pathComponent() determines the paths for each item.

For example, if internalBasePath() is "/examples/" and pathComponent() for a particular item is "charts/", then the internal path for that item will be "/examples/charts/".

See also:
setInternalPathEnabled()
bool Wt::WMenu::internalPathEnabled (  )  const [inline]

Returns whether the menu generates internal paths entries.

See also:
setInternalPathEnabled()
const std::vector<WMenuItem *>& Wt::WMenu::items (  )  const [inline]

Returns the items.

Returns the list of menu items in this menu.

Signal<WMenuItem *>& Wt::WMenu::itemSelected (  )  [inline]

Signal which indicates that a new item was selected.

This signal is emitted when an item was selected. It is emitted both when the user activated an item, or when select() was invoked.

See also:
itemSelectRendered
Signal<WMenuItem *>& Wt::WMenu::itemSelectRendered (  )  [inline]

Signal which indicates that a new selected item is rendered.

This signal is similar to itemSelected, but is emitted from within a stateless slot. Therefore, any slot connected to this signal will be optimized to client-side JavaScript, and must support the contract of a stateless slot.

If you are unsure what is the difference with the itemSelected signal, you'll probably need the latter instead.

See also:
itemSelected
Orientation Wt::WMenu::orientation (  )  const [inline]

Returns the orientation.

The orientation is set at time of construction.

void Wt::WMenu::removeItem ( WMenuItem item  ) 

Removes an item.

Removes the given item. The item and its contents is not deleted.

See also:
addItem()
bool Wt::WMenu::renderAsList (  )  const [inline]

Returns whether the menu is rendered as an HTML list.

See also:
setRenderAsList(bool)
void Wt::WMenu::select ( int  index  ) 

Selects an item.

Menu items in a menu with N items are numbered from 0 to N - 1.

See also:
select(WMenuItem *), currentIndex()
void Wt::WMenu::select ( WMenuItem item  ) 

Selects an item.

Select the menu item item.

See also:
select(int), currentItem()
void Wt::WMenu::setInternalBasePath ( const std::string &  basePath  ) 

Sets the internal base path.

A '/' is appended to turn it into a folder, if needed.

See also:
setInternalPathEnabled(), internalBasePath()
void Wt::WMenu::setInternalPathEnabled ( const std::string &  basePath = ""  ) 

Enables internal paths for items.

The menu participates in the internal path by changing the internal path when an item has been selected, and listening for path changes to react to path selections. As a consequence this allows the user to bookmark the current menu selection and revisit it later, use back/forward buttons to navigate through history of visited menu items, and allows indexing of pages.

For each menu item, WMenuItem::pathComponent() is appended to the internal base path (internalBasePath()), which defaults to the internal path (WApplication::internalPath()) but may be changed using setInternalBasePath(), with a '/' appended to turn it into a folder, if needed.

By default, menu interaction does not change the application internal path.

See also:
WMenuItem::setPathComponent().
void Wt::WMenu::setRenderAsList ( bool  enable  ) 

Renders using an HTML list.

By default, the the menu is rendered using an HTML <table> element for layout. Setting this option enables rendering using <ul> and <il> elements, as is commonly done for CSS-based designs.

Note:
You cannot use this method after items have been added to the menu.

Generated on Thu May 13 05:16:17 2010 for Wt by doxygen 1.6.3