Wt  4.15.0
Loading...
Searching...
No Matches
Wt::WLength Class Reference

A value class that describes a CSS length. More...

#include <Wt/WLength.h>

Public Types

typedef LengthUnit Unit
 Typedef for enum Wt::LengthUnit.

Public Member Functions

 WLength ()
 Creates an 'auto' length.
template<std::size_t N>
 WLength (const char(&str)[N])
 Creates a length by parsing the argument as a css length string.
 WLength (const std::string &str)
 Creates a length by parsing the argument as a css length string.
 WLength (double value, LengthUnit unit=LengthUnit::Pixel)
 Creates a length with value and unit.
 WLength (LengthUnit unit)
 Creates a length with a keyword unit.
bool isAuto () const
 Returns whether the length is 'auto'.
double value () const
 Returns the value.
LengthUnit unit () const
 Returns the unit.
const std::string cssText () const
 Returns the CSS text.
bool operator== (const WLength &other) const
 Comparison operator.
bool operator!= (const WLength &other) const
 Comparison operator.
double toPixels (double fontSize=16.0) const
 Returns the (approximate) length in pixels.

Static Public Attributes

static WLength Auto
 An 'auto' length.

Detailed Description

A value class that describes a CSS length.

The class combines a value with a unit. There is a special value auto which has a different meaning depending on the context.

The keyword units LengthUnit::FitContent, LengthUnit::MaxContent and LengthUnit::MinContent have, like auto, no numeric component: the value of a length with such a unit is ignored.

Constructor & Destructor Documentation

◆ WLength() [1/5]

Wt::WLength::WLength ( )

Creates an 'auto' length.

Specifies an 'auto' length.

See also
Auto

◆ WLength() [2/5]

template<std::size_t N>
Wt::WLength::WLength ( const char(&) str[N])

Creates a length by parsing the argument as a css length string.

This supports all CSS length formats that have an API counterpart.

This is an overload for the std::string version that accepts a string literal.

◆ WLength() [3/5]

Wt::WLength::WLength ( const std::string & str)

Creates a length by parsing the argument as a css length string.

This supports all CSS length formats that have an API counterpart.

◆ WLength() [4/5]

Wt::WLength::WLength ( double value,
LengthUnit unit = LengthUnit::Pixel )

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.

For the keyword units (LengthUnit::FitContent, LengthUnit::MaxContent and LengthUnit::MinContent) the value is ignored and normalized to -1.

◆ WLength() [5/5]

Wt::WLength::WLength ( LengthUnit unit)

Creates a length with a keyword unit.

The length has no numeric component: value() is -1, as if constructed by parsing the bare keyword.

This constructor is also used for the implicit conversion of a LengthUnit to a WLength.

Exceptions
WExceptionif the unit is not one of the keyword units (LengthUnit::FitContent, LengthUnit::MaxContent or LengthUnit::MinContent), since all other units require a numeric value.

Member Function Documentation

◆ isAuto()

bool Wt::WLength::isAuto ( ) const

Returns whether the length is 'auto'.

See also
WLength(), Auto

◆ toPixels()

double Wt::WLength::toPixels ( double fontSize = 16.0) const

Returns the (approximate) length in pixels.

When the length isAuto(), 0 is returned, otherwise the approximate length in pixels.

Note
For percentages (LengthUnit::Percentage), and units relative to viewport size (LengthUnit::ViewportWidth, LengthUnit::ViewportHeight, LengthUnit::ViewportMin, LengthUnit::ViewportMax), a percentage of the font size is used. For the keyword units (LengthUnit::FitContent, LengthUnit::MaxContent and LengthUnit::MinContent), 0 is returned since the length depends on the content.

◆ unit()

LengthUnit Wt::WLength::unit ( ) const

Returns the unit.

See also
value()

◆ value()

double Wt::WLength::value ( ) const

Returns the value.

This is -1 for the keyword units (LengthUnit::FitContent, LengthUnit::MaxContent and LengthUnit::MinContent).

See also
unit()

Member Data Documentation

◆ Auto

WLength Wt::WLength::Auto
static

An 'auto' length.

See also
WLength()