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/paint/BoxDecorationData.h"
31 #include "core/paint/BoxPainter.h"
32 #include "core/rendering/PaintInfo.h"
33 #include "platform/graphics/GraphicsContextStateSaver.h"
40 using namespace HTMLNames;
42 RenderFieldset::RenderFieldset(Element* element)
43 : RenderBlockFlow(element)
47 void RenderFieldset::computePreferredLogicalWidths()
49 RenderBlockFlow::computePreferredLogicalWidths();
50 if (RenderBox* legend = findLegend()) {
51 int legendMinWidth = legend->minPreferredLogicalWidth();
53 Length legendMarginLeft = legend->style()->marginLeft();
54 Length legendMarginRight = legend->style()->marginLeft();
56 if (legendMarginLeft.isFixed())
57 legendMinWidth += legendMarginLeft.value();
59 if (legendMarginRight.isFixed())
60 legendMinWidth += legendMarginRight.value();
62 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, legendMinWidth + borderAndPaddingWidth());
66 RenderObject* RenderFieldset::layoutSpecialExcludedChild(bool relayoutChildren, SubtreeLayoutScope&)
68 RenderBox* legend = findLegend();
71 legend->setNeedsLayoutAndFullPaintInvalidation();
72 legend->layoutIfNeeded();
74 LayoutUnit logicalLeft;
75 if (style()->isLeftToRightDirection()) {
76 switch (legend->style()->textAlign()) {
78 logicalLeft = (logicalWidth() - logicalWidthForChild(legend)) / 2;
81 logicalLeft = logicalWidth() - borderEnd() - paddingEnd() - logicalWidthForChild(legend);
84 logicalLeft = borderStart() + paddingStart() + marginStartForChild(legend);
88 switch (legend->style()->textAlign()) {
90 logicalLeft = borderStart() + paddingStart();
93 // Make sure that the extra pixel goes to the end side in RTL (since it went to the end side
95 LayoutUnit centeredWidth = logicalWidth() - logicalWidthForChild(legend);
96 logicalLeft = centeredWidth - centeredWidth / 2;
100 logicalLeft = logicalWidth() - borderStart() - paddingStart() - marginStartForChild(legend) - logicalWidthForChild(legend);
105 setLogicalLeftForChild(legend, logicalLeft);
107 LayoutUnit fieldsetBorderBefore = borderBefore();
108 LayoutUnit legendLogicalHeight = logicalHeightForChild(legend);
110 LayoutUnit legendLogicalTop;
111 LayoutUnit collapsedLegendExtent;
112 // FIXME: We need to account for the legend's margin before too.
113 if (fieldsetBorderBefore > legendLogicalHeight) {
114 // The <legend> is smaller than the associated fieldset before border
115 // so the latter determines positioning of the <legend>. The sizing depends
116 // on the legend's margins as we want to still follow the author's cues.
117 // Firefox completely ignores the margins in this case which seems wrong.
118 legendLogicalTop = (fieldsetBorderBefore - legendLogicalHeight) / 2;
119 collapsedLegendExtent = max<LayoutUnit>(fieldsetBorderBefore, legendLogicalTop + legendLogicalHeight + marginAfterForChild(legend));
121 collapsedLegendExtent = legendLogicalHeight + marginAfterForChild(legend);
123 setLogicalTopForChild(legend, legendLogicalTop);
124 setLogicalHeight(paddingBefore() + collapsedLegendExtent);
129 RenderBox* RenderFieldset::findLegend(FindLegendOption option) const
131 for (RenderObject* legend = firstChild(); legend; legend = legend->nextSibling()) {
132 if (option == IgnoreFloatingOrOutOfFlow && legend->isFloatingOrOutOfFlowPositioned())
135 if (isHTMLLegendElement(legend->node()))
136 return toRenderBox(legend);
141 void RenderFieldset::paintBoxDecorationBackground(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
143 if (!paintInfo.shouldPaintWithinRoot(this))
146 LayoutRect paintRect(paintOffset, size());
147 RenderBox* legend = findLegend();
149 return RenderBlockFlow::paintBoxDecorationBackground(paintInfo, paintOffset);
151 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
152 // cases the legend is embedded in the right and bottom borders respectively.
153 // https://bugs.webkit.org/show_bug.cgi?id=47236
154 if (style()->isHorizontalWritingMode()) {
155 LayoutUnit yOff = (legend->y() > 0) ? LayoutUnit() : (legend->height() - borderTop()) / 2;
156 paintRect.setHeight(paintRect.height() - yOff);
157 paintRect.setY(paintRect.y() + yOff);
159 LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2;
160 paintRect.setWidth(paintRect.width() - xOff);
161 paintRect.setX(paintRect.x() + xOff);
164 BoxDecorationData boxDecorationData(*style(), canRenderBorderImage(), backgroundHasOpaqueTopLayer(), paintInfo.context);
166 if (boxDecorationData.bleedAvoidance() == BackgroundBleedNone)
167 BoxPainter::paintBoxShadow(paintInfo, paintRect, style(), Normal);
168 BoxPainter(*this).paintFillLayers(paintInfo, boxDecorationData.backgroundColor, style()->backgroundLayers(), paintRect);
169 BoxPainter::paintBoxShadow(paintInfo, paintRect, style(), Inset);
171 if (!boxDecorationData.hasBorder)
174 // Create a clipping region around the legend and paint the border as normal
175 GraphicsContext* graphicsContext = paintInfo.context;
176 GraphicsContextStateSaver stateSaver(*graphicsContext);
178 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
179 // cases the legend is embedded in the right and bottom borders respectively.
180 // https://bugs.webkit.org/show_bug.cgi?id=47236
181 if (style()->isHorizontalWritingMode()) {
182 LayoutUnit clipTop = paintRect.y();
183 LayoutUnit clipHeight = max(static_cast<LayoutUnit>(style()->borderTopWidth()), legend->height() - ((legend->height() - borderTop()) / 2));
184 graphicsContext->clipOut(pixelSnappedIntRect(paintRect.x() + legend->x(), clipTop, legend->width(), clipHeight));
186 LayoutUnit clipLeft = paintRect.x();
187 LayoutUnit clipWidth = max(static_cast<LayoutUnit>(style()->borderLeftWidth()), legend->width());
188 graphicsContext->clipOut(pixelSnappedIntRect(clipLeft, paintRect.y() + legend->y(), clipWidth, legend->height()));
191 BoxPainter::paintBorder(*this, paintInfo, paintRect, style());
194 void RenderFieldset::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
196 if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
199 LayoutRect paintRect = LayoutRect(paintOffset, size());
200 RenderBox* legend = findLegend();
202 return RenderBlockFlow::paintMask(paintInfo, paintOffset);
204 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
205 // cases the legend is embedded in the right and bottom borders respectively.
206 // https://bugs.webkit.org/show_bug.cgi?id=47236
207 if (style()->isHorizontalWritingMode()) {
208 LayoutUnit yOff = (legend->y() > 0) ? LayoutUnit() : (legend->height() - borderTop()) / 2;
209 paintRect.expand(0, -yOff);
210 paintRect.move(0, yOff);
212 LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2;
213 paintRect.expand(-xOff, 0);
214 paintRect.move(xOff, 0);
217 BoxPainter(*this).paintMaskImages(paintInfo, paintRect);