14eb2b21df9cade4536a22f33dd079c54c664e68
[WebKit-https.git] / Source / WebCore / rendering / RenderTableRow.cpp
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 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 "RenderTableRow.h"
27
28 #include "CachedImage.h"
29 #include "Document.h"
30 #include "HTMLNames.h"
31 #include "PaintInfo.h"
32 #include "RenderTableCell.h"
33 #include "RenderView.h"
34
35 namespace WebCore {
36
37 using namespace HTMLNames;
38
39 RenderTableRow::RenderTableRow(Node* node)
40     : RenderBox(node)
41 {
42     // init RenderObject attributes
43     setInline(false);   // our object is not Inline
44 }
45
46 void RenderTableRow::willBeDestroyed()
47 {
48     RenderTableSection* recalcSection = section();
49     
50     RenderBox::willBeDestroyed();
51     
52     if (recalcSection)
53         recalcSection->setNeedsCellRecalc();
54 }
55
56 void RenderTableRow::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
57 {
58     if (section() && style() && style()->logicalHeight() != newStyle->logicalHeight())
59         section()->setNeedsCellRecalc();
60
61     ASSERT(newStyle->display() == TABLE_ROW);
62
63     RenderBox::styleWillChange(diff, newStyle);
64 }
65
66 void RenderTableRow::updateBeforeAndAfterContent()
67 {
68     if (!isAnonymous() && document()->usesBeforeAfterRules()) {
69         children()->updateBeforeAfterContent(this, BEFORE);
70         children()->updateBeforeAfterContent(this, AFTER);
71     }
72 }
73
74 void RenderTableRow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
75 {
76     RenderBox::styleDidChange(diff, oldStyle);
77     propagateStyleToAnonymousChildren();
78
79     if (parent())
80         updateBeforeAndAfterContent();
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 }
89
90 void RenderTableRow::addChild(RenderObject* child, RenderObject* beforeChild)
91 {
92     // Make sure we don't append things after :after-generated content if we have it.
93     if (!beforeChild)
94         beforeChild = findAfterContentRenderer();
95
96     if (!child->isTableCell()) {
97         RenderObject* last = beforeChild;
98         if (!last)
99             last = lastChild();
100         if (last && last->isAnonymous() && last->isTableCell() && !last->isBeforeOrAfterContent()) {
101             if (beforeChild == last)
102                 beforeChild = last->firstChild();
103             last->addChild(child, beforeChild);
104             return;
105         }
106
107         if (beforeChild && !beforeChild->isAnonymous() && beforeChild->parent() == this) {
108             RenderObject* cell = beforeChild->previousSibling();
109             if (cell && cell->isTableCell()) {
110                 ASSERT(cell->isAnonymous());
111                 cell->addChild(child);
112                 return;
113             }
114         }
115
116         // If beforeChild is inside an anonymous cell, insert into the cell.
117         if (last && !last->isTableCell() && last->parent() && last->parent()->isAnonymous() && !last->parent()->isBeforeOrAfterContent()) {
118             last->parent()->addChild(child, beforeChild);
119             return;
120         }
121
122         RenderTableCell* cell = new (renderArena()) RenderTableCell(document() /* anonymous object */);
123         RefPtr<RenderStyle> newStyle = RenderStyle::create();
124         newStyle->inheritFrom(style());
125         newStyle->setDisplay(TABLE_CELL);
126         cell->setStyle(newStyle.release());
127         addChild(cell, beforeChild);
128         cell->addChild(child);
129         return;
130     } 
131     
132     // If the next renderer is actually wrapped in an anonymous table cell, we need to go up and find that.
133     while (beforeChild && beforeChild->parent() != this)
134         beforeChild = beforeChild->parent();
135
136     RenderTableCell* cell = toRenderTableCell(child);
137
138     // Generated content can result in us having a null section so make sure to null check our parent.
139     if (parent())
140         section()->addCell(cell, this);
141
142     ASSERT(!beforeChild || beforeChild->isTableCell());
143     RenderBox::addChild(cell, beforeChild);
144
145     if (beforeChild || nextSibling())
146         section()->setNeedsCellRecalc();
147 }
148
149 void RenderTableRow::layout()
150 {
151     ASSERT(needsLayout());
152
153     // Table rows do not add translation.
154     LayoutStateMaintainer statePusher(view(), this, LayoutSize(), style()->isFlippedBlocksWritingMode());
155
156     bool paginated = view()->layoutState()->isPaginated();
157                 
158     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
159         if (child->isTableCell()) {
160             RenderTableCell* cell = toRenderTableCell(child);
161             if (!cell->needsLayout() && paginated && view()->layoutState()->pageLogicalHeight() && view()->layoutState()->pageLogicalOffset(cell->logicalTop()) != cell->pageLogicalOffset())
162                 cell->setChildNeedsLayout(true, false);
163
164             if (child->needsLayout()) {
165                 cell->computeBlockDirectionMargins(table());
166                 cell->layout();
167             }
168         }
169     }
170
171     // We only ever need to repaint if our cells didn't, which menas that they didn't need
172     // layout, so we know that our bounds didn't change. This code is just making up for
173     // the fact that we did not repaint in setStyle() because we had a layout hint.
174     // We cannot call repaint() because our clippedOverflowRectForRepaint() is taken from the
175     // parent table, and being mid-layout, that is invalid. Instead, we repaint our cells.
176     if (selfNeedsLayout() && checkForRepaintDuringLayout()) {
177         for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
178             if (child->isTableCell())
179                 child->repaint();
180         }
181     }
182
183     statePusher.pop();
184     // RenderTableSection::layoutRows will set our logical height and width later, so it calls updateLayerTransform().
185     setNeedsLayout(false);
186 }
187
188 IntRect RenderTableRow::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer) const
189 {
190     ASSERT(parent());
191
192     if (repaintContainer == this)
193         return RenderBox::clippedOverflowRectForRepaint(repaintContainer);
194
195     // For now, just repaint the whole table.
196     // FIXME: Find a better way to do this, e.g., need to repaint all the cells that we
197     // might have propagated a background color into.
198     // FIXME: do repaintContainer checks here
199     if (RenderTable* parentTable = table())
200         return parentTable->clippedOverflowRectForRepaint(repaintContainer);
201
202     return IntRect();
203 }
204
205 // Hit Testing
206 bool RenderTableRow::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
207 {
208     // Table rows cannot ever be hit tested.  Effectively they do not exist.
209     // Just forward to our children always.
210     for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
211         // FIXME: We have to skip over inline flows, since they can show up inside table rows
212         // at the moment (a demoted inline <form> for example). If we ever implement a
213         // table-specific hit-test method (which we should do for performance reasons anyway),
214         // then we can remove this check.
215         if (child->isTableCell() && !toRenderBox(child)->hasSelfPaintingLayer()) {
216             LayoutPoint cellPoint = flipForWritingModeForChild(toRenderTableCell(child), accumulatedOffset);
217             if (child->nodeAtPoint(request, result, pointInContainer, cellPoint, action)) {
218                 updateHitTestResult(result, pointInContainer - toLayoutSize(cellPoint));
219                 return true;
220             }
221         }
222     }
223     
224     return false;
225 }
226
227 void RenderTableRow::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
228 {
229     ASSERT(hasSelfPaintingLayer());
230     if (!layer())
231         return;
232     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
233         if (child->isTableCell()) {
234             // Paint the row background behind the cell.
235             if (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseChildBlockBackground) {
236                 RenderTableCell* cell = toRenderTableCell(child);
237                 cell->paintBackgroundsBehindCell(paintInfo, paintOffset, this);
238             }
239             if (!toRenderBox(child)->hasSelfPaintingLayer())
240                 child->paint(paintInfo, paintOffset);
241         }
242     }
243 }
244
245 void RenderTableRow::imageChanged(WrappedImagePtr, const IntRect*)
246 {
247     // FIXME: Examine cells and repaint only the rect the image paints in.
248     repaint();
249 }
250
251 } // namespace WebCore