// 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 class that specifies 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.
   *
   * Only a combination of a value and a unit is supported.
   * If the string is an illegal css length an exception is thrown.
   */
  WLength(const char* c);

  /*! \brief Creates a length with value and unit.
   *
   * This constructor will also provide the implicit conversion between
   * a double and WLength, using a pixel unit.
   */
  WLength(double value, Unit unit = Pixel);

  /*! \brief Creates a length with value and unit.
   *
   * This constructor will also provide the implicit conversion between
   * an int and WLength, using a pixel unit.
   */
  WLength(int value, Unit unit = Pixel);
  
  /*! \brief Creates a length with value and unit.
   *
   * This constructor will also provide the implicit conversion between
   * a long and WLength, using a pixel unit.
   */
  WLength(long value, Unit unit = Pixel);
  
  /*! \brief Creates a length with value and unit.
   *
   * This constructor will also provide the implicit conversion between
   * an unsigned int and WLength, using a pixel unit.
   */
  WLength(unsigned int value, Unit unit = Pixel);

  /*! \brief Creates a length with value and unit.
   *
   * This constructor will also provide the implicit conversion between
   * an unsigned long and WLength, using 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_;
};

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_
