2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "RenderGrid.h"
29 #include "LayoutRepainter.h"
30 #include "NotImplemented.h"
31 #include "RenderLayer.h"
32 #include "RenderView.h"
36 class RenderGrid::GridTrack {
43 LayoutUnit m_usedBreadth;
46 RenderGrid::RenderGrid(Node* node)
49 // All of our children must be block level.
50 setChildrenInline(false);
53 RenderGrid::~RenderGrid()
57 void RenderGrid::layoutBlock(bool relayoutChildren, LayoutUnit)
59 ASSERT(needsLayout());
61 if (!relayoutChildren && simplifiedLayout())
64 // FIXME: Much of this method is boiler plate that matches RenderBox::layoutBlock and Render*FlexibleBox::layoutBlock.
65 // It would be nice to refactor some of the duplicate code.
66 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
67 LayoutStateMaintainer statePusher(view(), this, locationOffset(), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
69 if (inRenderFlowThread()) {
70 // Regions changing widths can force us to relayout our children.
71 if (logicalWidthChangedInRegions())
72 relayoutChildren = true;
74 computeInitialRegionRangeForBlock();
76 LayoutSize previousSize = size();
79 computeLogicalWidth();
83 if (scrollsOverflow()) {
84 if (style()->overflowX() == OSCROLL)
85 layer()->setHasHorizontalScrollbar(true);
86 if (style()->overflowY() == OSCROLL)
87 layer()->setHasVerticalScrollbar(true);
92 LayoutUnit oldClientAfterEdge = clientLogicalBottom();
93 computeLogicalHeight();
95 if (size() != previousSize)
96 relayoutChildren = true;
98 layoutPositionedObjects(relayoutChildren || isRoot());
100 computeRegionRangeForBlock();
102 computeOverflow(oldClientAfterEdge);
105 updateLayerTransform();
107 // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
108 // we overflow or not.
109 if (hasOverflowClip())
110 layer()->updateScrollInfoAfterLayout();
112 repainter.repaintAfterLayout();
114 setNeedsLayout(false);
117 void RenderGrid::computedUsedBreadthOfGridTracks(TrackSizingDirection direction, Vector<GridTrack>& tracks)
119 const Vector<Length>& trackStyles = (direction == ForColumns) ? style()->gridColumns() : style()->gridRows();
120 for (size_t i = 0; i < trackStyles.size(); ++i) {
122 if (trackStyles[i].isFixed())
123 track.m_usedBreadth = trackStyles[i].getFloatValue();
127 tracks.append(track);
131 void RenderGrid::layoutGridItems()
133 Vector<GridTrack> columnTracks, rowTracks;
134 computedUsedBreadthOfGridTracks(ForColumns, columnTracks);
135 // FIXME: The logical width of Grid Columns from the prior step is used in
136 // the formatting of Grid items in content-sized Grid Rows to determine
137 // their required height. We will probably need to pass columns through.
138 computedUsedBreadthOfGridTracks(ForRows, rowTracks);
140 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
141 LayoutPoint childPosition = findChildLogicalPosition(child, columnTracks, rowTracks);
142 // FIXME: Grid items should stretch to fill their cells. Once we
143 // implement grid-{column,row}-align, we can also shrink to fit. For
144 // now, just size as if we were a regular child.
145 child->layoutIfNeeded();
147 // FIXME: Handle border & padding on the grid element.
148 child->setLogicalLocation(childPosition);
151 // FIXME: Handle border & padding on the grid element.
152 for (size_t i = 0; i < rowTracks.size(); ++i)
153 setLogicalHeight(logicalHeight() + rowTracks[i].m_usedBreadth);
156 LayoutPoint RenderGrid::findChildLogicalPosition(RenderBox* child, const Vector<GridTrack>& columnTracks, const Vector<GridTrack>& rowTracks)
158 Length column = child->style()->gridItemColumn();
159 Length row = child->style()->gridItemRow();
161 // FIXME: What does a non-positive integer mean for a column/row?
162 if (!column.isPositive() || !row.isPositive())
163 return LayoutPoint();
165 // FIXME: Handle other values for grid-{row,column} like ranges or line names.
166 if (!column.isFixed() || !row.isFixed())
167 return LayoutPoint();
169 size_t columnTrack = static_cast<size_t>(column.intValue()) - 1;
170 size_t rowTrack = static_cast<size_t>(row.intValue()) - 1;
173 for (size_t i = 0; i < columnTrack && i < columnTracks.size(); ++i)
174 offset.setX(offset.x() + columnTracks[i].m_usedBreadth);
175 for (size_t i = 0; i < rowTrack && i < rowTracks.size(); ++i)
176 offset.setY(offset.y() + rowTracks[i].m_usedBreadth);
178 // FIXME: Handle margins on the grid item.
182 const char* RenderGrid::renderName() const
185 return "RenderGrid (floating)";
186 if (isOutOfFlowPositioned())
187 return "RenderGrid (positioned)";
189 return "RenderGrid (generated)";
190 if (isRelPositioned())
191 return "RenderGrid (relative positioned)";
195 } // namespace WebCore