#include "config.h"
#include "RenderButton.h"
#include "Document.h"
#include "GraphicsContext.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "RenderTextFragment.h"
#include "RenderTheme.h"
#include "StyleInheritedData.h"
#if PLATFORM(IOS)
#include "RenderThemeIOS.h"
#endif
namespace WebCore {
using namespace HTMLNames;
RenderButton::RenderButton(HTMLFormControlElement& element, PassRef<RenderStyle> style)
: RenderFlexibleBox(element, WTF::move(style))
, m_buttonText(0)
, m_inner(0)
, m_default(false)
{
}
RenderButton::~RenderButton()
{
}
HTMLFormControlElement& RenderButton::formControlElement() const
{
return toHTMLFormControlElement(nodeForNonAnonymous());
}
bool RenderButton::canBeSelectionLeaf() const
{
return formControlElement().hasEditableStyle();
}
bool RenderButton::hasLineIfEmpty() const
{
return formControlElement().toInputElement();
}
void RenderButton::addChild(RenderObject* newChild, RenderObject* beforeChild)
{
if (!m_inner) {
ASSERT(!firstChild());
m_inner = createAnonymousBlock(style().display());
setupInnerStyle(&m_inner->style());
RenderFlexibleBox::addChild(m_inner);
}
m_inner->addChild(newChild, beforeChild);
}
RenderObject* RenderButton::removeChild(RenderObject& oldChild)
{
if (&oldChild == m_inner || !m_inner || oldChild.parent() == this) {
ASSERT(&oldChild == m_inner || !m_inner);
RenderObject* next = RenderFlexibleBox::removeChild(oldChild);
m_inner = nullptr;
return next;
} else
return m_inner->removeChild(oldChild);
}
void RenderButton::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
{
if (m_inner) {
m_inner->style().setFlexGrow(newStyle.initialFlexGrow());
m_inner->style().setMarginTop(newStyle.initialMargin());
m_inner->style().setMarginBottom(newStyle.initialMargin());
}
RenderBlock::styleWillChange(diff, newStyle);
}
void RenderButton::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlock::styleDidChange(diff, oldStyle);
if (m_inner) setupInnerStyle(&m_inner->style());
if (!m_default && theme().isDefault(*this)) {
if (!m_timer)
m_timer = std::make_unique<Timer<RenderButton>>(this, &RenderButton::timerFired);
m_timer->startRepeating(0.03);
m_default = true;
} else if (m_default && !theme().isDefault(*this)) {
m_default = false;
m_timer = nullptr;
}
}
void RenderButton::setupInnerStyle(RenderStyle* innerStyle)
{
ASSERT(innerStyle->refCount() == 1);
innerStyle->setFlexGrow(1.0f);
innerStyle->setMarginTop(Length());
innerStyle->setMarginBottom(Length());
innerStyle->setFlexDirection(style().flexDirection());
}
void RenderButton::updateFromElement()
{
if (isHTMLInputElement(formControlElement())) {
HTMLInputElement& input = toHTMLInputElement(formControlElement());
String value = input.valueWithDefault();
setText(value);
}
}
void RenderButton::setText(const String& str)
{
if (str.isEmpty()) {
if (m_buttonText) {
m_buttonText->destroy();
m_buttonText = 0;
}
} else {
if (m_buttonText)
m_buttonText->setText(str.impl());
else {
m_buttonText = new RenderTextFragment(document(), str);
addChild(m_buttonText);
}
}
}
String RenderButton::text() const
{
return m_buttonText ? m_buttonText->text() : 0;
}
bool RenderButton::canHaveGeneratedChildren() const
{
return !isHTMLInputElement(formControlElement());
}
LayoutRect RenderButton::controlClipRect(const LayoutPoint& additionalOffset) const
{
return LayoutRect(additionalOffset.x() + borderLeft(), additionalOffset.y() + borderTop(), width() - borderLeft() - borderRight(), height() - borderTop() - borderBottom());
}
void RenderButton::timerFired(Timer<RenderButton>&)
{
if (document().inPageCache())
return;
repaint();
}
#if PLATFORM(IOS)
void RenderButton::layout()
{
RenderFlexibleBox::layout();
RenderThemeIOS::adjustRoundBorderRadius(style(), *this);
}
#endif
}