Wt::WMenu Class Reference

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

#include <Wt/WMenu>

Inheritance diagram for Wt::WMenu:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 WMenu (WStackedWidget *contentsStack, Orientation orientation, WContainerWidget *parent=0)
 Construct a new menu.
 ~WMenu ()
 Destructor.
WMenuItemaddItem (const WString &name, WWidget *contents, WMenuItem::LoadPolicy policy=WMenuItem::LazyLoading)
 Add an item.
WMenuItemaddItem (WMenuItem *item)
 Add an item.
void removeItem (WMenuItem *item)
 Remove an item.
void select (WMenuItem *item)
 Select an item.
void select (int index)
 Select 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)
 Render using an HTML list.
bool renderAsList () const
 Return whether the menu is rendered as an HTML list.
void setInternalPathEnabled ()
 Enable internal paths for items.
bool internalPathEnabled () const
 Returns whether the menu generates internal paths entries.
void setInternalBasePath (const std::string &basePath)
 Set the internal base path.
const std::string & internalBasePath () const
 Returns the internal base path.


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(tr("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.

For example, the (old) Wt homepage used the following CSS rules to style the two menu (which both are assigned the style class .menu):

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

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

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

See also:
WMenuItem

Constructor & Destructor Documentation

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

Construct 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 ( const WString name,
WWidget contents,
WMenuItem::LoadPolicy  policy = WMenuItem::LazyLoading 
)

Add 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 *)

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

Add an item.

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

See also:
addItem(const WString&, WWidget *, WMenuItem::LoadPolicy)

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

Remove an item.

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

See also:
addItem(), ~WMenuItem()

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

Select an item.

Select the menu item item.

See also:
select(int), currentItem()

void Wt::WMenu::select ( int  index  ) 

Select an item.

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

See also:
select(WMenuItem *), currentIndex()

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

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

Returns the items.

Returns the list of menu items in this menu.

WMenuItem * Wt::WMenu::currentItem (  )  const

Returns the currently selected item.

See also:
currentIndex(), select(WMenuItem *)

int Wt::WMenu::currentIndex (  )  const [inline]

Returns the index of the currently selected item.

See also:
currentItem(), select(int)

Orientation Wt::WMenu::orientation (  )  const [inline]

Returns the orientation.

The orientation is set at time of construction.

void Wt::WMenu::setRenderAsList ( bool  enable  ) 

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

bool Wt::WMenu::renderAsList (  )  const [inline]

Return whether the menu is rendered as an HTML list.

See also:
setRenderAsList(bool)

void Wt::WMenu::setInternalPathEnabled (  ) 

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

bool Wt::WMenu::internalPathEnabled (  )  const [inline]

Returns whether the menu generates internal paths entries.

See also:
setInternalPathEnabled()

void Wt::WMenu::setInternalBasePath ( const std::string &  basePath  ) 

Set the internal base path.

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

See also:
setInternalPathEnabled(), internalBasePath()

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


Generated on Mon Mar 9 08:27:51 2009 for Wt by doxygen 1.5.6