// 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 <WFormWidget>
#include <string>

namespace Wt {

  class WAbstractItemModel;
  class WModelIndex;

/*! \class WComboBox WComboBox WComboBox
 *  \brief A widget that provides a drop-down combo-box control.
 *
 * %WComboBox is an \link WWidget::setInline(bool) inline \endlink widget.
 *
 * A combo-box provides the user with a choice of options to chose from.
 * Use addItem(const WString&) or insertItem(int, const WString&) to
 * populate the combo-box. All the content can be cleared through clear().
 *
 * To act on a new selection, either connect a slot to the changed() signal,
 * or to the activated(int) or activated(const std::string) signals.
 *
 * At all times, the current selection index is available through
 * currentIndex() or the text using currentText().
 *
 * By default the WComboBox uses a WStandardItemModel to store the items.
 * This may be changed by specifying a custom model with setModel(). 
 */
class WT_API WComboBox : public WFormWidget
{
public:
  /*! \brief Create an empty combo-box with optional <i>parent</i>.
   */
  WComboBox(WContainerWidget *parent = 0);

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

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

  /*! \brief Get the currently selected item.
   *
   * If no item is currently selected, the method returns -1.
   */
  int  currentIndex() const;

  /*! \brief Insert an option item at the specified position.
   *
   * The option item is inserted at position <i>index</i>.
   */
  void insertItem(int index, const WString& text);

  /*! \brief Remove the option item at the specified position.
   *
   * The option item at position <i>index</i> will be removed.
   */
  void removeItem(int index);

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

  /*! \brief Change the text for a specified option.
   *
   * Use this method to change the text for the item at position <i>index</i>.
   */
  void setItemText(int index, const WString& text);

  /*! \brief Get the text of the currently selected item.
   */
  const WString currentText() const;

  /*! \brief Get the text of a particular item.
   *
   * Use this method to get the text for the item at position <i>index</i>.
   */
  const WString itemText(int index) const;

  /*! \brief Set the model to be used for the items.
   *
   * The <i>model</i> may not be 0, and ownership of the model is not
   * transferred.
   *
   * \sa setModelColumn(int)
   */
  void setModel(WAbstractItemModel *model);

  /*! \brief Set the column in the model to be used for the items.
   *
   * The column <i>index</i> in the model will be used to retrieve data.
   *
   * \sa setModel()
   */
  void setModelColumn(int index);

  WValidator::State validate();

  virtual void refresh();

public slots:
  /*! \brief Clear all items.
   */
  void clear();

public:
  /*! \brief Signal emitted when the selection changed.
   */
  Signal<int> activated;

  /*! \brief Signal emitted when the selection changed.
   */
  Signal<WString> sactivated;

private slots:
  void propagateChange();

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

  bool itemsChanged_;
  bool maxVisibleItemsChanged_;
  bool selectionChanged_;
  bool currentlyConnected_;

  void itemsChanged();

protected:
  virtual void updateDom(DomElement& element, bool all);
  virtual bool isSelected(int index) const;
  virtual DomElement *createDomElement();
  virtual void getDomChanges(std::vector<DomElement *>& result);

  virtual void setFormData(CgiEntry *entry);

  virtual void dummy();

  friend class WSelectionBox;
};

}

#endif // WCOMBOBOX_H_
