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 "CSSPropertyNames.h"
28 #include "HTMLNames.h"
29 #include "core/rendering/PaintInfo.h"
30 #include "platform/graphics/GraphicsContextStateSaver.h"
37 using namespace HTMLNames;
39 RenderFieldset::RenderFieldset(Element* element)
40 : RenderBlockFlow(element)
44 void RenderFieldset::computePreferredLogicalWidths()
46 RenderBlock::computePreferredLogicalWidths();
47 if (RenderBox* legend = findLegend()) {
48 int legendMinWidth = legend->minPreferredLogicalWidth();
50 Length legendMarginLeft = legend->style()->marginLeft();
51 Length legendMarginRight = legend->style()->marginLeft();
53 if (legendMarginLeft.isFixed())
54 legendMinWidth += legendMarginLeft.value();
56 if (legendMarginRight.isFixed())
57 legendMinWidth += legendMarginRight.value();
59 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, legendMinWidth + borderAndPaddingWidth());
63 RenderObject* RenderFieldset::layoutSpecialExcludedChild(bool relayoutChildren, SubtreeLayoutScope&)
65 RenderBox* legend = findLegend();
68 legend->setNeedsLayout();
69 legend->layoutIfNeeded();
71 LayoutUnit logicalLeft;
72 if (style()->isLeftToRightDirection()) {
73 switch (legend->style()->textAlign()) {
75 logicalLeft = (logicalWidth() - logicalWidthForChild(legend)) / 2;
78 logicalLeft = logicalWidth() - borderEnd() - paddingEnd() - logicalWidthForChild(legend);
81 logicalLeft = borderStart() + paddingStart() + marginStartForChild(legend);
85 switch (legend->style()->textAlign()) {
87 logicalLeft = borderStart() + paddingStart();
90 // Make sure that the extra pixel goes to the end side in RTL (since it went to the end side
92 LayoutUnit centeredWidth = logicalWidth() - logicalWidthForChild(legend);
93 logicalLeft = centeredWidth - centeredWidth / 2;
97 logicalLeft = logicalWidth() - borderStart() - paddingStart() - marginStartForChild(legend) - logicalWidthForChild(legend);
102 setLogicalLeftForChild(legend, logicalLeft);
104 LayoutUnit fieldsetBorderBefore = borderBefore();
105 LayoutUnit legendLogicalHeight = logicalHeightForChild(legend);
107 LayoutUnit legendLogicalTop;
108 LayoutUnit collapsedLegendExtent;
109 // FIXME: We need to account for the legend's margin before too.
110 if (fieldsetBorderBefore > legendLogicalHeight) {
111 // The <legend> is smaller than the associated fieldset before border
112 // so the latter determines positioning of the <legend>. The sizing depends
113 // on the legend's margins as we want to still follow the author's cues.
114 // Firefox completely ignores the margins in this case which seems wrong.
115 legendLogicalTop = (fieldsetBorderBefore - legendLogicalHeight) / 2;
116 collapsedLegendExtent = max<LayoutUnit>(fieldsetBorderBefore, legendLogicalTop + legendLogicalHeight + marginAfterForChild(legend));
118 collapsedLegendExtent = legendLogicalHeight + marginAfterForChild(legend);
120 setLogicalTopForChild(legend, legendLogicalTop);
121 setLogicalHeight(paddingBefore() + collapsedLegendExtent);
126 RenderBox* RenderFieldset::findLegend(FindLegendOption option) const
128 for (RenderObject* legend = firstChild(); legend; legend = legend->nextSibling()) {
129 if (option == IgnoreFloatingOrOutOfFlow && legend->isFloatingOrOutOfFlowPositioned())
132 if (legend->node() && (legend->node()->hasTagName(legendTag)))
133 return toRenderBox(legend);
138 void RenderFieldset::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
140 if (!paintInfo.shouldPaintWithinRoot(this))
143 LayoutRect paintRect(paintOffset, size());
144 RenderBox* legend = findLegend();
146 return RenderBlock::paintBoxDecorations(paintInfo, paintOffset);
148 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
149 // cases the legend is embedded in the right and bottom borders respectively.
150 // https://bugs.webkit.org/show_bug.cgi?id=47236
151 if (style()->isHorizontalWritingMode()) {
152 LayoutUnit yOff = (legend->y() > 0) ? LayoutUnit() : (legend->height() - borderTop()) / 2;
153 paintRect.setHeight(paintRect.height() - yOff);
154 paintRect.setY(paintRect.y() + yOff);
156 LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2;
157 paintRect.setWidth(paintRect.width() - xOff);
158 paintRect.setX(paintRect.x() + xOff);
161 if (!boxShadowShouldBeAppliedToBackground(determineBackgroundBleedAvoidance(paintInfo.context)))
162 paintBoxShadow(paintInfo, paintRect, style(), Normal);
163 paintFillLayers(paintInfo, resolveColor(CSSPropertyBackgroundColor), style()->backgroundLayers(), paintRect);
164 paintBoxShadow(paintInfo, paintRect, style(), Inset);
166 if (!style()->hasBorder())
169 // Create a clipping region around the legend and paint the border as normal
170 GraphicsContext* graphicsContext = paintInfo.context;
171 GraphicsContextStateSaver stateSaver(*graphicsContext);
173 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
174 // cases the legend is embedded in the right and bottom borders respectively.
175 // https://bugs.webkit.org/show_bug.cgi?id=47236
176 if (style()->isHorizontalWritingMode()) {
177 LayoutUnit clipTop = paintRect.y();
178 LayoutUnit clipHeight = max(static_cast<LayoutUnit>(style()->borderTopWidth()), legend->height() - ((legend->height() - borderTop()) / 2));
179 graphicsContext->clipOut(pixelSnappedIntRect(paintRect.x() + legend->x(), clipTop, legend->width(), clipHeight));
181 LayoutUnit clipLeft = paintRect.x();
182 LayoutUnit clipWidth = max(static_cast<LayoutUnit>(style()->borderLeftWidth()), legend->width());
183 graphicsContext->clipOut(pixelSnappedIntRect(clipLeft, paintRect.y() + legend->y(), clipWidth, legend->height()));
186 paintBorder(paintInfo, paintRect, style());
189 void RenderFieldset::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
191 if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
194 LayoutRect paintRect = LayoutRect(paintOffset, size());
195 RenderBox* legend = findLegend();
197 return RenderBlock::paintMask(paintInfo, paintOffset);
199 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
200 // cases the legend is embedded in the right and bottom borders respectively.
201 // https://bugs.webkit.org/show_bug.cgi?id=47236
202 if (style()->isHorizontalWritingMode()) {
203 LayoutUnit yOff = (legend->y() > 0) ? LayoutUnit() : (legend->height() - borderTop()) / 2;
204 paintRect.expand(0, -yOff);
205 paintRect.move(0, yOff);
207 LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2;
208 paintRect.expand(-xOff, 0);
209 paintRect.move(xOff, 0);
212 paintMaskImages(paintInfo, paintRect);
215 } // namespace WebCore