Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RenderGrid.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "RenderGrid.h"
28
29 #include "LayoutRepainter.h"
30 #include "NotImplemented.h"
31 #include "RenderLayer.h"
32 #include "RenderView.h"
33
34 namespace WebCore {
35
36 class RenderGrid::GridTrack {
37 public:
38     GridTrack()
39         : m_usedBreadth(0)
40     {
41     }
42
43     LayoutUnit m_usedBreadth;
44 };
45
46 RenderGrid::RenderGrid(Node* node)
47     : RenderBlock(node)
48 {
49     // All of our children must be block level.
50     setChildrenInline(false);
51 }
52
53 RenderGrid::~RenderGrid()
54 {
55 }
56
57 void RenderGrid::layoutBlock(bool relayoutChildren, LayoutUnit)
58 {
59     ASSERT(needsLayout());
60
61     if (!relayoutChildren && simplifiedLayout())
62         return;
63
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());
68
69     if (inRenderFlowThread()) {
70         // Regions changing widths can force us to relayout our children.
71         if (logicalWidthChangedInRegions())
72             relayoutChildren = true;
73     }
74     computeInitialRegionRangeForBlock();
75
76     LayoutSize previousSize = size();
77
78     setLogicalHeight(0);
79     computeLogicalWidth();
80
81     m_overflow.clear();
82
83     if (scrollsOverflow()) {
84         if (style()->overflowX() == OSCROLL)
85             layer()->setHasHorizontalScrollbar(true);
86         if (style()->overflowY() == OSCROLL)
87             layer()->setHasVerticalScrollbar(true);
88     }
89
90     layoutGridItems();
91
92     LayoutUnit oldClientAfterEdge = clientLogicalBottom();
93     computeLogicalHeight();
94
95     if (size() != previousSize)
96         relayoutChildren = true;
97
98     layoutPositionedObjects(relayoutChildren || isRoot());
99
100     computeRegionRangeForBlock();
101
102     computeOverflow(oldClientAfterEdge);
103     statePusher.pop();
104
105     updateLayerTransform();
106
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();
111
112     repainter.repaintAfterLayout();
113
114     setNeedsLayout(false);
115 }
116
117 void RenderGrid::computedUsedBreadthOfGridTracks(TrackSizingDirection direction, Vector<GridTrack>& tracks)
118 {
119     const Vector<Length>& trackStyles = (direction == ForColumns) ? style()->gridColumns() : style()->gridRows();
120     for (size_t i = 0; i < trackStyles.size(); ++i) {
121         GridTrack track;
122         if (trackStyles[i].isFixed())
123             track.m_usedBreadth = trackStyles[i].getFloatValue();
124         else
125             notImplemented();
126
127         tracks.append(track);
128     }
129 }
130
131 void RenderGrid::layoutGridItems()
132 {
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);
139
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();
146
147         // FIXME: Handle border & padding on the grid element.
148         child->setLogicalLocation(childPosition);
149     }
150
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);
154 }
155
156 LayoutPoint RenderGrid::findChildLogicalPosition(RenderBox* child, const Vector<GridTrack>& columnTracks, const Vector<GridTrack>& rowTracks)
157 {
158     Length column = child->style()->gridItemColumn();
159     Length row = child->style()->gridItemRow();
160
161     // FIXME: What does a non-positive integer mean for a column/row?
162     if (!column.isPositive() || !row.isPositive())
163         return LayoutPoint();
164
165     // FIXME: Handle other values for grid-{row,column} like ranges or line names.
166     if (!column.isFixed() || !row.isFixed())
167         return LayoutPoint();
168
169     size_t columnTrack = static_cast<size_t>(column.intValue()) - 1;
170     size_t rowTrack = static_cast<size_t>(row.intValue()) - 1;
171
172     LayoutPoint offset;
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);
177
178     // FIXME: Handle margins on the grid item.
179     return offset;
180 }
181
182 const char* RenderGrid::renderName() const
183 {
184     if (isFloating())
185         return "RenderGrid (floating)";
186     if (isOutOfFlowPositioned())
187         return "RenderGrid (positioned)";
188     if (isAnonymous())
189         return "RenderGrid (generated)";
190     if (isRelPositioned())
191         return "RenderGrid (relative positioned)";
192     return "RenderGrid";
193 }
194
195 } // namespace WebCore