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

#include <Wt/WAnchor>
#include <Wt/WFormWidget>
#include <Wt/WJavaScript>
#include <Wt/WLink>
#include <Wt/WText>

namespace Wt {

/*! \class WPushButton Wt/WPushButton Wt/WPushButton
 *  \brief A widget that represents a push button.
 *
 * To act on a button click, connect a slot to the clicked() signal.
 * 
 * \if cpp
 * Usage example:
 * \code
 * Wt::WPushButton *ok = new Wt::WPushButton("Okay");
 * ok->clicked().connect(ok, &Wt::WPushButton::disable);
 * ok->clicked().connect(this, &MyClass::processData);
 * \endcode
 * \endif
 *
 * %WPushButton is an \link WWidget::setInline(bool) inline \endlink widget.
 *
 * <h3>CSS</h3>
 *
 * The widget corresponds to the HTML <tt>&lt;button&gt;</tt> tag (with some
 * exceptions in the bootstrap theme).
 */
class WT_API WPushButton : public WFormWidget
{
public:
  /*! \brief Creates a push button.
   */
  WPushButton(WContainerWidget *parent = 0);

  /*! \brief Creates a push button with given label text.
   *
   * The default text format is PlainText.
   */
  WPushButton(const WString& text, WContainerWidget *parent = 0);

  virtual ~WPushButton();

  /*! \brief Sets the default property.
   *
   * The only effect of a default button is that it is may be rendered
   * in a different style, depending on the theme.
   */
  void setDefault(bool enabled);

  /*! \brief Returns whether the button is a default button.
   *
   * \sa setDefault()
   */
  bool isDefault() const;

  /*! \brief Sets whether the button is checkable.
   *
   * A checkable button can be checked and unchecked, and clicking
   * will toggle between these two states.
   *
   * \sa setChecked(bool)
   */
  void setCheckable(bool checkable);

  /*! \brief Returns whether a button is checkable.
   *
   * \sa setCheckable()
   */
  bool isCheckable() const;

  /*! \brief Sets the button state.
   *
   * This is ignored for a button which is not checkable.
   *
   * This method does not emit one of the checked() or unChecked()
   * signals.
   *
   * \sa setCheckable(), setChecked(), setUnChecked()
   */
  void setChecked(bool checked);

  /*! \brief Checks the button.
   *
   * Does not emit the checked() signal.
   *
   * \sa setChecked(bool)
   */
  void setChecked();

  /*! \brief Unchecks the button.
   *
   * Does not emit the unChecked() signal.
   *
   * \sa setChecked(bool)
   */
  void setUnChecked();

  /*! \brief Returns the button state.
   *
   * \sa setChecked()
   */
  bool isChecked() const;

  /*! \brief Sets the button text.
   *
   * The default text format is Wt::PlainText.
   *
   * When the current text format is Wt::XHTMLText, and \p text is
   * literal (not created using WString::tr()), it is parsed using an
   * XML parser which discards malicious tags and attributes
   * silently. When the parser encounters an XML parse error, the
   * textFormat is changed to Wt::PlainText.
   *
   * Returns whether the text could be set using the current
   * textFormat. A return value of \c false indicates that the text
   * format was changed in order to be able to accept the new text.
   *
   * \sa setTextFormat()
   */
  bool setText(const WString& text);

  /*! \brief Returns the button text.
   *
   * \sa setText()
   */
  const WString& text() const { return text_.text; }

  /*! \brief Sets the text format.
   *
   * The textFormat controls how the string should be interpreted:
   * either as plain text, which is displayed literally, or as
   * XHTML-markup.
   *
   * When changing the textFormat to Wt::XHTMLText, and the
   * current text is literal (not created using WString::tr()), the
   * current text is parsed using an XML parser which discards
   * malicious tags and attributes silently. When the parser
   * encounters an XML parse error, the textFormat is left unchanged,
   * and this method returns false.
   *
   * Returns whether the textFormat could be set for the current text.
   *
   * The default format is Wt::PlainText.
   */
  bool setTextFormat(TextFormat format);

  /*! \brief Returns the text format.
   *
   * \sa setTextFormat()
   */
  TextFormat textFormat() const { return text_.format; }

  /*! \brief Sets an icon.
   *
   * The icon is placed to the left of the text.
   */
  void setIcon(const WLink& link);

  /*! \brief Returns the icon.
   *
   * \sa setIcon()
   */
  WLink icon() const { return icon_; }

