1ca34015509106ff8a02147ac6e99788f0592499
[WebKit-https.git] / Source / WebCore / rendering / RenderGrid.h
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 #ifndef RenderGrid_h
27 #define RenderGrid_h
28
29 #include "RenderBlock.h"
30
31 namespace WebCore {
32
33 class GridTrack;
34
35 class RenderGrid FINAL : public RenderBlock {
36 public:
37     explicit RenderGrid(Element&);
38     virtual ~RenderGrid();
39
40     Element& existingElement() const { return *RenderBlock::element(); }
41
42     virtual void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) OVERRIDE;
43
44     virtual bool avoidsFloats() const OVERRIDE { return true; }
45     virtual bool canCollapseAnonymousBlockChild() const OVERRIDE { return false; }
46
47     enum GridPositionSide {
48         ColumnStartSide,
49         ColumnEndSide,
50         RowStartSide,
51         RowEndSide
52     };
53
54 private:
55     void element() const WTF_DELETED_FUNCTION;
56
57     virtual const char* renderName() const OVERRIDE;
58     virtual bool isRenderGrid() const OVERRIDE { return true; }
59     virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const OVERRIDE;
60     virtual void computePreferredLogicalWidths() OVERRIDE;
61
62     LayoutUnit computePreferredTrackWidth(const Length&, size_t) const;
63
64     struct GridSpan {
65         static PassOwnPtr<GridSpan> create(size_t initialPosition, size_t finalPosition)
66         {
67             return adoptPtr(new GridSpan(initialPosition, finalPosition));
68         }
69
70         GridSpan(size_t initialPosition, size_t finalPosition)
71             : initialPositionIndex(initialPosition)
72             , finalPositionIndex(finalPosition)
73         {
74             ASSERT(initialPositionIndex <= finalPositionIndex);
75         }
76
77         size_t initialPositionIndex;
78         size_t finalPositionIndex;
79     };
80
81     struct GridCoordinate {
82         // HashMap requires a default constuctor.
83         GridCoordinate()
84             : columns(0, 0)
85             , rows(0, 0)
86         {
87         }
88
89         GridCoordinate(const GridSpan& r, const GridSpan& c)
90             : columns(c)
91             , rows(r)
92         {
93         }
94
95         GridSpan columns;
96         GridSpan rows;
97     };
98
99     class GridIterator;
100     enum TrackSizingDirection { ForColumns, ForRows };
101     void computedUsedBreadthOfGridTracks(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks);
102     LayoutUnit computeUsedBreadthOfMinLength(TrackSizingDirection, const Length&) const;
103     LayoutUnit computeUsedBreadthOfMaxLength(TrackSizingDirection, const Length&) const;
104     LayoutUnit computeUsedBreadthOfSpecifiedLength(TrackSizingDirection, const Length&) const;
105     void resolveContentBasedTrackSizingFunctions(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, LayoutUnit& availableLogicalSpace);
106
107     void growGrid(TrackSizingDirection);
108     void insertItemIntoGrid(RenderBox*, size_t rowTrack, size_t columnTrack);
109     void insertItemIntoGrid(RenderBox*, const GridCoordinate&);
110     void placeItemsOnGrid();
111     void placeSpecifiedMajorAxisItemsOnGrid(Vector<RenderBox*>);
112     void placeAutoMajorAxisItemsOnGrid(Vector<RenderBox*>);
113     void placeAutoMajorAxisItemOnGrid(RenderBox*);
114     TrackSizingDirection autoPlacementMajorAxisDirection() const;
115     TrackSizingDirection autoPlacementMinorAxisDirection() const;
116
117     void layoutGridItems();
118     void clearGrid();
119
120     typedef LayoutUnit (RenderGrid::* SizingFunction)(RenderBox*, TrackSizingDirection, Vector<GridTrack>&);
121     typedef LayoutUnit (GridTrack::* AccumulatorGetter)() const;
122     typedef void (GridTrack::* AccumulatorGrowFunction)(LayoutUnit);
123     typedef bool (GridTrackSize::* FilterFunction)() const;
124     void resolveContentBasedTrackSizingFunctionsForItems(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, RenderBox*, FilterFunction, SizingFunction, AccumulatorGetter, AccumulatorGrowFunction);
125     void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* tracksForGrowthAboveMaxBreadth, AccumulatorGetter, AccumulatorGrowFunction, LayoutUnit& availableLogicalSpace);
126
127     const GridTrackSize& gridTrackSize(TrackSizingDirection, size_t) const;
128     size_t explicitGridColumnCount() const;
129     size_t explicitGridRowCount() const;
130     size_t explicitGridSizeForSide(GridPositionSide) const;
131     size_t maximumIndexInDirection(TrackSizingDirection) const;
132
133     LayoutUnit logicalContentHeightForChild(RenderBox*, Vector<GridTrack>&);
134     LayoutUnit minContentForChild(RenderBox*, TrackSizingDirection, Vector<GridTrack>& columnTracks);
135     LayoutUnit maxContentForChild(RenderBox*, TrackSizingDirection, Vector<GridTrack>& columnTracks);
136     LayoutPoint findChildLogicalPosition(RenderBox*, const Vector<GridTrack>& columnTracks, const Vector<GridTrack>& rowTracks);
137     GridCoordinate cachedGridCoordinate(const RenderBox*) const;
138
139     GridSpan resolveGridPositionsFromAutoPlacementPosition(const RenderBox*, TrackSizingDirection, size_t) const;
140     PassOwnPtr<GridSpan> resolveGridPositionsFromStyle(const RenderBox*, TrackSizingDirection) const;
141     size_t resolveNamedGridLinePositionFromStyle(const GridPosition&, GridPositionSide) const;
142     size_t resolveGridPositionFromStyle(const GridPosition&, GridPositionSide) const;
143     PassOwnPtr<GridSpan> resolveGridPositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, GridPositionSide) const;
144     PassOwnPtr<GridSpan> resolveNamedGridLinePositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, GridPositionSide) const;
145     PassOwnPtr<GridSpan> resolveRowStartColumnStartNamedGridLinePositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, const Vector<size_t>&) const;
146     PassOwnPtr<GridSpan> resolveRowEndColumnEndNamedGridLinePositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, const Vector<size_t>&) const;
147
148     LayoutUnit gridAreaBreadthForChild(const RenderBox* child, TrackSizingDirection, const Vector<GridTrack>&) const;
149
150 #ifndef NDEBUG
151     bool tracksAreWiderThanMinTrackBreadth(TrackSizingDirection, const Vector<GridTrack>&);
152     bool gridWasPopulated() const { return !m_grid.isEmpty() && !m_grid[0].isEmpty(); }
153 #endif
154
155     size_t gridColumnCount() const
156     {
157         ASSERT(gridWasPopulated());
158         return m_grid[0].size();
159     }
160     size_t gridRowCount() const
161     {
162         ASSERT(gridWasPopulated());
163         return m_grid.size();
164     }
165
166     Vector<Vector<Vector<RenderBox*, 1> > > m_grid;
167     HashMap<const RenderBox*, GridCoordinate> m_gridItemCoordinate;
168 };
169
170 } // namespace WebCore
171
172 #endif // RenderGrid_h