Home | History | Annotate | Download | only in style
      1 /*
      2  * Copyright (C) 2012 Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef GridTrackSize_h
     32 #define GridTrackSize_h
     33 
     34 #include "core/rendering/style/GridLength.h"
     35 
     36 namespace blink {
     37 
     38 enum GridTrackSizeType {
     39     LengthTrackSizing,
     40     MinMaxTrackSizing
     41 };
     42 
     43 class GridTrackSize {
     44 public:
     45     GridTrackSize(const GridLength& length)
     46         : m_type(LengthTrackSizing)
     47         , m_minTrackBreadth(length)
     48         , m_maxTrackBreadth(length)
     49         , m_minTrackBreadthIsMinOrMaxContent(false)
     50         , m_minTrackBreadthIsMaxContent(false)
     51         , m_maxTrackBreadthIsMinOrMaxContent(false)
     52         , m_maxTrackBreadthIsMaxContent(false)
     53     {
     54         cacheMinMaxTrackBreadthTypes();
     55     }
     56 
     57     GridTrackSize(const GridLength& minTrackBreadth, const GridLength& maxTrackBreadth)
     58         : m_type(MinMaxTrackSizing)
     59         , m_minTrackBreadth(minTrackBreadth)
     60         , m_maxTrackBreadth(maxTrackBreadth)
     61         , m_minTrackBreadthIsMinOrMaxContent(false)
     62         , m_minTrackBreadthIsMaxContent(false)
     63         , m_maxTrackBreadthIsMinOrMaxContent(false)
     64         , m_maxTrackBreadthIsMaxContent(false)
     65     {
     66         cacheMinMaxTrackBreadthTypes();
     67     }
     68 
     69     const GridLength& length() const
     70     {
     71         ASSERT(m_type == LengthTrackSizing);
     72         ASSERT(m_minTrackBreadth == m_maxTrackBreadth);
     73         const GridLength& minTrackBreadth = m_minTrackBreadth;
     74         return minTrackBreadth;
     75     }
     76 
     77     const GridLength& minTrackBreadth() const
     78     {
     79         if (m_minTrackBreadth.isLength() && m_minTrackBreadth.length().isAuto()) {
     80             DEFINE_STATIC_LOCAL(GridLength, minContent, (Length(MinContent)));
     81             return minContent;
     82         }
     83         return m_minTrackBreadth;
     84     }
     85 
     86     const GridLength& maxTrackBreadth() const
     87     {
     88         if (m_maxTrackBreadth.isLength() && m_maxTrackBreadth.length().isAuto()) {
     89             DEFINE_STATIC_LOCAL(GridLength, maxContent, (Length(MaxContent)));
     90             return maxContent;
     91         }
     92         return m_maxTrackBreadth;
     93     }
     94 
     95     GridTrackSizeType type() const { return m_type; }
     96 
     97     bool isContentSized() const { return m_minTrackBreadth.isContentSized() || m_maxTrackBreadth.isContentSized(); }
     98 
     99     bool isPercentage() const { return m_type == LengthTrackSizing && length().isLength() && length().length().isPercent(); }
    100 
    101     bool operator==(const GridTrackSize& other) const
    102     {
    103         return m_type == other.m_type && m_minTrackBreadth == other.m_minTrackBreadth && m_maxTrackBreadth == other.m_maxTrackBreadth;
    104     }
    105 
    106     void cacheMinMaxTrackBreadthTypes()
    107     {
    108         m_minTrackBreadthIsMaxContent = minTrackBreadth().isLength() && minTrackBreadth().length().isMaxContent();
    109         m_minTrackBreadthIsMinOrMaxContent = m_minTrackBreadthIsMaxContent || (minTrackBreadth().isLength() && minTrackBreadth().length().isMinContent());
    110         m_maxTrackBreadthIsMaxContent = maxTrackBreadth().isLength() && maxTrackBreadth().length().isMaxContent();
    111         m_maxTrackBreadthIsMinOrMaxContent = m_maxTrackBreadthIsMaxContent || (maxTrackBreadth().isLength() && maxTrackBreadth().length().isMinContent());
    112     }
    113 
    114     bool hasMinOrMaxContentMinTrackBreadth() const { return m_minTrackBreadthIsMinOrMaxContent; }
    115     bool hasMaxContentMinTrackBreadth() const { return m_minTrackBreadthIsMaxContent; }
    116     bool hasMinOrMaxContentMaxTrackBreadth() const { return m_maxTrackBreadthIsMinOrMaxContent; }
    117     bool hasMaxContentMaxTrackBreadth() const { return m_maxTrackBreadthIsMaxContent; }
    118 
    119 private:
    120     GridTrackSizeType m_type;
    121     GridLength m_minTrackBreadth;
    122     GridLength m_maxTrackBreadth;
    123     bool m_minTrackBreadthIsMinOrMaxContent;
    124     bool m_minTrackBreadthIsMaxContent;
    125     bool m_maxTrackBreadthIsMinOrMaxContent;
    126     bool m_maxTrackBreadthIsMaxContent;
    127 };
    128 
    129 } // namespace blink
    130 
    131 #endif // GridTrackSize_h
    132