f766247e2302888be0241c67500f5db053bcd68b
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / measure-spec.h
1 #ifndef DALI_TOOLKIT_LAYOUTING_MEASURE_SPEC_H
2 #define DALI_TOOLKIT_LAYOUTING_MEASURE_SPEC_H
3
4 /*
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <dali/public-api/common/dali-common.h>
21 #include <dali-toolkit/devel-api/layouting/layout-length.h>
22
23 #include <sstream>
24
25 namespace Dali
26 {
27 namespace Toolkit
28 {
29
30 /**
31  * A MeasureSpec is used during the Measure pass by a LayoutGroup to inform it's children
32  * how to be measured. For instance, it may measure a child with an exact width and an unspecified
33  * height in order to determine height for width.
34  */
35 class DALI_IMPORT_API MeasureSpec
36 {
37 public:
38   using IntType = LayoutLength::IntType;
39
40   enum class Mode
41   {
42     UNSPECIFIED, ///< This is used by a parent to determine the desired dimension of a child layout.
43     EXACTLY, /** This is used by a parent to impose an exact size on the child. The child must use
44                  this size, and guarantee that all of its descendants will fit within this size */
45     AT_MOST /** This is used by the parent to impose a maximum size on the child. The child must guarantee
46              * that it and all of it's descendants will fit within this size. */
47   };
48
49   MeasureSpec( LayoutLength measureSpec, MeasureSpec::Mode mode )
50   : mSize( measureSpec.mValue ),
51     mMode( mode )
52   {
53   }
54
55   MeasureSpec( IntType measureSpec )
56   : mSize( measureSpec ),
57     mMode( Mode::UNSPECIFIED )
58   {
59   }
60
61   ~MeasureSpec() = default;
62
63   MeasureSpec& operator=( const MeasureSpec& rhs )
64   {
65     this->mSize = rhs.mSize;
66     this->mMode = rhs.mMode;
67     return *this;
68   }
69
70   bool operator==( MeasureSpec value )
71   {
72     return mSize == value.mSize;
73   }
74
75   bool operator!=( MeasureSpec value )
76   {
77     return mSize != value.mSize;
78   }
79
80   /**
81    * @brief Get the mode of the measure spec.
82    *
83    * @return The mode of the measure spec
84    */
85   MeasureSpec::Mode GetMode() const
86   {
87     return mMode;
88   }
89
90   /**
91    * @brief Get the size of the measure spec
92    *
93    * @return the size of the measure spec
94    */
95   IntType GetSize() const
96   {
97     return mSize;
98   }
99
100   /**
101    * @brief Adjust the measure size by the given delta.
102    *
103    * Used only for EXACT and AT_MOST modes.
104    * @param[in] measureSpec the measure spec to adjust
105    * @param[in] delta A positive or negative value to adjust the measure spec by.
106    *
107    * @note if the adjusted size is negative, it is zeroed.
108    * @return A new measure spec with the adjusted values.
109    */
110   static MeasureSpec Adjust( MeasureSpec measureSpec, int delta )
111   {
112     auto mode = measureSpec.GetMode();
113     auto size = measureSpec.GetSize();
114
115     if( mode == MeasureSpec::Mode::UNSPECIFIED )
116     {
117       return MeasureSpec( size, MeasureSpec::Mode::UNSPECIFIED );
118     }
119
120     if( delta < 0 && measureSpec.mSize < static_cast<IntType>(abs(delta)) )
121     {
122       size = 0;
123     }
124     else
125     {
126       size += delta;
127     }
128     return MeasureSpec( size, mode );
129   }
130
131 public:
132   IntType  mSize; ///< The specified size
133   Mode     mMode; ///< The measure mode
134 };
135
136 inline std::ostream& operator<< (std::ostream& o, const MeasureSpec& measureSpec )
137 {
138   return o << ( (measureSpec.GetMode() == MeasureSpec::Mode::UNSPECIFIED ? "Unspecified"
139                  : (measureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ? "Exactly":"At most" ) ) )
140            << " " << measureSpec.GetSize();
141 }
142
143 } //namespace Toolkit
144 } //namespace Dali
145
146
147 #endif // DALI_TOOLKIT_LAYOUTING_MEASURE_SPEC_H