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

#include <string>
#include <Wt/WDllDefs.h>

namespace Wt {

/*! \class WLength Wt/WLength Wt/WLength
 *  \brief A value class that describes a CSS length.
 *
 * The class combines a value with a unit. There is a special value
 * <i>auto</i> which has a different meaning depending on the context.
 */
class WT_API WLength
{
public:
  /*! \brief The unit
   */
  enum Unit { FontEm,     //!< The relative font size
	      FontEx,     //!< The height of an 'x' in the font
	      Pixel,      //!< Pixel, relative to canvas resolution
	      Inch,       //!< Inche
	      Centimeter, //!< Centimeter
	      Millimeter, //!< Millimeter
	      Point,      //!< Point (1/72 Inch)
	      Pica,       //!< Pica (12 Point)
	      Percentage  //!< Percentage (meaning context-sensitive)
  };

  /*! \brief An 'auto' length.
   *
   * \sa WLength()
   */
  static WLength Auto;

  /*! \brief Creates an 'auto' length
   *
   * Specifies an 'auto' length.
   *
   * \sa Auto
   */
  WLength();

  /*! \brief Creates a length by parsing the argument as a css length string.
   *
   * This supports all CSS length formats that have an API counterpart.
   */
  WLength(const char* c);

  /*! \brief Creates a length with value and unit.
   *
   * This constructor is also used for the implicit conversion of a
   * double to a WLength, assuming a pixel unit.
   */
  WLength(double value, Unit unit = Pixel);

  /*! \brief Creates a length with value and unit.
   *
   * This constructor is also used for the implicit conversion of a
   * int to a WLength, assuming a pixel unit.
   */
  WLength(int value, Unit unit = Pixel);
  
  /*! \brief Creates a length with value and unit.
   *
   * This constructor is also used for the implicit conversion of a
   * long to a WLength, assuming a pixel unit.
   */
  WLength(long value, Unit unit = Pixel);
  
  /*! \brief Creates a length with value and unit.
   *
   * This constructor is also used for the implicit conversion of an
   * unsigned to a WLength, assuming a pixel unit.
   */
  WLength(unsigned value, Unit unit = Pixel);

  /*! \brief Creates a length with value and unit.
   *
   * This constructor is also used for the implicit conversion of an
   * unsigned long to a WLength, assuming a pixel unit.
   */
  WLength(unsigned long value, Unit unit = Pixel);

  /*! \brief Returns whether the length is 'auto'.
   *
   * \sa WLength(), Auto
   */
  bool isAuto() const { return auto_; }

  /*! \brief Returns the value.
   *
   * \sa unit()
   */
  double value() const { return value_; }

  /*! \brief Returns the unit.
   *
   * \sa value()
   */
  Unit unit() const { return unit_; }

  /*! \brief Returns the CSS text.
   */
  const std::string cssText() const;

  /*! \brief Comparison operator.
   */
  bool operator== (const WLength& other) const;

  /*! \brief Comparison operator.
   */
  bool operator!= (const WLength& other) const;

  /*! \brief Returns the (approximate) length in pixels.
   *
   * When the length isAuto(), 0 is returned, otherwise the approximate
   * length in pixels.
   */
  double toPixels(double fontSize = 16.0) const;
  
private:
  bool auto_;
  Unit unit_;
  double value_;

  void setUnit(Unit unit);
};

inline Wt::WLength operator*(const Wt::WLength &l, double s)
{
  return Wt::WLength(l.value() * s, l.unit());
}

inline Wt::WLength operator*(double s, const Wt::WLength &l)
{
  return l * s;
}

inline Wt::WLength operator/(const Wt::WLength &l, double s)
{
  return l * (1/s);
}

}

#endif // WLENGTH_H_
