2 * Copyright (C) 2005 Apple Computer, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "core/rendering/RenderButton.h"
24 #include "core/dom/Document.h"
28 using namespace HTMLNames;
30 RenderButton::RenderButton(Element* element)
31 : RenderFlexibleBox(element)
36 RenderButton::~RenderButton()
40 void RenderButton::addChild(RenderObject* newChild, RenderObject* beforeChild)
43 // Create an anonymous block.
44 ASSERT(!firstChild());
45 m_inner = createAnonymousBlock(style()->display());
46 setupInnerStyle(m_inner->style());
47 RenderFlexibleBox::addChild(m_inner);
50 m_inner->addChild(newChild, beforeChild);
53 void RenderButton::removeChild(RenderObject* oldChild)
55 // m_inner should be the only child, but checking for direct children who
56 // are not m_inner prevents security problems when that assumption is
58 if (oldChild == m_inner || !m_inner || oldChild->parent() == this) {
59 ASSERT(oldChild == m_inner || !m_inner);
60 RenderFlexibleBox::removeChild(oldChild);
63 m_inner->removeChild(oldChild);
66 void RenderButton::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
69 // RenderBlock::setStyle is going to apply a new style to the inner block, which
70 // will have the initial flex value, 0. The current value is 1, because we set
71 // it right below. Here we change it back to 0 to avoid getting a spurious layout hint
72 // because of the difference. Same goes for the other properties.
73 // FIXME: Make this hack unnecessary.
74 m_inner->style()->setFlexGrow(newStyle.initialFlexGrow());
75 m_inner->style()->setMarginTop(newStyle.initialMargin());
76 m_inner->style()->setMarginBottom(newStyle.initialMargin());
78 RenderBlock::styleWillChange(diff, newStyle);
81 void RenderButton::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
83 RenderBlock::styleDidChange(diff, oldStyle);
85 if (m_inner) // RenderBlock handled updating the anonymous block's style.
86 setupInnerStyle(m_inner->style());
89 void RenderButton::setupInnerStyle(RenderStyle* innerStyle)
91 ASSERT(innerStyle->refCount() == 1);
92 // RenderBlock::createAnonymousBlock creates a new RenderStyle, so this is
94 innerStyle->setFlexGrow(1.0f);
95 // Use margin:auto instead of align-items:center to get safe centering, i.e.
96 // when the content overflows, treat it the same as align-items: flex-start.
97 innerStyle->setMarginTop(Length());
98 innerStyle->setMarginBottom(Length());
99 innerStyle->setFlexDirection(style()->flexDirection());
102 bool RenderButton::canHaveGeneratedChildren() const
104 // Input elements can't have generated children, but button elements can. We'll
105 // write the code assuming any other button types that might emerge in the future
106 // can also have children.
107 return !isHTMLInputElement(*node());
110 LayoutRect RenderButton::controlClipRect(const LayoutPoint& additionalOffset) const
112 // Clip to the padding box to at least give content the extra padding space.
113 return LayoutRect(additionalOffset.x() + borderLeft(), additionalOffset.y() + borderTop(), width() - borderLeft() - borderRight(), height() - borderTop() - borderBottom());
116 int RenderButton::baselinePosition(FontBaseline baseline, bool firstLine, LineDirectionMode direction, LinePositionMode linePositionMode) const
118 ASSERT(linePositionMode == PositionOnContainingLine);
119 // We want to call the RenderBlock version of firstLineBoxBaseline to
120 // avoid RenderFlexibleBox synthesizing a baseline that we don't want.
121 // We use this check as a proxy for "are there any line boxes in this button"
122 if (!hasLineIfEmpty() && RenderBlock::firstLineBoxBaseline() == -1) {
123 // To ensure that we have a consistent baseline when we have no children,
124 // even when we have the anonymous RenderBlock child, we calculate the
125 // baseline for the empty case manually here.
126 if (direction == HorizontalLine)
127 return marginTop() + borderTop() + paddingTop() + contentHeight();
129 return marginRight() + borderRight() + paddingRight() + contentWidth();
131 return RenderFlexibleBox::baselinePosition(baseline, firstLine, direction, linePositionMode);
134 } // namespace WebCore