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 "RenderFieldset.h"
27 #include "CSSPropertyNames.h"
28 #include "GraphicsContext.h"
29 #include "HTMLNames.h"
30 #include "PaintInfo.h"
37 using namespace HTMLNames;
39 RenderFieldset::RenderFieldset(Node* element)
40 : RenderBlock(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)
65 RenderBox* legend = findLegend();
68 legend->setNeedsLayout(true);
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 b = borderBefore();
105 LayoutUnit h = logicalHeightForChild(legend);
106 setLogicalTopForChild(legend, max<LayoutUnit>((b - h) / 2, 0));
107 setLogicalHeight(max(b, h) + paddingBefore());
112 RenderBox* RenderFieldset::findLegend() const
114 for (RenderObject* legend = firstChild(); legend; legend = legend->nextSibling()) {
115 if (!legend->isFloatingOrPositioned() && legend->node() && (legend->node()->hasTagName(legendTag)))
116 return toRenderBox(legend);
121 void RenderFieldset::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
123 if (!paintInfo.shouldPaintWithinRoot(this))
126 LayoutRect paintRect(paintOffset, size());
127 RenderBox* legend = findLegend();
129 return RenderBlock::paintBoxDecorations(paintInfo, paintOffset);
131 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
132 // cases the legend is embedded in the right and bottom borders respectively.
133 // https://bugs.webkit.org/show_bug.cgi?id=47236
134 if (style()->isHorizontalWritingMode()) {
135 LayoutUnit yOff = (legend->y() > 0) ? 0 : (legend->height() - borderTop()) / 2;
136 paintRect.setHeight(paintRect.height() - yOff);
137 paintRect.setY(paintRect.y() + yOff);
139 LayoutUnit xOff = (legend->x() > 0) ? 0 : (legend->width() - borderLeft()) / 2;
140 paintRect.setWidth(paintRect.width() - xOff);
141 paintRect.setX(paintRect.x() + xOff);
144 paintBoxShadow(paintInfo, paintRect, style(), Normal);
145 paintFillLayers(paintInfo, style()->visitedDependentColor(CSSPropertyBackgroundColor), style()->backgroundLayers(), paintRect);
146 paintBoxShadow(paintInfo, paintRect, style(), Inset);
148 if (!style()->hasBorder())
151 // Create a clipping region around the legend and paint the border as normal
152 GraphicsContext* graphicsContext = paintInfo.context;
153 GraphicsContextStateSaver stateSaver(*graphicsContext);
155 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
156 // cases the legend is embedded in the right and bottom borders respectively.
157 // https://bugs.webkit.org/show_bug.cgi?id=47236
158 if (style()->isHorizontalWritingMode()) {
159 LayoutUnit clipTop = paintRect.y();
160 LayoutUnit clipHeight = max(static_cast<LayoutUnit>(style()->borderTopWidth()), legend->height());
161 graphicsContext->clipOut(LayoutRect(paintRect.x() + legend->x(), clipTop, legend->width(), clipHeight));
163 LayoutUnit clipLeft = paintRect.x();
164 LayoutUnit clipWidth = max(static_cast<LayoutUnit>(style()->borderLeftWidth()), legend->width());
165 graphicsContext->clipOut(LayoutRect(clipLeft, paintRect.y() + legend->y(), clipWidth, legend->height()));
168 paintBorder(paintInfo, paintRect, style());
171 void RenderFieldset::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
173 if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
176 LayoutRect paintRect = LayoutRect(paintOffset, size());
177 RenderBox* legend = findLegend();
179 return RenderBlock::paintMask(paintInfo, paintOffset);
181 // FIXME: We need to work with "rl" and "bt" block flow directions. In those
182 // cases the legend is embedded in the right and bottom borders respectively.
183 // https://bugs.webkit.org/show_bug.cgi?id=47236
184 if (style()->isHorizontalWritingMode()) {
185 LayoutUnit yOff = (legend->y() > 0) ? 0 : (legend->height() - borderTop()) / 2;
186 paintRect.expand(0, -yOff);
187 paintRect.move(0, yOff);
189 LayoutUnit xOff = (legend->x() > 0) ? 0 : (legend->width() - borderLeft()) / 2;
190 paintRect.expand(-xOff, 0);
191 paintRect.move(xOff, 0);
194 paintMaskImages(paintInfo, paintRect);
197 } // namespace WebCore