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

#include <WFormWidget>
#include <string>

namespace Wt {

/*! \class WComboBox WComboBox WComboBox
 *  \brief 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().
 * 
 */
class WT_API WComboBox : public WFormWidget
{
public:
  /*! \brief Create an empty ComboBox with optional parent
   */
  WComboBox(WContainerWidget *parent = 0);

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

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

  /*! \brief Get the currently selected item.
   */
  int  currentIndex() const;

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

  /*! \brief Remove the option item at the specified position.
   */
  void removeItem(int index);

  /*! \brief Change the current selection.
   */
  void setCurrentIndex(int index);

  /*! \brief Change the text for a specified option.
   */
  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.
   */
  const WString& itemText(int index) const;

  WValidator::State validate();

  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:
  std::vector<WString> items_;
  int currentIndex_;

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

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

  void        setFormData(CgiEntry *entry);

};

}

#endif // WCOMBOBOX_H_
