0d14c80cf29b2131bd55174e57e32d6641f9e61d
[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     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 GridSpan {
55         GridSpan(size_t initialPosition, size_t finalPosition)
56             : initialPositionIndex(initialPosition)
57             , finalPositionIndex(finalPosition)
58         {
59             ASSERT(initialPositionIndex <= finalPositionIndex);
60         }
61
62         size_t initialPositionIndex;
63         size_t finalPositionIndex;
64     };
65
66     struct GridCoordinate {
67         // HashMap requires a default constuctor.
68         GridCoordinate()
69             : columns(0, 0)
70             , rows(0, 0)
71         {
72         }
73
74         GridCoordinate(const GridSpan& r, const GridSpan& c)
75             : columns(c)
76             , rows(r)
77         {
78         }
79
80         GridSpan columns;
81         GridSpan rows;
82     };
83
84     class GridIterator;
85     enum TrackSizingDirection { ForColumns, ForRows };
86     void computedUsedBreadthOfGridTracks(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks);
87     LayoutUnit computeUsedBreadthOfMinLength(TrackSizingDirection, const Length&) const;
88     LayoutUnit computeUsedBreadthOfMaxLength(TrackSizingDirection, const Length&) const;
89     LayoutUnit computeUsedBreadthOfSpecifiedLength(TrackSizingDirection, const Length&) const;
90     void resolveContentBasedTrackSizingFunctions(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, LayoutUnit& availableLogicalSpace);
91
92     void growGrid(TrackSizingDirection);
93     void insertItemIntoGrid(RenderBox*, size_t rowTrack, size_t columnTrack);
94     void insertItemIntoGrid(RenderBox*, const GridCoordinate&);
95     void placeItemsOnGrid();
96     void placeSpecifiedMajorAxisItemsOnGrid(Vector<RenderBox*>);
97     void placeAutoMajorAxisItemsOnGrid(Vector<RenderBox*>);
98     void placeAutoMajorAxisItemOnGrid(RenderBox*);
99     TrackSizingDirection autoPlacementMajorAxisDirection() const;
100     TrackSizingDirection autoPlacementMinorAxisDirection() const;
101
102     void layoutGridItems();
103     void clearGrid();
104
105     typedef LayoutUnit (RenderGrid::* SizingFunction)(RenderBox*, TrackSizingDirection, Vector<GridTrack>&);
106     typedef LayoutUnit (GridTrack::* AccumulatorGetter)() const;
107     typedef void (GridTrack::* AccumulatorGrowFunction)(LayoutUnit);
108     typedef bool (GridTrackSize::* FilterFunction)() const;
109     void resolveContentBasedTrackSizingFunctionsForItems(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, RenderBox*, FilterFunction, SizingFunction, AccumulatorGetter, AccumulatorGrowFunction);
110     void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* tracksForGrowthAboveMaxBreadth, AccumulatorGetter, AccumulatorGrowFunction, LayoutUnit& availableLogicalSpace);
111
112     const GridTrackSize& gridTrackSize(TrackSizingDirection, size_t) const;
113     size_t maximumIndexInDirection(TrackSizingDirection) const;
114
115     LayoutUnit logicalContentHeightForChild(RenderBox*, Vector<GridTrack>&);
116     LayoutUnit minContentForChild(RenderBox*, TrackSizingDirection, Vector<GridTrack>& columnTracks);
117     LayoutUnit maxContentForChild(RenderBox*, TrackSizingDirection, Vector<GridTrack>& columnTracks);
118     LayoutPoint findChildLogicalPosition(RenderBox*, const Vector<GridTrack>& columnTracks, const Vector<GridTrack>& rowTracks);
119     GridCoordinate cachedGridCoordinate(const RenderBox*) const;
120
121     GridSpan resolveGridPositionsFromAutoPlacementPosition(const RenderBox*, TrackSizingDirection, size_t) const;
122     PassOwnPtr<GridSpan> resolveGridPositionsFromStyle(const RenderBox*, TrackSizingDirection) const;
123     enum GridPositionSide {
124         StartSide,
125         EndSide,
126         BeforeSide,
127         AfterSide
128     };
129     size_t resolveGridPositionFromStyle(const GridPosition&, GridPositionSide) const;
130
131     LayoutUnit gridAreaBreadthForChild(const RenderBox* child, TrackSizingDirection, const Vector<GridTrack>&) const;
132
133 #ifndef NDEBUG
134     bool tracksAreWiderThanMinTrackBreadth(TrackSizingDirection, const Vector<GridTrack>&);
135     bool gridWasPopulated() const { return !m_grid.isEmpty() && !m_grid[0].isEmpty(); }
136 #endif
137
138     size_t gridColumnCount() const
139     {
140         ASSERT(gridWasPopulated());
141         return m_grid[0].size();
142     }
143     size_t gridRowCount() const
144     {
145         ASSERT(gridWasPopulated());
146         return m_grid.size();
147     }
148
149     Vector<Vector<Vector<RenderBox*, 1> > > m_grid;
150     HashMap<const RenderBox*, GridCoordinate> m_gridItemCoordinate;
151 };
152
153 } // namespace WebCore
154
155 #endif // RenderGrid_h