[Tizen] Add codes for Dali Windows Backend
[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-toolkit/public-api/dali-toolkit-common.h>
21 #include <dali/public-api/common/dali-common.h>
22 #include <dali-toolkit/devel-api/layouting/layout-length.h>
23 #include <dali-toolkit/public-api/dali-toolkit-common.h>
24
25 #include <sstream>
26
27 namespace Dali
28 {
29 namespace Toolkit
30 {
31
32 /**
33  * A MeasureSpec is used during the Measure pass by a LayoutGroup to inform it's children
34  * how to be measured. For instance, it may measure a child with an exact width and an unspecified
35  * height in order to determine height for width.
36  */
37 class DALI_TOOLKIT_API MeasureSpec
38 {
39 public:
40   using IntType = LayoutLength::IntType;
41
42   enum class Mode
43   {
44     UNSPECIFIED, ///< This is used by a parent to determine the desired dimension of a child layout.
45     EXACTLY, /** This is used by a parent to impose an exact size on the child. The child must use
46                  this size, and guarantee that all of its descendants will fit within this size */
47     AT_MOST /** This is used by the parent to impose a maximum size on the child. The child must guarantee
48              * that it and all of it's descendants will fit within this size. */
49   };
50
51   MeasureSpec( LayoutLength measureSpec, MeasureSpec::Mode mode )
52   : mSize( measureSpec.mValue ),
53     mMode( mode )
54   {
55   }
56
57   MeasureSpec( IntType measureSpec )
58   : mSize( measureSpec ),
59     mMode( Mode::UNSPECIFIED )
60   {
61   }
62
63   ~MeasureSpec() = default;
64
65   MeasureSpec& operator=( const MeasureSpec& rhs )
66   {
67     if( this != &rhs )
68     {
69       this->mSize = rhs.mSize;
70       this->mMode = rhs.mMode;
71     }
72     return *this;
73   }
74
75   bool operator==( MeasureSpec value )
76   {
77     return mSize == value.mSize;
78   }
79
80   bool operator!=( MeasureSpec value )
81   {
82     return mSize != value.mSize;
83   }
84
85   /**
86    * @brief Get the mode of the measure spec.
87    *
88    * @return The mode of the measure spec
89    */
90   MeasureSpec::Mode GetMode() const
91   {
92     return mMode;
93   }
94
95   /**
96    * @brief Get the size of the measure spec
97    *
98    * @return the size of the measure spec
99    */
100   IntType GetSize() const
101   {
102     return mSize;
103   }
104
105   /**
106    * @brief Adjust the measure size by the given delta.
107    *
108    * Used only for EXACT and AT_MOST modes.
109    * @param[in] measureSpec the measure spec to adjust
110    * @param[in] delta A positive or negative value to adjust the measure spec by.
111    *
112    * @note if the adjusted size is negative, it is zeroed.
113    * @return A new measure spec with the adjusted values.
114    */
115   static MeasureSpec Adjust( MeasureSpec measureSpec, int delta )
116   {
117     auto mode = measureSpec.GetMode();
118     auto size = measureSpec.GetSize();
119
120     if( mode == MeasureSpec::Mode::UNSPECIFIED )
121     {
122       return MeasureSpec( size, MeasureSpec::Mode::UNSPECIFIED );
123     }
124
125     if( delta < 0 && measureSpec.mSize < static_cast<IntType>(abs(delta)) )
126     {
127       size = 0;
128     }
129     else
130     {
131       size += delta;
132     }
133     return MeasureSpec( size, mode );
134   }
135
136 public:
137   IntType  mSize; ///< The specified size
138   Mode     mMode; ///< The measure mode
139 };
140
141 inline std::ostream& operator<< (std::ostream& o, const MeasureSpec& measureSpec )
142 {
143   return o << ( (measureSpec.GetMode() == MeasureSpec::Mode::UNSPECIFIED ? "Unspecified"
144                  : (measureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ? "Exactly":"At most" ) ) )
145            << " " << measureSpec.GetSize();
146 }
147
148 } //namespace Toolkit
149 } //namespace Dali
150
151
152 #endif // DALI_TOOLKIT_LAYOUTING_MEASURE_SPEC_H