#ifndef StepRange_h
#define StepRange_h
#include "platform/Decimal.h"
#include "wtf/Forward.h"
namespace WebCore {
class HTMLInputElement;
enum AnyStepHandling { RejectAny, AnyIsDefaultStep };
class StepRange {
public:
enum StepValueShouldBe {
StepValueShouldBeReal,
ParsedStepValueShouldBeInteger,
ScaledStepValueShouldBeInteger,
};
struct StepDescription {
WTF_MAKE_FAST_ALLOCATED;
public:
int defaultStep;
int defaultStepBase;
int stepScaleFactor;
StepValueShouldBe stepValueShouldBe;
StepDescription(int defaultStep, int defaultStepBase, int stepScaleFactor, StepValueShouldBe stepValueShouldBe = StepValueShouldBeReal)
: defaultStep(defaultStep)
, defaultStepBase(defaultStepBase)
, stepScaleFactor(stepScaleFactor)
, stepValueShouldBe(stepValueShouldBe)
{
}
StepDescription()
: defaultStep(1)
, defaultStepBase(0)
, stepScaleFactor(1)
, stepValueShouldBe(StepValueShouldBeReal)
{
}
Decimal defaultValue() const
{
return defaultStep * stepScaleFactor;
}
};
StepRange();
StepRange(const StepRange&);
StepRange(const Decimal& stepBase, const Decimal& minimum, const Decimal& maximum, const Decimal& step, const StepDescription&);
Decimal alignValueForStep(const Decimal& currentValue, const Decimal& newValue) const;
Decimal clampValue(const Decimal& value) const;
bool hasStep() const { return m_hasStep; }
Decimal maximum() const { return m_maximum; }
Decimal minimum() const { return m_minimum; }
static Decimal parseStep(AnyStepHandling, const StepDescription&, const String&);
Decimal step() const { return m_step; }
Decimal stepBase() const { return m_stepBase; }
bool stepMismatch(const Decimal&) const;
Decimal defaultValue() const
{
return clampValue((m_minimum + m_maximum) / 2);
}
Decimal proportionFromValue(const Decimal& value) const
{
if (m_minimum == m_maximum)
return 0;
return (value - m_minimum) / (m_maximum - m_minimum);
}
Decimal valueFromProportion(const Decimal& proportion) const
{
return m_minimum + proportion * (m_maximum - m_minimum);
}
private:
StepRange& operator =(const StepRange&);
Decimal acceptableError() const;
Decimal roundByStep(const Decimal& value, const Decimal& base) const;
const Decimal m_maximum;
const Decimal m_minimum;
const Decimal m_step;
const Decimal m_stepBase;
const StepDescription m_stepDescription;
const bool m_hasStep;
};
}
#endif