This source file includes following definitions.
- clampValue
- isInRange
- m_hasValue
- maximumWidth
- defaultValueForStepDown
- defaultValueForStepUp
- setFocus
- formatValue
- handleKeyboardEvent
- hasValue
- initialize
- maximum
- setEmptyValue
- setValueAsInteger
- stepDown
- stepUp
- value
- valueAsInteger
- typeAheadValue
- visibleValue
- roundDown
- roundUp
#include "config.h"
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
#include "core/html/shadow/DateTimeNumericFieldElement.h"
#include "CSSPropertyNames.h"
#include "CSSValueKeywords.h"
#include "core/events/KeyboardEvent.h"
#include "platform/fonts/Font.h"
#include "platform/text/PlatformLocale.h"
using namespace WTF::Unicode;
namespace WebCore {
int DateTimeNumericFieldElement::Range::clampValue(int value) const
{
return std::min(std::max(value, minimum), maximum);
}
bool DateTimeNumericFieldElement::Range::isInRange(int value) const
{
return value >= minimum && value <= maximum;
}
DateTimeNumericFieldElement::DateTimeNumericFieldElement(Document& document, FieldOwner& fieldOwner, const Range& range, const Range& hardLimits, const String& placeholder, const DateTimeNumericFieldElement::Step& step)
: DateTimeFieldElement(document, fieldOwner)
, m_placeholder(placeholder)
, m_range(range)
, m_hardLimits(hardLimits)
, m_step(step)
, m_value(0)
, m_hasValue(false)
{
ASSERT(m_step.step);
ASSERT(m_range.minimum <= m_range.maximum);
ASSERT(m_hardLimits.minimum <= m_hardLimits.maximum);
if (localeForOwner().isRTL()) {
Direction dir = direction(formatValue(this->maximum())[0]);
if (dir == LeftToRight || dir == EuropeanNumber || dir == ArabicNumber) {
setInlineStyleProperty(CSSPropertyUnicodeBidi, CSSValueBidiOverride);
setInlineStyleProperty(CSSPropertyDirection, CSSValueLtr);
}
}
}
float DateTimeNumericFieldElement::maximumWidth(const Font& font)
{
float maximumWidth = font.width(m_placeholder);
maximumWidth = std::max(maximumWidth, font.width(formatValue(maximum())));
maximumWidth = std::max(maximumWidth, font.width(value()));
return maximumWidth + DateTimeFieldElement::maximumWidth(font);
}
int DateTimeNumericFieldElement::defaultValueForStepDown() const
{
return m_range.maximum;
}
int DateTimeNumericFieldElement::defaultValueForStepUp() const
{
return m_range.minimum;
}
void DateTimeNumericFieldElement::setFocus(bool value)
{
if (!value) {
int value = typeAheadValue();
m_typeAheadBuffer.clear();
if (value >= 0)
setValueAsInteger(value, DispatchEvent);
}
DateTimeFieldElement::setFocus(value);
}
String DateTimeNumericFieldElement::formatValue(int value) const
{
Locale& locale = localeForOwner();
if (m_hardLimits.maximum > 999)
return locale.convertToLocalizedNumber(String::format("%04d", value));
if (m_hardLimits.maximum > 99)
return locale.convertToLocalizedNumber(String::format("%03d", value));
return locale.convertToLocalizedNumber(String::format("%02d", value));
}
void DateTimeNumericFieldElement::handleKeyboardEvent(KeyboardEvent* keyboardEvent)
{
ASSERT(!isDisabled());
if (keyboardEvent->type() != EventTypeNames::keypress)
return;
UChar charCode = static_cast<UChar>(keyboardEvent->charCode());
String number = localeForOwner().convertFromLocalizedNumber(String(&charCode, 1));
const int digit = number[0] - '0';
if (digit < 0 || digit > 9)
return;
unsigned maximumLength = DateTimeNumericFieldElement::formatValue(m_range.maximum).length();
if (m_typeAheadBuffer.length() >= maximumLength) {
String current = m_typeAheadBuffer.toString();
m_typeAheadBuffer.clear();
unsigned desiredLength = maximumLength - 1;
m_typeAheadBuffer.append(current, current.length() - desiredLength, desiredLength);
}
m_typeAheadBuffer.append(number);
int newValue = typeAheadValue();
if (newValue >= m_hardLimits.minimum)
setValueAsInteger(newValue, DispatchEvent);
else {
m_hasValue = false;
updateVisibleValue(DispatchEvent);
}
if (m_typeAheadBuffer.length() >= maximumLength || newValue * 10 > m_range.maximum)
focusOnNextField();
keyboardEvent->setDefaultHandled();
}
bool DateTimeNumericFieldElement::hasValue() const
{
return m_hasValue;
}
void DateTimeNumericFieldElement::initialize(const AtomicString& pseudo, const String& axHelpText)
{
DateTimeFieldElement::initialize(pseudo, axHelpText, m_range.minimum, m_range.maximum);
}
int DateTimeNumericFieldElement::maximum() const
{
return m_range.maximum;
}
void DateTimeNumericFieldElement::setEmptyValue(EventBehavior eventBehavior)
{
if (isDisabled())
return;
m_hasValue = false;
m_value = 0;
m_typeAheadBuffer.clear();
updateVisibleValue(eventBehavior);
}
void DateTimeNumericFieldElement::setValueAsInteger(int value, EventBehavior eventBehavior)
{
m_value = m_hardLimits.clampValue(value);
m_hasValue = true;
updateVisibleValue(eventBehavior);
}
void DateTimeNumericFieldElement::stepDown()
{
int newValue = roundDown(m_hasValue ? m_value - 1 : defaultValueForStepDown());
if (!m_range.isInRange(newValue))
newValue = roundDown(m_range.maximum);
m_typeAheadBuffer.clear();
setValueAsInteger(newValue, DispatchEvent);
}
void DateTimeNumericFieldElement::stepUp()
{
int newValue = roundUp(m_hasValue ? m_value + 1 : defaultValueForStepUp());
if (!m_range.isInRange(newValue))
newValue = roundUp(m_range.minimum);
m_typeAheadBuffer.clear();
setValueAsInteger(newValue, DispatchEvent);
}
String DateTimeNumericFieldElement::value() const
{
return m_hasValue ? formatValue(m_value) : emptyString();
}
int DateTimeNumericFieldElement::valueAsInteger() const
{
return m_hasValue ? m_value : -1;
}
int DateTimeNumericFieldElement::typeAheadValue() const
{
if (m_typeAheadBuffer.length())
return m_typeAheadBuffer.toString().toInt();
return -1;
}
String DateTimeNumericFieldElement::visibleValue() const
{
if (m_typeAheadBuffer.length())
return formatValue(typeAheadValue());
return m_hasValue ? value() : m_placeholder;
}
int DateTimeNumericFieldElement::roundDown(int n) const
{
n -= m_step.stepBase;
if (n >= 0)
n = n / m_step.step * m_step.step;
else
n = -((-n + m_step.step - 1) / m_step.step * m_step.step);
return n + m_step.stepBase;
}
int DateTimeNumericFieldElement::roundUp(int n) const
{
n -= m_step.stepBase;
if (n >= 0)
n = (n + m_step.step - 1) / m_step.step * m_step.step;
else
n = -(-n / m_step.step * m_step.step);
return n + m_step.stepBase;
}
}
#endif