~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/rendering/RenderGrid.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    updateRegionsAndExclusionsLogicalSize();
 
75
 
 
76
    LayoutSize previousSize = size();
 
77
 
 
78
    setLogicalHeight(0);
 
79
    updateLogicalWidth();
 
80
 
 
81
    m_overflow.clear();
 
82
 
 
83
    layoutGridItems();
 
84
 
 
85
    LayoutUnit oldClientAfterEdge = clientLogicalBottom();
 
86
    updateLogicalHeight();
 
87
 
 
88
    if (size() != previousSize)
 
89
        relayoutChildren = true;
 
90
 
 
91
    layoutPositionedObjects(relayoutChildren || isRoot());
 
92
 
 
93
    computeRegionRangeForBlock();
 
94
 
 
95
    computeOverflow(oldClientAfterEdge);
 
96
    statePusher.pop();
 
97
 
 
98
    updateLayerTransform();
 
99
 
 
100
    // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
 
101
    // we overflow or not.
 
102
    if (hasOverflowClip())
 
103
        layer()->updateScrollInfoAfterLayout();
 
104
 
 
105
    repainter.repaintAfterLayout();
 
106
 
 
107
    setNeedsLayout(false);
 
108
}
 
109
 
 
110
void RenderGrid::computePreferredLogicalWidths()
 
111
{
 
112
    ASSERT(preferredLogicalWidthsDirty());
 
113
 
 
114
    m_minPreferredLogicalWidth = 0;
 
115
    m_maxPreferredLogicalWidth = 0;
 
116
 
 
117
    // FIXME: We don't take our own logical width into account.
 
118
 
 
119
    const Vector<GridTrackSize>& trackStyles = style()->gridColumns();
 
120
 
 
121
    for (size_t i = 0; i < trackStyles.size(); ++i) {
 
122
        Length trackLength = trackStyles[i].length();
 
123
        if (!trackLength.isFixed()) {
 
124
            notImplemented();
 
125
            continue;
 
126
        }
 
127
 
 
128
        m_minPreferredLogicalWidth += trackLength.intValue();
 
129
        m_maxPreferredLogicalWidth += trackLength.intValue();
 
130
    }
 
131
 
 
132
    // FIXME: We should account for min / max logical width.
 
133
 
 
134
    // FIXME: Include borders and paddings in inline direction.
 
135
 
 
136
    setPreferredLogicalWidthsDirty(false);
 
137
}
 
138
 
 
139
void RenderGrid::computedUsedBreadthOfGridTracks(TrackSizingDirection direction, Vector<GridTrack>& tracks)
 
140
{
 
141
    const Vector<GridTrackSize>& trackStyles = (direction == ForColumns) ? style()->gridColumns() : style()->gridRows();
 
142
    for (size_t i = 0; i < trackStyles.size(); ++i) {
 
143
        GridTrack track;
 
144
        if (trackStyles[i].length().isFixed())
 
145
            track.m_usedBreadth = trackStyles[i].length().getFloatValue();
 
146
        else
 
147
            notImplemented();
 
148
 
 
149
        tracks.append(track);
 
150
    }
 
151
}
 
152
 
 
153
void RenderGrid::layoutGridItems()
 
