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/layout/LayoutFieldset.h"
27 #include "core/CSSPropertyNames.h"
28 #include "core/HTMLNames.h"
29 #include "core/html/HTMLLegendElement.h"
30 #include "core/paint/FieldsetPainter.h"
36 using namespace HTMLNames
;
38 LayoutFieldset::LayoutFieldset(Element
* element
)
39 : LayoutBlockFlow(element
)
43 void LayoutFieldset::computePreferredLogicalWidths()
45 LayoutBlockFlow::computePreferredLogicalWidths();
46 if (LayoutBox
* legend
= findInFlowLegend()) {
47 int legendMinWidth
= legend
->minPreferredLogicalWidth();
49 Length legendMarginLeft
= legend
->style()->marginLeft();
50 Length legendMarginRight
= legend
->style()->marginLeft();
52 if (legendMarginLeft
.isFixed())
53 legendMinWidth
+= legendMarginLeft
.value();
55 if (legendMarginRight
.isFixed())
56 legendMinWidth
+= legendMarginRight
.value();
58 m_minPreferredLogicalWidth
= max(m_minPreferredLogicalWidth
, legendMinWidth
+ borderAndPaddingWidth());
62 LayoutObject
* LayoutFieldset::layoutSpecialExcludedChild(bool relayoutChildren
, SubtreeLayoutScope
&)
64 LayoutBox
* legend
= findInFlowLegend();
66 LayoutRect oldLegendFrameRect
= legend
->frameRect();
69 legend
->setNeedsLayoutAndFullPaintInvalidation(LayoutInvalidationReason::FieldsetChanged
);
70 legend
->layoutIfNeeded();
72 LayoutUnit logicalLeft
;
73 if (style()->isLeftToRightDirection()) {
74 switch (legend
->style()->textAlign()) {
76 logicalLeft
= (logicalWidth() - logicalWidthForChild(*legend
)) / 2;
79 logicalLeft
= logicalWidth() - borderEnd() - paddingEnd() - logicalWidthForChild(*legend
);
82 logicalLeft
= borderStart() + paddingStart() + marginStartForChild(*legend
);
86 switch (legend
->style()->textAlign()) {
88 logicalLeft
= borderStart() + paddingStart();
91 // Make sure that the extra pixel goes to the end side in RTL (since it went to the end side
93 LayoutUnit centeredWidth
= logicalWidth() - logicalWidthForChild(*legend
);
94 logicalLeft
= centeredWidth
- centeredWidth
/ 2;
98 logicalLeft
= logicalWidth() - borderStart() - paddingStart() - marginStartForChild(*legend
) - logicalWidthForChild(*legend
);
103 setLogicalLeftForChild(*legend
, logicalLeft
);
105 LayoutUnit fieldsetBorderBefore
= borderBefore();
106 LayoutUnit legendLogicalHeight
= logicalHeightForChild(*legend
);
108 LayoutUnit legendLogicalTop
;
109 LayoutUnit collapsedLegendExtent
;
110 // FIXME: We need to account for the legend's margin before too.
111 if (fieldsetBorderBefore
> legendLogicalHeight
) {
112 // The <legend> is smaller than the associated fieldset before border
113 // so the latter determines positioning of the <legend>. The sizing depends
114 // on the legend's margins as we want to still follow the author's cues.
115 // Firefox completely ignores the margins in this case which seems wrong.
116 legendLogicalTop
= (fieldsetBorderBefore
- legendLogicalHeight
) / 2;
117 collapsedLegendExtent
= max
<LayoutUnit
>(fieldsetBorderBefore
, legendLogicalTop
+ legendLogicalHeight
+ marginAfterForChild(*legend
));
119 collapsedLegendExtent
= legendLogicalHeight
+ marginAfterForChild(*legend
);
122 setLogicalTopForChild(*legend
, legendLogicalTop
);
123 setLogicalHeight(paddingBefore() + collapsedLegendExtent
);
125 if (legend
->frameRect() != oldLegendFrameRect
) {
126 // We need to invalidate the fieldset border if the legend's frame changed.
127 setShouldDoFullPaintInvalidation();
133 LayoutBox
* LayoutFieldset::findInFlowLegend() const
135 for (LayoutObject
* legend
= firstChild(); legend
; legend
= legend
->nextSibling()) {
136 if (legend
->isFloatingOrOutOfFlowPositioned())
139 if (isHTMLLegendElement(legend
->node()))
140 return toLayoutBox(legend
);
145 void LayoutFieldset::paintBoxDecorationBackground(const PaintInfo
& paintInfo
, const LayoutPoint
& paintOffset
)
147 FieldsetPainter(*this).paintBoxDecorationBackground(paintInfo
, paintOffset
);
150 void LayoutFieldset::paintMask(const PaintInfo
& paintInfo
, const LayoutPoint
& paintOffset
)
152 FieldsetPainter(*this).paintMask(paintInfo
, paintOffset
);