initial import
[vuplus_webkit] / Source / WebCore / rendering / RenderFieldset.cpp
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
32 using std::min;
33 using std::max;
34
35 namespace WebCore {
36
37 using namespace HTMLNames;
38
39 RenderFieldset::RenderFieldset(Node* element)
40     : RenderBlock(element)
41 {
42 }
43
44 void 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
63 RenderObject* 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 b = borderBefore();
105         LayoutUnit h = logicalHeightForChild(legend);
106         setLogicalTopForChild(legend, max<LayoutUnit>((b - h) / 2, 0));
107         setLogicalHeight(max(b, h) + paddingBefore());
108     }
109     return legend;
110 }
111
112 RenderBox* RenderFieldset::findLegend() const
113 {
114     for (RenderObject* legend = firstChild(); legend; legend = legend->nextSibling()) {
115         if (!legend->isFloatingOrPositioned() && legend->node() && (legend->node()->hasTagName(legendTag)))
116             return toRenderBox(legend);
117     }
118     return 0;
119 }
120
121 void RenderFieldset::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
122 {
123     if (!paintInfo.shouldPaintWithinRoot(this))
124         return;
125
126     LayoutRect paintRect(paintOffset, size());
127     RenderBox* legend = findLegend();
128     if (!legend)
129         return RenderBlock::paintBoxDecorations(paintInfo, paintOffset);
130
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);
138     } else {
139         LayoutUnit xOff = (legend->x() > 0) ? 0 : (legend->width() - borderLeft()) / 2;
140         paintRect.setWidth(paintRect.width() - xOff);
141         paintRect.setX(paintRect.x() + xOff);
142     }
143     
144     paintBoxShadow(paintInfo, paintRect, style(), Normal);
145     paintFillLayers(paintInfo, style()->visitedDependentColor(CSSPropertyBackgroundColor), style()->backgroundLayers(), paintRect);
146     paintBoxShadow(paintInfo, paintRect, style(), Inset);
147
148     if (!style()->hasBorder())
149         return;
150     
151     // Create a clipping region around the legend and paint the border as normal
152     GraphicsContext* graphicsContext = paintInfo.context;
153     GraphicsContextStateSaver stateSaver(*graphicsContext);
154
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));
162     } else {
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()));
166     }
167
168     paintBorder(paintInfo, paintRect, style());
169 }
170
171 void RenderFieldset::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
172 {
173     if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
174         return;
175
176     LayoutRect paintRect = LayoutRect(paintOffset, size());
177     RenderBox* legend = findLegend();
178     if (!legend)
179         return RenderBlock::paintMask(paintInfo, paintOffset);
180
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);
188     } else {
189         LayoutUnit xOff = (legend->x() > 0) ? 0 : (legend->width() - borderLeft()) / 2;
190         paintRect.expand(-xOff, 0);
191         paintRect.move(xOff, 0);
192     }
193
194     paintMaskImages(paintInfo, paintRect);
195 }
196
197 } // namespace WebCore