// This may look like C code, but it's really -*- C++ -*-
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#ifndef WMENU_H_
#define WMENU_H_

#include <vector>

#include <Wt/WCompositeWidget>
#include <Wt/WMenuItem>

namespace Wt {

class WStackedWidget;
class WTable;

/*! \class WMenu Wt/WMenu Wt/WMenu
 *  \brief 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 is dedicated to
 * the menu, and should not contain any other widgets than those that are
 * added to it by the %WMenu.
 *
 * An example for using %WMenu is:
 *
 * \code
   // create the stack where the contents will be located
   WStackedWidget *contents = new WStackedWidget(contentsParent);

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

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

 * \endcode
 *
 * After contruction, by default, the first entry will be selected.
 * At any time, it is possible to select a particular item using the
 * select() member.
 *
 * 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. Once
 * loaded, the contents will be 'cached', and menu operation is also
 * completely client-side.
 *
 * 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 %Wt homepage uses the following CSS rules to
 * style the two menu (which both are assigned the style class .menu):
 *
 * \code
.menu * .item {
  cursor: pointer; cursor: hand;
  color: blue;
  text-decoration: underline;
}

.menu * .itemselected {
  color: blue;
  text-decoration: underline;
  font-weight: bold;  
}
 * \endcode
 *
 * \sa WMenuItem
 */
class WT_API WMenu : public WCompositeWidget
{
public:
  /*! \brief Construct a new WMenu.
   *
   * Construct a menu to manage the widgets in the given contents stack,
   * and with the given orientation.
   */
  WMenu(WStackedWidget *contentsStack,
	Orientation orientation,
	WContainerWidget *parent = 0);

  /*! \brief Destructor.
   */
  ~WMenu();

  /*! \brief Add a menu item.
   *
   * Adds a menu text item, associated with the contents.
   *
   * Returns the corresponding WMenuItem.
   *
   * \sa addItem(WMenuItem *)
   */
  WMenuItem *addItem(const WString& name, WWidget *contents,
		     WMenuItem::LoadPolicy policy = WMenuItem::LazyLoading);

  /*! \brief Add a menu item.
   *
   * Adds a menu item. Use this form to add specialized WMenuItem
   * implementations (with a different look than the default text).
   *
   * \sa addItem(const WString&, WWidget *, WMenuItem::LoadPolicy)
   */
  WMenuItem *addItem(WMenuItem *item);

  /*! \brief Select the menu item.
   *
   * Select the given menu item.
   *
   * \sa select(int), currentItem()
   */
  void select(WMenuItem *item);

  /*! \brief Select the menu item with the given index number.
   *
   * Menu items in a menu with N items are indexed from 0 to N-1.
   *
   * \sa select(WMenuItem *), currentIndex()
   */
  void select(int index);

  /*! \brief %Signal emitted when a new item is selected.
   *
   * This signal is emitted when a new menu item is selected, either by
   * the user or through a call to one of the select() methods.
   *
   * \sa itemSelectRendered
   */
  Signal<WMenuItem *> itemSelected;

  /*! \brief %Signal emitted when a new selected item is rendered.
   *
   * This signal is similar to \link WMenu::itemSelected
   * itemSelected\endlink, 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 \link
   * WMenu::itemSelected itemSelected\endlink signal, you'll probably
   * need the latter instead.
   *
   * \sa itemSelected
   */
  Signal<WMenuItem *> itemSelectRendered;

  /*! \brief Make the menu react to browser history.
   *
   * By reacting to browser history, the menu will set and read its
   * state in user bookmarks, and will react to the user navigating
   * through its history with the forward and backward buttons.
   *
   * The id must be an application-wide unique id that identifies this
   * menu, and will become part of the URL.
   *
   * \sa browserHistoryId()
   */
  void enableBrowserHistory(const std::string& id);

  /*! \brief Get the browser history key for this menu.
   *
   * Returns the browser history id that was previously set using
   * enableBrowserHistory(), or an empty string otherwise.
   *
   * \sa enableBrowserHistory()
   */
  const std::string& browserHistoryId() const { return historyScope_; }

  /*! \brief The menu items.
   *
   * Returns the list of menu items in this menu.
   */
  const std::vector<WMenuItem *>& items() const { return items_; }

  /*! \brief The currently selected item.
   *
   * \sa currentIndex(), select(WMenuItem *)
   */
  WMenuItem *currentItem() const;

  /*! \brief Get the index of the currently selected item.
   *
   * \sa currentItem(), select(int)
   */
  int currentIndex() const { return current_; }

  /*! \brief Get the orientation for this menu.
   *
   * The orientation must be set at time of construction.
   */
  Orientation orientation() const { return orientation_; }

  /*! \brief Render using an HTML list.
   *
   * By default, the the menu is rendered using an HTML &lt;table&gt;
   * element for layout. Setting this option enables rendering using
   * &lt;ul&gt; and &lt;il&gt; elements, as is commonly done for
   * CSS-based designs.
   *
   * <i>You cannot use this method after items have been added to the
   * menu.</i>
   */
  void setRenderAsList(bool enable);

  /*! \brief Return whether the menu is rendered as an HTML list.
   *
   * \sa setRenderAsList(bool)
   */
  bool renderAsList() const { return renderAsList_; }

private:
  WWidget          *impl_;
  WStackedWidget   *contentsStack_;
  Orientation       orientation_;
  bool              renderAsList_;
  std::string       historyScope_;

  std::vector<WMenuItem *> items_;

  int current_;
  int previousCurrent_;

  int indexOf(WMenuItem *item);

  void selectVisual(WMenuItem *item);
  void selectVisual(int item);
  void undoSelectVisual();
  friend class WMenuItem;

  void appStateChanged(std::string scope, std::string value);
  void setFromState(const std::string& value);
};

}

#endif // HOME_H_
