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

#include <Wt/WAbstractItemModel>
#include <Wt/WFormWidget>
#include <string>

namespace Wt {

/*! \class WComboBox Wt/WComboBox Wt/WComboBox
 *  \brief A widget that provides a drop-down combo-box control.
 *
 * A combo box provides the user with a set of options, from which one
 * option may be selected.
 *
 * %WComboBox is an MVC view class, using a simple string list model
 * by default. The model may be populated using 
 * addItem(const WString&) or 
 * insertItem(int, const WString&) and the contents can
 * be cleared through clear(). These methods manipulate the underlying
 * model().
 *
 * To use the combo box with a custom model instead of the default
 * WStringListModel, use setModel().
 *
 * To react to selection events, connect to the changed(), activated()
 * or sactivated() signals.
 *
 * At all times, the current selection index is available through
 * currentIndex() and the current selection text using currentText().
 *
 * WComboBox does not have support for auto-completion, this behaviour
 * can be found in the WSuggestionPopup.
 *
 * \if cpp
 * Usage example:
 * \code
 * Wt::WComboBox *combo = new Wt::WComboBox(this);
 * combo->addItem("Clint Eastwood");
 * combo->addItem("Mick Jagger");
 * combo->addItem("Johnny Depp");
 * combo->addItem("Kate Winslet");
 *
 * combo->setCurrentIndex(2); // Johnny Depp
 * combo->activated().connect(this, &MyWidget::comboChanged);
 * \endcode
 * \endif 
 *
 * %WComboBox is an \link WWidget::setInline(bool) inline \endlink widget.
 *
 * <h3>CSS</h3>
 *
 * The widget corresponds to the HTML <tt>&lt;select&gt;</tt> tag and
 * does not provide styling. It can be styled using inline or external
 * CSS as appropriate.
 *
 * \ingroup modelview
 */
class WT_API WComboBox : public WFormWidget
{
public:
  /*! \brief Creates an empty combo-box with optional <i>parent</i>.
   */
  WComboBox(WContainerWidget *parent = 0);

  /*! \brief Adds an option item.
   *
   * Equivalent to
   * \link insertItem(int, const WString&) insertItem\endlink (count(),
   * \p text).
   */
  void addItem(const WString& text);

  /*! \brief Returns the number of items
   */
  int count() const;

  /*! \brief Returns the currently selected item.
   *
   * If no item is currently selected, the method returns -1.
   *
   * The default value is 0, unless the combo box is empty.
   */
  int currentIndex() const;

  /*! \brief Inserts an item at the specified position.
   *
   * The item is inserted in the underlying model at position
   * \p index. This requires that the model() is editable.
   *
   * \sa addItem(const WString&), removeItem(int)
   */
  void insertItem(int index, const WString& text);

  /*! \brief Removes the item at the specified position.
   *
   * The item is removed from the underlying model. This requires that
   * the model() is editable.
   *
   * \sa insertItem(int index, const WString&), clear()
   */
  void removeItem(int index);

  /*! \brief Changes the current selection.
   *
   * Specify a value of -1 for \p index to clear the selection.
   */
  void setCurrentIndex(int index);

  /*! \brief Changes the text for a specified option.
   *
   * The text for the item at position \p index is changed. This requires
   * that the model() is editable.
   */
  void setItemText(int index, const WString& text);

  /*! \brief Returns the text of the currently selected item.
   *
   * \sa currentIndex(), itemText(int) const
   */
  const WString currentText() const;

  /*! \brief Returns the text of a particular item.
   *
   * \sa setItemText(int, const WString&), currentText()
   */
  const WString itemText(int index) const;

  /*! \brief Sets the model to be used for the items.
   *
   * The \p model may not be 0, and ownership of the model is not
   * transferred.
   *
   * The default value is a WStringListModel that is owned by the
   * combo box.
   *
   * \sa setModelColumn(int)
   */
  void setModel(WAbstractItemModel *model);

  /*! \brief Sets the column in the model to be used for the items.
   *
   * The column \p index in the model will be used to retrieve data.
   *
   * The default value is 0.
   *
   * \sa setModel()
   */
  void setModelColumn(int index);

  /*! \brief Returns the data model.
   *
   * \sa setModel()
   */
  WAbstractItemModel *model() const { return model_; }

  /*! \brief Returns the index of the first item that matches a text.
   */
  int findText(const WString& text,
	       WFlags<MatchFlag> flags = MatchExactly | MatchCaseSensitive);

  /*! \brief Returns the current value.
   *
   * Returns currentText() converted from a WString to a string.
   */
  virtual WT_USTRING valueText() const;

  /*! \brief Sets the current value.
   *
   * Sets the current index to the item corresponding to \p value.
   */
  virtual void setValueText(const WT_USTRING& value);

  virtual void refresh();

  /*! \brief Clears all items.
   *
   * Removes all items from the underlying model. This requires that the
   * model() is editable.
   */
  void clear();

  /*! \brief %Signal emitted when the selection changed.
   *
   * The newly selected item is passed as an argument.
   *
   * \sa sactivated(), currentIndex()
   */
  Signal<int>& activated() { return activated_; }

  /*! \brief %Signal emitted when the selection changed.
   *
   * The newly selected text is passed as an argument.
   *
   * \sa activated(), currentText()
   */
  Signal<WString>& sactivated() { return sactivated_; }

private:
  WAbstractItemModel *model_;
  int modelColumn_;
  int currentIndex_;

  bool itemsChanged_;
  bool selectionChanged_;
  bool currentlyConnected_;

  std::vector<boost::signals::connection> modelConnections_;

  Signal<int>     activated_;
  Signal<WString> sactivated_;

  void itemsChanged();
  void propagateChange();

  void rowsAboutToBeInserted(const WModelIndex &index, int from, int to);
  void rowsInserted(const WModelIndex &index, int from, int to);
  void rowsAboutToBeRemoved(const WModelIndex &index, int from, int to);
  void rowsRemoved(const WModelIndex &index, int from, int to);

  virtual bool supportsNoSelection() const;

protected:
  virtual void           updateDom(DomElement& element, bool all);
  virtual DomElementType domElementType() const;
  virtual void propagateRenderOk(bool deep);

  virtual void setFormData(const FormData& formData);

  virtual bool isSelected(int index) const;
  virtual void dummy();

  friend class WSelectionBox;
};

}

#endif // WCOMBOBOX_H_
