// 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/WFormWidget>
#include <Wt/WJavaScript>
#include <Wt/WLink>

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 and
 * has the <tt>Wt-btn</tt> style. It may be styled through the current
 * theme, or you can override the style using internal or external CSS
 * as appropriate.
 */
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.
   */
  WPushButton(const WString& text, WContainerWidget *parent = 0);

  virtual ~WPushButton();

  /*! \brief Sets the button text.
   */
  void setText(const WString& text);

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

  /*! \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 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 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 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 linkTarget_; }

  virtual void refresh();

private:
  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;

  WString        text_;
  WLink          icon_;
  WLink          link_;
  AnchorTarget   linkTarget_;
  std::bitset<4> flags_;

  JSlot         *redirectJS_;

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();
};

}

#endif // WPUSHBUTTON_H_