  /*! \brief Sets a destination link.
   *
   * This method can be used to make the button behave like a WAnchor
   * (or conversely, an anchor look like a button) and redirect to
   * another URL when clicked.
   *
   * The \p link may be to a URL, a resource, or an internal path.
   *
   * By default, a button does not link to an URL and you should
   * listen to the clicked() signal to react to a click event.
   */
  void setLink(const WLink& link);

  /*! \brief Returns the destination link.
   *
   * \sa setLink()
   */
  const WLink& link() const { return linkState_.link; }

  /*! \brief Sets a destination URL (<b>deprecated</b>).
   *
   * \deprecated Use setLink() insteadd.
   */
  void setRef(const std::string& url);

  /*! \brief Returns the destination URL (<b>deprecated</b>).
   *
   * When the button refers to a resource, the current resource URL is
   * returned. Otherwise, the URL is returned that was set using
   * setRef().
   *
   * \sa setRef(), WResource::url()
   *
   * \deprecated Use link() instead.
   */
  std::string ref() const { return linkState_.link.url(); }

  /*! \brief Sets a destination resource (<b>deprecated</b>).
   *
   * This method can be used to make the button behave like a WAnchor
   * (or conversely, an anchor look like a button) and redirect to
   * another resource when clicked.
   *
   * A resource specifies application-dependent content, which may be
   * generated by your application on demand.
   *
   * This sets the \p resource as the destination of the button,
   * and is an alternative to setRef(). The resource may be cleared by
   * passing \p resource = \c 0.
   *
   * The button does not assume ownership of the resource.
   *
   * \sa setRef()
   *
   * \deprecated Use setLink() instead.
   */
  void setResource(WResource *resource);

  /*! \brief Returns the destination resource (<b>deprecated</b>).
   *
   * Returns \c 0 if no resource has been set.
   *
   * \sa setResource()
   *
   * \deprecated Use link() instead.
   */
  WResource *resource() const { return linkState_.link.resource(); }

  /*! \brief Returns the current value.
   *
   * Returns an empty string, since a button has no value.
   */
  virtual WT_USTRING valueText() const;

  /*! \brief Sets the current value.
   *
   * Has no effect, since a button has not value.
   */
  virtual void setValueText(const WT_USTRING& value);

  /*! \brief Sets the link target.
   *
   * This sets the target where the linked contents should be displayed.
   * The default target is TargetSelf.
   */
  void setLinkTarget(AnchorTarget target);

  /*! \brief Returns the location where the linked content should be
   *         displayed.
   *
   * \sa setLinkTarget()
   */
  AnchorTarget linkTarget() const { return linkState_.target; }

  void setMenu(WPopupMenu *menu);
  WPopupMenu *menu() const { return popupMenu_; }

  virtual void refresh();

  /*! \brief %Signal emitted when the button gets checked.
   *
   * This signal is emitted when the user checks the button.
   *
   * You can use the clicked() signal to react to any change of the
   * button state.
   *
   * \sa setCheckable()
   */
  EventSignal<>& checked();

  /*! \brief %Signal emitted when the button gets unchecked.
   *
   * This signal is emitted when the user unchecks the button.
   *
   * You can use the clicked() signal to react to any change of the
   * button state.
   *
   * \sa setCheckable()
   */
  EventSignal<>& unChecked();

private:
  static const char *CHECKED_SIGNAL;
  static const char *UNCHECKED_SIGNAL;

  static const int BIT_TEXT_CHANGED = 0;
  static const int BIT_ICON_CHANGED = 1;
  static const int BIT_ICON_RENDERED = 2;
  static const int BIT_LINK_CHANGED = 3;
  static const int BIT_DEFAULT = 4;
  static const int BIT_IS_CHECKABLE = 5;
  static const int BIT_IS_CHECKED = 6;
  static const int BIT_CHECKED_CHANGED = 7;

  WAnchor::LinkState linkState_;
  WText::RichText text_;

  WLink          icon_;
  std::bitset<8> flags_;
  WPopupMenu    *popupMenu_;

protected:
  virtual void           updateDom(DomElement& element, bool all);
  virtual DomElementType domElementType() const;
  virtual void           propagateRenderOk(bool deep);
  virtual void           getDomChanges(std::vector<DomElement *>& result,
				       WApplication *app);

private:
  void doRedirect();
  void resourceChanged();
  void renderHRef(DomElement& href);
  void toggled();
};

}

#endif // WPUSHBUTTON_H_
