RenderGrid.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef RenderGrid_h
  26. #define RenderGrid_h
  27. #include "RenderBlock.h"
  28. namespace WebCore {
  29. class GridTrack;
  30. class RenderGrid : public RenderBlock {
  31. public:
  32. RenderGrid(Element*);
  33. virtual ~RenderGrid();
  34. virtual const char* renderName() const OVERRIDE;
  35. virtual void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) OVERRIDE;
  36. virtual bool avoidsFloats() const OVERRIDE { return true; }
  37. virtual bool canCollapseAnonymousBlockChild() const OVERRIDE { return false; }
  38. private:
  39. virtual bool isRenderGrid() const OVERRIDE { return true; }
  40. virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const OVERRIDE;
  41. virtual void computePreferredLogicalWidths() OVERRIDE;
  42. LayoutUnit computePreferredTrackWidth(const Length&, size_t) const;
  43. struct GridSpan {
  44. GridSpan(size_t initialPosition, size_t finalPosition)
  45. : initialPositionIndex(initialPosition)
  46. , finalPositionIndex(finalPosition)
  47. {
  48. ASSERT(initialPositionIndex <= finalPositionIndex);
  49. }
  50. size_t initialPositionIndex;
  51. size_t finalPositionIndex;
  52. };
  53. struct GridCoordinate {
  54. // HashMap requires a default constuctor.
  55. GridCoordinate()
  56. : columns(0, 0)
  57. , rows(0, 0)
  58. {
  59. }
  60. GridCoordinate(const GridSpan& r, const GridSpan& c)
  61. : columns(c)
  62. , rows(r)
  63. {
  64. }
  65. GridSpan columns;
  66. GridSpan rows;
  67. };
  68. class GridIterator;
  69. enum TrackSizingDirection { ForColumns, ForRows };
  70. void computedUsedBreadthOfGridTracks(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks);
  71. LayoutUnit computeUsedBreadthOfMinLength(TrackSizingDirection, const Length&) const;
  72. LayoutUnit computeUsedBreadthOfMaxLength(TrackSizingDirection, const Length&) const;
  73. LayoutUnit computeUsedBreadthOfSpecifiedLength(TrackSizingDirection, const Length&) const;
  74. void resolveContentBasedTrackSizingFunctions(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, LayoutUnit& availableLogicalSpace);
  75. void growGrid(TrackSizingDirection);
  76. void insertItemIntoGrid(RenderBox*, size_t rowTrack, size_t columnTrack);
  77. void insertItemIntoGrid(RenderBox*, const GridCoordinate&);
  78. void placeItemsOnGrid();
  79. void placeSpecifiedMajorAxisItemsOnGrid(Vector<RenderBox*>);
  80. void placeAutoMajorAxisItemsOnGrid(Vector<RenderBox*>);
  81. void placeAutoMajorAxisItemOnGrid(RenderBox*);
  82. TrackSizingDirection autoPlacementMajorAxisDirection() const;
  83. TrackSizingDirection autoPlacementMinorAxisDirection() const;
  84. void layoutGridItems();
  85. void clearGrid();
  86. typedef LayoutUnit (RenderGrid::* SizingFunction)(RenderBox*, TrackSizingDirection, Vector<GridTrack>&);
  87. typedef LayoutUnit (GridTrack::* AccumulatorGetter)() const;
  88. typedef void (GridTrack::* AccumulatorGrowFunction)(LayoutUnit);
  89. typedef bool (GridTrackSize::* FilterFunction)() const;
  90. void resolveContentBasedTrackSizingFunctionsForItems(TrackSizingDirection, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, RenderBox*, FilterFunction, SizingFunction, AccumulatorGetter, AccumulatorGrowFunction);
  91. void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* tracksForGrowthAboveMaxBreadth, AccumulatorGetter, AccumulatorGrowFunction, LayoutUnit& availableLogicalSpace);
  92. const GridTrackSize& gridTrackSize(TrackSizingDirection, size_t) const;
  93. size_t maximumIndexInDirection(TrackSizingDirection) const;
  94. LayoutUnit logicalContentHeightForChild(RenderBox*, Vector<GridTrack>&);
  95. LayoutUnit minContentForChild(RenderBox*, TrackSizingDirection, Vector<GridTrack>& columnTracks);
  96. LayoutUnit maxContentForChild(RenderBox*, TrackSizingDirection, Vector<GridTrack>& columnTracks);
  97. LayoutPoint findChildLogicalPosition(RenderBox*, const Vector<GridTrack>& columnTracks, const Vector<GridTrack>& rowTracks);
  98. GridCoordinate cachedGridCoordinate(const RenderBox*) const;
  99. GridSpan resolveGridPositionsFromAutoPlacementPosition(const RenderBox*, TrackSizingDirection, size_t) const;
  100. PassOwnPtr<GridSpan> resolveGridPositionsFromStyle(const RenderBox*, TrackSizingDirection) const;
  101. enum GridPositionSide {
  102. StartSide,
  103. EndSide,
  104. BeforeSide,
  105. AfterSide
  106. };
  107. size_t resolveGridPositionFromStyle(const GridPosition&, GridPositionSide) const;
  108. LayoutUnit gridAreaBreadthForChild(const RenderBox* child, TrackSizingDirection, const Vector<GridTrack>&) const;
  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. size_t gridColumnCount() const
  114. {
  115. ASSERT(gridWasPopulated());
  116. return m_grid[0].size();
  117. }
  118. size_t gridRowCount() const
  119. {
  120. ASSERT(gridWasPopulated());
  121. return m_grid.size();
  122. }
  123. Vector<Vector<Vector<RenderBox*, 1> > > m_grid;
  124. HashMap<const RenderBox*, GridCoordinate> m_gridItemCoordinate;
  125. };
  126. } // namespace WebCore
  127. #endif // RenderGrid_h