[dali_1.3.42] Merge branch 'devel/master'
[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 #include <dali-toolkit/public-api/dali-toolkit-common.h>
23
24 #include <sstream>
25
26 namespace Dali
27 {
28 namespace Toolkit
29 {
30
31 /**
32  * A MeasureSpec is used during the Measure pass by a LayoutGroup to inform it's children
33  * how to be measured. For instance, it may measure a child with an exact width and an unspecified
34  * height in order to determine height for width.
35  */
36 class DALI_TOOLKIT_API MeasureSpec
37 {
38 public:
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 ),
51     mMode( mode )
52   {
53   }
54
55   MeasureSpec( LayoutLength measureSpec )
56   : mSize( measureSpec ),
57     mMode( Mode::UNSPECIFIED )
58   {
59   }
60
61   ~MeasureSpec() = default;
62
63   MeasureSpec& operator=( const MeasureSpec& rhs )
64   {
65     if( this != &rhs )
66     {
67       this->mSize = rhs.mSize;
68       this->mMode = rhs.mMode;
69     }
70     return *this;
71   }
72
73   bool operator==( MeasureSpec value )
74   {
75     return mSize == value.mSize;
76   }
77
78   bool operator!=( MeasureSpec value )
79   {
80     return mSize != value.mSize;
81   }
82
83   /**
84    * @brief Set the mode of the measure spec.
85    *
86    * @param mode The mode to set
87    */
88   void SetMode( MeasureSpec::Mode mode )
89   {
90     mMode = mode;
91   }
92
93   /**
94    * @brief Get the mode of the measure spec.
95    *
96    * @return The mode of the measure spec
97    */
98   MeasureSpec::Mode GetMode() const
99   {
100     return mMode;
101   }
102
103   /**
104    * @brief Set the size of the measure spec
105    *
106    * @param size the size to set
107    */
108   void SetSize( LayoutLength size )
109   {
110     mSize = size;
111   }
112
113   /**
114    * @brief Get the size of the measure spec
115    *
116    * @return the size of the measure spec
117    */
118   LayoutLength GetSize() const
119   {
120     return mSize;
121   }
122
123   /**
124    * @brief Adjust the measure size by the given delta.
125    *
126    * Used only for EXACT and AT_MOST modes.
127    * @param[in] measureSpec the measure spec to adjust
128    * @param[in] delta A positive or negative value to adjust the measure spec by.
129    *
130    * @note if the adjusted size is negative, it is zeroed.
131    * @return A new measure spec with the adjusted values.
132    */
133   static MeasureSpec Adjust( MeasureSpec measureSpec, int delta )
134   {
135     auto mode = measureSpec.GetMode();
136     auto size = measureSpec.GetSize();
137
138     if( mode == MeasureSpec::Mode::UNSPECIFIED )
139     {
140       return MeasureSpec( size, MeasureSpec::Mode::UNSPECIFIED );
141     }
142
143     if( delta < 0 && measureSpec.mSize < abs(delta) )
144     {
145       size = 0;
146     }
147     else
148     {
149       size += delta;
150     }
151     return MeasureSpec( size, mode );
152   }
153
154 private:
155
156   LayoutLength  mSize; ///< The specified size
157   Mode     mMode; ///< The measure mode
158
159 };
160
161 inline std::ostream& operator<< (std::ostream& o, const MeasureSpec& measureSpec )
162 {
163   return o << ( (measureSpec.GetMode() == MeasureSpec::Mode::UNSPECIFIED ? "Unspecified"
164                  : (measureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ? "Exactly":"At most" ) ) )
165            << " " << measureSpec.GetSize();
166 }
167
168 } //namespace Toolkit
169 } //namespace Dali
170
171
172 #endif // DALI_TOOLKIT_LAYOUTING_MEASURE_SPEC_H