1/*
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.
6 *
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.
11 *
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.
16 *
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.
21 *
22 */
23
24#include "config.h"
25#include "RenderFieldset.h"
26
27#include "CSSPropertyNames.h"
28#include "GraphicsContext.h"
29#include "HTMLNames.h"
30#include "PaintInfo.h"
31
32using std::min;
33using std::max;
34
35namespace WebCore {
36
37using namespace HTMLNames;
38
39RenderFieldset::RenderFieldset(Element* element)
40    : RenderBlock(element)
41{
42}
43
44void RenderFieldset::computePreferredLogicalWidths()
45{
46    RenderBlock::computePreferredLogicalWidths();
47    if (RenderBox* legend = findLegend()) {
48        int legendMinWidth = legend->minPreferredLogicalWidth();
49
50        Length legendMarginLeft = legend->style()->marginLeft();
51        Length legendMarginRight = legend->style()->marginLeft();
52
53        if (legendMarginLeft.isFixed())
54            legendMinWidth += legendMarginLeft.value();
55
56        if (legendMarginRight.isFixed())
57            legendMinWidth += legendMarginRight.value();
58
59        m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, legendMinWidth + borderAndPaddingWidth());
60    }
61}
62
63RenderObject* RenderFieldset::layoutSpecialExcludedChild(bool relayoutChildren)
64{
65    RenderBox* legend = findLegend();
66    if (legend) {
67        if (relayoutChildren)
68            legend->setNeedsLayout(true);
69        legend->layoutIfNeeded();
70
71        LayoutUnit logicalLeft;
72        if (style()->isLeftToRightDirection()) {
73            switch (legend->style()->textAlign()) {
74            case CENTER:
75                logicalLeft = (logicalWidth() - logicalWidthForChild(legend)) / 2;
76                break;
77            case RIGHT:
78                logicalLeft = logicalWidth() - borderEnd() - paddingEnd() - logicalWidthForChild(legend);
79                break;
80            default:
81                logicalLeft = borderStart() + paddingStart() + marginStartForChild(legend);
82                break;
83            }
84        } else {
85            switch (legend->style()->textAlign()) {
86            case LEFT:
87                logicalLeft = borderStart() + paddingStart();
88                break;
89            case CENTER: {
90                // Make sure that the extra pixel goes to the end side in RTL (since it went to the end side
91                // in LTR).
92                LayoutUnit centeredWidth = logicalWidth() - logicalWidthForChild(legend);
93                logicalLeft = centeredWidth - centeredWidth / 2;
94                break;
95            }
96            default:
97                logicalLeft = logicalWidth() - borderStart() - paddingStart() - marginStartForChild(legend) - logicalWidthForChild(legend);
98                break;
99            }
100        }
101
102        setLogicalLeftForChild(legend, logicalLeft);
103
104        LayoutUnit fieldsetBorderBefore = borderBefore();
105        LayoutUnit legendLogicalHeight = logicalHeightForChild(legend);
106
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));
117        } else
118            collapsedLegendExtent = legendLogicalHeight + marginAfterForChild(legend);
119
120        setLogicalTopForChild(legend, legendLogicalTop);
121        setLogicalHeight(paddingBefore() + collapsedLegendExtent);
122    }
123    return legend;
124}
125
126RenderBox* RenderFieldset::findLegend(FindLegendOption option) const
127{
128    for (RenderObject* legend = firstChild(); legend; legend = legend->nextSibling()) {
129        if (option == IgnoreFloatingOrOutOfFlow && legend->isFloatingOrOutOfFlowPositioned())
130            continue;
131
132        if (legend->node() && (legend->node()->hasTagName(legendTag)))
133            return toRenderBox(legend);
134    }
135    return 0;
136}
137
138void RenderFieldset::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
139{
140    if (!paintInfo.shouldPaintWithinRoot(this))
141        return;
142
143    LayoutRect paintRect(paintOffset, size());
144    RenderBox* legend = findLegend();
145    if (!legend)
146        return RenderBlock::paintBoxDecorations(paintInfo, paintOffset);
147
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);
155    } else {
156        LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2;
157        paintRect.setWidth(paintRect.width() - xOff);
158        paintRect.setX(paintRect.x() + xOff);
159    }
160
161    if (!boxShadowShouldBeAppliedToBackground(determineBackgroundBleedAvoidance(paintInfo.context)))
162        paintBoxShadow(paintInfo, paintRect, style(), Normal);
163    paintFillLayers(paintInfo, style()->visitedDependentColor(CSSPropertyBackgroundColor), style()->backgroundLayers(), paintRect);
164    paintBoxShadow(paintInfo, paintRect, style(), Inset);
165
166    if (!style()->hasBorder())
167        return;
168
169    // Create a clipping region around the legend and paint the border as normal
170    GraphicsContext* graphicsContext = paintInfo.context;
171    GraphicsContextStateSaver stateSaver(*graphicsContext);
172
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));
180    } else {
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()));
184    }
185
186    paintBorder(paintInfo, paintRect, style());
187}
188
189void RenderFieldset::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
190{
191    if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
192        return;
193
194    LayoutRect paintRect = LayoutRect(paintOffset, size());
195    RenderBox* legend = findLegend();
196    if (!legend)
197        return RenderBlock::paintMask(paintInfo, paintOffset);
198
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);
206    } else {
207        LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2;
208        paintRect.expand(-xOff, 0);
209        paintRect.move(xOff, 0);
210    }
211
212    paintMaskImages(paintInfo, paintRect);
213}
214
215} // namespace WebCore
216