[CSS Grid Layout] Implement grid growth during auto placement
[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 : public RenderBlock {
36 public:
37     RenderGrid(Element*);
38     virtual ~RenderGrid();
39
40     virtual const char* renderName() const OVERRIDE;
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 private:
48     virtual bool isRenderGrid() const OVERRIDE { return true; }
49     virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const OVERRIDE;
50     virtual void computePreferredLogicalWidths() OVERRIDE;
51
52     LayoutUnit computePreferredTrackWidth(const Length&, size_t) const;
53
54     struct GridCoordinate {
55         // HashMap requires a default constuctor.
56         GridCoordinate()
57             : columnIndex(0)
58             , rowIndex(0)
59         {
60         }
61
62         GridCoordinate(size_t row, size_t column)
63             : columnIndex(column)
64             , rowIndex(row)
65         {
66         }
67
68         size_t columnIndex;
69         size_t rowIndex;
70     };
71     class GridIterator;
72     enum TrackSizingDirection { ForColumns, ForRows };
73     void computedUsedBreadthOfGridTracks(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks);
74     LayoutUnit computeUsedBreadthOfMinLength(TrackSizingDirection, const Length&) const;
75     LayoutUnit computeUsedBreadthOfMaxLength(TrackSizingDirection, const Length&) const;
76     LayoutUnit computeUsedBreadthOfSpecifiedLength(TrackSizingDirection, const Length&) const;
77     void resolveContentBasedTrackSizingFunctions(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, LayoutUnit& availableLogicalSpace);
78
79     void growGrid(TrackSizingDirection);
80     void insertItemIntoGrid(RenderBox*, size_t rowTrack, size_t columnTrack);
81     void placeItemsOnGrid();
82     void placeSpecifiedMajorAxisItemsOnGrid(Vector<RenderBox*>);
83     void placeAutoMajorAxisItemsOnGrid(Vector<RenderBox*>);
84     void placeAutoMajorAxisItemOnGrid(RenderBox*);
85     const GridPosition& autoPlacementMajorAxisPositionForChild(const RenderBox*) const;
86     const GridPosition& autoPlacementMinorAxisPositionForChild(const RenderBox*) const;
87     TrackSizingDirection autoPlacementMajorAxisDirection() const;
88     TrackSizingDirection autoPlacementMinorAxisDirection() const;
89
90     void layoutGridItems();
91     void clearGrid();
92
93     typedef LayoutUnit (RenderGrid::* SizingFunction)(RenderBox*, TrackSizingDirection, Vector<GridTrack>&);
94     typedef LayoutUnit (GridTrack::* AccumulatorGetter)() const;
95     typedef void (GridTrack::* AccumulatorGrowFunction)(LayoutUnit);
96     void resolveContentBasedTrackSizingFunctionsForItems(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, size_t, SizingFunction, AccumulatorGetter, AccumulatorGrowFunction);
97     void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* tracksForGrowthAboveMaxBreadth, AccumulatorGetter, AccumulatorGrowFunction, LayoutUnit& availableLogicalSpace);
98
99     const GridTrackSize& gridTrackSize(TrackSizingDirection, size_t);
100     size_t maximumIndexInDirection(TrackSizingDirection) const;
101
102     LayoutUnit logicalContentHeightForChild(RenderBox*, Vector<GridTrack>&);
103     LayoutUnit minContentForChild(RenderBox*, TrackSizingDirection, Vector<GridTrack>& columnTracks);
104     LayoutUnit maxContentForChild(RenderBox*, TrackSizingDirection, Vector<GridTrack>& columnTracks);
105     LayoutPoint findChildLogicalPosition(RenderBox*, const Vector<GridTrack>& columnTracks, const Vector<GridTrack>& rowTracks);
106     GridCoordinate cachedGridCoordinate(const RenderBox*) const;
107     size_t resolveGridPositionFromStyle(const GridPosition&) const;
108
109 #ifndef NDEBUG
110     bool tracksAreWiderThanMinTrackBreadth(TrackSizingDirection, const Vector<GridTrack>&);
111     bool gridWasPopulated() const { return !m_grid.isEmpty() && !m_grid[0].isEmpty(); }
112 #endif
113
114     size_t gridColumnCount() const
115     {
116         ASSERT(gridWasPopulated());
117         return m_grid[0].size();
118     }
119     size_t gridRowCount() const
120     {
121         ASSERT(gridWasPopulated());
122         return m_grid.size();
123     }
124
125     Vector<Vector<Vector<RenderBox*, 1> > > m_grid;
126     HashMap<const RenderBox*, GridCoordinate> m_gridItemCoordinate;
127 };
128
129 } // namespace WebCore
130
131 #endif // RenderGrid_h