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

#include <Wt/WCompositeWidget>
#include <Wt/WDate>
#include <set>

namespace Wt {

class WCalendar;
class WInteractWidget;
class WLineEdit;

/*! \class WDatePicker Wt/WDatePicker Wt/WDatePicker
 *  \brief A date picker.
 *
 * A date picker works in conjunction with a WLineEdit to popup a
 * WCalendar for editing the date. The date picker itself is typically
 * displayed as a button next to the line edit. When the user clicks
 * the button, a calendar pops up which may be used to pick a new
 * date. Any date entered in the line edit is reflected in the calendar,
 * and vice-versa.
 *
 * Each of these widgets may be accessed individually (lineEdit(),
 * calendar(), and displayWidget()).
 * 
 * The date format used by default is 'dd/MM/yyyy', but can be changed
 * using setFormat(). At any time, the date set may be read using
 * date(), or can be changed using setDate().
 *
 * \image html WDatePicker-1.png "WDatePicker using a WPushButton as displayWidget"
 */
class WT_API WDatePicker : public WCompositeWidget
{
public:
  /*! \brief Create a new date picker.
   *
   * The <i>displayWidget</i> represents the button or image which
   * much be clicked to open the date picker. This widget may not yet
   * have a parent, and becomes owned by the picker.
   *
   * The <i>forEdit</i> argument is the lineEdit which works in
   * conjunction with the date picker. This widget is not part of the
   * date picker, and may be located anywhere else.
   *
   * <i>i18n</i> is passed to the WCalendar constructor.
   */
  WDatePicker(WInteractWidget *displayWidget,
	      WLineEdit *forEdit,
	      bool i18n = false, WContainerWidget *parent = 0);

  /*! \brief Set the format used for parsing or writing the date in
   *         the line edit.
   *
   * Sets the format used for parsing the value of the lineEdit, or
   * for setting the value of the lineEdit.
   *
   * \sa format(), WDate::toString(const WString&) const
   */
  void setFormat(const WString& format);

  /*! \brief Get the format.
   *
   * Get the current format used to parse dates from the lineEdit text, or
   * to set the lineEdit text from a selected date in the calendar.
   *
   * \sa setFormat()
   */
  const WString& format() const { return format_; }

  /*! \brief The calendar widget.
   *
   * Returns the calendar widget.
   */
  WCalendar *calendar() const { return calendar_; }

  /*! \brief The line edit.
   *
   * Returns the line edit which works in conjunction with this date
   * picker.
   */
  WLineEdit *lineEdit() const { return forEdit_; }

  /*! \brief The display widget.
   *
   * Returns the widget which is displayed to activate the calendar.
   */
  WInteractWidget *displayWidget() const { return displayWidget_; }

  /*! \brief The current date.
   *
   * Reads the current date from the lineEdit().
   *
   * Returns an invalid date (for which WDate::isValid() returns
   * false) if the date could not be parsed using the current
   * format(). <br>
   *
   * \sa setDate()
   * \sa WDate::fromString(const WString&, const WString&)
   * \sa WLineEdit::text()
   */
  WDate date() const;

  /*! \brief Set the current date.
   *
   * Does nothing if the current date is <i>Null</i>.
   *
   * \sa date()
   */
  void setDate(const WDate& date);

private:
  WString          format_;
  WInteractWidget  *displayWidget_;
  WLineEdit        *forEdit_;

  WContainerWidget *layout_;
  WContainerWidget *popup_;
  WCalendar        *calendar_;

  void setFromCalendar();
  void setFromLineEdit();
};

}

#endif // WDATEPICKER_H_
