1/*
2 * Copyright (C) 1997 Martin Jones (mjones@kde.org)
3 *           (C) 1997 Torben Weis (weis@kde.org)
4 *           (C) 1998 Waldo Bastian (bastian@kde.org)
5 *           (C) 1999 Lars Knoll (knoll@kde.org)
6 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
7 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2013 Apple Inc. All rights reserved.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB.  If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25#include "config.h"
26#include "core/rendering/RenderTableRow.h"
27
28#include "core/HTMLNames.h"
29#include "core/fetch/ImageResource.h"
30#include "core/paint/TableRowPainter.h"
31#include "core/rendering/GraphicsContextAnnotator.h"
32#include "core/rendering/HitTestResult.h"
33#include "core/rendering/PaintInfo.h"
34#include "core/rendering/RenderTableCell.h"
35#include "core/rendering/RenderView.h"
36#include "core/rendering/SubtreeLayoutScope.h"
37#include "core/rendering/style/StyleInheritedData.h"
38
39namespace blink {
40
41using namespace HTMLNames;
42
43RenderTableRow::RenderTableRow(Element* element)
44    : RenderBox(element)
45    , m_rowIndex(unsetRowIndex)
46{
47    // init RenderObject attributes
48    setInline(false);   // our object is not Inline
49}
50
51void RenderTableRow::trace(Visitor* visitor)
52{
53    visitor->trace(m_children);
54    RenderBox::trace(visitor);
55}
56
57void RenderTableRow::willBeRemovedFromTree()
58{
59    RenderBox::willBeRemovedFromTree();
60
61    section()->setNeedsCellRecalc();
62}
63
64static bool borderWidthChanged(const RenderStyle* oldStyle, const RenderStyle* newStyle)
65{
66    return oldStyle->borderLeftWidth() != newStyle->borderLeftWidth()
67        || oldStyle->borderTopWidth() != newStyle->borderTopWidth()
68        || oldStyle->borderRightWidth() != newStyle->borderRightWidth()
69        || oldStyle->borderBottomWidth() != newStyle->borderBottomWidth();
70}
71
72void RenderTableRow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
73{
74    ASSERT(style()->display() == TABLE_ROW);
75
76    RenderBox::styleDidChange(diff, oldStyle);
77    propagateStyleToAnonymousChildren();
78
79    if (section() && oldStyle && style()->logicalHeight() != oldStyle->logicalHeight())
80        section()->rowLogicalHeightChanged(this);
81
82    // If border was changed, notify table.
83    if (parent()) {
84        RenderTable* table = this->table();
85        if (table && !table->selfNeedsLayout() && !table->normalChildNeedsLayout() && oldStyle && oldStyle->border() != style()->border())
86            table->invalidateCollapsedBorders();
87
88        if (table && oldStyle && diff.needsFullLayout() && needsLayout() && table->collapseBorders() && borderWidthChanged(oldStyle, style())) {
89            // If the border width changes on a row, we need to make sure the cells in the row know to lay out again.
90            // This only happens when borders are collapsed, since they end up affecting the border sides of the cell
91            // itself.
92            for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox()) {
93                if (!childBox->isTableCell())
94                    continue;
95                childBox->setChildNeedsLayout();
96            }
97        }
98    }
99}
100
101const BorderValue& RenderTableRow::borderAdjoiningStartCell(const RenderTableCell* cell) const
102{
103    ASSERT_UNUSED(cell, cell->isFirstOrLastCellInRow());
104    // FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
105    return style()->borderStart();
106}
107
108const BorderValue& RenderTableRow::borderAdjoiningEndCell(const RenderTableCell* cell) const
109{
110    ASSERT_UNUSED(cell, cell->isFirstOrLastCellInRow());
111    // FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
112    return style()->borderEnd();
113}
114
115void RenderTableRow::addChild(RenderObject* child, RenderObject* beforeChild)
116{
117    if (!child->isTableCell()) {
118        RenderObject* last = beforeChild;
119        if (!last)
120            last = lastCell();
121        if (last && last->isAnonymous() && last->isTableCell() && !last->isBeforeOrAfterContent()) {
122            RenderTableCell* lastCell = toRenderTableCell(last);
123            if (beforeChild == lastCell)
124                beforeChild = lastCell->firstChild();
125            lastCell->addChild(child, beforeChild);
126            return;
127        }
128
129        if (beforeChild && !beforeChild->isAnonymous() && beforeChild->parent() == this) {
130            RenderObject* cell = beforeChild->previousSibling();
131            if (cell && cell->isTableCell() && cell->isAnonymous()) {
132                cell->addChild(child);
133                return;
134            }
135        }
136
137        // If beforeChild is inside an anonymous cell, insert into the cell.
138        if (last && !last->isTableCell() && last->parent() && last->parent()->isAnonymous() && !last->parent()->isBeforeOrAfterContent()) {
139            last->parent()->addChild(child, beforeChild);
140            return;
141        }
142
143        RenderTableCell* cell = RenderTableCell::createAnonymousWithParentRenderer(this);
144        addChild(cell, beforeChild);
145        cell->addChild(child);
146        return;
147    }
148
149    if (beforeChild && beforeChild->parent() != this)
150        beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
151
152    RenderTableCell* cell = toRenderTableCell(child);
153
154    // Generated content can result in us having a null section so make sure to null check our parent.
155    if (parent())
156        section()->addCell(cell, this);
157
158    ASSERT(!beforeChild || beforeChild->isTableCell());
159    RenderBox::addChild(cell, beforeChild);
160
161    if (beforeChild || nextRow())
162        section()->setNeedsCellRecalc();
163}
164
165void RenderTableRow::layout()
166{
167    ASSERT(needsLayout());
168
169    // Table rows do not add translation.
170    LayoutState state(*this, LayoutSize());
171
172    for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
173        SubtreeLayoutScope layouter(*cell);
174        if (!cell->needsLayout())
175            cell->markForPaginationRelayoutIfNeeded(layouter);
176        if (cell->needsLayout()) {
177            cell->computeAndSetBlockDirectionMargins(table());
178            cell->layout();
179        }
180    }
181
182    m_overflow.clear();
183    addVisualEffectOverflow();
184
185    // We only ever need to issue paint invalidations if our cells didn't, which means that they didn't need
186    // layout, so we know that our bounds didn't change. This code is just making up for
187    // the fact that we did not invalidate paints in setStyle() because we had a layout hint.
188    // We cannot call paintInvalidationForWholeRenderer() because our clippedOverflowRectForPaintInvalidation() is taken from the
189    // parent table, and being mid-layout, that is invalid. Instead, we issue paint invalidations for our cells.
190    if (selfNeedsLayout() && checkForPaintInvalidation()) {
191        for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
192            // FIXME: Is this needed when issuing paint invalidations after layout?
193            cell->setShouldDoFullPaintInvalidation(true);
194        }
195    }
196
197    // RenderTableSection::layoutRows will set our logical height and width later, so it calls updateLayerTransform().
198    clearNeedsLayout();
199}
200
201// Hit Testing
202bool RenderTableRow::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
203{
204    // Table rows cannot ever be hit tested.  Effectively they do not exist.
205    // Just forward to our children always.
206    for (RenderTableCell* cell = lastCell(); cell; cell = cell->previousCell()) {
207        // FIXME: We have to skip over inline flows, since they can show up inside table rows
208        // at the moment (a demoted inline <form> for example). If we ever implement a
209        // table-specific hit-test method (which we should do for performance reasons anyway),
210        // then we can remove this check.
211        if (!cell->hasSelfPaintingLayer()) {
212            LayoutPoint cellPoint = flipForWritingModeForChild(cell, accumulatedOffset);
213            if (cell->nodeAtPoint(request, result, locationInContainer, cellPoint, action)) {
214                updateHitTestResult(result, locationInContainer.point() - toLayoutSize(cellPoint));
215                return true;
216            }
217        }
218    }
219
220    return false;
221}
222
223void RenderTableRow::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
224{
225    TableRowPainter(*this).paint(paintInfo, paintOffset);
226}
227
228void RenderTableRow::imageChanged(WrappedImagePtr, const IntRect*)
229{
230    // FIXME: Examine cells and issue paint invalidations of only the rect the image paints in.
231    setShouldDoFullPaintInvalidation(true);
232}
233
234RenderTableRow* RenderTableRow::createAnonymous(Document* document)
235{
236    RenderTableRow* renderer = new RenderTableRow(0);
237    renderer->setDocumentForAnonymous(document);
238    return renderer;
239}
240
241RenderTableRow* RenderTableRow::createAnonymousWithParentRenderer(const RenderObject* parent)
242{
243    RenderTableRow* newRow = RenderTableRow::createAnonymous(&parent->document());
244    RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(parent->style(), TABLE_ROW);
245    newRow->setStyle(newStyle.release());
246    return newRow;
247}
248
249} // namespace blink
250