Legacy Size negotiation mapper
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / layouting / size-negotiation-mapper.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/layouting/size-negotiation-mapper.h>
19
20 // EXTERNAL HEADER
21 #include <dali/integration-api/debug.h>
22
23 // INTERNAL HEADER
24 #include <dali-toolkit/devel-api/controls/control-devel.h>
25 #include <dali-toolkit/devel-api/layouting/child-layout-data.h>
26 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
27
28 namespace
29 {
30 #if defined(DEBUG_ENABLED)
31 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
32 #endif
33
34 /**
35  * Set the width specification with the given layout params
36  */
37 void SetWidthLayoutParams( Dali::Toolkit::Control control, int layoutParams )
38 {
39   DALI_LOG_INFO( gLogFilter, Debug::General, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy Setting WIDTH LayoutParam(%d) for %s\n",
40                                              layoutParams,
41                                              control.GetName().c_str() );
42   control.SetProperty( Dali::Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, layoutParams );
43 }
44
45 /**
46  * Set the height specification with the given layout params
47  */
48 void SetHeightLayoutParams( Dali::Toolkit::Control control, int layoutParams )
49 {
50   DALI_LOG_INFO( gLogFilter, Debug::General, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy Setting HEIGHT LayoutParam(%d) for %s\n",
51                                              layoutParams,
52                                              control.GetName().c_str() );
53   control.SetProperty( Dali::Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, layoutParams );
54 }
55
56 /**
57  * Call SetResizePolicyRequired true on the layout belonging to the provided control
58  */
59 void SetResizePolicyRequiredFlag( Dali::Toolkit::Internal::LayoutItemPtr layout )
60 {
61   layout->SetResizePolicyRequired( true );
62 }
63
64 } // namspace
65
66 namespace Dali
67 {
68 namespace Toolkit
69 {
70 namespace Internal
71 {
72
73 void SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( Toolkit::Control control, Toolkit::Internal::LayoutItemPtr layout, Dimension::Type dimension )
74 {
75   // Get control's current width specification
76   int matchedLayoutParamWidth = control.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
77   int matchedLayoutParamHeight = control.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
78
79   // Get control's Resize Policy that should be mapped to a width specification
80   const ResizePolicy::Type resizePolicy = control.GetResizePolicy( dimension );
81
82   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy ResizePolicy for control:%s resizePolicy %d for dimension %d\n",
83                                               control?control.GetName().c_str():"Invalid",
84                                               resizePolicy,
85                                               dimension );
86
87   bool matchFound( false );
88
89   // Use the control's ResizePolicy to determine the new specification
90   switch( resizePolicy )
91   {
92     case ResizePolicy::USE_ASSIGNED_SIZE :
93     {
94       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy USE_ASSIGNED_SIZE\n");
95       // Set by legacy controls on their children. Will not be exposed to a Layout.
96       break;
97     }
98
99     case ResizePolicy::FIXED :
100     {
101       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy FIXED\n");
102       Vector3 controlSize = control.GetTargetSize();
103       matchedLayoutParamWidth = controlSize.width;
104       matchedLayoutParamHeight = controlSize.height;
105       matchFound = true;
106       break;
107     };
108     case ResizePolicy::USE_NATURAL_SIZE :
109     {
110       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy USE_NATURAL_SIZE\n");
111       // Default ResizePolicy for controls.
112       // LayoutGroups are containers, containers will not have a natural size.
113       break;
114     };
115     case ResizePolicy::FILL_TO_PARENT :
116     {
117       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy FILL_TO_PARENT\n");
118       matchedLayoutParamWidth = ChildLayoutData::MATCH_PARENT;
119       matchedLayoutParamHeight = ChildLayoutData::MATCH_PARENT;
120       matchFound = true;
121       break;
122     };
123     case ResizePolicy::SIZE_RELATIVE_TO_PARENT :
124     {
125       SetResizePolicyRequiredFlag( layout ); // Defer setting the exact size until the parent size is known.
126       break;
127     };
128     case ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT :
129     {
130       SetResizePolicyRequiredFlag( layout ); // Defer setting the exact size until the parent size is known.
131       break;
132     };
133     case ResizePolicy::FIT_TO_CHILDREN :
134     {
135       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy FIT_TO_CHILDREN\n");
136       matchedLayoutParamWidth = ChildLayoutData::WRAP_CONTENT;
137       matchedLayoutParamHeight = ChildLayoutData::WRAP_CONTENT;
138       matchFound = true;
139       break;
140     };
141     case ResizePolicy::DIMENSION_DEPENDENCY :
142     {
143       // Not supported
144       break;
145     };
146   }
147
148   // Use the resize policy for the required dimensions only.
149   // Possible that only one dimension was set hence either the matchedLayoutParamWidth or
150   // matchedLayoutParamHeight should not be used.
151   if( matchFound )
152   {
153     if( dimension == Dimension::WIDTH )
154     {
155       SetWidthLayoutParams( control, matchedLayoutParamWidth );
156     }
157     else if( dimension == Dimension::HEIGHT )
158     {
159       SetHeightLayoutParams( control, matchedLayoutParamHeight );
160     }
161     else if( Dimension::ALL_DIMENSIONS )
162     {
163       SetWidthLayoutParams( control, matchedLayoutParamWidth );
164       SetHeightLayoutParams( control, matchedLayoutParamHeight );
165     }
166   }
167 };
168
169 void SizeNegotiationMapper::GetSizeofChildForParentDependentResizePolicy( Toolkit::Control control, const MeasureSpec parentWidthSpecification, const MeasureSpec parentHeightSpecification, LayoutLength& childWidth, LayoutLength& childHeight)
170 {
171   DALI_LOG_INFO( gLogFilter, Debug::General, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy ResizePolicy required for %s\n", control.GetName().c_str() );
172   ResizePolicy::Type widthResizePolicy = control.GetResizePolicy( Dimension::WIDTH );
173   ResizePolicy::Type heightResizePolicy = control.GetResizePolicy( Dimension::HEIGHT );
174   Vector3 sizeModeFactor = control.GetSizeModeFactor();
175
176   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::MeasureChild SetLayoutParamsForParentDependantResizePolicy for control:%s resizePolicy w:%d h:%d  modeFactor(%f,%f,%f)\n",
177                                               control?control.GetName().c_str():"Invalid",
178                                               widthResizePolicy, heightResizePolicy,
179                                               sizeModeFactor.x,
180                                               sizeModeFactor.y,
181                                               sizeModeFactor.z );
182
183   if( widthResizePolicy == ResizePolicy::SIZE_RELATIVE_TO_PARENT )
184   {
185     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy width SIZE_RELATIVE_TO_PARENT\n");
186     childWidth = parentWidthSpecification.GetSize().AsDecimal() * sizeModeFactor.x;
187   }
188   else if( widthResizePolicy == ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT )
189   {
190     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy width SIZE_FIXED_OFFSET_FROM_PARENT\n");
191     childWidth = parentWidthSpecification.GetSize().AsDecimal() + sizeModeFactor.x;
192   }
193
194   if( heightResizePolicy == ResizePolicy::SIZE_RELATIVE_TO_PARENT )
195   {
196     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy height  SIZE_RELATIVE_TO_PARENT\n");
197     childHeight = parentHeightSpecification.GetSize().AsDecimal() * sizeModeFactor.y;
198   }
199   else if( heightResizePolicy == ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT )
200   {
201     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy height  SIZE_FIXED_OFFSET_FROM_PARENT\n");
202     childHeight = parentHeightSpecification.GetSize().AsDecimal() + sizeModeFactor.y;
203   }
204
205   // DIMENSION_DEPENDENCY not supported
206
207   DALI_LOG_INFO( gLogFilter, Debug::General, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy child size(%f,%f)\n",  childWidth.AsInteger(), childHeight.AsInteger() );
208 }
209
210 } // namespace Internal
211
212 } // namespace Toolkit
213
214 } // namespace Dali