Home | History | Annotate | Download | only in style
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef GridResolvedPosition_h
      6 #define GridResolvedPosition_h
      7 
      8 #include "core/rendering/style/GridPosition.h"
      9 
     10 namespace blink {
     11 
     12 struct GridSpan;
     13 class RenderBox;
     14 class RenderStyle;
     15 
     16 enum GridPositionSide {
     17     ColumnStartSide,
     18     ColumnEndSide,
     19     RowStartSide,
     20     RowEndSide
     21 };
     22 
     23 enum GridTrackSizingDirection {
     24     ForColumns,
     25     ForRows
     26 };
     27 
     28 // This class represents an index into one of the dimensions of the grid array.
     29 // Wraps a size_t integer just for the purpose of knowing what we manipulate in the grid code.
     30 class GridResolvedPosition {
     31 public:
     32     static GridResolvedPosition adjustGridPositionForAfterEndSide(size_t resolvedPosition)
     33     {
     34         return resolvedPosition ? GridResolvedPosition(resolvedPosition - 1) : GridResolvedPosition(0);
     35     }
     36 
     37     static GridResolvedPosition adjustGridPositionForSide(size_t resolvedPosition, GridPositionSide side)
     38     {
     39         // An item finishing on the N-th line belongs to the N-1-th cell.
     40         if (side == ColumnEndSide || side == RowEndSide)
     41             return adjustGridPositionForAfterEndSide(resolvedPosition);
     42 
     43         return GridResolvedPosition(resolvedPosition);
     44     }
     45 
     46     static void initialAndFinalPositionsFromStyle(const RenderStyle&, const RenderBox&, GridTrackSizingDirection, GridPosition &initialPosition, GridPosition &finalPosition);
     47     static GridSpan resolveGridPositionsFromAutoPlacementPosition(const RenderStyle&, const RenderBox&, GridTrackSizingDirection, const GridResolvedPosition&);
     48     static PassOwnPtr<GridSpan> resolveGridPositionsFromStyle(const RenderStyle&, const RenderBox&, GridTrackSizingDirection);
     49     static GridResolvedPosition resolveNamedGridLinePositionFromStyle(const RenderStyle&, const GridPosition&, GridPositionSide);
     50     static GridResolvedPosition resolveGridPositionFromStyle(const RenderStyle&, const GridPosition&, GridPositionSide);
     51     static PassOwnPtr<GridSpan> resolveGridPositionAgainstOppositePosition(const RenderStyle&, const GridResolvedPosition& resolvedOppositePosition, const GridPosition&, GridPositionSide);
     52     static PassOwnPtr<GridSpan> resolveNamedGridLinePositionAgainstOppositePosition(const RenderStyle&, const GridResolvedPosition& resolvedOppositePosition, const GridPosition&, GridPositionSide);
     53 
     54     GridResolvedPosition(size_t position)
     55         : m_integerPosition(position)
     56     {
     57     }
     58 
     59     GridResolvedPosition(const GridPosition& position, GridPositionSide side)
     60     {
     61         ASSERT(position.integerPosition());
     62         size_t integerPosition = position.integerPosition() - 1;
     63 
     64         m_integerPosition = adjustGridPositionForSide(integerPosition, side).toInt();
     65     }
     66 
     67     GridResolvedPosition& operator++()
     68     {
     69         m_integerPosition++;
     70         return *this;
     71     }
     72 
     73     bool operator==(const GridResolvedPosition& other) const
     74     {
     75         return m_integerPosition == other.m_integerPosition;
     76     }
     77 
     78     bool operator!=(const GridResolvedPosition& other) const
     79     {
     80         return m_integerPosition != other.m_integerPosition;
     81     }
     82 
     83     bool operator<(const GridResolvedPosition& other) const
     84     {
     85         return m_integerPosition < other.m_integerPosition;
     86     }
     87 
     88     bool operator>(const GridResolvedPosition& other) const
     89     {
     90         return m_integerPosition > other.m_integerPosition;
     91     }
     92 
     93     bool operator<=(const GridResolvedPosition& other) const
     94     {
     95         return m_integerPosition <= other.m_integerPosition;
     96     }
     97 
     98     bool operator>=(const GridResolvedPosition& other) const
     99     {
    100         return m_integerPosition >= other.m_integerPosition;
    101     }
    102 
    103     size_t toInt() const
    104     {
    105         return m_integerPosition;
    106     }
    107 
    108     GridResolvedPosition next() const
    109     {
    110         return GridResolvedPosition(m_integerPosition + 1);
    111     }
    112 
    113     static size_t explicitGridColumnCount(const RenderStyle&);
    114     static size_t explicitGridRowCount(const RenderStyle&);
    115 
    116 private:
    117 
    118     static size_t explicitGridSizeForSide(const RenderStyle&, GridPositionSide);
    119 
    120     size_t m_integerPosition;
    121 };
    122 
    123 } // namespace blink
    124 
    125 #endif // GridResolvedPosition_h
    126