HTMLMeterElement.cpp [plain text]
#include "config.h"
#if ENABLE(METER_TAG)
#include "HTMLMeterElement.h"
#include "Attribute.h"
#include "EventNames.h"
#include "ExceptionCode.h"
#include "FormDataList.h"
#include "NodeRenderingContext.h"
#include "HTMLFormElement.h"
#include "HTMLNames.h"
#include "HTMLParserIdioms.h"
#include "MeterShadowElement.h"
#include "RenderMeter.h"
#include "ShadowRoot.h"
#include <wtf/StdLibExtras.h>
namespace WebCore {
using namespace HTMLNames;
HTMLMeterElement::HTMLMeterElement(const QualifiedName& tagName, Document* document)
: LabelableElement(tagName, document)
{
ASSERT(hasTagName(meterTag));
}
HTMLMeterElement::~HTMLMeterElement()
{
}
PassRefPtr<HTMLMeterElement> HTMLMeterElement::create(const QualifiedName& tagName, Document* document)
{
RefPtr<HTMLMeterElement> meter = adoptRef(new HTMLMeterElement(tagName, document));
meter->createShadowSubtree();
return meter;
}
RenderObject* HTMLMeterElement::createRenderer(RenderArena* arena, RenderStyle*)
{
return new (arena) RenderMeter(this);
}
bool HTMLMeterElement::childShouldCreateRenderer(const NodeRenderingContext& childContext) const
{
return childContext.isOnUpperEncapsulationBoundary() && HTMLElement::childShouldCreateRenderer(childContext);
}
bool HTMLMeterElement::supportsFocus() const
{
return Node::supportsFocus() && !disabled();
}
void HTMLMeterElement::parseAttribute(Attribute* attribute)
{
if (attribute->name() == valueAttr || attribute->name() == minAttr || attribute->name() == maxAttr || attribute->name() == lowAttr || attribute->name() == highAttr || attribute->name() == optimumAttr)
didElementStateChange();
else
LabelableElement::parseAttribute(attribute);
}
double HTMLMeterElement::min() const
{
double min = 0;
parseToDoubleForNumberType(getAttribute(minAttr), &min);
return min;
}
void HTMLMeterElement::setMin(double min, ExceptionCode& ec)
{
if (!isfinite(min)) {
ec = NOT_SUPPORTED_ERR;
return;
}
setAttribute(minAttr, String::number(min));
}
double HTMLMeterElement::max() const
{
double max = std::max(1.0, min());
parseToDoubleForNumberType(getAttribute(maxAttr), &max);
return std::max(max, min());
}
void HTMLMeterElement::setMax(double max, ExceptionCode& ec)
{
if (!isfinite(max)) {
ec = NOT_SUPPORTED_ERR;
return;
}
setAttribute(maxAttr, String::number(max));
}
double HTMLMeterElement::value() const
{
double value = 0;
parseToDoubleForNumberType(getAttribute(valueAttr), &value);
return std::min(std::max(value, min()), max());
}
void HTMLMeterElement::setValue(double value, ExceptionCode& ec)
{
if (!isfinite(value)) {
ec = NOT_SUPPORTED_ERR;
return;
}
setAttribute(valueAttr, String::number(value));
}
double HTMLMeterElement::low() const
{
double low = min();
parseToDoubleForNumberType(getAttribute(lowAttr), &low);
return std::min(std::max(low, min()), max());
}
void HTMLMeterElement::setLow(double low, ExceptionCode& ec)
{
if (!isfinite(low)) {
ec = NOT_SUPPORTED_ERR;
return;
}
setAttribute(lowAttr, String::number(low));
}
double HTMLMeterElement::high() const
{
double high = max();
parseToDoubleForNumberType(getAttribute(highAttr), &high);
return std::min(std::max(high, low()), max());
}
void HTMLMeterElement::setHigh(double high, ExceptionCode& ec)
{
if (!isfinite(high)) {
ec = NOT_SUPPORTED_ERR;
return;
}
setAttribute(highAttr, String::number(high));
}
double HTMLMeterElement::optimum() const
{
double optimum = (max() + min()) / 2;
parseToDoubleForNumberType(getAttribute(optimumAttr), &optimum);
return std::min(std::max(optimum, min()), max());
}
void HTMLMeterElement::setOptimum(double optimum, ExceptionCode& ec)
{
if (!isfinite(optimum)) {
ec = NOT_SUPPORTED_ERR;
return;
}
setAttribute(optimumAttr, String::number(optimum));
}
HTMLMeterElement::GaugeRegion HTMLMeterElement::gaugeRegion() const
{
double lowValue = low();
double highValue = high();
double theValue = value();
double optimumValue = optimum();
if (optimumValue < lowValue) {
if (theValue <= lowValue)
return GaugeRegionOptimum;
if (theValue <= highValue)
return GaugeRegionSuboptimal;
return GaugeRegionEvenLessGood;
}
if (highValue < optimumValue) {
if (highValue <= theValue)
return GaugeRegionOptimum;
if (lowValue <= theValue)
return GaugeRegionSuboptimal;
return GaugeRegionEvenLessGood;
}
if (lowValue <= theValue && theValue <= highValue)
return GaugeRegionOptimum;
return GaugeRegionSuboptimal;
}
double HTMLMeterElement::valueRatio() const
{
double min = this->min();
double max = this->max();
double value = this->value();
if (max <= min)
return 0;
return (value - min) / (max - min);
}
void HTMLMeterElement::didElementStateChange()
{
m_value->setWidthPercentage(valueRatio()*100);
if (RenderObject* render = renderer())
render->updateFromElement();
}
void HTMLMeterElement::createShadowSubtree()
{
ASSERT(!hasShadowRoot());
RefPtr<MeterBarElement> bar = MeterBarElement::create(document());
m_value = MeterValueElement::create(document());
m_value->setWidthPercentage(0);
ExceptionCode ec = 0;
bar->appendChild(m_value, ec);
RefPtr<ShadowRoot> root = ShadowRoot::create(this, ShadowRoot::CreatingUserAgentShadowRoot);
root->appendChild(bar, ec);
}
} #endif