#include <Wt/WMenu>
Public Member Functions | |
WMenu (Orientation orientation, WContainerWidget *parent=0) | |
Creates a new menu. | |
WMenu (WStackedWidget *contentsStack, Orientation orientation, WContainerWidget *parent=0) | |
Creates a new menu. | |
~WMenu () | |
Destructor. | |
WMenuItem * | addItem (const WString &name, WWidget *contents, WMenuItem::LoadPolicy policy=WMenuItem::LazyLoading) |
Adds an item. | |
WMenuItem * | addItem (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. | |
WMenuItem * | currentItem () 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. | |
WStackedWidget * | contentsStack () const |
Returns the contents stack associated with the menu. | |
Protected Member Functions | |
virtual void | enableAjax () |
Progresses to an Ajax-enabled widget. | |
virtual void | internalPathChanged (const std::string &path) |
Handling of internal path changes. |
The WMenu widget offers menu navigation.
Typically, a menu is used in conjunction with a WStackedWidget (but can be used without too), 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.
When used without a contents stack, you can react to menu item selection using the itemSelected() signal, to implement some custom handling of item selection.
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.
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; }
Wt::WMenu::WMenu | ( | Orientation | orientation, | |
WContainerWidget * | parent = 0 | |||
) |
Creates a new menu.
Construct a menu with given orientation
. The menu is not associated with a contents stack, and thus you will want to react to the itemSelected() signal to react to menu changes.
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.
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
for two reasons:
Returns the corresponding WMenuItem.
void Wt::WMenu::removeItem | ( | WMenuItem * | item | ) |
Removes an item.
Removes the given item. The item and its contents is not deleted.
void Wt::WMenu::select | ( | WMenuItem * | item | ) |
void Wt::WMenu::select | ( | int | index | ) |
Selects an item.
Menu items in a menu with N
items are numbered from 0 to N
- 1.
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.
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.
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 |
int Wt::WMenu::currentIndex | ( | ) | const [inline] |
Orientation Wt::WMenu::orientation | ( | ) | const [inline] |
Returns the orientation.
The orientation is set at time of construction.
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.
bool Wt::WMenu::renderAsList | ( | ) | const [inline] |
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.
bool Wt::WMenu::internalPathEnabled | ( | ) | const [inline] |
void Wt::WMenu::setInternalBasePath | ( | const std::string & | basePath | ) |
Sets the internal base path.
A '/' is appended to turn it into a folder, if needed.
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/"
.
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.
Reimplemented from Wt::WCompositeWidget.
void Wt::WMenu::internalPathChanged | ( | const std::string & | path | ) | [protected, virtual] |
Handling of internal path changes.
This methods makes the menu react to internal path changes (and also the initial internal path).
You may want to reimplement this if you want to customize the internal path handling.