2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 #include "core/rendering/RenderFieldset.h"
27 #include "core/CSSPropertyNames.h"
28 #include "core/HTMLNames.h"
29 #include "core/html/HTMLLegendElement.h"
30 #include "core/rendering/PaintInfo.h"
31 #include "platform/graphics/GraphicsContextStateSaver.h"
38 using namespace HTMLNames;
40 RenderFieldset::RenderFieldset(Element* element)
41 : RenderBlockFlow(element)
45 void RenderFieldset::computePreferredLogicalWidths()
47 RenderBlockFlow::computePreferredLogicalWidths();
48 if (RenderBox* legend = findLegend()) {
49 int legendMinWidth = legend->minPreferredLogicalWidth();
51 Length legendMarginLeft = legend->style()->marginLeft();
52 Length legendMarginRight = legend->style()->marginLeft();
54 if (legendMarginLeft.isFixed())
55 legendMinWidth += legendMarginLeft.value();
57 if (legendMarginRight.isFixed())
58 legendMinWidth += legendMarginRight.value();
60 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, legendMinWidth + borderAndPaddingWidth());
64 RenderObject* RenderFieldset::layoutSpecialExcludedChild(bool relayoutChildren, SubtreeLayoutScope&)
66 RenderBox* legend = findLegend();
69 legend->setNeedsLayoutAndFullPaintInvalidation();
70 legend->layoutIfNeeded();
72 LayoutUnit logicalLeft;
73 if (style()->isLeftToRightDirection()) {
74 switch (legend->style()->textAlign()) {
76 logicalLeft = (logicalWidth() - logicalWidthForChild(legend)) / 2;
79 logicalLeft = logicalWidth() - borderEnd() - paddingEnd() - logicalWidthForChild(legend);
82 logicalLeft = borderStart() + paddingStart() + marginStartForChild(legend);
86 switch (legend->style()->textAlign()) {
88 logicalLeft = borderStart() + paddingStart();
91 // Make sure that the extra pixel goes to the end side in RTL (since it went to the end side
93 LayoutUnit centeredWidth = logicalWidth() - logicalWidthForChild(legend);
94 logicalLeft = centeredWidth - centeredWidth / 2;
98 logicalLeft = logicalWidth() - borderStart() - paddingStart() - marginStartForChild(legend) - logicalWidthForChild(legend);
103 setLogicalLeftForChild(legend, logicalLeft);
105 LayoutUnit fieldsetBorderBefore = borderBefore();
106 LayoutUnit legendLogicalHeight = logicalHeightForChild(legend);
108 LayoutUnit legendLogicalTop;
109 LayoutUnit collapsedLegendExtent;
110 // FIXME: We need to account for the legend's margin before too.
111 if (fieldsetBorderBefore > legendLogicalHeight) {
112 // The <legend> is smaller than the associated fieldset before border
113 // so the latter determines positioning of the <legend>. The sizing depends
114 // on the legend's margins as we want to still follow the author's cues.
115 // Firefox completely ignores the margins in this case which seems wrong.
116 legendLogicalTop = (fieldsetBorderBefore - legendLogicalHeight) / 2;
117 collapsedLegendExtent = max<LayoutUnit>(fieldsetBorderBefore, legendLogicalTop + legendLogicalHeight + marginAfterForChild(legend));
119 collapsedLegendExtent = legendLogicalHeight + marginAfterForChild(legend);
121 setLogicalTopForChild(legend, legendLogicalTop);
122 setLogicalHeight(paddingBefore() + collapsedLegendExtent);
127 RenderBox* RenderFieldset::findLegend(FindLegendOption option) const
129 for (RenderObject* legend = firstChild(); legend; legend = legend->nextSibling()) {
130 if (option == IgnoreFloatingOrOutOfFlow && legend->isFloatingOrOutOfFlowPositioned())
133 if (isHTMLLegendElement(legend->node()))
134 return toRenderBox(legend);
139 void RenderFieldset::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
141 if (!paintInfo.shouldPaintWithinRoot(this))
144 LayoutRect paintRect(paintOffset, size());
145 RenderBox* legend = findLegend();
147 return RenderBlockFlow::paintBoxDecorations(paintInfo, paintOffset);
149 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
150 // cases the legend is embedded in the right and bottom borders respectively.
151 // https://bugs.webkit.org/show_bug.cgi?id=47236
152 if (style()->isHorizontalWritingMode()) {
153 LayoutUnit yOff = (legend->y() > 0) ? LayoutUnit() : (legend->height() - borderTop()) / 2;
154 paintRect.setHeight(paintRect.height() - yOff);
155 paintRect.setY(paintRect.y() + yOff);
157 LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2;
158 paintRect.setWidth(paintRect.width() - xOff);
159 paintRect.setX(paintRect.x() + xOff);
162 if (!boxShadowShouldBeAppliedToBackground(determineBackgroundBleedAvoidance(paintInfo.context)))
163 paintBoxShadow(paintInfo, paintRect, style(), Normal);
164 paintFillLayers(paintInfo, resolveColor(CSSPropertyBackgroundColor), style()->backgroundLayers(), paintRect);
165 paintBoxShadow(paintInfo, paintRect, style(), Inset);
167 if (!style()->hasBorder())
170 // Create a clipping region around the legend and paint the border as normal
171 GraphicsContext* graphicsContext = paintInfo.context;
172 GraphicsContextStateSaver stateSaver(*graphicsContext);
174 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
175 // cases the legend is embedded in the right and bottom borders respectively.
176 // https://bugs.webkit.org/show_bug.cgi?id=47236
177 if (style()->isHorizontalWritingMode()) {
178 LayoutUnit clipTop = paintRect.y();
179 LayoutUnit clipHeight = max(static_cast<LayoutUnit>(style()->borderTopWidth()), legend->height() - ((legend->height() - borderTop()) / 2));
180 graphicsContext->clipOut(pixelSnappedIntRect(paintRect.x() + legend->x(), clipTop, legend->width(), clipHeight));
182 LayoutUnit clipLeft = paintRect.x();
183 LayoutUnit clipWidth = max(static_cast<LayoutUnit>(style()->borderLeftWidth()), legend->width());
184 graphicsContext->clipOut(pixelSnappedIntRect(clipLeft, paintRect.y() + legend->y(), clipWidth, legend->height()));
187 paintBorder(paintInfo, paintRect, style());
190 void RenderFieldset::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
192 if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
195 LayoutRect paintRect = LayoutRect(paintOffset, size());
196 RenderBox* legend = findLegend();
198 return RenderBlockFlow::paintMask(paintInfo, paintOffset);
200 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
201 // cases the legend is embedded in the right and bottom borders respectively.
202 // https://bugs.webkit.org/show_bug.cgi?id=47236
203 if (style()->isHorizontalWritingMode()) {
204 LayoutUnit yOff = (legend->y() > 0) ? LayoutUnit() : (legend->height() - borderTop()) / 2;
205 paintRect.expand(0, -yOff);
206 paintRect.move(0, yOff);
208 LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2;
209 paintRect.expand(-xOff, 0);
210 paintRect.move(xOff, 0);
213 paintMaskImages(paintInfo, paintRect);
216 } // namespace WebCore