1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2000 Dirk Mueller (mueller (at) 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 "core/rendering/RenderFieldset.h" 26 27 #include "core/CSSPropertyNames.h" 28 #include "core/HTMLNames.h" 29 #include "core/html/HTMLLegendElement.h" 30 #include "core/paint/BoxDecorationData.h" 31 #include "core/paint/BoxPainter.h" 32 #include "core/rendering/PaintInfo.h" 33 #include "platform/graphics/GraphicsContextStateSaver.h" 34 35 using std::min; 36 using std::max; 37 38 namespace blink { 39 40 using namespace HTMLNames; 41 42 RenderFieldset::RenderFieldset(Element* element) 43 : RenderBlockFlow(element) 44 { 45 } 46 47 void RenderFieldset::computePreferredLogicalWidths() 48 { 49 RenderBlockFlow::computePreferredLogicalWidths(); 50 if (RenderBox* legend = findLegend()) { 51 int legendMinWidth = legend->minPreferredLogicalWidth(); 52 53 Length legendMarginLeft = legend->style()->marginLeft(); 54 Length legendMarginRight = legend->style()->marginLeft(); 55 56 if (legendMarginLeft.isFixed()) 57 legendMinWidth += legendMarginLeft.value(); 58 59 if (legendMarginRight.isFixed()) 60 legendMinWidth += legendMarginRight.value(); 61 62 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, legendMinWidth + borderAndPaddingWidth()); 63 } 64 } 65 66 RenderObject* RenderFieldset::layoutSpecialExcludedChild(bool relayoutChildren, SubtreeLayoutScope&) 67 { 68 RenderBox* legend = findLegend(); 69 if (legend) { 70 if (relayoutChildren) 71 legend->setNeedsLayoutAndFullPaintInvalidation(); 72 legend->layoutIfNeeded(); 73 74 LayoutUnit logicalLeft; 75 if (style()->isLeftToRightDirection()) { 76 switch (legend->style()->textAlign()) { 77 case CENTER: 78 logicalLeft = (logicalWidth() - logicalWidthForChild(legend)) / 2; 79 break; 80 case RIGHT: 81 logicalLeft = logicalWidth() - borderEnd() - paddingEnd() - logicalWidthForChild(legend); 82 break; 83 default: 84 logicalLeft = borderStart() + paddingStart() + marginStartForChild(legend); 85 break; 86 } 87 } else { 88 switch (legend->style()->textAlign()) { 89 case LEFT: 90 logicalLeft = borderStart() + paddingStart(); 91 break; 92 case CENTER: { 93 // Make sure that the extra pixel goes to the end side in RTL (since it went to the end side 94 // in LTR). 95 LayoutUnit centeredWidth = logicalWidth() - logicalWidthForChild(legend); 96 logicalLeft = centeredWidth - centeredWidth / 2; 97 break; 98 } 99 default: 100 logicalLeft = logicalWidth() - borderStart() - paddingStart() - marginStartForChild(legend) - logicalWidthForChild(legend); 101 break; 102 } 103 } 104 105 setLogicalLeftForChild(legend, logicalLeft); 106 107 LayoutUnit fieldsetBorderBefore = borderBefore(); 108 LayoutUnit legendLogicalHeight = logicalHeightForChild(legend); 109 110 LayoutUnit legendLogicalTop; 111 LayoutUnit collapsedLegendExtent; 112 // FIXME: We need to account for the legend's margin before too. 113 if (fieldsetBorderBefore > legendLogicalHeight) { 114 // The <legend> is smaller than the associated fieldset before border 115 // so the latter determines positioning of the <legend>. The sizing depends 116 // on the legend's margins as we want to still follow the author's cues. 117 // Firefox completely ignores the margins in this case which seems wrong. 118 legendLogicalTop = (fieldsetBorderBefore - legendLogicalHeight) / 2; 119 collapsedLegendExtent = max<LayoutUnit>(fieldsetBorderBefore, legendLogicalTop + legendLogicalHeight + marginAfterForChild(legend)); 120 } else 121 collapsedLegendExtent = legendLogicalHeight + marginAfterForChild(legend); 122 123 setLogicalTopForChild(legend, legendLogicalTop); 124 setLogicalHeight(paddingBefore() + collapsedLegendExtent); 125 } 126 return legend; 127 } 128 129 RenderBox* RenderFieldset::findLegend(FindLegendOption option) const 130 { 131 for (RenderObject* legend = firstChild(); legend; legend = legend->nextSibling()) { 132 if (option == IgnoreFloatingOrOutOfFlow && legend->isFloatingOrOutOfFlowPositioned()) 133 continue; 134 135 if (isHTMLLegendElement(legend->node())) 136 return toRenderBox(legend); 137 } 138 return 0; 139 } 140 141 void RenderFieldset::paintBoxDecorationBackground(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 142 { 143 if (!paintInfo.shouldPaintWithinRoot(this)) 144 return; 145 146 LayoutRect paintRect(paintOffset, size()); 147 RenderBox* legend = findLegend(); 148 if (!legend) 149 return RenderBlockFlow::paintBoxDecorationBackground(paintInfo, paintOffset); 150 151 // FIXME: We need to work with "rl" and "bt" block flow directions. In those 152 // cases the legend is embedded in the right and bottom borders respectively. 153 // https://bugs.webkit.org/show_bug.cgi?id=47236 154 if (style()->isHorizontalWritingMode()) { 155 LayoutUnit yOff = (legend->y() > 0) ? LayoutUnit() : (legend->height() - borderTop()) / 2; 156 paintRect.setHeight(paintRect.height() - yOff); 157 paintRect.setY(paintRect.y() + yOff); 158 } else { 159 LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2; 160 paintRect.setWidth(paintRect.width() - xOff); 161 paintRect.setX(paintRect.x() + xOff); 162 } 163 164 BoxDecorationData boxDecorationData(*style(), canRenderBorderImage(), backgroundHasOpaqueTopLayer(), paintInfo.context); 165 166 if (boxDecorationData.bleedAvoidance() == BackgroundBleedNone) 167 BoxPainter::paintBoxShadow(paintInfo, paintRect, style(), Normal); 168 BoxPainter(*this).paintFillLayers(paintInfo, boxDecorationData.backgroundColor, style()->backgroundLayers(), paintRect); 169 BoxPainter::paintBoxShadow(paintInfo, paintRect, style(), Inset); 170 171 if (!boxDecorationData.hasBorder) 172 return; 173 174 // Create a clipping region around the legend and paint the border as normal 175 GraphicsContext* graphicsContext = paintInfo.context; 176 GraphicsContextStateSaver stateSaver(*graphicsContext); 177 178 // FIXME: We need to work with "rl" and "bt" block flow directions. In those 179 // cases the legend is embedded in the right and bottom borders respectively. 180 // https://bugs.webkit.org/show_bug.cgi?id=47236 181 if (style()->isHorizontalWritingMode()) { 182 LayoutUnit clipTop = paintRect.y(); 183 LayoutUnit clipHeight = max(static_cast<LayoutUnit>(style()->borderTopWidth()), legend->height() - ((legend->height() - borderTop()) / 2)); 184 graphicsContext->clipOut(pixelSnappedIntRect(paintRect.x() + legend->x(), clipTop, legend->width(), clipHeight)); 185 } else { 186 LayoutUnit clipLeft = paintRect.x(); 187 LayoutUnit clipWidth = max(static_cast<LayoutUnit>(style()->borderLeftWidth()), legend->width()); 188 graphicsContext->clipOut(pixelSnappedIntRect(clipLeft, paintRect.y() + legend->y(), clipWidth, legend->height())); 189 } 190 191 BoxPainter::paintBorder(*this, paintInfo, paintRect, style()); 192 } 193 194 void RenderFieldset::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 195 { 196 if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask) 197 return; 198 199 LayoutRect paintRect = LayoutRect(paintOffset, size()); 200 RenderBox* legend = findLegend(); 201 if (!legend) 202 return RenderBlockFlow::paintMask(paintInfo, paintOffset); 203 204 // FIXME: We need to work with "rl" and "bt" block flow directions. In those 205 // cases the legend is embedded in the right and bottom borders respectively. 206 // https://bugs.webkit.org/show_bug.cgi?id=47236 207 if (style()->isHorizontalWritingMode()) { 208 LayoutUnit yOff = (legend->y() > 0) ? LayoutUnit() : (legend->height() - borderTop()) / 2; 209 paintRect.expand(0, -yOff); 210 paintRect.move(0, yOff); 211 } else { 212 LayoutUnit xOff = (legend->x() > 0) ? LayoutUnit() : (legend->width() - borderLeft()) / 2; 213 paintRect.expand(-xOff, 0); 214 paintRect.move(xOff, 0); 215 } 216 217 BoxPainter(*this).paintMaskImages(paintInfo, paintRect); 218 } 219 220 } // namespace blink 221