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, 2009, 2010 Apple Inc. All rights reserved.
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.
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.
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.
28 #include "CSSPropertyNames.h"
29 #include "CollapsedBorderValue.h"
30 #include "RenderBlock.h"
31 #include <wtf/Vector.h>
36 class RenderTableCaption;
37 class RenderTableCell;
38 class RenderTableSection;
41 enum SkipEmptySectionsValue { DoNotSkipEmptySections, SkipEmptySections };
43 class RenderTable : public RenderBlock {
45 explicit RenderTable(Node*);
46 virtual ~RenderTable();
48 int getColumnPos(unsigned col) const { return m_columnPos[col]; }
50 int hBorderSpacing() const { return m_hSpacing; }
51 int vBorderSpacing() const { return m_vSpacing; }
53 bool collapseBorders() const { return style()->borderCollapse(); }
55 int borderStart() const { return m_borderStart; }
56 int borderEnd() const { return m_borderEnd; }
57 int borderBefore() const;
58 int borderAfter() const;
60 int borderLeft() const
62 if (style()->isHorizontalWritingMode())
63 return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
64 return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
67 int borderRight() const
69 if (style()->isHorizontalWritingMode())
70 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
71 return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
76 if (style()->isHorizontalWritingMode())
77 return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
78 return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
81 int borderBottom() const
83 if (style()->isHorizontalWritingMode())
84 return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
85 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
88 Color bgColor() const { return style()->visitedDependentColor(CSSPropertyBackgroundColor); }
90 int outerBorderBefore() const;
91 int outerBorderAfter() const;
92 int outerBorderStart() const;
93 int outerBorderEnd() const;
95 int outerBorderLeft() const
97 if (style()->isHorizontalWritingMode())
98 return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
99 return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
102 int outerBorderRight() const
104 if (style()->isHorizontalWritingMode())
105 return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
106 return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
109 int outerBorderTop() const
111 if (style()->isHorizontalWritingMode())
112 return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
113 return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
116 int outerBorderBottom() const
118 if (style()->isHorizontalWritingMode())
119 return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
120 return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
123 int calcBorderStart() const;
124 int calcBorderEnd() const;
125 void recalcBordersInRowDirection();
127 virtual void addChild(RenderObject* child, RenderObject* beforeChild = 0);
129 struct ColumnStruct {
138 void forceSectionsRecalc()
140 setNeedsSectionRecalc();
144 Vector<ColumnStruct>& columns() { return m_columns; }
145 Vector<int>& columnPositions() { return m_columnPos; }
146 RenderTableSection* header() const { return m_head; }
147 RenderTableSection* footer() const { return m_foot; }
148 RenderTableSection* firstBody() const { return m_firstBody; }
150 // This function returns 0 if the table has no section.
151 RenderTableSection* topSection() const;
152 RenderTableSection* bottomSection() const;
154 // This function returns 0 if the table has no non-empty sections.
155 RenderTableSection* topNonEmptySection() const;
157 unsigned lastColumnIndex() const { return numEffCols() - 1; }
159 void splitColumn(unsigned position, unsigned firstSpan);
160 void appendColumn(unsigned span);
161 unsigned numEffCols() const { return m_columns.size(); }
162 unsigned spanOfEffCol(unsigned effCol) const { return m_columns[effCol].span; }
164 unsigned colToEffCol(unsigned column) const
166 unsigned effColumn = 0;
167 unsigned numColumns = numEffCols();
168 for (unsigned c = 0; effColumn < numColumns && c + m_columns[effColumn].span - 1 < column; ++effColumn)
169 c += m_columns[effColumn].span;
173 unsigned effColToCol(unsigned effCol) const
176 for (unsigned i = 0; i < effCol; i++)
177 c += m_columns[i].span;
181 LayoutUnit bordersPaddingAndSpacingInRowDirection() const
183 return borderStart() + borderEnd() +
184 (collapseBorders() ? ZERO_LAYOUT_UNIT : (paddingStart() + paddingEnd() + static_cast<LayoutUnit>(numEffCols() + 1) * hBorderSpacing()));
187 // Return the first column or column-group.
188 RenderTableCol* firstColumn() const;
190 RenderTableCol* colElement(unsigned col, bool* startEdge = 0, bool* endEdge = 0) const;
192 bool needsSectionRecalc() const { return m_needsSectionRecalc; }
193 void setNeedsSectionRecalc()
195 if (documentBeingDestroyed())
197 m_needsSectionRecalc = true;
198 setNeedsLayout(true);
201 RenderTableSection* sectionAbove(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
202 RenderTableSection* sectionBelow(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
204 RenderTableCell* cellAbove(const RenderTableCell*) const;
205 RenderTableCell* cellBelow(const RenderTableCell*) const;
206 RenderTableCell* cellBefore(const RenderTableCell*) const;
207 RenderTableCell* cellAfter(const RenderTableCell*) const;
209 typedef Vector<CollapsedBorderValue> CollapsedBorderValues;
210 void invalidateCollapsedBorders()
212 m_collapsedBordersValid = false;
213 m_collapsedBorders.clear();
215 const CollapsedBorderValue* currentBorderValue() const { return m_currentBorder; }
217 bool hasSections() const { return m_head || m_foot || m_firstBody; }
219 void recalcSectionsIfNeeded() const
221 if (m_needsSectionRecalc)
225 static RenderTable* createAnonymousWithParentRenderer(const RenderObject*);
226 virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const OVERRIDE
228 return createAnonymousWithParentRenderer(parent);
231 const BorderValue& tableStartBorderAdjoiningCell(const RenderTableCell*) const;
232 const BorderValue& tableEndBorderAdjoiningCell(const RenderTableCell*) const;
235 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
238 virtual const char* renderName() const { return "RenderTable"; }
240 virtual bool isTable() const { return true; }
242 virtual bool avoidsFloats() const { return true; }
244 virtual void removeChild(RenderObject* oldChild);
246 virtual void paint(PaintInfo&, const LayoutPoint&);
247 virtual void paintObject(PaintInfo&, const LayoutPoint&);
248 virtual void paintBoxDecorations(PaintInfo&, const LayoutPoint&);
249 virtual void paintMask(PaintInfo&, const LayoutPoint&);
250 virtual void layout();
251 virtual void computePreferredLogicalWidths();
252 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) OVERRIDE;
254 virtual LayoutUnit firstLineBoxBaseline() const OVERRIDE;
255 virtual LayoutUnit lastLineBoxBaseline() const OVERRIDE;
257 virtual RenderBlock* firstLineBlock() const;
258 virtual void updateFirstLetter();
260 virtual void setCellLogicalWidths();
262 virtual void computeLogicalWidth();
264 LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth);
266 virtual LayoutRect overflowClipRect(const LayoutPoint& location, RenderRegion*, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize);
268 virtual void addOverflowFromChildren();
270 void subtractCaptionRect(LayoutRect&) const;
272 void recalcCollapsedBorders();
273 void recalcSections() const;
274 void layoutCaption(RenderTableCaption*);
276 void distributeExtraLogicalHeight(int extraLogicalHeight);
278 mutable Vector<int> m_columnPos;
279 mutable Vector<ColumnStruct> m_columns;
280 mutable Vector<RenderTableCaption*> m_captions;
282 mutable RenderTableSection* m_head;
283 mutable RenderTableSection* m_foot;
284 mutable RenderTableSection* m_firstBody;
286 OwnPtr<TableLayout> m_tableLayout;
288 CollapsedBorderValues m_collapsedBorders;
289 const CollapsedBorderValue* m_currentBorder;
290 bool m_collapsedBordersValid : 1;
292 mutable bool m_hasColElements : 1;
293 mutable bool m_needsSectionRecalc : 1;
301 inline RenderTableSection* RenderTable::topSection() const
303 ASSERT(!needsSectionRecalc());
311 inline RenderTable* toRenderTable(RenderObject* object)
313 ASSERT(!object || object->isTable());
314 return static_cast<RenderTable*>(object);
317 inline const RenderTable* toRenderTable(const RenderObject* object)
319 ASSERT(!object || object->isTable());
320 return static_cast<const RenderTable*>(object);
323 // This will catch anyone doing an unnecessary cast.
324 void toRenderTable(const RenderTable*);
326 } // namespace WebCore
328 #endif // RenderTable_h