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

#include <WContainerWidget>

namespace Wt {

class WText;
class WImage;

/*! \brief Enumeration that specifies where the target of an anchor should
 *         be displayed. 
 */
enum AnchorTarget {
  TargetSelf,      //!< Show Instead of the application
  TargetThisWindow,//!< Show in the top level frame of the application window
  TargetNewWindow  //!< Show in a separate new tab or window
};

/*! \class WAnchor WAnchor WAnchor
 *  \brief A widget that represents an HTML anchor (to link to other documents)
 *
 * %WAnchor is an \link WWidget::setInline(bool) inline \endlink widget.
 *
 * Use an anchor to link to another web page, using a text and/or image.
 */
class WAnchor : public WContainerWidget
{
public:
  /*! \brief Create an anchor going nowhere without any text
   */
  WAnchor(WContainerWidget *parent = 0);

  /*! \brief Create an anchor with a destination URL, but without any text.
   */
  WAnchor(const std::string& ref, WContainerWidget *parent = 0);

  /*! \brief Create an anchor with a dynamically generated document,
   *         but without any text.
   */
  WAnchor(WResource *resource, WContainerWidget *parent = 0);

  /*! \brief Create an anchor with a destination URL and anchor message.
   */
  WAnchor(const std::string& ref, const WString& text,
	  WContainerWidget *parent = 0);

  /*! \brief Create an anchor with a dynamically generated document
   *         and anchor message.
   */
  WAnchor(WResource *resource, const WString& text,
	  WContainerWidget *parent = 0);

  /*! \brief Create an anchor with a destination URL and anchor image.
   */
  WAnchor(const std::string& ref, WImage *image, WContainerWidget *parent = 0);

  /*! \brief Create an anchor with a dynamically generated document
   *         and anchor image.
   */
  WAnchor(WResource *resource, WImage *image, WContainerWidget *parent = 0);

  /*! \brief Change the destination URL for this anchor.
   *
   * Not available when the document is specified as a resource.
   */
  void setRef(const std::string& ref);

  /*! \brief Get the destination URL for this anchor.
   *
   * When the document is specified as a resource, this returns the current
   * resource URL.
   */
  const std::string& ref() const { return ref_; }

  /*! \brief Set the resource for a dynamically generated document.
   *
   * This overrides the URL set by setRef() with the URL that corresponds to
   * the resource.
   */
  void setResource(WResource *resource);

  /*! \brief Get the resource.
   */
  WResource *resource() const { return resource_; }

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

  /*! \brief Get the label text.
   */
  const WString& text() const;

  /*! \brief Set the image.
   */
  void setImage(WImage *image);

  /*! \brief Get the image.
   */
  WImage *image() const { return image_; }

  /*! \brief Specify where the location where the reference should be displayed.
   *
   * By default, the reference is displayed instead of the application.
   *
   * \sa target()
   */
  void setTarget(AnchorTarget target);

  /*! \brief Get the location where the reference should be displayed.
   *
   * \sa setTarget(AnchorTarget)
   */
  AnchorTarget target() const { return target_; }

private:
  std::string   ref_;
  WResource    *resource_;
  WText        *text_;
  WImage       *image_;
  AnchorTarget  target_;

  bool refChanged_, targetChanged_;

  void resourceChanged();

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

}

#endif // WANCHOR_H_
