// 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 WSELECTIONBOX_H_
#define WSELECTIONBOX_H_

#include <Wt/WComboBox>

namespace Wt {

/*! \class WSelectionBox Wt/WSelectionBox Wt/WSelectionBox
 *  \brief A selection box allows selection from a list of options.
 *
 * %WSelectionBox is an \link WWidget::setInline(bool) inline \endlink widget.
 *
 * By default, a selection box may be used to let the user select one
 * item from a list. This may be changed to multiple selection mode
 * using setSelectionMode().
 *
 * The current selection may be set and read using setCurrentIndex()
 * and currentIndex(), for \link Wt::SingleSelection
 * SingleSelection\endlink mode, or setSelectedIndexes() and
 * selectedIndexes() for \link Wt::ExtendedSelection
 * ExtendedSelection\endlink mode. The inherited signals
 * \link WComboBox::activated activated\endlink and
 * \link WComboBox::sactivated sactivated\endlink are not emited
 * in the ExtendedSelection mode, use the signal
 * \link WFormWidget::changed changed\endlink instead.
 *
 * \ingroup modelview
 */
class WT_API WSelectionBox : public WComboBox
{
public:
  WSelectionBox(WContainerWidget *parent = 0);

  /*! \brief Set the number of items that are visible.
   *
   * If more items are available, a scroll-bar is provided.
   */
  int verticalSize() const { return verticalSize_; }

  /*! \brief Get the number of items that are visible.
   */
  void setVerticalSize(int items);

  /*! \brief Set the selection mode.
   *
   * The default selection mode is SingleSelection. You can change to
   * ExtendedSelection to allow selection of multiple items.
   */
  void setSelectionMode(SelectionMode mode);

  /*! \brief Get the selection mode.
   *
   * \sa setSelectionMode(SelectionMode)
   */
  SelectionMode selectionMode() const { return selectionMode_; }   

  /*! \brief Get the current selection (in ExtendedSelection mode).
   *
   * Get the list of currently selected items. This method is only defined
   * when selectionMode() is ExtendedSelection. Otherwise, you should use
   * currentIndex() to get item currently selected.
   *
   * \sa currentIndex()
   */
  const std::set<int>& selectedIndexes() const { return selection_; }

  /*! \brief Set the selection (in ExtendedSelection mode).
   *
   * For an ExtendedSelection mode, set the list of currently selected
   * items.
   *
   * \sa selectedIndexes()
   */
  void setSelectedIndexes(const std::set<int>& selection);

  /*! \brief Clear the current selection.
   *
   * Clears the current selection.
   *
   * \sa setCurrentIndex(), setSelectedIndexes()
   */
  void clearSelection();

private:
  int           verticalSize_;
  SelectionMode selectionMode_;
  std::set<int> selection_;

  bool configChanged_;

protected:
  virtual void updateDom(DomElement& element, bool all);
  virtual void setFormData(CgiEntry *entry);

  virtual bool isSelected(int index) const;
};

}

#endif // WSELECTIONBOX_