154
{
 
155
    Vector<GridTrack> columnTracks, rowTracks;
 
156
    computedUsedBreadthOfGridTracks(ForColumns, columnTracks);
 
157
    // FIXME: The logical width of Grid Columns from the prior step is used in
 
158
    // the formatting of Grid items in content-sized Grid Rows to determine
 
159
    // their required height. We will probably need to pass columns through.
 
160
    computedUsedBreadthOfGridTracks(ForRows, rowTracks);
 
161
 
 
162
    for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
 
163
        LayoutPoint childPosition = findChildLogicalPosition(child, columnTracks, rowTracks);
 
164
 
 
165
        size_t columnTrack = resolveGridPosition(child->style()->gridItemColumn());
 
166
        size_t rowTrack = resolveGridPosition(child->style()->gridItemRow());
 
167
 
 
168
        // FIXME: Properly support implicit rows and columns (bug 103573).
 
169
        if (columnTrack < columnTracks.size() && rowTrack < rowTracks.size()) {
 
170
            // Because the grid area cannot be styled, we don't need to adjust
 
171
            // the grid breadth to account for 'box-sizing'.
 
172
            child->setOverrideContainingBlockContentLogicalWidth(columnTracks[columnTrack].m_usedBreadth);
 
173
            child->setOverrideContainingBlockContentLogicalHeight(rowTracks[rowTrack].m_usedBreadth);
 
174
        }
 
175
 
 
176
        // FIXME: Grid items should stretch to fill their cells. Once we
 
177
        // implement grid-{column,row}-align, we can also shrink to fit. For
 
178
        // now, just size as if we were a regular child.
 
179
        child->layoutIfNeeded();
 
180
 
 
181
        // FIXME: Handle border & padding on the grid element.
 
182
        child->setLogicalLocation(childPosition);
 
183
    }
 
184
 
 
185
    // FIXME: Handle border & padding on the grid element.
 
186
    for (size_t i = 0; i < rowTracks.size(); ++i)
 
187
        setLogicalHeight(logicalHeight() + rowTracks[i].m_usedBreadth);
 
188
}
 
189
 
 
190
size_t RenderGrid::resolveGridPosition(const GridPosition& position) const
 
191
{
 
192
    // FIXME: Handle other values for grid-{row,column} like ranges or line names.
 
193
    switch (position.type()) {
 
194
    case IntegerPosition:
 
195
        // FIXME: What does a non-positive integer mean for a column/row?
 
196
        if (!position.isPositive())
 
197
            return 0;
 
198
 
 
199
        return position.integerPosition() - 1;
 
200
    case AutoPosition:
 
201
        // FIXME: We should follow 'grid-auto-flow' for resolution.
 
202
        // Until then, we use the 'grid-auto-flow: none' behavior (which is the default)
 
203
        // and resolve 'auto' as the first row / column.
 
204
        return 0;
 
205
    }
 
206
    ASSERT_NOT_REACHED();
 
207
    return 0;
 
208
}
 
209
 
 
210
LayoutPoint RenderGrid::findChildLogicalPosition(RenderBox* child, const Vector<GridTrack>& columnTracks, const Vector<GridTrack>& rowTracks)
 
211
{
 
212
    size_t columnTrack = resolveGridPosition(child->style()->gridItemColumn());
 
213
    size_t rowTrack = resolveGridPosition(child->style()->gridItemRow());
 
214
 
 
215
    LayoutPoint offset;
 
216
    // FIXME: |columnTrack| and |rowTrack| should be smaller than our column / row count.
 
217
    for (size_t i = 0; i < columnTrack && i < columnTracks.size(); ++i)
 
218
        offset.setX(offset.x() + columnTracks[i].m_usedBreadth);
 
219
    for (size_t i = 0; i < rowTrack && i < rowTracks.size(); ++i)
 
220
        offset.setY(offset.y() + rowTracks[i].m_usedBreadth);
 
221
 
 
222
    // FIXME: Handle margins on the grid item.
 
223
    return offset;
 
224
}
 
225
 
 
226
const char* RenderGrid::renderName() const
 
227
{
 
228
    if (isFloating())
 
229
        return "RenderGrid (floating)";
 
230
    if (isOutOfFlowPositioned())
 
231
        return "RenderGrid (positioned)";
 
232
    if (isAnonymous())
 
233
        return "RenderGrid (generated)";
 
234
    if (isRelPositioned())
 
235
        return "RenderGrid (relative positioned)";
 
236
    return "RenderGrid";
 
237
}
 
238
 
 
239
} // namespace WebCore